Reputation: 41
I am using Windows7 and PyCharm 2019.3 and I've tried everything I knew but I'm unable to rectify this error. Kindly Help.
Error:
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
Code:
import numpy as np
import cv2
import os
from matplotlib import pyplot as plt
img = r'C:\Users\Vinay\Desktop\picture.jpg'
cascade_path = os.path.dirname(os.path.abspath(__file__))
face_cascade = cv2.CascadeClassifier(cascade_path+r'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cascade_path+r'haarcascade_eye.xml')
img = cv2.imread(img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Picture", gray)
cv2.waitKey(500)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
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:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Upvotes: 0
Views: 373
Reputation: 41
For anyone who comes across this question, I'd like to help you since I did find the solution to this. I just copied the required .xml files in the same directory I was working. This simple step cleared the error. The reason behind it is that the interpreter was having trouble finding the classifiers even after specifying the path. Also, you can try dropping the cascade_path line and directly mention the path in the face_cascade line. Hope this helps.
Upvotes: 2