Sunday, January 20, 2008

webcam with linux and python


















I needed a very super simple code snippet to get live video from a webcam. After some Google searches I realized that is not that simple. A lot of great applications can capture video (videolan, mplayer, gstreamer launchpad, etc.) but if you want a snippet of code to embed in your applications you need to fight with the v4l driver. In windows there is http://videocapture.sourceforge.net/ but it does not run in Linux. Fortunately I've found a wrapper called pyv4l (http://pyv4l.sourceforge.net/) quite old but it works. pyv4l makes all the uninteresting low level work so I implemented a GUI with pygtk2 and a simple camera.py module which makes another step of abstraction on top of pyv4l

Usage:
make an instance of class Camera -> cam=camera.Camera()
Open the camera: cam.open()
If you need a one shot use grab: cam.grab()
If you need a stream register a callback: cam.start(callback)
To stop the stream use stop: cam.stop()
To release the device use close: cam.close()

camera.grab is for pulling images, while start,stop are for pushing asynchronous data


Source code is zipped here.

videocapture-0.0.1.py
videocapture-0.0.1...
Hosted by eSnips

No comments: