# cv2.error: OpenCV(3.4.3) (-215:Assertion failed) !empty() in function

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

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

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>

• 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 – Alberto Jan 31 '19 at 16:43
• Check whether your image is uint8 or not: image = image.astype(np.uint8) – vishal Jun 17 '19 at 13:42
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.