Python - Face detection (detecMultiScale)

时间:2016-02-12 21:43:47

标签: python image opencv

So I was trying a face detection program, and used this code below:

Microsoft.Web.WebJobs.Publish

The code is actually from here and it did not do its job. It showed me the image, but did not mark the face or eyes. I'm working on Raspberry Pi with Rasbian Jessie OS.

Here are some things I tried:

I tried cv2.imshow('img',gray) and it showed the gray image, which means BRG => gray is good.

I tried drawing a rectangle at a random place, so the cv2.rectangle function is good.

I checked the OpenCV/data folder, both 'haarcascade_frontalface_default.xml' and 'haarcascade_eye.xml' files are there.

I tried print (faces_cascade), and it returned "CascadeClaasifier 0x741b48d0"

I tried print (faces), and it returned (), which means nothing is inside this array.

I have tried 4,5 images with different brightness, none of them worked

I feel like the only part that could go wrong is this line:

import numpy as np
import cv2

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

img = cv2.imread('sachin.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()

since print(faces) did not return any value.

Could anyone help? Any help is appreciate.

Solved, instead of just 'haarcascade_frontalface_default,xml', type in the full directory to the file. Same to the eye detection part.

2 个答案:

答案 0 :(得分:0)

Solve. Instead of just 'haarcascade_frontalface_defuault.xml', type in the full directory to the file. Same to the eye detection part.

答案 1 :(得分:0)

我发现此代码也显示了错误的结果。有什么问题?