问题描述
请告诉我这里有什么问题。它给出了这样的错误
if face == True:#i have used this to make if there is no face found then stop if face found go forward
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
import RPi.GPIO as GPIO
import cv2
import carapp
import sys
vid = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('/home/pi/harr cascade/haarcascade_frontalface_default.xml')
Motor1A = 21
Motor1B = 20
Motor2A = 16
Motor2B = 26
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(Motor1A,GPIO.OUT)
GPIO.setup(Motor1B,GPIO.OUT)
GPIO.setup(Motor2A,GPIO.OUT)
GPIO.setup(Motor2B,GPIO.OUT)
def forward():
print("GOING FORWARD")
GPIO.output(Motor1A,GPIO.LOW)
GPIO.output(Motor1B,GPIO.HIGH)
GPIO.output(Motor2A,GPIO.LOW)
GPIO.output(Motor2B,GPIO.HIGH)
def backward():
print("GOING BACKWARD")
GPIO.output(Motor1A,GPIO.HIGH)
GPIO.output(Motor1B,GPIO.LOW)
GPIO.output(Motor2A,GPIO.HIGH)
GPIO.output(Motor2B,GPIO.LOW)
def Left():
print("Going Left")
GPIO.output(Motor1A,GPIO.HIGH)
def Right():
print("Going Right")
GPIO.output(Motor1A,GPIO.LOW)
def stop():
print("Stopping")
GPIO.output(Motor1A,GPIO.LOW)
def cameo():
while(True):
_,img = vid.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray,1.1,4)
for (x,y,w,h) in face:
cv2.rectangle(img,(x,y),(x+w,y+h),(50,20,70),3)
if face == True:#i have used this to make if there is no face found then stop if face found go forward
carapp.forward()
else:
carapp.stop()
cv2.imshow('img',img)
if cv2.waitKey(1) & 0xff == ord('q'):
break
sys.exit()
vid.release()
cv2.destroyAllWindows()
import tkinter as tk
gui = tk.Tk()
gui.title("Car control")
gui.geometry("500x500")
lol = tk.Button(gui,text="Forward",bg="red",command=forward)
lol.grid(row=2,column=5)
bot = tk.Button(gui,text="Backward",bg="green",command=backward)
bot.grid(row=10,column=5)
ron = tk.Button(gui,text="Left",bg="orange",command=Left)
ron.grid(row=5,column=0)
bob = tk.Button(gui,text="Right",bg="yellow",command=Right)
bob.grid(row=5,column=10)
dol = tk.Button(gui,text="camera",bg="blue",command = cameo)
dol.grid(row=5,column=100)
sod = tk.Button(gui,text="stop",bg="cyan",command = stop)
sod.grid(row=5,column=5)
button = tk.Button(text = "Click and Quit",command = sys.exit)
button.grid(row=15,column=10)
gui.mainloop()
#this product is copytright of shouryawadhwa aka @programmerShourya
解决方法
返回值来自
face_cascade = cv2.CascadeClassifier('/home/pi/harr cascade/haarcascade_frontalface_default.xml')
不是单个 True/False 值 - 它是(来自错误消息)包含多个值的东西 - 也许这是您可以迭代的东西,或者交替使用所有,任何函数来对集合执行布尔测试。
结果甚至是布尔值吗?
为什么不看看 face_cascade
的内部,看看它保存了什么样的数据?一旦你知道了这一点,你就能更好地应对结果。
也在这里:
for (x,y,w,h) in face:
cv2.rectangle(img,(x,y),(x+w,y+h),(50,20,70),3)
if face == True:#i have used this to make if there is no face found then stop if face found go forward
carapp.forward()
else:
carapp.stop()
您开始在 for 循环中迭代 face
,但随后您尝试询问它是 True 还是 False。
这是不一致的。
x,h 的内容是什么?它们看起来像是在描述边界框,而不是我想象的您可以明智地使用 True 或 False 进行测试。
这些是布尔值吗?
此外,其中一些“受版权保护的代码”看起来像是您从这些文档中复制/粘贴的: