0

I would like to use HAAR to detect faces.

I first ran the following code:

#use HAAR cascade to detect faces
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Detect faces
faces = face_cascade.detectMultiScale(img)

When doing so, I get the following error: error: OpenCV(4.1.0) /home/vocuser/tmp/xeus/4.1.opencv/opencv/modules/objdetect/src/cascadedetect.cpp:1658: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

I read that I am getting the error because the XML file could not be found. So I did the following:

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

# Detect faces
faces = face_cascade.detectMultiScale(img)

But now I'm getting the following error: AttributeError: module 'cv2' has no attribute 'data'

So what is the correct approach? How can I use the detectMultiScale function?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
sos.cott
  • 435
  • 3
  • 17
  • Use the full path of the file. – stateMachine Dec 23 '21 at 13:28
  • I also did: cv2_base_dir = os.path.dirname(os.path.abspath(cv2.__file__)) haar_model = os.path.join(cv2_base_dir, 'data/haarcascade_frontalface_default.xml') This also does not work. – sos.cott Dec 23 '21 at 13:40
  • for that first error, are you sure `img` isn't empty? – DNy Dec 23 '21 at 14:29
  • 1
    `!empty()` means the cascade couldn't be loaded. trust me, or look at the source code. -- if you used a recent version, instead of 4.1.0, you would be able to use `cv2.data.haarcascades` – Christoph Rackwitz Dec 23 '21 at 15:26
  • Does this answer your question? [cv2 has no attribute data](https://stackoverflow.com/questions/65357932/cv2-has-no-attribute-data) – Alex W Dec 23 '21 at 15:34

0 Answers0