What's the fastest way to trigger a PiCamera using a light barrier for capturing small, fast-moving objects?

244 views Asked by At

I want to trigger a PiCamera by a light barrier to catch up small, fast moving objects with the Camera.

Setup: IR-Light barrier module connected to GPIO Pin 4 on a Raspberry Pi zero as well as a Raspberry Pi Camera

from picamera import PiCamera
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(4, GPIO.IN)

camera = PiCamera()
camera.shutter_speed = 500
camera.iso = 400

while True:
    
    sensor = GPIO.input(4)

    if sensor == 0:
        camera.capture('/home/pi/Documents/Fotos/immage_001.jpg')
        print ('ok')
        time.sleep(0.00001)

    
    elif sensor == 1:
        time.sleep(0.00001)
    


With my Setup, I have a time delay of about half a second, which is way too much. Do you have any idea how to speed it up? For example, to take a row of pictures but only save the one which was taken while the light barrier was interrupted?

1

There are 1 answers

0
Mark Setchell On

The Raspberry Pi camera can either use video mode or still photo mode. In video mode, it acquires more frames per second but the quality is lower and the capture area is smaller. In stills mode, the resolution is higher and it uses a better de-noising algorithm and the that makes the quality better. From your question, I think you are looking for the fastest possible frame rate, so you will want to see if the video quality is adequate (in terms of resolution and noise) for your purposes and if so, use that.

In general, you will get a higher frame rate (and less blurring due to movement) if your subject is better illuminated - because the exposure time can be shorter, so try to get your subject well lit if possible.

You will want to avoid writing to disk if capturing (stills) at high speed - unless you find an H264 video adequate in which case the RasPi can write to disk fast enough. If we guess an individual JPEG image (whether a still photo or a video frame) is around 200kB, you will see you can store 5 frames in 1MB and therefore 5,000 frames in 1GB while 60s of 30fps only produces 1,800 frames, so you can buffer your frames in memory (in a Python list for example) rather than writing to disk.

As regards the light barrier, I think @larsks suggestion is a good one. So you would set up an interrupt when your light barrier is broken (probably a rising edge) and also when it is unbroken (probably falling edge interrupt) - this probably means you will use BOTH for the rising and falling edge. There is a good example here. In the interrupt service routine (callback) you would set/clear a global variable so that it is True whenever the barrier is broken. You can then access this variable in your frame buffering code to decide whether to save it or not.