计算使用计算机视觉移动的角度时出错

时间:2019-06-08 01:49:57

标签: python opencv computer-vision trigonometry angle

我正在运行深度学习对象检测,以使用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

我认为由于某种原因,构成除数的表达式始终等于零。对于可能出什么问题以及如何进行改进,或者是否有其他方法可以计算角度,我将不胜感激。

整个代码块都比上面的代码块长,但是如果需要,我也可以添加它。

谢谢。

0 个答案:

没有答案