【问题标题】:FileNotFound error while reading images from GDrive in Colab在 Colab 中从 GDrive 读取图像时出现 FileNotFound 错误
【发布时间】:2021-04-10 13:28:04
【问题描述】:

我正在尝试操作存储在我的 Google Drive 中不同图像目录中的图像以进行深度学习。但是,我收到了 FileNotFound 异常。

假设,我在 Google Colab 中的主目录是:

MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'

该文件夹中有两个目录,分别称为DogCat。我可以通过运行以下代码来验证这一点:

MainDirectory ='/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']

for category in Categories:
    path = os.path.join(MainDirectory,category)
    print(path)

打印目录:

/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog
/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Cat

我现在要做的是显示图像,为此我编写了以下代码:

MainDirectory = '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'
Categories =['Dog','Cat']
for category in Categories:
   path =os.path.join(MainDirectory,category)
   print(path)
    
   for img in os.listdir(path):
      img_array =cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
      plt.imshow(img_array,cmap="gray")
      plt.show()

但是这会导致以下错误:

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog'

附加信息


我已导入以下库:

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2

并使用以下命令安装我的驱动器:

from google.colab import drive
drive.mount('/content/drive')

【问题讨论】:

  • 我刚刚在我的本地目录中使用了您的示例代码,创建了一个名为“我的驱动器”的文件夹,并包含带有空格和图像的不同子文件夹,它运行没有问题。可能问题来自 drive.mount()
  • 奇怪,我已经注销并再次登录但没有效果
  • 但奇怪的是它可以看到实际路径/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog

标签: python opencv image-processing google-colaboratory


【解决方案1】:

该错误很可能是由文件路径中的空格引起的。在MainDirectory 文件路径之前添加r 字符串文字前缀,用作原始字符串。这是更正后的代码:

MainDirectory = r'/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/'

【讨论】:

  • 您确定文件路径存在吗?
  • 那么,我不确定。也许是大写?
  • 看它可以看到目录/content/drive/My Drive/Colab Notebooks/2020YearDeepLearning/Animals/PetImages/Dog
  • 嗯...请复制并粘贴其中一张图片的路径
  • 这是一个奇怪的空间,我看不到,抱歉浪费您的时间
猜你喜欢
  • 2011-10-21
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 2020-02-21
  • 2016-07-13
  • 2020-09-01
  • 2018-08-09
  • 1970-01-01
相关资源
最近更新 更多