Skip to content

Instantly share code, notes, and snippets.

@wwj718
Forked from radames/opencv_video_to_pygame.py
Created January 24, 2018 13:50
Show Gist options
  • Save wwj718/5948efa4b87adc404f34a976abf08d0e to your computer and use it in GitHub Desktop.
Save wwj718/5948efa4b87adc404f34a976abf08d0e to your computer and use it in GitHub Desktop.
OpenCV VideoCapture running on PyGame
import pygame
from pygame.locals import *
import cv2
import numpy as np
import sys
camera = cv2.VideoCapture(0)
pygame.init()
pygame.display.set_caption("OpenCV camera stream on Pygame")
screen = pygame.display.set_mode([1280,720])
try:
while True:
ret, frame = camera.read()
screen.fill([0,0,0])
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = np.rot90(frame)
frame = pygame.surfarray.make_surface(frame)
screen.blit(frame, (0,0))
pygame.display.update()
for event in pygame.event.get():
if event.type == KEYDOWN:
sys.exit(0)
except KeyboardInterrupt,SystemExit:
pygame.quit()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment