I'm following a tutorial for creating a facial recognition software (link: https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ ) and I keep getting the error:
OpenCV Error: Assertion failed (!empty()) in
cv::CascadeClassifier::detectMultiScale, file C:\projects\opencv-
python\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698
Traceback (most recent call last):
File "C:/Users/Jacob/PycharmProjects/DesignProject/main.py", line 17, in
<module>
faces = face_cascade.detectMultiScale(gray, 1.3, 5)".
Looking at my code (below) and other questions like this online, it says that it was because I didn't specify my file path, but i still get the same error. I'm still new to this type of software, and python in general, so I'm not sure where I'm going wrong. Any help would be appreciated!!!
import cv2
face_cascade = cv2.CascadeClassifier('/C:/User/Jacob/Downloads/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('/C:/User/Jacob/Downloads/haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
if ret is True:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
continue
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+h]
roi_color = img[y:y+h, x:x+h]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(roi_color, (ex, ey), (ex+ew), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xFf
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Thank you to those that helped!! the solution was to put the xml files into the working directory of the project instead of trying to type out the entire file path
your path is wrong. You don't need /
before C
.
This is how it should be,
But I believe you are on windows and I recommend using something like below,
face_cascade = cv2.CascadeClassifier(r'C:\User\Jacob\Downloads\haarcascade_frontalface_default.xml')
In windows when you are mentioning any file you should '\' instead of a forward slash'/'. So your file path should look somewhat like this:
C:\Users\XYZ\Desktop\project code\abc.xml
Also, I suggest that you put all your XML files in the same working directory(where your code is placed) so that you don't have to mention the entire file path. You can just mention the file name. This helps when you are experimenting with different XML files and need to change the input files often. Hope this helps,cheers!
import cv2
import sys
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
#it must be noted that original size of video must be written correctly because it is not shown in video player
while(cap.isOpened()):
ret,frame = cap.read()
if ret == True:
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 34), 1,1)
cv2.imshow("Test",frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.