Tuesday, 14 January 2014

Face Tracking and IP camera control with Python

How to read IP camera stream with OpenCV 2.4.6, track the face and adjust the camera to keep the face in center.

In this project I wanted to use face detection and track it.
For this I use OpenCV 2.4.6.
Also I use Python 2.6, gstreamer 0.10 and the python module Pycurl.

For IP camera I use the Wanscam FR4020A2 PT.
The computer hardware I have is a i5 Laptop with 8 Gig of ram.
All running on Ubuntu 10.04 x64, but I also used Ubuntu 12.04 x64, perhaps anything that runs OpenCV and Python will do.
Just remember that gstreamer can (also) take a lot of CPU so this can hang the Python script in adjusting the view and thus you lose face focus!

In OpenCV you can adjust the level how hard it will work to recognize the face.
This can help if slowness is an issue (please read OpenCV documentation for details on this).

It all started when I came across this tutorial to control you webcam with servo's.
I just needed to adjust the servo control with IP camera control, I got from here.

A few things you need to do yourself:
Set up a loopback device 
 modprobe v4l2loopback devices=2
and a gstreamer stream.
 gst-launch-0.10 souphttpsrc location=""  ! multipartdemux ! jpegdec !  v4l2sink device=/dev/video1
I did need to compile and install the v4l2loopback what I got from here.

All this combined will show you:
At some point the script crashed because of connection lost with the IP camera.

You can get my code from github.

It is still a work in progress, because I would like to be able to do error catching when connection with camera is lost. Also looking in to use Python threading so perhaps some speed increase!

Finally I have another one of the same IP camera so want to try and enable stereo version.

Thanks for reading!