【问题标题】:piCameraValueError : Incorrect buffer length for resolution 640x480piCameraValueError:分辨率 640x480 的缓冲区长度不正确
【发布时间】:2018-02-03 12:09:17
【问题描述】:

我正在尝试让我的树莓派检测来自 pi 相机的视频源中的人脸,这是我的代码

import time
import cv2
import sys
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera

# camera settings
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640,480))

time.sleep(1)

# video input 
faceCascade = cv2.CascadeClassifier('/home/pi/opencv-3.1.0/data/haarcascades/haarcascade_frontalface_default.xml')


# capture frame from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

    image = frame.array

# face detection
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

#show the frames

        cv2.imshow("Frame", image)
        key = cv2.waitKey(1) & 0xFF

        rawCapture.truncate(0)

        if key == ord("q"):
            break

我尝试运行它,但收到此错误消息

Traceback (most recent call last):
    File"/home/pi/pythonpy/videofacedet/craft/videofacedet(selfmade).py", line 21, in <module>
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1702, in capture_continuous
if not encoder.wait(self.CAPTURE_TIMEOUT):
File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 395, in wait
self.stop()
File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 419, in stop
self._close_output()
File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 349, in _close_output
mo.close_stream(output, opened)
File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 371, in close_stream
stream.flush()
File "/usr/lib/python2.7/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/python2.7/dist-packages/picamera/array.py", line 127, in bytes_to_rgb
    'Incorrect buffer length for resolution %dx%d' % (width, height))
PiCameraValueError: Incorrect buffer length for resolution 640x480

哪里出错了?我是 python 编程的新手,所以我对如何修复它以及从哪里开始感到困惑。提前感谢您的回答

【问题讨论】:

    标签: python-2.7 raspberry-pi3 face-detection


    【解决方案1】:

    您的代码似乎没有正确缩进。我建议缩进这些行:

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    faces = faceCascade.detectMultiScale(
    
    for (x, y, w, h) in faces:
    

    缩进与image = frame.array 行相同

    我认为这是错误的原因,因为您应该在完成后清除当前帧以准备下一帧,我看到您正在尝试使用 rawCapture.truncate(0) 执行此操作。

    缩进在 python 中非常重要,因为这就是代码行被视为块的方式。我认为它是一些编程语言中的花括号如何将代码行视为块。

    【讨论】:

      【解决方案2】:

      我觉得可能是你设置的帧率太高了,我把camera.framerate= 32这行删了,然后相机窗口就出现在屏幕上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 1970-01-01
        • 2020-09-05
        • 2021-02-15
        相关资源
        最近更新 更多