首页 > 解决方案 > GOOGLE COLAB:cv2.imread 返回 NoneType

问题描述

我无法cv2.imread在谷歌 colab 中使用。我试过这样;

import cv2
img = cv2.imread('/content/gdrive/My Drive/path_to_image/1.png')
print(type(img))

>> `<class 'NoneType'>`

我不知道为什么它会发出这个!我在下面再次尝试...使用r

img = cv2.imread(r'/content/gdrive/My Drive/path_to_image/1.png')

我不知道为什么它会返回一个NoneType对象。图片路径没有问题。我尝试了以下链接,但未能成功。 cv2.imread 总是返回 NoneType

标签: pythonpython-3.xopencvgoogle-colaboratory

解决方案


我希望我的解决方案对某人有所帮助。就我而言,调用img = cv2.imread('/content/gdrive/My Drive/path_to_image/1.png')实际上是 FASTER RCNN 中函数的一部分。我无法解决互联网上的任何其他现有方法。最后我像下面那样做了,它奏效了。

首先,我认为问题是cv2.imread()不承认My Drive.. My&之间的空间Drive。虽然我从一开始就认识到这一点,但我的尝试如下所示。

"/content/gdrive/'My Drive'/path_to_image/1.png"

但简单地投入My Drive一个single quotation没有工作!

在我的情况下,它必须如下所示;

"/content/gdrive/"+"My Drive"+"/path_to_image/1.png"

编辑:

我想出了另一个解决方案来克服"My Drive"Colab 中这个烦人的问题。

第1步:

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

第2步:

import os
os.getcwd()
!mkdir MyDrive   # make a directory called MyDrive

第 3 步:

!mount --bind /content/drive/My\ Drive /content/MyDrive

推荐阅读