Skip to content

Instantly share code, notes, and snippets.

@embed
Created August 5, 2009 14:41
Show Gist options
  • Save embed/162705 to your computer and use it in GitHub Desktop.
Save embed/162705 to your computer and use it in GitHub Desktop.
PyGame + OpenCV
# -*- coding: utf-8 -*-
import pygame
import Image
from pygame.locals import *
import sys
import opencv
#this is important for capturing/displaying images
from opencv import highgui
camerar = highgui.cvCreateCameraCapture(0)
cameral = highgui.cvCreateCameraCapture(1)
def get_image_l():
im = highgui.cvQueryFrame(cameral)
# 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)
def get_image_r():
im = highgui.cvQueryFrame(camerar)
# 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()
left = 1
while True:
events = pygame.event.get()
for event in events:
if event.type == QUIT:
sys.exit(0)
if event.type == KEYDOWN:
left = 1-left
if left==1:
im = get_image_l()
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))
else:
im = get_image_r()
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment