首页 > 解决方案 > 当我运行此代码以制作带有 gui 的树莓派人脸跟踪机器人时出现错误,没有任何伺服电机只是为了跟随人

问题描述

请告诉我这里出了什么问题。它给出了这样的错误

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)

    GPIO.output(Motor1B,GPIO.LOW)

    GPIO.output(Motor2A,GPIO.LOW)

    GPIO.output(Motor2B,GPIO.HIGH)

def Right():

    print("Going Right")

    GPIO.output(Motor1A,GPIO.LOW)

    GPIO.output(Motor1B,GPIO.HIGH)

    GPIO.output(Motor2A,GPIO.HIGH)

    GPIO.output(Motor2B,GPIO.LOW)

def stop():

    print("Stopping")

    GPIO.output(Motor1A,GPIO.LOW)

    GPIO.output(Motor1B,GPIO.LOW)

    GPIO.output(Motor2A,GPIO.LOW)

    GPIO.output(Motor2B,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

标签: pythonperformanceraspberry-picomputer-visionrobotics

解决方案


返回值来自

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,y,w,h 的内容是什么?它们看起来可能会描述边界框,而不是我想象的你可以用真或假来测试的东西。

这些是布尔值吗?

此外,其中一些“受版权保护的代码”看起来像是您从这些文档中复制/粘贴的:

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_objdetect/py_face_detection/py_face_detection.html


推荐阅读