I am currently using a pi camera module to stream video using flask and at some point, it randomly crashes. I found a similar question over here and they say that clearing the stream helped them sort this out, but in my case, it doesn't seem to work.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 1704, in capture_continuous
    'Timed out waiting for capture to end')
picamera.exc.PiCameraRuntimeError: Timed out waiting for capture to end

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
  File "/usr/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Desktop/secure-pi-tensorflow/securepi/pivideostream.py", line 39, in update
    for f in self.stream:
  File "/usr/lib/python3/dist-packages/picamera/camera.py", line 1710, in capture_continuous
  File "/usr/lib/python3/dist-packages/picamera/encoders.py", line 431, in close
  File "/usr/lib/python3/dist-packages/picamera/encoders.py", line 419, in stop
  File "/usr/lib/python3/dist-packages/picamera/encoders.py", line 349, in _close_output
    mo.close_stream(output, opened)
  File "/usr/lib/python3/dist-packages/picamera/mmalobj.py", line 371, in close_stream
  File "/usr/lib/python3/dist-packages/picamera/array.py", line 238, in flush
    self.array = bytes_to_rgb(self.getvalue(), self.size or self.camera.resolution)
  File "/usr/lib/python3/dist-packages/picamera/array.py", line 127, in bytes_to_rgb
    'Incorrect buffer length for resolution %dx%d' % (width, height))
picamera.exc.PiCameraValueError: Incorrect buffer length for resolution 1920x1080

this is the code that I am currently using and as said above I clear the stream but I still get the error.

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
from threading import Thread
import cv2

class PiVideoStream:
    def __init__(self, resolution=(320, 240), framerate=32, **kwargs):
        # initialize the camera
        self.camera = PiCamera()

        # set camera parameters
        self.camera.resolution = resolution
        self.camera.framerate = framerate

        # set optional camera parameters (refer to PiCamera docs)
        for (arg, value) in kwargs.items():
            setattr(self.camera, arg, value)

        # initialize the stream
        self.rawCapture = PiRGBArray(self.camera, size=resolution)
        self.stream = self.camera.capture_continuous(self.rawCapture,
            format="bgr", use_video_port=True)

        # initialize the frame and the variable used to indicate
        # if the thread should be stopped
        self.frame = None
        self.stopped = False

    def start(self):
        # start the thread to read frames from the video stream
        t = Thread(target=self.update, args=())
        t.daemon = True
        return self

    def update(self):
        # keep looping infinitely until the thread is stopped
        for f in self.stream:
            # grab the frame from the stream and clear the stream in
            # preparation for the next frame
            self.frame = f.array

            # if the thread indicator variable is set, stop the thread
            # and resource camera resources
            if self.stopped:

    def read(self):
        # return the frame most recently read
        return self.frame

    def stop(self):
        # indicate that the thread should be stopped
        self.stopped = True


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.