Skip to content

Instantly share code, notes, and snippets.

@gretel
Created January 24, 2017 01:02
Show Gist options
  • Save gretel/73fb72ff48db4cfea71a650f4cc72ba7 to your computer and use it in GitHub Desktop.
Save gretel/73fb72ff48db4cfea71a650f4cc72ba7 to your computer and use it in GitHub Desktop.
hack to display video stream from usb connected camera (via libuvc, pyuvc, pygame)
# https://gist.github.com/gretel/73fb72ff48db4cfea71a650f4cc72ba7
# based on example at https://github.com/pupil-labs/pyuvc
# install libuvc and pyuvc - see https://github.com/pupil-labs/pyuvc/blob/master/README.md
# install pygame (pip install pygame)
import uvc
import sys
import logging
import pygame
from pygame.locals import *
logging.basicConfig(level=logging.INFO)
SCREEN_SIZE = (1280, 720)
CAP_MODE = (1280, 720, 30) # TODO: how to add int to tuple?
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('wtfcam')
dev_list = uvc.device_list()
print dev_list
cap = uvc.Capture(dev_list[0]['uid']) # TODO: choosable camera
print cap.avaible_modes
cap.frame_mode = CAP_MODE
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
frame = cap.get_frame_robust()
# TODO: fails with anything else than RGB
surf = pygame.image.fromstring(frame.bgr.tostring(), SCREEN_SIZE, 'RGB', True).convert()
screen.blit(surf, (0,0))
pygame.display.update()
pygame.time.delay(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment