Blog :: Joseph Javier Perla

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))

867 days ago on September 26, 2007 at 12:41 pm and written by Joseph Perla in Hacks, Technology


To write a comment, just email me

Comments

Newest

Oldest

Hi, im looking for “capturing images using Python under windows”, where can i find some information about it??
Im also looking for imformation about steaming real time video from an ip cam in python.!!!
If anyone can help me it please emial at aagarcia@uninorte.edu.co or write here

Thank you.

183 days ago on August 10, 2009 at 4:09 am and written by Andres Garcia


Thanks for taking the time to post this. It worked great for me!

194 days ago on July 30, 2009 at 3:52 am and written by Jay


same error, my video device is on /dev/video0. How can we solve this problem?

202 days ago on July 21, 2009 at 1:37 pm and written by pione


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).

221 days ago on July 2, 2009 at 10:57 pm and written by Benjamin


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…

231 days ago on June 22, 2009 at 8:01 pm and written by Metal3d


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

245 days ago on June 9, 2009 at 10:40 am and written by Hogwog


Thanks a lot, you really help me out, i’ve been searching for some code like this for hours!!

Thanks again

254 days ago on May 31, 2009 at 12:18 am and written by Daniel Banda


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?

295 days ago on April 20, 2009 at 9:21 am and written by Pho


works great, but having trouble trying to get multiple webcams to work.

295 days ago on April 19, 2009 at 10:25 pm and written by endeavormac


Hey man works great on my mobii netbook with Ubuntu 8.04
Thanks a lot!

296 days ago on April 19, 2009 at 1:06 pm and written by Seppel


Thanks for posting this, exactly what I was looking for to do video processing on linux in python.

324 days ago on March 21, 2009 at 7:01 pm and written by Clayton Gulick


Thanks for this!! Also spent 5 hours searching and finally found this article.

333 days ago on March 13, 2009 at 8:33 am and written by Luke Venediger


I got the “NULL array pointer is passed”, when I forgot to compile opencv with v4l2 support.

333 days ago on March 12, 2009 at 4:02 pm and written by unlotto


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. :)

341 days ago on March 5, 2009 at 12:46 pm and written by roy


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!

343 days ago on March 3, 2009 at 3:54 am and written by Brian


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?

370 days ago on February 3, 2009 at 11:13 pm and written by dan miller


Works like a charm .. thanks a billion;
Could you tell how we can capture images from the feed.
That would be helpful..

374 days ago on January 31, 2009 at 6:26 am and written by Snape


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

375 days ago on January 30, 2009 at 12:56 am and written by julian


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. :)

381 days ago on January 23, 2009 at 11:53 pm and written by Danny


You, Sir, are my hero! Thank you so much! I’ve spent countless hours trying to get one of the pyv4l libs to work!

381 days ago on January 23, 2009 at 7:27 pm and written by Galileon Galilei


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

384 days ago on January 21, 2009 at 5:58 am and written by Max M


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

407 days ago on December 29, 2008 at 11:16 am and written by Mir


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!

414 days ago on December 22, 2008 at 10:53 am and written by Steve B


Hey thanks a lot!

Work very nice with my camera, good work!, Busman.

460 days ago on November 5, 2008 at 3:01 pm and written by Busman


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)

475 days ago on October 22, 2008 at 1:35 am and written by kpoman


Hello Man.

10x, I checked around 5 other examples and this one works like a charm.

Shalom,
Ram-on.

489 days ago on October 7, 2008 at 6:33 pm and written by Ram-on Agmon


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!

498 days ago on September 29, 2008 at 7:48 am and written by Harshad


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 :D
Thanks a million
Coral

576 days ago on July 12, 2008 at 3:29 pm and written by [ZAF]-Coral


I would love to play with multiple view geometry also, is there anyone who has done that with python?

598 days ago on June 20, 2008 at 7:14 pm and written by Bryan


Great!
tanks it works for me!!

652 days ago on April 28, 2008 at 1:08 pm and written by diego


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

:)

750 days ago on January 20, 2008 at 3:47 pm and written by alon


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!

765 days ago on January 5, 2008 at 8:02 pm and written by webbge


I searching this for hour, and it’s works !!
thank you, that really help me a lot.

771 days ago on December 30, 2007 at 8:03 pm and written by NoopyKs


I searching this for hour, and it’s works !!
thank you, that really help me a lot.

771 days ago on December 30, 2007 at 8:03 pm and written by NoopyKs


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!

765 days ago on January 5, 2008 at 8:02 pm and written by webbge


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

:)

750 days ago on January 20, 2008 at 3:47 pm and written by alon


Great!
tanks it works for me!!

652 days ago on April 28, 2008 at 1:08 pm and written by diego


I would love to play with multiple view geometry also, is there anyone who has done that with python?

598 days ago on June 20, 2008 at 7:14 pm and written by Bryan


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 :D
Thanks a million
Coral

576 days ago on July 12, 2008 at 3:29 pm and written by [ZAF]-Coral


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!

498 days ago on September 29, 2008 at 7:48 am and written by Harshad


Hello Man.

10x, I checked around 5 other examples and this one works like a charm.

Shalom,
Ram-on.

489 days ago on October 7, 2008 at 6:33 pm and written by Ram-on Agmon


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)

475 days ago on October 22, 2008 at 1:35 am and written by kpoman


Hey thanks a lot!

Work very nice with my camera, good work!, Busman.

460 days ago on November 5, 2008 at 3:01 pm and written by Busman


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!

414 days ago on December 22, 2008 at 10:53 am and written by Steve B


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

407 days ago on December 29, 2008 at 11:16 am and written by Mir


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

384 days ago on January 21, 2009 at 5:58 am and written by Max M


You, Sir, are my hero! Thank you so much! I’ve spent countless hours trying to get one of the pyv4l libs to work!

381 days ago on January 23, 2009 at 7:27 pm and written by Galileon Galilei


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. :)

381 days ago on January 23, 2009 at 11:53 pm and written by Danny


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

375 days ago on January 30, 2009 at 12:56 am and written by julian


Works like a charm .. thanks a billion;
Could you tell how we can capture images from the feed.
That would be helpful..

374 days ago on January 31, 2009 at 6:26 am and written by Snape


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?

370 days ago on February 3, 2009 at 11:13 pm and written by dan miller


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!

343 days ago on March 3, 2009 at 3:54 am and written by Brian


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. :)

341 days ago on March 5, 2009 at 12:46 pm and written by roy


I got the “NULL array pointer is passed”, when I forgot to compile opencv with v4l2 support.

333 days ago on March 12, 2009 at 4:02 pm and written by unlotto


Thanks for this!! Also spent 5 hours searching and finally found this article.

333 days ago on March 13, 2009 at 8:33 am and written by Luke Venediger


Thanks for posting this, exactly what I was looking for to do video processing on linux in python.

324 days ago on March 21, 2009 at 7:01 pm and written by Clayton Gulick


Hey man works great on my mobii netbook with Ubuntu 8.04
Thanks a lot!

296 days ago on April 19, 2009 at 1:06 pm and written by Seppel


works great, but having trouble trying to get multiple webcams to work.

295 days ago on April 19, 2009 at 10:25 pm and written by endeavormac


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?

295 days ago on April 20, 2009 at 9:21 am and written by Pho


Thanks a lot, you really help me out, i’ve been searching for some code like this for hours!!

Thanks again

254 days ago on May 31, 2009 at 12:18 am and written by Daniel Banda


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

245 days ago on June 9, 2009 at 10:40 am and written by Hogwog


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…

231 days ago on June 22, 2009 at 8:01 pm and written by Metal3d


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).

221 days ago on July 2, 2009 at 10:57 pm and written by Benjamin


same error, my video device is on /dev/video0. How can we solve this problem?

202 days ago on July 21, 2009 at 1:37 pm and written by pione


Thanks for taking the time to post this. It worked great for me!

194 days ago on July 30, 2009 at 3:52 am and written by Jay


Hi, im looking for “capturing images using Python under windows”, where can i find some information about it??
Im also looking for imformation about steaming real time video from an ip cam in python.!!!
If anyone can help me it please emial at aagarcia@uninorte.edu.co or write here

Thank you.

183 days ago on August 10, 2009 at 4:09 am and written by Andres Garcia


Books
E-mail
My Amazon Wishlist

Favorite Posts

YCombinator Application Guide
Telecommunications
Font Comic: Attempt #1
Returns year 2
Why Plant Rights?

Popular Posts

Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
Capturing frames from a webcam on Linux
How to Ace an IQ Test
A Clean Python Shell Script
Learn 100 digits of pi at lightning speed
BankOfAmerica.com > Mint.com

Recent Posts

Apple's amazing customer support system
Poem #0
Hot Stock Tip #0
Labmeeting rocks
Log Reader 3000
A Clean Python Shell Script

Recent Comments

Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Andres Garcia: <p>Hi, im looking for “capturing images using Python under windows”, where can i fi...


Categories

Follow

More...