Ask Your Question
0

Python. Simple image Face Detection using OpenCV

asked 2013-07-07 03:46:26 -0600

CharlyChi gravatar image

I'm noobe in OpenCV and looking for simple python face detecting script. I've find one here http://suksant.com/2013/04/03/simple-face-detection-using-opencv/

But there is no rect. on face when script stops.

#!/usr/bin/python

import cv2
img = cv2.imread("Lenna.png")
hc = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
faces = hc.detectMultiScale(img)

for face in faces:
  cv2.rectangle(img, (face[0], face[1]), (face[0] + face[2], face[0] + face[3]), (255, 0, 0), 3)

cv2.imshow("Lenna's face", img)
if cv2.waitKey(5000) == 27:
  cv2.destroyWindow("Lenna's face")
cv2.imwrite("LennaFace.png", img)
edit retag flag offensive close merge delete

Comments

That should be cv2.rectangle(img, (face[0], face[1]), (face[0] + face[2], face[1] + face[3]), (255, 0, 0), 3) -- not face[0] + face[3]

Wilfred Hughes gravatar imageWilfred Hughes ( 2013-09-20 10:59:23 -0600 )edit

face[0] + face[3] at line 8 should be face[1] + face[3] . It also makes sense to iterate over tuples of (x, y, w, h) and set points to (x, y) and (x + w, y + h).

ffriend gravatar imageffriend ( 2013-10-19 15:35:05 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-11-02 03:27:48 -0600

There's no needed for grayscale images. Only thing you needed to do is, be sure "haarcascade_frontalface_alt2.xml" file nearby your source code directory. If not write the whole path to it.

Ps.: you may cheat from .../opencv-(version)/data/haarcascades/haarcascade_frontalface_alt2.xml

edit flag offensive delete link more
0

answered 2013-07-07 04:49:43 -0600

berak gravatar image

you need a grayscale image for this.

could convert after loading, but telling imread to do it, is easier:

img = cv2.imread("Lenna.png",0); // load 8bit gray
edit flag offensive delete link more

Comments

Works fine in my testing without converting to grayscale.

Wilfred Hughes gravatar imageWilfred Hughes ( 2013-09-20 10:58:38 -0600 )edit

Question Tools

Stats

Asked: 2013-07-07 03:46:26 -0600

Seen: 2,531 times

Last updated: Nov 02 '13