Error: (-215) !Empty() in Function Detectmultiscale

OpenCV-Python Error (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Those files being in the same folder means nothing.

Relative paths are resolved relative to the current working directory, not the directory the script resides in.

Further, your code uses cv2.data.haarcascades + 'haarcascade_frontalface_default.xml' which is likely an absolute path.

You should check if a file exists at that location.

Then... always error-check.

You are missing several error checks.

  1. Right after classifier = cv2.CascadeClassifier... you must check if that succeeded:
assert not classifier.empty()

That is likely the issue here. The violated assertion (error message) checked for this condition.


  1. Right after vid = cv2.VideoCapture... you must check if that succeeded:
assert vid.isOpened()

  1. After reading each frame (ret, frame = vid.read()), you must check if that succeeded:
if not ret: break

How to solve. error: (-215) !empty() in function detectMultiScale

That's because there is no such file in cv2.data.haarcascades as it can be seen here.

You should download the "haarcascade_hand.xml" from this project or any other repo that you have in mind, then provide the full path for this to work.

it should look like this

cascade_path = "fullpath_to_hand_cascade/haarcascade_hand.xml"
hand_cascade = cv2.CascadeClassifier(cascade_path)


Related Topics



Leave a reply



Submit