Skip to content

Instantly share code, notes, and snippets.

@kriben
Created November 30, 2011 18:10
Show Gist options
  • Save kriben/1410088 to your computer and use it in GitHub Desktop.
Save kriben/1410088 to your computer and use it in GitHub Desktop.
Python opencv feed from webcam
import opencv
#this is important for capturing/displaying images
from opencv import highgui
import pygame
import sys
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("Demo")
screen = pygame.display.get_surface()
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT or event.type == pygame.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))
@kriben
Copy link
Author

kriben commented Nov 30, 2011

@wildlux
Copy link

wildlux commented Nov 15, 2013

Hi =)
With open CV you can see an video? For example
I whant see a video inside a windows custom or fullscreen.
you could do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment