我的印刷品(“找到面孔”,str(len(面)))未执行

时间:2018-03-15 20:52:10

标签: python raspberry-pi

我正在尝试使用python中的opencv检测多个面孔。我在raspbian OS(raspberry Pi 3)中这样做。尽管代码工作正常,即它正在检测一个面并在面部周围绘制一个矩形边界。它也成功地将图像保存在我的本地文件夹中。问题是:语句print(“Found faces”,str(len(faces)))不起作用,控制台保持空白。我在这里错过了什么,或者我哪里出错了?

import io
import picamera
import cv2
import numpy
stream = io.BytesIO()

with picamera.PiCamera() as camera:
    camera.resolution = (320, 240)
    camera.hflip = True
    camera.capture(stream, format='jpeg')


buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)

image = cv2.imdecode(buff, 1)

face_cascade = cv2.CascadeClassifier('face1.xml')

gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.1, 5)

print("Found faces", str(len(faces)))

for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)

cv2.imwrite('result.jpg',image)

1 个答案:

答案 0 :(得分:-2)

正确的代码是

print("Found faces= "+str(len(faces)))