Demo Failure : Cannot detect face with funcntion face_cascade.detectMultiScale()

71 views
Skip to first unread message

Cool Brin

unread,
Dec 24, 2016, 8:21:10 PM12/24/16
to OpenCV with Python Blueprints
This is the result:


This is the demo codes:
 
import numpy as np
import cv2

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

img = cv2.imread('./nbaplayer/203500.png')
print img.shape
print img.size
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#print gray.shape
#print gray.size
#cv2.imshow('img1',gray)
cv2.imshow('hsv',hsv)
faces = face_cascade.detectMultiScale(gray,2,1)
print faces
for (x,y,w,h) in faces:
    img = 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.rectangle(img,(38,0),(23,128),(255,255,255),3)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 Where is wrong????

Thanks!!

Michael Beyeler

unread,
Dec 27, 2016, 11:42:38 PM12/27/16
to OpenCV with Python Blueprints
Hi,

If the pre-trained Haar classifier doesn't detect a face in your image, there are a few things you can try. The `detectMultiScale` function takes a few optional arguments that let you specify what to look for:
  • `minFeatureSize`: The minimum face size to consider (for example, 20x20 pixels)
  • `searchScaleFactor`: Amount by which to rescale the image (scale pyramid). For example, a value of 1.1 will gradually reduce the size of the input image by 10%, making it more likely for a face to be found than a larger value.
  • `minNeighbors`: The number of neighbors each candidate rectangle should have to retain it. Typically, choose 3 or 5.
Then it is still possible for the eye detector to fail. It's a good idea to crop the image so that only the face region is retained, which is exactly what you're doing with `roi_gray`. Great! There are a few more things you can try:
  • Use the same optional arguments as above to specify a typical eye size.
  • Further crop `roi_gray` to remove for example the mouth region.
  • Maximize the contrast of `roi_gray` using adaptive histogram equalization.
  • Switch to a better eye cascade, such as `haarcascade_lefteye_2splits.xml` and `haarcascade_righteye_2splits.xml`. If some of the players are wearing glasses, use `haarcascade_eye_tree_eyeglasses.xml`.
More details can be found in Chapter 7 of the book.

Best,
Michael
Reply all
Reply to author
Forward
0 new messages