Skip to content

Instantly share code, notes, and snippets.

@f41ardu
Forked from radames/opencv_video_to_pygame.py
Created February 10, 2020 10:13
Show Gist options
  • Save f41ardu/b07eb1079afc2a463cf8dac7f44f3eba to your computer and use it in GitHub Desktop.
Save f41ardu/b07eb1079afc2a463cf8dac7f44f3eba 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