7
votes

I am using the inbuilt cascade classifier for the face detection. This is how the code is (OpenCV Python Tutorials):

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

img = cv2.imread('ammma.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

for (x,y,w,h) in faces:
    cv2.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]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for(ex,ey,ew,eh) in eyes:
        cv2.Rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

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

But when I run the code I am getting the following error:

C:\Python27\python.exe C:/Users/DELL/Downloads/Amma/code/fd.py

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp, line 1634 Traceback (most recent call last): File "C:/Users/DELL/Downloads/Amma/code/fd.py", line 10, in faces = face_cascade.detectMultiScale(img, 1.3, 5) cv2.error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\objdetect\src\cascadedetect.cpp:1634: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale

9
It should be cv2.rectangle(...) instead of cv2.Rectangle(...)Jeel Shah

9 Answers

30
votes

Refer to this line of code, it failed on checking that cascade is non empty. Please check path to XML files with trained cascades. You may need to specify full path to XML's like this:

face_cascade = cv2.CascadeClassifier('D:\opencv\data\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('D:\opencv\data\haarcascades\haarcascade_eye.xml')

Or just put this files to directory containig your script.

11
votes

You do NOT need to download or copy the .xml files. According to the OpenCV-Python PyPi page, you can simply use the packaged path to the installed cascades - cv2.data.haarcascades:

import cv2

# Globals
FACE_CLASSIFIER = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
EYE_CLASSIFIER = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
SCALE_FACTOR = 1.3
BLUE_COLOR = (255, 0, 0)
MIN_NEIGHBORS = 5

# Then use it however you'd like
try:
    faces = FACE_CLASSIFIER.detectMultiScale(gray, SCALE_FACTOR, MIN_NEIGHBORS)
    for (x, y, w, h) in faces:
        cv2.rectangle(self.roi_frame, (x, y), (x+w, y+h), BLUE_COLOR, HAAR_LINE_THICKNESS)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = self.roi_frame[y:y+h, x:x+w]
        eyes = EYE_CLASSIFIER.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), GREEN_COLOR, HAAR_LINE_THICKNESS)
except Exception as e:
    warnings.warn('{}.show_haar_features: got exception {}'.format(__name__, e))
2
votes

I just happened to see this post when I faced a similar issue. I successfully resolved the error by executing the below 2 lines:

face_cascade = cv2.CascadeClassifier('opencv-3.0.0/data/harcascades/haarcascade_frontalface.xml')

eye_cascade = cv2.CascadeClassifier('opencv-3.0.0/data/harcascades/haarcascade_eye.xml')

Maybe, it will help others in resolving the same!

1
votes

I faced the same problem. First you will have to give the correct path of the file to the system, which looks like this: /home/xxxx/Desktop/Projects/haarcascade_eye.xml. Next, you'll have to open the code on github and save the raw version of the code for it run. It's advisable for both the files to be in the folder where you are storing your project file. Happy coding

0
votes

Well If you are getting this error then you need to download those 2 .xml files, because python can't find them on pc. I had same problem, then I downloaded the .xml files and kept it in folder where my .py file is. and then I got the perfect output. For downloading the files, search the file name and download it from sourceforge.

0
votes

This post is old, but here is my answer nevertheless. Using absolute path did not work. Turns out that the XML file that I had downloaded (using curl) was corrupt. I had to manually copy and paste the contents from the github folder after which everything started working. Zhanwen Chen's answer also works (which I believe is the right way to do it for in-built classifiers).

0
votes

Maybe, you use virtualenv for python like me. You try this.

import os

base_directory = os.path.abspath(os.getcwd())
directory_cv2 = os.path.join(base_directory, "Lib", "site-packages", "cv2","data")
print("la carpeta es: {}".format(directory_cv2))
0
votes

In newer versions of opencv haarcascades also gets installed you just need the file location of haarcascades, to get it you can use cv2.data.haarcascades just like shown below

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

0
votes

https://github.com/udacity/P1_Facial_Keypoints/issues/13#issuecomment-385461579

After following the following comment. In you jupyter lab, open the kernel from the options where 'File', 'Edit', 'View', etc. Now choose, 'Change Kernel' and select 'Python [conda env:root]'.

Do this and you are done.