Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Face detection using Cascade Classifier in opencv python

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

like image 381
praddy26 Avatar asked Jun 16 '15 02:06

praddy26


2 Answers

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.

like image 190
akarsakov Avatar answered Oct 04 '22 07:10

akarsakov


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))
like image 39
Zhanwen Chen Avatar answered Oct 04 '22 07:10

Zhanwen Chen