[英]How do I mount one Google Drive folder with images only onto Google Colab and iterate through it?
I have a folder of images I extracted for a computer-vision project, but don't know how to mount it onto Google Colab and iterate through the folder.我有一个为计算机视觉项目提取的图像文件夹,但不知道如何将其装载到 Google Colab 上并遍历该文件夹。 I have a function I need to apply to each image inside the folder, but don't know how to get the images.
我有一个功能需要应用于文件夹内的每个图像,但不知道如何获取图像。
I've tried looking for resources but haven't found anything helpful to my situation, because most of them were for unzipping files that were not images.我曾尝试寻找资源,但没有找到对我的情况有帮助的任何资源,因为它们中的大多数都是用于解压缩不是图像的文件。 Can you please help me out?
你能帮帮我吗? Thank you.
谢谢你。
You can use the OpenCV library for this.您可以为此使用 OpenCV 库。
from google.colab import drive
import os
import cv2
First you need to change the current working directory to your image folder directory.首先,您需要将当前工作目录更改为图像文件夹目录。
os.chdir("/content/drive/MyDrive/yourfolder")
You can iterate through every image, apply your function to them and save the final version like this:您可以遍历每个图像,将您的函数应用于它们并保存最终版本,如下所示:
for file in os.listdir():
img = cv2.imread(file)
result = myfunction(img)
cv2.imwrite(file, result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.