0

I continue getting this error. I've read that it means the images are not available. I've copied the path and used the proper directory. I've cloned the haarcascade_frontalface_default.xml directly from opencv Github. Not sure what I messed up on here.

I'm following this tutorial for facetracking online: https://www.youtube.com/watch?v=LmEcyQnfpDA&t=7600s

Any help appreciated!

Here are the code and errors:

Code:

import cv2
import numpy as np

def findFace(img):
    faceCascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_default.xml')
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(imgGray,1.1,8)

    myFaceListC = []
    myFaceListArea = []

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x + w, y + h),(0,0,255),2)


cap = cv2.VideoCapture(0)

while True:
    _, img = cap.read()
    findFace(img)
    cv2.imshow("Output",img)
    cv2.waitKey(1)

Errors:

Traceback (most recent call last):
  File "C:/Users/zachd/PycharmProjects/droneproject2/venv/Face Tracking.py", line 20, in <module>
    findFace(img)
  File "C:/Users/zachd/PycharmProjects/droneproject2/venv/Face Tracking.py", line 7, in findFace
    faces = faceCascade.detectMultiScale(imgGray,1.1,8)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
zdavis
  • 1

2 Answers2

0

Avoid jiggle or jumpy rectangles for the best results. In line 7, I changed the second and third parameters.

import cv2
import numpy as np

def findFace(img):
    faceCascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_default.xml')
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(imgGray,1.3, 5)

    myFaceListC = []
    myFaceListArea = []

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x + w, y + h),(0,0,255),2)


cap = cv2.VideoCapture(0)

while True:
    _, img = cap.read()
    findFace(img)
    cv2.imshow("Output", img)
    cv2.waitKey(1)  
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
0

Your error:

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

OpenCV uses the empty() method of the CascadeClassifier to test whether the cascade was loaded properly, in each detect() call. It does not refer to image data passed to the detect() call.

Right after instantiation, check:

# faceCascade = cv2.CascadeClassifier('Resources/haarcascade_frontalface_default.xml')
assert not faceCascade.empty(), "it didn't find the XML file"

If that fails, you need to learn about relative paths and the Current Working Directory. print(os.getcwd()) and consider if that is where you expected your program to run.

There are a few ways to fix the issue. The recommended way is:

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

but that assumes them to be where OpenCV was installed. If you put those files elsewhere, you are responsible for finding them.

Many people have had this error message. Always use the search function. You should have found this previous question with some good answers


Messing with the parameters of the detect call won't do anything at all to fix your issue.

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