Python picamera缓冲区长度不正确
我目前正在使用pi摄像头模块使用flask传输视频,在某个时候,它会随机崩溃。我在这里找到了一个,他们说清理小溪有助于他们解决这个问题,但在我的情况下,它似乎不起作用Python picamera缓冲区长度不正确,python,flask,raspberry-pi3,Python,Flask,Raspberry Pi3,我目前正在使用pi摄像头模块使用flask传输视频,在某个时候,它会随机崩溃。我在这里找到了一个,他们说清理小溪有助于他们解决这个问题,但在我的情况下,它似乎不起作用 Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/picamera/camera.py", line 1704, in capture_continuous 'Tim
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
self.run()
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
encoder.close()
File "/usr/lib/python3/dist-packages/picamera/encoders.py", line 431, in close
self.stop()
File "/usr/lib/python3/dist-packages/picamera/encoders.py", line 419, in stop
self._close_output()
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
stream.flush()
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
这是我目前正在使用的代码,如上所述,我清除了流,但仍然得到错误
# 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
t.start()
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
self.rawCapture.truncate(0)
self.rawCapture.seek(0)
# if the thread indicator variable is set, stop the thread
# and resource camera resources
if self.stopped:
self.stream.close()
self.rawCapture.close()
self.camera.close()
return
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
我经历同样的问题已经有一段时间了。 在测试不同版本的脚本时,我注意到,如果将分辨率配置为640x480,则不会出现问题。这让我觉得它可能与皮卡梅拉版本有关 rapsberry pi 4B配备raspbian Buster和v1摄像头模块5Mp,代码如下:
import io
import picamera
from picamera import PiCamera
from picamera.array import PiRGBArray
import cv2 as cv
import time
import traceback
import sys
res = (1920, 1080) #(640, 480) #(1296, 976) # (640, 480)
print(res)
source = PiCamera()
source.resolution = res #
# source.start_preview()
source.framerate = 30
source.awb_mode = 'auto'
source.exposure_mode = 'auto' # ['night', 'nightpreview', 'backlight', 'spotlight', 'sports', 'snow',
# 'beach', 'verylong', 'fixedfps', 'antishake', 'fireworks']
source.image_denoise = True
source.image_effect = 'colorbalance'
source.image_effect_params = (1, 2, 1, 1)
time.sleep(2.1)
raw_capture = picamera.array.PiRGBArray(source)
# source.stop_preview()
raw_capture.truncate(0)
print(source)
print(raw_capture)
try:
for frame in source.capture_continuous(raw_capture, format="bgr", use_video_port=True):
local_frame = frame.array
cv.cvtColor(local_frame, cv.COLOR_BGR2RGB, dst=local_frame)
cv.imshow('frame', local_frame)
cv.waitKey(1)
raw_capture.truncate(0)
pass
except Exception as erro:
exctype, value = sys.exc_info()[:2]
print(exctype, value)
print(traceback.format_exc())
print(erro)
finally:
source.close()
cv.destroyAllWindows()
pass
如果分辨率不同于640x480,我也会遇到同样的问题。
所以我把picamera降到了1.10版本
sudo pip3 uninstall picamera -y
sudo pip3 install "picamera[array]"==1.10
然后它开始以所有分辨率和与上面相同的代码再次工作。
从网站上得到这条线索:
希望能有帮助。谢谢,伙计!我来看看。我已经有一段时间遇到同样的问题了。在测试不同版本的脚本时,我注意到,如果将分辨率配置为640x480,则不会出现问题。什么问题?报告的问题:picamera.exc.PiCameraValueError:分辨率1920x1080的缓冲区长度不正确