I was testing OpenCV with their code provided within the documentation. Unfortunately, I'm facing an unexpected error. I think, it might be due to the newer version that I've. I've tried the fix of version 3.4.3 but it's not working! Can anyone solve my problem?

I'm working on a project currently! So, if anyone can help me out fast then it would be helpful!

import numpy as np
import cv2 as cv
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_alt.xml')
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml')
img = cv.imread('sachin.jpg')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:

error message:

Traceback (most recent call last):
  File "f:\Python Projects-2021\facial_detector.py", line 6, in <module>
    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.4) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

1 Answer 1


This is more of a programming question than a data science question and would therefore be better suited for the stackoverflow stackexchange website. Regarding the error you are getting, this is likely caused by the fact that your image is not being read in correctly in the line before (see also this answer on stackoverflow. Try checking if the filepath to your image is correct and check the values in the img variable.


Not the answer you're looking for? Browse other questions tagged or ask your own question.