Fun with Python, OpenCV and face detection
I had some fun with Gary Bishop’s OpenCV Python wrapper this morning. I wanted to try out OpenCV for detecting faces using a web cam. This could be used for instance to see if someone is sitting behind his desk or not. I used Gary’s Python wrapper since I didn’t want to code in C++.
I didn’t know where to start, so I searched for existing OpenCV face detection examples. I found a blog post by Nirav Patel explaining how to use OpenCV’s official Python bindings to perform face detection. Nirav will be working on a webcam module for Pygame for the Google Summer of Code.
I managed to rewrite Nirav’s example to get it working with CVtypes:
Here’s the code. Although it’s just a quick and dirty hack, it might be useful to others. It requires CVtypes and OpenCV, and was tested on Ubuntu Hardy with a Logitech QuickCam Communicate Deluxe webcam. You will need Nirav’s Haar cascade file as well.
import sys from CVtypes import cv def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point( int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) if __name__ == "__main__": print "OpenCV version: %s (%d, %d, %d)" % (cv.VERSION, cv.MAJOR_VERSION, cv.MINOR_VERSION, cv.SUBMINOR_VERSION) print "Press ESC to exit ..." # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480) # check if capture device is OK if not capture: print "Error opening capture device" sys.exit(1) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) if frame is None: break # mirror cv.Flip(frame, None, 1) # face detection detect(frame) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
A known problem is that pressing the escape key doesn’t quit the program. Might be something wrong in my use of the cv.WaitKey function. Meanwhile you can just use Ctrl+C. All in all, the face detection works pretty well. It doesn’t recognize multiple faces yet, but that might be due to the training data. It would be interesting to experiment with OpenCV’s support for eye tracking in the future.
Update: the script does recognize multiple faces in a frame. Yesterday when Alex stood at my desk, it recognized his face as well. I think it didn’t work before because I used cv.Size(100, 100) for the last parameter of cv.HaarDetectObjects instead of cv.Size(50, 50). This parameter indicates the minimum face size (in pixels). When people were standing around my desk, they were usually farther away from the camera. Their face was then probably smaller than 100×100 pixels.
Just a quick note on ctypes. I remember when I created PydgetRFID that I tried to use libphidgets’ SWIG-generated Python bindings, but couldn’t get them to work properly. I had read about ctypes, and decided to use it for creating my own wrapper around libphidgets. Within a few hours I had a working prototype. When you’re struggling with SWIG-generated Python bindings, or have some C library without bindings that you would like to use, give ctypes a try. Gary Bishop wrote about a couple of interesting ctypes tricks to make the process easier.
Tags: CVtypes, facade, facedetection, opencv, programming, python, webcam

June 27th, 2008 at 9:03 pm
[...] Blog « Fun with Python, OpenCV and face detection [...]
June 28th, 2008 at 8:09 am
excellent stuff. Many thanks
June 28th, 2008 at 10:53 am
You're welcome! I'm glad you find it useful.
June 30th, 2008 at 9:57 pm
Just to clarify, the Haar cascade file is one of the sample ones that comes with OpenCV. The script I wrote was based on the OpenCV face detection sample too, but using gstreamer instead of OpenCV's HighGUI to interface the webcam (HighGUI didn't support the camera in the XO).
Its good that you got it working though. There is some pretty amazing stuff in OpenCV.
July 1st, 2008 at 1:34 am
You are right, I noticed yesterday that the cascade file was part of OpenCV. Thanks for clarifying!
July 2nd, 2008 at 4:09 pm
Nice script , worked fine for me @30fps
September 25th, 2008 at 2:18 pm
I figured it out how to properly use cvWaitKey.
In your code you are waiting just 10 ms for a key, and only after the image was processed.
So, if you wait a little bit longer(maybe 100ms, but this will slow down a lot the fps) you will be able to catch the keys.
However, there is also another problem cvWaitKey returns -1 when no key was pressed, but return a string(char) holding the key pressed, so you need to do the following:
if k!=-1 and ord(k) == 0x1b: # ESC
print 'ESC pressed. Exiting …'
break
October 12th, 2008 at 7:25 am
Ah thanks, that's very helpful!
October 17th, 2008 at 9:26 am
this is great stuff! thanks for this…
We managed to get up to 4 faces recognized at the same time.
October 17th, 2008 at 9:29 am
You're welcome, I'm glad it was useful for you.
December 10th, 2008 at 6:52 pm
[...] detection, I just found that OpenCV has camera capture program built in. Somebody has been playing (Fun with Python, OpenCV and face detection) with it for a while. His approach is to use a OpenCV python wrapper – CVtypes , the premise is to [...]
January 4th, 2009 at 3:32 pm
When I tried runing the above code in windows xp, encountered the following error.
D:SWCamFD>python test2.py
Traceback (most recent call last):
File “test2.py”, line 2, in <module>
from CVtypes import cv
File “D:SWCamFDCVtypes.py”, line 42, in <module>
_cxDLL = cdll.cxcore100
File “C:Python25libctypes__init__.py”, line 423, in getattr
dll = self._dlltype(name)
File “C:Python25libctypes__init__.py”, line 348, in init
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
Any help regarding this will be great!
January 5th, 2009 at 4:38 am
You need to have OpenCV installed. I guess that is the problem. I didn't try it on Windows yet since there's no D-BUS support, but in theory the face detection code should work.
I will probably release a first version of the code in the following weeks, so stay tuned!
January 8th, 2009 at 9:26 pm
Hello, Sir
I'm from Asia. I'm just starting to learn openCV.
Is there any opinion or advise from Sir as an expert to me to start learning opencv
Thank you very much.
February 20th, 2009 at 11:31 pm
[...] it seems that most OpenCV face detector examples are meant to be run “live”, usually taking the image from a webcam and highlighting [...]
April 17th, 2009 at 1:25 am
WaitKey function. Meanwhile you can just use Ctrl+C. All in all, the face detection works pretty well. It doesn’t recognize multiple faces yet, but that might be due to the training data.
May 9th, 2009 at 8:16 am
[...] the way how to access the camera is inspired by this nice blog post about face recognition using [...]
May 11th, 2009 at 2:56 am
I am into doing same stuff but sad to say I'm still at the first phase of learning. I look forward to being like you so I can also share my know how for others to use.
May 19th, 2009 at 2:14 pm
how is running? fast or not? I think opencv is universal thing… if u write your own codes it will be faster than opencv because there too many functions and processes unneedful… so goodluck!!!
May 30th, 2009 at 6:15 pm
hi i want to put the window's webcam in other program both under python, but i dont how to do it, the project is a program with bottons and they controll a robot and the robot get the webcam, by the way im using linux mint 6
August 9th, 2009 at 2:36 am
Hello!
Your if script silently hangs on this line for me:
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))
What can be the problem here? This xml exists in same directory and the is no any error output.
Thanks in advance!
August 10th, 2009 at 7:22 am
Are you using Ubuntu?
August 10th, 2009 at 7:23 am
It might be good to use a specific GUI toolkit in this case, such as WxWindows.
Have a look at Gary Bishop's blog post, where he grabs a bitmap from the camera and displays this in a Wx window: http://wwwx.cs.unc.edu/~gb/wp/blog/2007/02/04/p…;
August 20th, 2009 at 7:32 pm
The script I wrote was based on the OpenCV face detection sample too, but using gstreamer instead of OpenCV's HighGUI to interface the webcam (HighGUI didn't support the camera in the XO).
August 27th, 2009 at 1:00 pm
hi, i'm having this problem when i'm running your code..
i have an integrated camera on my laptop, which works with some apps like cheese.
while running this code, the camera(led) turns on, but nothing comes on the screen.
the script hangs and i have to ctrl+C to close it.
any pointers to help me with this situation will be appreciated.
thanks
September 11th, 2009 at 12:00 pm
Hello, i try your code but i have this:
cyril@zilo2:~/Bureau/test$ sudo python webcam2.py
Traceback (most recent call last):
File “webcam2.py”, line 3, in <module>
from CVtypes import cv
ImportError: No module named CVtypes
i use ubuntu, how cab i install opencv? sudo apt-get install ?
Thinks @+
September 13th, 2009 at 7:26 am
yes, 9.04
September 13th, 2009 at 7:39 am
In synaptic you can fing python-opencv
September 14th, 2009 at 3:08 pm
Hello, i have this:
Traceback (most recent call last):
File “opencv.py”, line 2, in <module>
from CVtypes import cv
ImportError: No module named CVtypes
What is this mistake? how install cvtypes?
Thinks
September 14th, 2009 at 3:10 pm
I have do sudo apt-get install python-opencv. It is already installed. What can i do?
September 23rd, 2009 at 5:23 pm
I'm still having trouble with the escape key. Any tips?
October 30th, 2009 at 6:39 am
The script uses Gary Bishop's CVtypes, not python-opencv.
October 30th, 2009 at 3:59 pm
[...] previous posts on face detection with Python and on changing the user’s presence when he/she is detected by the webcam are two of the most [...]
November 4th, 2009 at 9:25 am
this is awesome, i have been wanting to do this with python for awhile now. great stuff.
November 13th, 2009 at 12:35 am
hey!! i am nj here, i am trying to execute this program on windows I am getting following error. I saw a post with same thing, where u said u will be releasing new version so that it works on windows. Is it not released yet?
November 13th, 2009 at 7:06 am
You should install OpenCV first, and try to run the simple_winclient.py script. The software is available at my website.
December 4th, 2009 at 2:50 pm
hi, i stumbled upon your little nice python script a few days ago and ported it to opencv 2.0 with its new python bindings. Hope anyone is interested, maybe you can update your version with this.
import sys
import cv
class FaceDetect():
def init(self):
cv.NamedWindow (“CamShiftDemo”, 1)
device = 0
self.capture = cv.CaptureFromCAM
capture_size = (320,200)
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH, capture_size0)
cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT, capture_size1)
def detect(self):
cv.CvtColor(self.frame, self.grayscale, cv.CV_RGB2GRAY)
#equalize histogram
cv.EqualizeHist(self.grayscale, self.grayscale)
# detect objects
faces = cv.HaarDetectObjects(image=self.grayscale, cascade=self.cascade, storage=self.storage, scale_factor=1.2,
min_neighbors=2, flags=cv.CV_HAAR_DO_CANNY_PRUNING)
if faces:
#print 'face detected!'
for i in faces:
if i1 > 10:
cv.Circle(self.frame, ((2*i0[0]+i0[2])/2,(2*i0[1]+i0[3])/2), (i0[2]+i0[3])/4, (128, 255, 128), 2, 8, 0)
def run(self):
# check if capture device is OK
if not self.capture:
print “Error opening capture device”
sys.exit(1)
self.frame = cv.QueryFrame(self.capture)
self.image_size = cv.GetSize(self.frame)
# create grayscale version
self.grayscale = cv.CreateImage(self.image_size, 8, 1)
# create storage
self.storage = cv.CreateMemStorage(128)
self.cascade = cv.Load('haarcascade_frontalface_default.xml')
while 1:
# do forever
# capture the current frame
self.frame = cv.QueryFrame(self.capture)
if self.frame is None:
break
# mirror
cv.Flip(self.frame, None, 1)
# face detection
self.detect()
# display webcam image
cv.ShowImage('CamShiftDemo', self.frame)
# handle events
k = cv.WaitKey(10)
if k 0x1b: # ESC
print 'ESC pressed. Exiting …'
break
sys.exit(1)
if __name__ “main“:
print “Press ESC to exit …”
face_detect = FaceDetect()
face_detect.run()
December 4th, 2009 at 2:52 pm
argh, it killed my tabs!!!
December 7th, 2009 at 3:21 pm
Thanks a lot! I'll give it a try!
December 17th, 2009 at 11:53 pm
So cool. Python can make opencv more easy and funny.
December 23rd, 2009 at 3:20 am
Its really cool, I came to know this really worth visiting, just bookmarked your site.
http://gisnap.com/
The place where fun never ends
December 30th, 2009 at 3:44 am
hi…hw can i edit this coding so that i can detect an image that i have saved. i'm using ubuntu 9.04….need ur help…thank u…
December 30th, 2009 at 8:42 am
its been great coming across your site.however is there a way for me detect and display the available webcams conected to a pc.i tried searching online buy couldnt comeup with anything.Pygame 1.9 however has a module to take care of that.but the problem with pygame is .i cant include it in the gui of my project.not can i attach any stuff like buttons onto the defaul gui provided.would really appreciate if you could help my out.thanks in advance
January 4th, 2010 at 3:41 am
I know there is a function to get the number of cameras:
cvcamGetCamerascount()
Maybe that helps?
January 5th, 2010 at 10:39 am
thanks jo.well i kindda figured a way out last week. and these are basically the codes
def listWebcam(self,path='/dev'):
“”"detects the available webcam mounted to a computer “”"
devicelist = []
for device in os.listdir(path):#loops through devices all mounted devices
if device.startswith('video'):#returns true if a device with video prefix is found
devicenum = device.rsplit('o')#seperates video from the device index
devicelist.append(devicenum1)
return devicelist
January 5th, 2010 at 10:53 am
That's Linux-specific of course, but will indeed do the trick.
January 5th, 2010 at 11:09 am
yup,sorry i guess i forgot to mention i was looking for linux specific solution.
oh and by the way.i do have some issues with the camera though.when i connect to the webcam.sometimes it just doesnt connect and the entire application just closes.
that is when i change the device value to different numbers .”self.capture = cv.CaptureFromCAM.i tested it with three webcams connected.it connected to two but not to the third.and then i removed the third and it just connected to one.
any idea why this is happening
January 12th, 2010 at 1:05 am
[...] site. Strange! Well, I still managed to put together a small prorgram (borrowing heavily from here) called grabface that grabs a picture using the webcam and then figures out the position of your [...]
January 12th, 2010 at 8:13 am
Thank you for posting it online. I find it useful.
January 12th, 2010 at 8:26 am
You're welcome! I'm glad you found it useful.
January 19th, 2010 at 3:24 am
Really-really helpful! Thank you so much for sharing this code
January 20th, 2010 at 3:59 am
hi. i have this
Null pointer (Invalid classifier cascade)
in function cvHaarDetectObjects,
C:UserVPoperncvcvsrccvhaar.cpp(890)
January 20th, 2010 at 4:48 am
Are you sure you have the cascade file in the same directory?
January 20th, 2010 at 4:58 am
C:DevOpenCVdatahaarcascades
January 20th, 2010 at 5:11 am
I currently don't have a Linux machine for testing, but IIRC the line
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))
requires the cascade file to be in the same directory as the script.
By the way, the script I presented in this blog post won't work on Windows. You should use the simple_winclient.py script for that (which can be found in the tarball available from my website).
January 20th, 2010 at 8:31 am
thank you very much Jo ^^
January 20th, 2010 at 8:33 am
You're welcome!
January 20th, 2010 at 9:59 am
hi. i have this
Null pointer (Invalid classifier cascade)
in function cvHaarDetectObjects,
C:UserVPoperncvcvsrccvhaar.cpp(890)
January 20th, 2010 at 10:48 am
Are you sure you have the cascade file in the same directory?
January 20th, 2010 at 10:58 am
C:DevOpenCVdatahaarcascades
January 20th, 2010 at 11:11 am
I currently don't have a Linux machine for testing, but IIRC the line
cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1,1))
requires the cascade file to be in the same directory as the script.
By the way, the script I presented in this blog post won't work on Windows. You should use the simple_winclient.py script for that (which can be found in the tarball available from my website).
January 20th, 2010 at 2:31 pm
thank you very much Jo ^^
January 20th, 2010 at 2:33 pm
You're welcome!
March 2nd, 2010 at 8:00 am
Hey great post. I can't wait to get started hacking it to my needs. I made a script that tracts an IR LED and maps the mouse to the movements(http://hackaday.com/2010/02/25/python-ir-tracking-for-the-handicapped/).
I used VideoCapture(http://videocapture.sourceforge.net/) for the webcam interface though. I'm going to try and map the mouse movements to this new face-recognition.
March 11th, 2010 at 5:28 pm
After about a week of work, I couldn't get opencv to work. I used VideoCapture again, and fdlib.dll to detect the faces. http://www.kyb.mpg.de/bs/people/kienzle/facedem…;
April 6th, 2010 at 8:05 am
Me and my friend want to set up a funny type call of duty video type thing, but to do so we would need to. BlackStar69
April 6th, 2010 at 8:05 am
Me and my friend want to set up a funny type call of duty video type thing, but to do so we would need to. BlackStar69
April 12th, 2010 at 11:09 pm
Hey,
I just found your amazing code on the web. I'm trying to make a eye tracking device for a paralized girl. I tryed your code but got this error.
Can you help me with this problem. I really don't know what to do.
Traceback (most recent call last):
File “D:My DocumentsMijn documentenTWGanotherdetection.py”, line 65, in <module>
detect(frame)
File “D:My DocumentsMijn documentenTWGanotherdetection.py”, line 22, in detect
faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50))
WindowsError: exception: access violation reading 0×00000028
Thx,
Stefan (from the Netherlands)
April 13th, 2010 at 8:03 am
Hey,
I found the solution already. Needed to put the
'haarcascade_frontalface_default.xml' file in the same directory as your little programm. When I runned the code, it seems to work fine. I only noticed that the program is often detecting my chin and mouth.. and not the whole face. Is this because I'm to close to my cam or is there a other solution for.
Thx,
Stefan
ps: I also needed to put “from opencv.highgui import *” above the code. Did yours work without?
April 20th, 2010 at 12:34 am
OpenCV has reasonably robust Python interface created with SWIG. This makes it easy to integrate with existing libraries like Pygame and olpcgames. As an example, OpenCV can be combined with Pygame to make a simple face tracking xeyes clone.
Thanks.
May 4th, 2010 at 3:05 am
dont work for me
from CVtypes import cv
ImportError: No module named CVtypes
May 11th, 2010 at 9:58 am
i read this and detection is one component of … It is a simple python script that will connect to your webcam
June 15th, 2010 at 11:15 pm
“When you’re struggling with SWIG-generated Python bindings, or have some C library without bindings that you would like to use, give ctypes a try. “
This reminded me of this
“If you have a problem – if no one else can help – and if you can find
them – maybe you can hire: The A-Team.”
I almost want somebody to geekerise the lyrics and make them about ctypes now.
June 20th, 2010 at 2:04 am
[...] In Traction » Blog Archive » Fun with Python, OpenCV and face detection (tags: python opencv webcam facedetection face) Published: June 20, 2010 Filed Under: Delicious Leave a Comment Name: Required [...]
July 2nd, 2010 at 11:27 pm
wow thats some crazy code
July 3rd, 2010 at 11:58 pm
I got lost half way through your code
July 5th, 2010 at 6:19 am
Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
Gucci sweaters
If its common it might be difficult to get, however, be creative and you can usually find something.
July 13th, 2010 at 2:34 am
Golden. Great, useful info.
August 2nd, 2010 at 1:00 am
Absolutely incredible – technology is certainly an amazing thing!
—
Rick, MJ Researcher
August 2nd, 2010 at 5:35 am
Nice! I've got some motion detection going on over here if you're interested: http://appdelegateinc.com/blog/2010/08/02/motio…;
Did you buy the book? I find the OpenCV documentation is very hard to follow.
August 22nd, 2010 at 10:09 am
Muito obrigado pelos elogios…de coração.
Se depender de mim, estarei sempre compartlhando minha paixão, que são os games!!
Quanto a fase da aranha realmente eh dificil, mas é ali onde vc realmente fica “ninja” nas suas habilidades , e ai o resto do game fica muito mais atenuado, hehe como se fosse uma academia essa fase, rs.
Abração amigo
online jogos