Capturing frames from a webcam on Linux
Not many people are trying to capture images from their webcam using Python under Linux and blogging about it. In fact, I could find nobody who did that. I found people capturing images using Python under Windows, and people capturing images using C under Linux, and finally some people capturing images with Python under Linux but not blogging about it. This instructional post I wrote to help those people who want to start processing images from a webcam using the great Python language and a stable Linux operating system.
There is a very good library for capturing images in Windows called VideoCapture. It works, and a number of people blogged about using it. I was jealous for a long time.
There are a number of very old libraries which were meant to help with capturing images on Linux: libfg, two separate versions of pyv4l, and pyv4l2. But the first doesn’t work on my computer, the two versions of pyv4l cause segfaults because they are so old and not updated, and the last has no code written.
Finally, I learned that OpenCV has an interface to V4L/V4L2. OpenCv is Intel’s Open Source Computer Vision library. It’s excellent, extensive, and has a good community behind it. V4L is Linux’s standard abstraction for reading in video data. V4L2 is the newer version which Ubuntu also has installed.
Plus, OpenCV has very complete Python bindings. Unfortunately, these bindings and how to use them properly to capture images from a webcam are not documented. Only after careful searching on the sizable OpenCV mailing list did I finally find the answer.
Below is code that reads in up to 30 frames per second from a web cam while simultaneously displaying what it reads in. It’s very cool. It uses opencv’s camera acquisition abstraction, PIL, and pygame for speed in the looping. Note that with the images read into Python, you and I can now do arbitrary things with the image. We can flip it, track objects, draw markers, or do really anything.
This is example utility code. It is not a well structured program. Much of the code I use below is from techlists.org.
[?]import pygame import Image from pygame.locals import * import sys import opencv #this is important for capturing/displaying images from opencv import highgui camera = highgui.cvCreateCameraCapture(0) def get_image(): im = highgui.cvQueryFrame(camera)# Add the line below if you need it (Ubuntu 8.04+) #im = opencv.cvGetMat(im)#convert Ipl image to PIL image return opencv.adaptors.Ipl2PIL(im) fps = 30.0 pygame.init() window = pygame.display.set_mode((640,480)) pygame.display.set_caption("WebCam Demo") screen = pygame.display.get_surface() while True: events = pygame.event.get() for event in events: if event.type == QUIT or event.type == KEYDOWN: sys.exit(0) im = get_image() pg_img = pygame.image.frombuffer(im.tostring(), im.size, im.mode) screen.blit(pg_img, (0,0)) pygame.display.flip() pygame.time.delay(int(1000 * 1.0/fps))
I searching this for hour, and it’s works !!
thank you, that really help me a lot.
NoopyKs
30 Dec 07 at 8:03 pm
Thanks so much for this post. 1 page of code. Great.
Just switched to linux and this was one of the main hardware
problems I ran into.
Nice work!
webbge
5 Jan 08 at 8:02 pm
Thx a lot. Does anybody knows if there is info on synchronizing the acquisition from two or more webcams. I want to play a bit with multiple view geometry
alon
20 Jan 08 at 3:47 pm
Great!
tanks it works for me!!
diego
28 Apr 08 at 1:08 pm
I would love to play with multiple view geometry also, is there anyone who has done that with python?
Bryan
20 Jun 08 at 7:14 pm
Thank you so much!!! I LOVE YOU TO BITS!!!!
I have been searching for about 4-5 hours now … And as you say in your intro .. no one blogs these kind of things… I almost stopped searching
BUT … You saved me
Thanks a million
Coral
[ZAF]-Coral
12 Jul 08 at 3:29 pm
Thanks for the research and code!
I tried this a few weeks back, and gave up after finding no hints that gave me the right direction.
This code works out of the box. Thank you!
Harshad
29 Sep 08 at 7:48 am
Hello Man.
10x, I checked around 5 other examples and this one works like a charm.
Shalom,
Ram-on.
Ram-on Agmon
7 Oct 08 at 6:33 pm
Hi,
I had to slightly modify it to run on ubuntu 8.04:
def get_image():
im = highgui.cvQueryFrame(camera)
# this one is the new line to make it work
tmp = opencv.cvGetMat(im)
#convert Ipl image to PIL image
return opencv.adaptors.Ipl2PIL(tmp)
kpoman
22 Oct 08 at 1:35 am
Hey thanks a lot!
Work very nice with my camera, good work!, Busman.
Busman
5 Nov 08 at 3:01 pm