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
Cool! I will be trying this out very soon, I think you have saved me a bit of work!
This was the first thing Google brought me, and probably all I need to get started!
Steve B
22 Dec 08 at 10:53 am
doesnt work with me in ubuntu 8.10
Traceback (most recent call last):
File “web.py”, line 30, in
im = get_image()
File “web.py”, line 15, in get_image
tmp = opencv.cvGetMat(im)
File “/var/lib/python-support/python2.5/opencv/cv.py”, line 3826, in cvGetMat
return _cv.cvGetMat(*args)
RuntimeError: openCV Error:
Status=Null pointer
function name=cvGetMat
error message=NULL array pointer is passed
file_name=cxar
Mir
29 Dec 08 at 11:16 am
Ubuntu 8.10, still compiles and installs, just throws the error (same as Mir, above) :
Traceback (most recent call last):
File “example.py”, line 29, in
im = get_image()
File “example.py”, line 14, in get_image
im = opencv.cvGetMat(im)
File “/var/lib/python-support/python2.5/opencv/cv.py”, line 3826, in cvGetMat
return _cv.cvGetMat(*args)
RuntimeError: openCV Error:
Status=Null pointer
function name=cvGetMat
error message=NULL array pointer is passed
file_name=cxarray.cpp
line=2780
Max M
21 Jan 09 at 5:58 am
You, Sir, are my hero! Thank you so much! I’ve spent countless hours trying to get one of the pyv4l libs to work!
Galileon Galilei
23 Jan 09 at 7:27 pm
I to have had trouble getting it to run on Ubuntu 8.10. I get the same error as those above. Any help would be greatly appreciated.
Danny
23 Jan 09 at 11:53 pm
Genius! It works “out of the box” (out of the paste and copy, actually) for me, and I use ubuntu 8.10 too.
Maybe because I’m using a v4l2 webcam?
Thanks anyway,
Julian
julian
30 Jan 09 at 12:56 am
Works like a charm .. thanks a billion;
Could you tell how we can capture images from the feed.
That would be helpful..
Snape
31 Jan 09 at 6:26 am
almost works for me (hardy) — but the image comes up upside down & green.
First frame looks like proper color, then it changes. Not a display thing, I can save the file as .jpg and it looks the same.
Anyone have any ideas?
dan miller
3 Feb 09 at 11:13 pm
Worked as shown on Jaunty Alpha 5 on an Acer Aspire One with built-in webcam – nice, compact code and great exposure to OpenCV, PIL, pygame and other code. Thanks!
Brian
3 Mar 09 at 3:54 am
hi i clicked the V4L/V4L2 link above but it gave me an error 404: The page you have requested could not be found.
any links?
thank you so much for the help.
roy
5 Mar 09 at 12:46 pm
I got the “NULL array pointer is passed”, when I forgot to compile opencv with v4l2 support.
unlotto
12 Mar 09 at 4:02 pm
Thanks for this!! Also spent 5 hours searching and finally found this article.
Luke Venediger
13 Mar 09 at 8:33 am
Thanks for posting this, exactly what I was looking for to do video processing on linux in python.
Clayton Gulick
21 Mar 09 at 7:01 pm
Hey man works great on my mobii netbook with Ubuntu 8.04
Thanks a lot!
Seppel
19 Apr 09 at 1:06 pm
works great, but having trouble trying to get multiple webcams to work.
endeavormac
19 Apr 09 at 10:25 pm
I’m still getting this error like other users above:
pho@Asgard:~/Desktop$ python image.py
Traceback (most recent call last):
File “image.py”, line 29, in
im = get_image()
File “image.py”, line 14, in get_image
im = opencv.cvGetMat(im)
File “/var/lib/python-support/python2.5/opencv/cv.py”, line 3826, in cvGetMat
return _cv.cvGetMat(*args)
RuntimeError: openCV Error:
Status=Null pointer
function name=cvGetMat
error message=NULL array pointer is passed
file_name=cxarray.cpp
line=2780
Any solutions?
Pho
20 Apr 09 at 9:21 am
Thanks a lot, you really help me out, i’ve been searching for some code like this for hours!!
Thanks again
Daniel Banda
31 May 09 at 12:18 am
Awesome!
for the guys having trouble with it:
take a closer look at the function get_image() and dont run another webcam application at the same time
Hogwog
9 Jun 09 at 10:40 am
Same here, same error…
I tried to “print” “camera” object wich is “None”. So, it means that “highgui.cvCreateCameraCapture(0)” returns None object… And I don’t know why, how…
Metal3d
22 Jun 09 at 8:01 pm
If you are receiving a None object from cvCreateCameraCapture, make sure you are passing the correct argument (it appears to be the video device).
For example, my camera is connected to /dev/video2, so I instantiate with highgui.cvCreateCameraCapture(2).
Benjamin
2 Jul 09 at 10:57 pm