Quickly Capture and Place 2D Images as a 3D Numpy Array with Python and Raspberry Pi

I'm working on a Raspberry Pi project where I need to take about 30 images per second (no movie) and add each 2D image to a 3D array using a numpy array without saving each 2D capture as a file (because it's slow).

I found this Python code as fast as possible, but don't know how to quickly stack all images into a 3D image stack.

import io
import time
import picamera
#from PIL import Image

def outputs():
    stream = io.BytesIO()
    for i in range(40):
        # This returns the stream for the camera to capture to
        yield stream
        # Once the capture is complete, the loop continues here
        # (read up on generator functions in Python to understand
        # the yield statement). Here you could do some processing
        # on the image...
        #stream.seek(0)
        #img = Image.open(stream)
        # Finally, reset the stream for the next capture
        stream.seek(0)
        stream.truncate()

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.framerate = 80
    time.sleep(2)
    start = time.time()
    camera.capture_sequence(outputs(), 'jpeg', use_video_port=True)
    finish = time.time()
    print('Captured 40 images at %.2ffps' % (40 / (finish - start)))

      

Does anyone of you know how to photograph 2D images taken in this code into a 3D numpy array using Python and a Raspberry Pi camera module? Without saving every 2D capture as a file

Best wishes, Agustin

+3


source to share


1 answer


This may not work like copy-n-paste, but should demonstrate how to pre-allocate memory and write the results there. I'm not familiar with pycamera

, but the example here shows slightly different uses of streams in memory.



import numpy as np

def outputs(resolution, n_pics, clr_channels=3):
    # Note that the first dimension is height and second is width
    images = np.zeros((resolution[1], resolution[0], clr_channels, n_pics), dtype=np.uint8)
    stream = io.BytesIO()

    for i in range(n_pics):
        yield stream
        images[:,:,:,i] = Image.open(stream)

        stream.seek(0)
        stream.truncate()

      

0


source







All Articles