0

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?

Progman
  • 16,827
  • 6
  • 33
  • 48
Liam
  • 11
  • 1
  • Do you have some idea when the object is going to break the light barrier? I mean, do you have to start waiting and it could be 24 hours? Or do you know it's likely to be in the next 5 seconds or similar? – Mark Setchell May 28 '23 at 14:42
  • 1
    If you use an interrupt based solution, instead of polling the input value, is it any faster? How fast are the objects moving (that is, what is the maximum response time you can tolerate)? – larsks May 28 '23 at 16:55
  • @MarkSetchell I know there will be a few interruptions within the next minute when I turn the machine on. – Liam Jun 07 '23 at 07:58
  • @larsks Do you have en example code for an interrupted based solution? I just start with programming. I would guess the objects move with a speed of about 1 m/sec, but I don't know exactly, and the speed is not always the same. So I can not put the light barrier far away from the camera be course, then I would miss either the faster or slower moving objects. The camera faces a distance of about 4 cm and the objects are about 1 cm long so that would be the tolerance range. – Liam Jun 07 '23 at 08:08

1 Answers1

0

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.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432