【发布时间】:2017-08-17 09:37:10
【问题描述】:
我正在建造一个机器人。我几乎所有的代码都是从其他人的教程项目中复制而来的。
我正在使用 Raspberry Pi 相机来检测面部,并且我有这个电动 Nerf 枪,我想在 opencv 检测到面部之后才可以开火。现在,无论是否检测到人脸,我的代码都会触发 Nerf 枪。请告诉我我做错了什么?我认为问题出在if len(faces) > 0 区域。这是我所有的代码。
程序 1,名为 cbcnn.py
>import RPi.GPIO as gpio
import time
def init():
gpio.setmode(gpio.BOARD)
gpio.setup(22, gpio.OUT)
def fire(tf):
init()
gpio.output(22, True)
time.sleep(tf)
gpio.cleanup()
print 'fire'
fire(3)
程序 2,名为 cbfd2.py
import io
import picamera
import cv2
import numpy
from cbcnn import fire
#Create a memory stream so photos doesn't need to be saved in a file
stream = io.BytesIO()
#Get the picture (low resolution, so it should be quite fast)
#Here you can also specify other parameters (e.g.:rotate the image)
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.vflip = True
camera.capture(stream, format='jpeg')
#Convert the picture into a numpy array
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
#Now creates an OpenCV image
image = cv2.imdecode(buff, 1)
#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/home/pi/cbot/faces.xml /haarcascade_frontalface_default.xml')
#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print "Found "+str(len(faces))+" face(s)"
if len(faces) > 0:
("fire")
#Draw a rectangle around every found face
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
#Save the result image
cv2.imwrite('result.jpg',image)
【问题讨论】:
-
哦,不。你不会因为我非常希望一个 nerf 瞄准机器人成为存在的东西而逃避一个离题的问题。请看How to Ask和minimal reproducible example。
-
我不明白我在帖子中做错了什么?请澄清。
-
不。你读过链接吗?这里没有具体的编程问题,也没有办法复制错误,甚至无法说明具体错误是什么(“不拍人脸”虽然很搞笑,但不是正确的错误跟踪。)
-
@Ironchop 阅读了 Daniel 提供的 MCVE 链接。这个想法是,如果您有问题,请不要给我们很多可能与问题相关或无关的无用信息。首先,发现
faces包含的内容。然后以此为起点提问。据推测,我们不需要所有 PiCamera 的东西或 CV 堆栈。 -
哦。我认为这个网站的全部目的是解决编程问题。我想我不明白这个网站的目的。
标签: python opencv numpy raspberry-pi3 robotics