Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GOOGLE COLAB: cv2.imread return NoneType

I could not use cv2.imread in google colab. I tried like this;

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

>> `<class 'NoneType'>`

I dont know why it is giving this out put!! I tried below this again... using r

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

I could not figure out why it is returning a NoneType object. There is no issue with the picture path. I tried the following link, but could not succeed. cv2.imread always returns NoneType

like image 759
Mass17 Avatar asked Sep 20 '25 04:09

Mass17


1 Answers

I hope my solution will help someone. In my case, calling img = cv2.imread('/content/gdrive/My Drive/path_to_image/1.png') was actually a part of a function in FASTER RCNN. I could not resolve any other existing methods in the internet. Finally I did it like below and it worked.

First of all I think the problem is cv2.imread() doesnt recognize My Drive.. the space between My & Drive. Although I recognized this from the beginning, my attempt was like below..

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

But simply putting My Drive in a single quotation didnt work!!

It must be like below to work in my case;

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

Edited:

I came up with another solution to overcome this annoying "My Drive" issue in Colab.

step 1:

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

step 2:

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

step 3:

!mount --bind /content/drive/My\ Drive /content/MyDrive
like image 105
Mass17 Avatar answered Sep 22 '25 00:09

Mass17