-1

So I'm getting the following error:

Traceback (most recent call last): File "/Users/matt/Dropbox/HumanPiano/Test/Test_1.py", line 17, in faces = face_cascade.detectMultiScale(gray, 1.1, 4) cv2.error: OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-gi6lxw0x/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

And am trying to run the example code:

import cv2  
  
# Load the cascade  
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')  
  
# To capture video from webcam.   
cap = cv2.VideoCapture(0)  
  
while True:  
    # Read the frame  
    _, img = cap.read()  
  
    # Convert to grayscale  
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
  
    # Detect the faces  
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)  
  
    # Draw the rectangle around each face  
    for (x, y, w, h) in faces:  
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)  
  
    # Display  
    cv2.imshow('Video', img)  
  
    # Stop if escape key is pressed  
    k = cv2.waitKey(30) & 0xff  
    if k==27:  
        break  
          
# Release the VideoCapture object  
cap.release() 

How do I fix/address it?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

1 Answers1

0

Here you need to add this before referring to the path of .xml.

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

Hope this would solve your problem.

Kalyan Reddy
  • 326
  • 1
  • 8