Display IO Stream from Raspberry Pi Camera as video in PyGame

10,682

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()

while True:
   image1 = cam.get_image()
   image1 = pygame.transform.scale(image1,(640,480))
   screen.blit(image1,(0,0))
   pygame.display.update()

   for event in pygame.event.get():
          if event.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 = 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()
Share:
10,682
RPiAwesomeness
Author by

RPiAwesomeness

Socials Old Blog: Sudo'd Youtube Channel: AEVES Tech Rigs Desktop - Custom built, you can see the specs/build stuff here ThinkPad T540p #Skills Advanced: googlefu python Intermediate: gimp building-pcs c++ golang Acceptable: c# bash zsh rust java Absolutely No Clue: perl windows

Updated on June 05, 2022

Comments

  • RPiAwesomeness
    RPiAwesomeness about 2 years

    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 the whole detection and whatnot programmed.

    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.)

    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.

    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?

    Basically put, I need a way to take the io.BytesIO stream created in this example code, and display it in PyGame.

  • BruceJohnJennerLawso
    BruceJohnJennerLawso about 5 years
    fwiw this worked but I found it was highly unstable
  • danmcb
    danmcb almost 5 years
    thank you - this is really nice because you can explicitly set the sensor_mode of the camera, rather than use the pygame generic V4L2 driver which doesn't do so. Worked well for me.