本文介绍了python人脸检测覆盆子pi与picamera的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python和opencv的新手,我正在尝试使用树莓派构建人脸检测项目.我收到此错误,这是我的代码

I am a newbie with python and opencv i am trying to build a face detection project with raspberry pi. i am getting this error and here is my code

回溯(最近通话最近一次):

Traceback (most recent call last):

 File "/home/pi/Desktop/picamera-code/FaceDetection1.0", line 19, in <module>
for frame in
    camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

代码:

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


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

time.sleep(0.1)



face_cascade =  cv2.CascadeClassifier('/home/pi/Downloads/haarcascade_frontalface_default.xml')

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

    img=np.asarray(frame.array)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        img = cv2.Rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]


cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

推荐答案

问题出在您的camera.capture_continuos中.正如文档所说,第一个值(输出)不能只是一个数组,因为它记录了无限次迭代.代替此,您应该放置一个输出文件.如果您想让流捕获它,则也可以使用io.Bytes.

The problem is in your camera.capture_continuos. First value, output, cannot be just an array as it records with an infinite iteration as the docs says. Instead of this you should put an output file. If you want an stream to capture this you can use the io.Bytes as well.

在此链接中,它解释了您将了解有关tu如何使用框架以及在何处重定向输出的示例.

In this link it explains you examples on how tu use the frame and where should you redirect the output.

您可以执行类似API文档中建议的操作.获取流并将其截断以获取您当前正在获取的图像:

You can do something like what suggest on the API docs. Take the stream and truncate it to get the image that you are currently getting:

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

这篇关于python人脸检测覆盆子pi与picamera的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:30