Posts: 25
Joined: Wed Jun 20, 2012 7:49 pm
Location: Dresden, Germany
Contact: Website

picamera stream freezes after a while

Thu Jul 04, 2019 6:46 pm


I'm using Raspbian stretch with kernel 4.19.56, picamera 1.13 and pygame1.9.6 to display the camera livestream in a pygame surface and capture a high-res image on button press.

I created this class to capture low-res images for faster livestream and take high-res images with different splitter port:

Code: Select all

class RaspiboothCam(PhotoboothCam):

    def __init__(self, *args, **kwargs):
        super(RaspiboothCam, self).__init__(*args, **kwargs)
        self.lock = Lock()

        self.img = None
        self.camera = None

        self.run_camera = Event()
        self.camera_thread = None

    def init_camera(self):
        import picamera
        self.log.info("Open PiCamera...")
        self.camera = picamera.PiCamera(resolution=(1920, 1080))
        self.camera.hflip = self.flip_h
        self.camera.vflip = self.flip_v
        self.camera.drc_strength = "off"
        self.camera_thread = Thread(target=self.run)
        return True

    def close(self):
        self.log.info("close camera")

    def run(self):
        from picamera.array import PiRGBArray
        i = 0
        raw_preview = PiRGBArray(self.camera, size=(800, 480))
        stream = self.camera.capture_continuous(raw_preview,
                                                resize=(800, 480),
        for frame in stream:
            self.log.debug("got frame {:d}".format(i))
            self.img = frame.array.copy()

            i += 1

            if self.run_camera.is_set():
                self.log.debug("close camera preview")

    def take_preview_image(self):
        while self.img is None:
            self.log.warning("Waiting for preview image...")
        return self.img

    def take_photo(self):
        img = np.empty((1088, 1920, 3), dtype=np.uint8)
        self.camera.capture(img, 'bgr', use_video_port=True, splitter_port=1)
        return img

My main loop reads preview images with the take_preview_image method and shows them on screen with pygame. But after a while the streaming freezes and I see always the same image on my pygame surface. It seems capture_continuous waits for something. When I press the take_photo button I get a up-to-date image from the camera via splitter port 1 but my preview stream still hangs. When I restart the app I can't even create the camera object. It just hangs at the PiCamera constructor (same if I just start raspivid).

Any suggestions what's wrong?

Posts: 25
Joined: Wed Jun 20, 2012 7:49 pm
Location: Dresden, Germany
Contact: Website

Re: picamera stream freezes after a while

Mon Aug 12, 2019 2:27 pm

Weird, I downgraded to kernel 4.4 via rpi-update and now it doesn't freeze anymore. With kernel >= 4.9 the image freezes after some seconds or minutes.

Return to “Camera board”