Home > Net >  Python opencv error (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->siz
Python opencv error (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->siz

Time:12-17

I'm using the CascadeClassifier of the opencv-python package to perform face detection with the haarcascade_frontalface_default.xml with this code:

self.face_cascade = cv2.CascadeClassifier(haar_cascade_path)

...

gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray_frame, 1.3, 5)

It's working fine for most of the frames, but sometimes I'm getting this exception:

cv2.error: OpenCV(4.6.0) d:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.hpp:46: error: (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->size() in function 'cv::FeatureEvaluator::getScaleData'

The error occurs during the detectMultiScale call. I already checked the gray_frame and it looks good (shape is 1366x1060 and it's not none or something like that). Do you have any idea on how the fix this?

CodePudding user response:

I actually managed to solve this issue. What caused this error to happen was, that I'm preprocessing the dataset multithreaded and I only used a single instance for multiple workers concurrently. It seems like the CascadeClassifier (or at least the detect method) isn't thread-safe and feeding images with a different size concurrently caused the problem. Creating one instance for each thread/worker solved the issue.

  • Related