PiCameraValueError:分辨率为1920x1080的缓冲区长度不正确 [英] PiCameraValueError: Incorrect buffer length for resolution 1920x1080

查看:640
本文介绍了PiCameraValueError:分辨率为1920x1080的缓冲区长度不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my code to detect circle/balls. I want to detect soccer ball from Pi camera. (soccer ball could be in any color.) I am having trouble with some PiCameraValueError. What is wrong with this code. I am using Raspberry Pi 2, Python2, and 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() 


推荐答案

此处


PiCameraValueError 是一个响应
的KeyboardInterrupt异常而引发的次要异常。这是很正常的,不是bug
(这更多是
picamera中输出机制的通用性的结果),但是在Python 2
中很难检测和响应。通常的方法可能是在您的主脚本运行的任何循环中都捕获
PiCameraValueError)。
在Python 3下,实现了异常链接,因此您可以
可能会回头在异常堆栈中找到
KeyboardInterrupt一个。

the PiCameraValueError is a secondary exception raised in response to the KeyboardInterrupt exception. This is quite normal and not a bug (it's more a consequence of how generic the output mechanism is in picamera), but it is difficult to detect and respond to in Python 2 (the usual method there would probably be to catch the PiCameraValueError as well in whatever loop your main script runs). Under Python 3, exception chaining is implemented so you could potentially look back through the stack of exceptions to find the KeyboardInterrupt one.

您需要做的是清除两次捕获之间的流程。 PiRGBArray 文档涵盖了通过截断来完成此操作函数。

What you need to do is clear the stream between captures. The PiRGBArray docs cover doing this with truncate function.

这里是

Here is an example from, 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

这篇关于PiCameraValueError:分辨率为1920x1080的缓冲区长度不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆