Face Detection using Open CV and Python

Swami Nikhilaananda said:
"Face Detection and other awesome stuff have been greatly simplified with OpenCV and Python"

I am no expert in this field, but I was just excited to have a piece of code working. In this little program, we attempt to do a face detection using Python and OpenCV.

First get Open CV and Python up and running following simple tutorials found here.

The following is a piece of code - Replace the path of the Haar Classfier file (xml) with the one in your system. Save the file, and run the script.

As you could have probably guessed, absolutely nothing intelligent is there in the program above. Everything is taken care of by OpenCV. Which is what makes it really good for people who don't know much of Image Processing, nor want to know the mathematical nitty gritties.

Perhaps in the next post, you should be able to see how to Glow an LED when a face is detected - by interfacing with Arduino

You can see LED Glowing with Face Detection using Arduino here

Taking Face Detection to the next level? Instead of merely putting a rectangle, you can perhaps overlay stuff... Here's my first rough attempt to mimic Google's Reindeer stunt on Hangout


Ahirnish said…
Hi, Thanks for the code. It worked well.

Can you tell me what changes should I make if i want to detect face in an image. Your code basically activates the webcam and face is detected in the live feed from the webcam. I want the code to look up for an image, detect face in it and save it.

Please help me. Thanks
Hi Ahirnish,

That's actually easier. Instead of reading the image from the webcam, you just read it from a file. Just use cv.LoadImage("filename") to get the image, instead of from webcam... and use cv.SaveImage("filename", image) to save it.
Ahirnish said…
So instead of the line "cap=cv.CreateCameraCapture(0)", I replace it with "cap=cv.LoadImage("myimage.jpg")" and rest of the things remains same.
Am I right?
Ahirnish said…
Hi again,

I am able to do it but your code does not detect multiple faces. I have a image in which there are 2 faces but the saved image has rectangle only on 1 image and that too on left one. Any possible explanation?

How to detect multiple faces?

Also I want to click image from a webcam, store it, then invoke the image by LoadImage function and then save the image with detected faces. So I've already implemented the latter part. How to click images from webcam and store it using python code and opencv?

Thank You.
Multiple face detection is something even I haven't tried as of now.

Regarding the other requirement, it's very simple. Follow the steps below
1. Read from webcam like how you did before
image = cv.QueryFrame(cap)

2. Save image - cv.SaveImage("myimage.jpg", image)
yudhaardiansyah said…
sorry for distrubing you, but i just want to share the article about face detection,
let's check this link http://repository.gunadarma.ac.id/bitstream/123456789/3365/1/Automed%20Face%20Detection%20and%20Feature%20Extraction%20Using%20Color%20Feret%20Image%20Database.pdf
i wish that article can be usefull

Popular posts from this blog


The (fake) Quest To Eradicate AIDS with Mythical Mystical Indian roots

Mongoose - An Indian Card Game