Skip to content

Instantly share code, notes, and snippets.

@radames
Last active August 11, 2022 17:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save radames/f7b46828929c78bd66b5 to your computer and use it in GitHub Desktop.
Save radames/f7b46828929c78bd66b5 to your computer and use it in GitHub Desktop.
OpenCV VideoCapture running on PyGame on Raspberry PI
import pygame
from pygame.locals import *
import cv2
import numpy as np
import time
import picamera
import picamera.array
screen_width = 640
screen_height = 480
camera = picamera.PiCamera()
camera.resolution = (screen_width, screen_height)
pygame.init()
pygame.display.set_caption("OpenCV camera stream on Pygame")
screen = pygame.display.set_mode([screen_width, screen_height])
video = picamera.array.PiRGBArray(camera)
try:
for frameBuf in camera.capture_continuous(video, format ="rgb", use_video_port=True):
frame = np.rot90(frameBuf.array)
video.truncate(0)
frame = pygame.surfarray.make_surface(frame)
screen.fill([0,0,0])
screen.blit(frame, (0,0))
pygame.display.update()
for event in pygame.event.get():
if event.type == KEYDOWN:
raise KeyboardInterrupt
except KeyboardInterrupt,SystemExit:
pygame.quit()
cv2.destroyAllWindows()
@keverkever
Copy link

Cool code!

@shiva-karthick
Copy link

Thanks a lot for the code, worked flawlessly :)

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