2

I am working on a project where we are using the Raspicam attached to a Raspberry Pi to capture (and process) images with python using the PiCamera module.

With our current implementation I am experiencing an unexpected behaviour. The camera is configured to capture images with 15 frames per second. To simulate processing time the program waits 5 seconds.

Minimal example:

#!/usr/bin/env python
import cv2
from picamera import PiCamera
from picamera.array import PiRGBArray

class RaspiCamera:

    def __init__(self, width, height, framerate):

        self.camera = PiCamera()
        self.camera.resolution = (width, height)
        self.camera.framerate = framerate
        self.rawCapture = PiRGBArray(self.camera, size=self.camera.resolution)
        self.capture_continuous = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True)

    def capture(self):

        frame = self.capture_continuous.next()
        image = self.rawCapture.array
        self.rawCapture.truncate(0)

        return image

if __name__ == "__main__":

    camera = RaspiCamera(640, 480, 15)
    while True:
        frame = camera.capture()
        cv2.imshow("Image", frame)
        if cv2.waitKey(5000) & 0xFF == ord('q'):
            break

When capture() is called for the first time, self.capture_continuous.next() returns an up to date image. When calling capture() consecutively, it often happens that self.capture_continuous.next() does not return the latest image but one that is already a few seconds old (verified by pointing the camera at a clock). From time to time, it's even older than 10 seconds. On the other hand, sometimes self.capture_continuous.next() actually returns the latest image.

Since capture_continuous is an object of the type generator, my assumption is that it keeps generating camera images in the background that accumulate in a queue while the program waits and on the next call of self.capture_continuous.next() the next element in the queue is returned.

Anyway, I am only interested in the latest, most up to date image the camera has captured. Some first attempts to get hold of the latest images failed. I tried to call self.capture_continuous.next() repeatedly in a while loop to get to the latest image. Since a generator is apparently also an iterator I tried some methods mentioned in this post: Cleanest way to get last item from Python iterator.

Simply using the capture() function of the PiCamera class itself is not an option since it takes approx. 0.3 seconds till the image is captured what is too much for our use case.

Does anyone have a clue what might cause the delay described above and how it could be avoided?

sven
  • 21
  • 3
  • Have you found a solution? I'm facing a similar problem with this unexpected behavior. – omar Jul 24 '17 at 06:40

0 Answers0