OpenCV(3.4.1) Error: Assertion failed
While executing the program i am getting following error:
OpenCV(3.4.1) Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\build\master_winpack-bindings-win32-vc14-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1698 Traceback (most recent call last): File "new24.py", line 20, in <module> flags=cv2.CASCADE_SCALE_IMAGE cv2.error: OpenCV(3.4.1) C:\build\master_winpack-bindings-win32-vc14-static\open cv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
What could be the reason for the error,how should i react to fix it? Thank you for your time and advise,
regards
pooja
i am trying to execute following program
import cv2
import sys
cascPath = "C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
Without any code it is difficult to answer but it seems that cascade classifier is empty : are you sure that path to cascade file is good?
i am trying to execute following program:
you must use double backslash \ \ and not \ . for opencv filename you can use / it is easier
The error is saying you're not passing a valid file to the classifier. Double check your path or this could be because your path contains "\", which in python are used to escape text characters.
Try this:
cascPath = r"C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt2.xml"
Notice the "r" in front of the path. This tells python to treat the string as raw, and not interpret character escapes.