Face detection using Cascade Classifier in opencv pyhton

asked 2015-06-15 23:34:11 -0500

pradeepln gravatar image

updated 2019-02-12 14:55:27 -0500

LBerger gravatar image

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:

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

What is wrong with the code?

edit retag flag offensive close merge delete

Comments

one of the cascade files probably was not found. add some check, like:

if cascade.empty(): raise Exception("your cascade is empty. are you sure, the path is correct ?")

berak gravatar imageberak ( 2015-06-16 00:04:07 -0500 )edit

you mean to say the 'haarcascade_frontalface_default.xml' file path?

pradeepln gravatar imagepradeepln ( 2015-06-16 00:07:14 -0500 )edit

yes, exactly. try an absolute path, like /my/opencv/data/haarcascades/haarcascade_frontalface_default.xml

berak gravatar imageberak ( 2015-06-16 00:09:15 -0500 )edit

yeah that was solved..but then faces are not detected...

pradeepln gravatar imagepradeepln ( 2015-06-16 00:22:19 -0500 )edit

problem solved

pradeepln gravatar imagepradeepln ( 2015-06-16 01:12:17 -0500 )edit