1
$\begingroup$

Would anyone have a tip on how to fix this empty function error in OpenCV? I am attempting to follow the guides on OpenCV.org

The script will detect a face in an image and draw boxes around detections.

import numpy as np
import cv2 as cv



face_cascade = cv.CascadeClassifier('haarcascade_frontalface_alt.xml')

img = cv.imread('images/1.png')
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    cv.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]


cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()

I get an error:

>>> 
=== RESTART: C:/Users/Desktop/distance-to-camera/selectHaar2.py ===
Traceback (most recent call last):
  File "C:/Users/Desktop/distance-to-camera/selectHaar2.py", line 11, in <module>
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
$\endgroup$
4
  • $\begingroup$ I think this may be due to the fact that XML file is missing or the path to it is incorrect. Could you please check that? Maybe you coul try with the full path for the haarcascade_frontalface_alt.xmlfile $\endgroup$ – Alberto Jan 31 '19 at 16:43
  • $\begingroup$ Yes that worked thank you. If you post an answer ill hit the green check box. Also do you know the difference between the haar XMLs for faces? Whats a best practice between frontface_alt2, frontalface_default, _alt_tree, and _alt...? It seems like most tutorials use _alt or _default... Thanks $\endgroup$ – HenryHub Jan 31 '19 at 18:20
  • $\begingroup$ Hi, I'm sorry but I don't know the answer to the second question about the best practice. $\endgroup$ – Alberto Jan 31 '19 at 21:16
  • $\begingroup$ Check whether your image is uint8 or not: image = image.astype(np.uint8) $\endgroup$ – vishal Jun 17 '19 at 13:42
1
$\begingroup$

I think this may be due to the fact that XML file is missing or the path to it is incorrect. Could you please check that? Maybe you coul try with the full path for the haarcascade_frontalface_alt.xml file.

$\endgroup$
0
$\begingroup$

I was facing this issue and by removing special characters from the image_file_name the issue was resolved.

Example: Renaming "48172454-thymianblätter.jpg" to "48172454-thymian.jpg".

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.