首页 > 解决方案 > 使用计算机视觉计算移动角度时出错

问题描述

我正在运行深度学习对象检测,以使用 OpenCV、SingleShot Detector 和 MobileNets 在空间中跟踪一个人。我正在用 Python 编写代码。我需要计算当人从一个位置移动到另一个位置时行进的角度。

我编写了一个算法,首先使用边界框尺寸计算检测到的人的质心。接下来,它使用使用回归获得的公式计算与人的距离。完成后,将生成的值与之前的值进行比较,并计算它们的水平距离。因此,对于每个实例,当前距离、先前距离和它们之间的水平距离形成一个具有已知边的三角形。因此,可以使用余弦规则计算行进的角度,该角度本质上是表示先前距离和当前距离的线之间的角度。我在我的算法中针对各种可能的情况执行了此操作。

if CLASSES[idx] == "person":
    heig = endY-startY
    wid = endX-startX
    currDistance = 23.0591-(0.0686*heig)
    currCentX = (startX + (endX / 2.0))
    currCentY = (startY + (endY / 2.0))
    if initValues > 0:
        if (prevCentX > currCentX):
            if (prevCentY == currCentY):
                horizDistMoved = abs((prevCentX-currCentX))
                angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
            elif (prevCentY < currCentY):
                horizDistMoved = hypot((prevCentX-currCentX),(currCentY-prevCentY))
                angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
            elif (prevCentY > currCentY):
                horizDistMoved = hypot((prevCentX-currCentX),(prevCentY-currCentY))
                angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
            elif (prevCentX == currCentX):
                horizDistMoved = 0
                angle = 0
            elif (prevCentX < currCentX):
                if (prevCentY == currCentY):
                    horizDistMoved = abs((currCentX-prevCentX))
                    angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
                elif (prevCentY < currCentY):
                    horizDistMoved = hypot((currCentX-prevCentX),(currCentY-prevCentY))
                    angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
                elif (prevCentY > currCentY):
                    horizDistMoved = hypot((currCentX-prevCentX),(prevCentY-currCentY))
                    angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
            print("Angle is " +str(angle))
        else:
            time.sleep(0.2)
        prevCentX = currCentX
        prevCentY = currCentY
        prevDistance = currDistance
        initValues += 1

当人移动时,我期望打印出角度。但是,每次我移动时,算法都会崩溃并出现以下错误。根据我的位置,这发生在不同的行上。

Traceback (most recent call last):
  File "Navigation+Servo_lite.py", line 146, in <module>
     angle = degrees(acos(((currDistance**2 + prevDistance**2)-(horizDistMoved**2))/(2*currDistance*prevDistance)))
ValueError: math domain error

我认为由于某种原因,构成除数的表达式一直为零。我将不胜感激有关可能出了什么问题以及如何改进它的任何建议,或者是否有另一种方法来计算角度。

整个代码块比上面的要长,但如果需要,我也可以包含它。

谢谢你。

标签: pythonopencvcomputer-visiontrigonometryangle

解决方案


推荐阅读