Opencv与picamera和haar cascades python

时间:2016-02-15 16:15:29

标签: python opencv raspberry-pi2 haar-classifier

您好我正在尝试使用PiCamera和Haar Cascade检测汽车,但遇到了错误。

'~[^0-9a-z-]+~i'

运行上述代码后,我收到了这些错误

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

car_cascade = cv2.CascadeClassifier('cars3.xml')

camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32

rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)

for frame in camera.capture_continuous(rawCapture, format="rgb", use_video_port=True):
         image = frame.array

while 1:
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    cars = car_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in cars:
        cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = image[y:y+h, x:x+w]


        cv2.imshow("Frame", image)
        key = cv2.waitKey(1) & 0xff
        rawCapture.truncate(0)
        rawCapture.seek(0)

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



cap.release()
cv2.destroyAllWindows()

*我怀疑是因为 Traceback (most recent call last): File "test1.py", line 18, in <module> for frame in camera.capture_continuous(rawCapture, format="rgb", use_video_port=True): File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1851, in capture_continuous if not encoder.wait(self.CAPTURE_TIMEOUT): File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 850, in wait self.stop() File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 881, in stop self._close_output() File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 795, in _close_output output.flush() File "/usr/lib/python2.7/dist-packages/picamera/array.py", line 285, 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 174, in bytes_to_rgb 'Incorrect buffer length for resolution %dx%d' % (width, height)) picamera.exc.PiCameraValueError: Incorrect buffer length for resolution 640x480 我需要插入 camera.capture_continuous {{ 1}} 在它下面工作。 但是当我将这两行插入 rawCapture.truncate(0) 时,我需要插入 < / em> rawCapture.seek(0) 也在下方,否则框架将无法显示。但是,如果我这样做,我的haar级联代码会被忽略而不起作用。我刚刚开始编码,所以我的假设可能是错的。任何帮助将不胜感激,谢谢。*

0 个答案:

没有答案