How to solve error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::de
When I run the programm, it shows
Traceback (most recent call last):
File "F:/pycharm file/webcam.py", line 18, in <module>
faces = face_cascade.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::de
My code is here :
import cv2
face_cascade = cv2.CascadeClassifier('F:/pycharm file/webcam.py/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while True:
# Read the frame
_, img = cap.read()
#ret, im = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 5, cv2.CASCADE_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 255, 255), 3)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break
cap.release()
I tried every solution available on the internet. Most of the solutions say to locate the xml file properly. I located that path properly. Then another group of solution says to check the webcam. I checked the webcam through another code and it properly gives grayscale image when asked. I'm using python 3.7 and opencv 4.1.2