我正试图让我的覆盆子pi检测来自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编程的新手,所以我对如何修复它以及从哪里开始感到困惑。提前感谢您的回答
答案 0 :(得分:1)
您的代码似乎没有正确缩进。我建议缩进这些行:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
for (x, y, w, h) in faces:
与第image = frame.array
行
我认为这是错误的原因,因为当你完成它以准备下一帧时你应该清除当前帧,我看到你试图用{{1}做到这一点}。
缩进在python中非常重要,因为这就是代码行被视为块的方式。我认为它是一些编程语言中的花括号如何将代码行视为块。
答案 1 :(得分:1)
我认为您设置的帧率可能太高,我删除了//str = 23f
var ex=str.somefunq()
//ex=false
//str = 2356
var ex=str.somefunq()
//ex=true
行,然后屏幕上显示了相机窗口。