将来自 Raspberry Pi Camera 的 IO 流显示为 PyGame 中的视频 [英] Display IO Stream from Raspberry Pi Camera as video in PyGame
问题描述
我正在做一个需要我有一个取景器(条形码扫描仪)的项目.
I'm working on a project that requires me to have a viewfinder (barcode scanner).
我正在通过 picamera
python 模块使用 Raspberry Pi 摄像头模块执行此操作,并且我已经完成了整个检测和未编程的内容.
I'm doing this with the Raspberry Pi Camera Module by the picamera
python module, and I've got the whole detection and whatnot programmed.
现在我需要弄清楚如何在 PyGame 电影模块.(如果有更好的方法来显示来自 PyGame 中 IO 流的视频,请告诉我.)
Now I need to figure out how to display the preview from the Pi's Camera Module in a PyGame movie module. (If there's a better way to display video from an IO Stream in PyGame, please let me know.)
我需要在 PyGame 中显示它的原因是因为我需要在视频顶部叠加控件并且能够从我将用作 Pi 的取景器/屏幕的触摸屏获取输入/项目.
The reason I need to display it in PyGame is because I'll need to overlay controls on top of the video and be able to get input from a touchscreen I'm going to use as the viewfinder/screen for the Pi/project.
就我从 pygame.movie 文档 中所见,pygame.movie
仅从文件加载.有没有一种方法可以将流转换为类似文件的对象并从中播放 PyGame?
As far as I can see from the pygame.movie documentation, pygame.movie
only loads from a file. Is there a way that I could convert the stream into a file-like object and have PyGame play from that?
基本上,我需要一种方法来获取在 此示例代码,并在 PyGame 中显示.
Basically put, I need a way to take the io.BytesIO stream created in this example code, and display it in PyGame.
推荐答案
您可以使用 'pygame.image.frombuffer' 命令执行此操作.
You can do this with the 'pygame.image.frombuffer' command.
这是一个例子:
import picamera
import pygame
import io
# Init pygame
pygame.init()
screen = pygame.display.set_mode((0,0))
# Init camera
camera = picamera.PiCamera()
camera.resolution = (1280, 720)
camera.crop = (0.0, 0.0, 1.0, 1.0)
x = (screen.get_width() - camera.resolution[0]) / 2
y = (screen.get_height() - camera.resolution[1]) / 2
# Init buffer
rgb = bytearray(camera.resolution[0] * camera.resolution[1] * 3)
# Main loop
exitFlag = True
while(exitFlag):
for event in pygame.event.get():
if(event.type is pygame.MOUSEBUTTONDOWN or
event.type is pygame.QUIT):
exitFlag = False
stream = io.BytesIO()
camera.capture(stream, use_video_port=True, format='rgb')
stream.seek(0)
stream.readinto(rgb)
stream.close()
img = pygame.image.frombuffer(rgb[0:
(camera.resolution[0] * camera.resolution[1] * 3)],
camera.resolution, 'RGB')
screen.fill(0)
if img:
screen.blit(img, (x,y))
pygame.display.update()
camera.close()
pygame.display.quit()
这篇关于将来自 Raspberry Pi Camera 的 IO 流显示为 PyGame 中的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!