我正在尝试使用Raspberry Pi相机构建一个简单的监控程序,使用OpenCV进行人脸检测。这是我的代码:
# -*- coding: utf-8 -*-
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
#setting up the camera
camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(320, 240))
#basic cascade classifier
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#0.1 sec warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#rectangle loop
for(x, y, w, h) in faces:
image = cv2.rectangle(image,(x, y), (x+w, y+h), (255, 0, 0), 2)
# show the frame [here comes the problem]
cv2.imshow("Frame", image)
# cv2.imshow("Gray", gray
key = cv2.waitKey(100) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
关键是,当我在图像中绘制矩形时,会出现此错误:
OpenCV错误:断言失败(size.width> 0&& size.height> 0)in imshow,file /home/pi/opencv-2.4.10/modules/highgui/src/window.cpp, 第261行追溯(最近一次调用最后一次):文件" camera2.py",行 36,在 cv2.imshow(" Frame",image)cv2.error:/home/pi/opencv-2.4.10/modules/highgui/src/window.cpp:261:错误: (-215)size.width> 0&&函数imshow中的size.height> 0
如果我把cv2.imshow放在矩形循环之前,那就完美了。此外,打印 faces 对象显示 detectMultiScale 实际上正在识别面部。我该怎么办?
感谢。