简体   繁体   English

如何将一个仅包含图像的 Google 云端硬盘文件夹挂载到 Google Colab 并遍历它?

[英]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.

相关问题 如何遍历 Google Drive 文件夹中的多个文本 (.txt) 文件以上传到 Google Colab? - How do I iterate through multiple text(.txt) files in a folder on Google Drive to upload on Google Colab? Google colab:如何挂载两个 Google Drive 帐户? - Google colab : How can i mount two google drive accounts? Google Colab:如何循环浏览文件夹中的图像? - Google Colab: How to loop through images in a folder? 如何通过谷歌 colab 代码在我的谷歌驱动器中创建一个文件夹? - how to create a folder in my google drive through google colab code? 如何在colab中将谷歌驱动器安装到R笔记本上? - How to mount google drive to R notebook in colab? 如何仅加载 google colab 上的特定文件夹? - How do I load only a specific folder on google colab? 无法在 colab 笔记本中挂载 Google 驱动器文件夹 - Can't mount Google drive folder in colab notebook 如何自动将文件生成到与colab笔记本相同的google驱动器文件夹? - How do I automatically generate files to the same google drive folder as my colab notebook? 将 Drive Mount 与 Google Colab 一起使用时出错 - Error Using Drive Mount with Google Colab 如何将 github 存储库挂载为 google colab 中的当前目录文件夹? - how to mount a github repository as the current directory folder in google colab?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM