OpenCV !_src.empty() in function 'cvtColor' error

This error happened because the image didn't load properly . So you have problem with the previous line cv2.imread my suggestion is :

  • check if the images exist in the path you give

  • check the count variable if he have valid number


If anyone is experiencing this same problem when reading a frame from a webcam:

Verify if your webcam is being used on another task and close it. This wil solve the problem.

I spent some time with this error when I realized my camera was online in a google hangouts group. Also, Make sure your webcam drivers are up to date


I kept getting this error too:

Traceback (most recent call last):
  File "face_detector.py", line 6, in <module>
    gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor

My cv2.cvtColor(...) was working fine with \photo.jpg but not with \news.jpg. For me, I finally realized that when working on Windows with python, those escape characters will get you every time!! So my "bad" photo was being escaped because of the file name beginning with "n". Python took the \n as an escape character and OpenCV couldn't find the file!

Solution:
Preface file names in Windows python with r"...\...\" as in

cv2.imread(r".\images\news.jpg")

Tags:

Python

Opencv