Saturday, February 26, 2011

[Py] Python+OpenCV+pygame for capturing webcam frames on Ubuntu (10.04)

Several weeks ago, I tried to find some sample code which can get the frames of the webcam on Ubuntu. Some examples were written in C/C++, some were in Python, and them are all based on OpenCV. I tested them and got some failed. Unfortunately, I have neither enough experience nor enough time to figure out where the problems are, so I started to search some more examples again.

The first light came from Joseph Perla's blog. He gave an example which worked fine on my Ubuntu 10.04. I post the example with minor modifications (using ImageOps.mirror to flip the image horizontally) as the follows.



import pygame
import Image
import ImageOps
from pygame.locals import *
import sys 

import opencv
from opencv import highgui

camera = highgui.cvCreateCameraCapture(0)
def get_image():
        im = highgui.cvQueryFrame(camera)
        return ImageOps.mirror(opencv.adaptors.Ipl2PIL(im))

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:
                        highgui.cvReleaseCapture(camera) # I add this, but don't know whether it is necessary
                        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()


The example remind me about pygame, which I found but had not given further attention to. I don't know the role of pygame in webcam applications so far, but I do know it works with OpenCV on my system. I would keep on studying more about it.

In addition to Joseph's example code, I also found pycam, again. This time it is found on the pygame webpage. I've tested pycam's examples roughly in my previous post when I didn't notice that it utilizes pygame modules.

My summary is: to capture and to show the webcam's image in real-time on Ubuntu, use Python, OpenCV, and pygame.

For me, the next step will be learning how to apply OpenCV functions to do some image processing on the webcam's image.

No comments:

Post a Comment