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). I'm doing this with the Raspberry Pi Camera Module by the picamera python module, and I've got th
Solution 1:
If I understand excatly , you need to instant and infinite preview from camera module to your screen.
there is a way that I figure it out. first you must install Official V4L2 driver.
sudo modprobe bcm2835-v4l2
reference https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=62364
and than you should create a python file to compile and code this
import sys
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
screen = pygame.display.set_mode((640,480),0)
cam_list = pygame.camera.list_cameras()
cam = pygame.camera.Camera(cam_list[0],(32,24))
cam.start()
whileTrue:
image1 = cam.get_image()
image1 = pygame.transform.scale(image1,(640,480))
screen.blit(image1,(0,0))
pygame.display.update()
foreventin pygame.event.get():
ifevent.type == pygame.QUIT:
cam.stop()
pygame.quit()
sys.exit()
this code from http://blog.danielkerris.com/?p=225 , in this blog they did with a webcam. you define your camera module as a webcam with v4l2 driver
also you should check this tutorial https://www.pygame.org/docs/tut/camera/CameraIntro.html
I hope this will works for you
Solution 2:
You can do this with the 'pygame.image.frombuffer' command.
Here's an example:
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 = Truewhile(exitFlag):
for event in pygame.event.get():
if(event.typeis pygame.MOUSEBUTTONDOWN or
event.typeis 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()
Post a Comment for "Display Io Stream From Raspberry Pi Camera As Video In Pygame"