简体   繁体   English

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

[英]I am getting error when i run this code to make raspberry pi face tracking robot with gui without any servo motor just to follow only people

Please tell me what wrong here.请告诉我这里出了什么问题。 It gives error like this它给出了这样的错误

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()

The code is here:代码在这里:

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

The return value from返回值来自

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

Is not a single True/False value - it's something that (from the error message) contains multiple values - perhaps that's something over which you can iterate, or alternately use the all, any functions to perform boolean tests over the collection.不是单个 True/False 值 - 它是(从错误消息中)包含多个值的东西 - 也许这是您可以迭代的东西,或者交替使用所有功能来对集合执行 boolean 测试。

Are the results even boolean?结果甚至是boolean吗?

Why don't you look inside of face_cascade and see what kind of data it holds?你为什么不看看face_cascade里面有什么数据呢? Once you know that, you'll be better equipped to deal with the result.一旦你知道了这一点,你就会更好地处理结果。

Also here:也在这里:

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() 

You start iterating over face in your for-loop, but then later you try to ask whether it's True or False.您开始在 for 循环中遍历face ,但稍后您尝试询问它是 True 还是 False。

Which is inconsistent.这是不一致的。

What are the contents of x,y,w,h? x,y,w,h 的内容是什么? They look like they might describe bounding boxes, not what I'd imagine you'd sensibly be able to test with a True or False.它们看起来可能会描述边界框,而不是我想象的你可以用真或假来测试的东西。

Are any of those boolean?那些boolean中的任何一个?

Also, some of this "copyrighted code" looks like you copy/pasted it from these docs:此外,其中一些“受版权保护的代码”看起来像是您从这些文档中复制/粘贴的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Raspberry Pi 3 B+ 控制伺服电机的 Python 代码错误 - Error in Python code in controlling servo motor using Raspberry Pi 3 B+ Raspberry Pi 连接到电机控制器 - 代码中出现错误 - Raspberry Pi connected to Motor Controller- Getting error in code 通过树莓派控制伺服电机速度 - control servo motor speed by raspberry pi Raspberry Pi 3 +温度传感器+伺服电机 - Raspberry Pi 3 + temperature sensor + servo motor 用 Raspberry Pi 和 Python 控制连续伺服电机 - Control continuous servo motor with Raspberry Pi and Python 为什么我在运行此代码时会收到错误代码? - Why am I getting an error code when I run this code? Raspberry Pi 4 控制伺服而不晃动 - Raspberry Pi 4 controlling a Servo without shaking 机器人框架:将机器人框架与MySql连接时,出现“ FAIL:NoSectionError:No section:'default'”错误 - Robot Framework :I am getting “FAIL : NoSectionError: No section: 'default' ” error when i connect the Robot Framework with MySql 为什么我会收到此错误:imutils VideoStream(src=0) return VIDIOC_QBUF: Invalid argument in raspberry pi - Why am I getting this error: imutils VideoStream(src=0) return VIDIOC_QBUF: Invalid argument in raspberry pi 为什么在Raspberry Pi 3上下载virtualenv时会出现错误? - Why am I getting errors while downloading virtualenv on Raspberry Pi 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM