Archive | December, 2012

Face Detection using OpenCV in python

8 Dec

Well, this is my first hand experience with openCV as well as with python.So, I’ll start slow but keep patience.But First let me clear a misconception of most of the newbie(I myself had for a long time), OpenCV is not a software or a programming language.It is a library like any other library but with special functionality.

I’ll start with installation of OpenCV. Wait, there are many blogs for it.Just Google it and first 4 links will get you the latest installation of OpenCV.If you dont get it search again :P. That’s how you learn to explore.

Now getting back to face detection problem.Its a quite tricky problem if you dont know from where to start. but here i’ll provide you with just brief idea of how the backstage things works

My machine includes a Compaq presario i3 station with in-built webcam and I am working on ubuntu 11.10, openCv 2.4.0 and latest python.

So here it is 

######################################################################

import cv
capture=cv.CaptureFromCAM(0)
image=cv.QueryFrame(capture)

 

count=0

while count<250:
image1=cv.QueryFrame(capture)
storage = cv.CreateMemStorage()
hc=cv.Load(“/home/abhinav/OpenCV-2.4.0/data/haarcascades/haarcascade_frontalface_default.xml”)

face=cv.HaarDetectObjects(image1,hc,cv.CreateMemStorage(),1.2,2,cv.CV_HAAR_DO_CANNY_PRUNING,(0,0))
for ((x,y,w,h),k) in face:
cv.Rectangle(image1,(int(x),int(y)),(int(x)+w,int(y)+h),(255,0,0),1,0);
cv.ShowImage(“image1”,image1)

cv.WaitKey(1)
count=+1

#####################################################################

Now if you copy paste the code it might not work because of indentation problem in python. So check the indentation, and one more thing I deliberately haven’t added ending the program. 😛

Now exams are over and BTP presentation is over so lets have a Blast. 

 

Advertisements