【问题标题】:PiCameraValueError: Incorrect buffer length for resolution 1920x1080PiCameraValueError:分辨率 1920x1080 的缓冲区长度不正确
【发布时间】:2016-10-15 23:38:13
【问题描述】:

这是我检测圆圈/球的代码。 我想从 Pi 相机检测足球。 (足球可以是任何颜色。)我遇到了一些 PiCameraValueError 问题。这段代码有什么问题。我正在使用 Raspberry Pi 2、Python2 和 OpenCV。

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import sys
import imutils
import cv2.cv as cv
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()

rawCapture = PiRGBArray(camera)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr"):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array
    img   = cv2.medianBlur(image, 5)
    imgg  = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    cimg  = cv2.cvtColor(imgg, cv2.COLOR_GRAY2BGR)
    imgg  = cv2.blur(imgg, (3,3))
            #imgg = cv2.dilate(imgg, np.ones((5, 5)))
            #imgg = cv2.GaussianBlur(imgg,(5,5),0)
    circles = cv2.HoughCircles(imgg, cv.CV_HOUGH_GRADIENT, 1, 20, param1=100, param2=40, minRadius=5, maxRadius=90)
    cv2.imshow("cimg", imgg)
    key = cv2.waitKey(1)
    if key & 0xFF == ord('q'):
        break
    if circles is None:
        continue

    print circles
            #circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
                   #cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle
        cv2.imshow("Filtered", cimg)

cv2.destroyWindow("cimg")
cv2.destroyAllWindows() 

【问题讨论】:

    标签: python opencv raspberry-pi detection raspbian


    【解决方案1】:

    正如here 解释的那样,

    PiCameraValueError 是响应于 键盘中断异常。这是很正常的,不是错误 (这更多是输出机制的通用性的结果 picamera),但在 Python 2 中很难检测和响应 (通常的方法可能是抓住 PiCameraValueError 以及在您的主脚本运行的任何循环中)。 在 Python 3 下,exception chaining 已实现,因此您可以 可能会回顾异常堆栈以找到 键盘中断之一。

    您需要做的是清除捕获之间的流。 PiRGBArray 文档介绍了使用 truncate 函数执行此操作。

    这是来自python face detection raspberry pi with picamera的示例

    import io
    import time
    import picamera
    with picamera.PiCamera() as camera:
        stream = io.BytesIO()
        for foo in camera.capture_continuous(stream, format='jpeg'):
        # YOURS:  for frame in camera.capture_continuous(stream, format="bgr",  use_video_port=True):
            # Truncate the stream to the current position (in case
            # prior iterations output a longer image)
            stream.truncate()
            stream.seek(0)
            if process(stream):
                break
    

    【讨论】:

    • 谢谢。我在使用此代码进行球检测时遇到问题。我有不同类型的足球。
    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2020-09-05
    • 2012-05-17
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多