Last active
August 11, 2022 17:55
-
-
Save radames/f7b46828929c78bd66b5 to your computer and use it in GitHub Desktop.
OpenCV VideoCapture running on PyGame on Raspberry PI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
Cool code!