【问题标题】:python opencv - Object Tracking and coordinatespython opencv - 对象跟踪和坐标
【发布时间】:2021-03-05 06:01:13
【问题描述】:

我正在使用对象跟踪代码。 https://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/。有几次它工作得很好。它跟踪球很好。然后,打开相机没有问题,但是当我在相机的视野范围内移动网球时,我得到了以下错误

if counter >= 10 and i == 1 and pts[-10] is not None:IndexError: deque index out of range

dX = pts[-10][0] - pts[i][0]
        dY = pts[-10][1] - pts[i][1]
        (dirX, dirY) = ("", "")

如果我删除if counter >= 10 and i == 1 and pts[-10] is not None:

代码可以在不显示 x 和 y 的位置的情况下工作,但这不是我想要的。可能是什么问题?

【问题讨论】:

  • 你试过用括号括住的两个参数吗?我认为 and 首先被评估,所以你先做10 and i 然后剩下的......所以:if (counter >= 10) and (i == 1) and (pts[-10] is not None):
  • 它没有用。相机打开没有问题,但是当我将网球移到相机的视野范围内时,窗户就关闭了。

标签: python opencv


【解决方案1】:

同样的问题(至少我是这么认为的)

一旦检测到这条线的某事,它就会崩溃

if counter >= 10 and i == 1 and pts[-10] is not None:

我的“解决方案”不好,而且它的准确性大大降低,但至少我可以运行它而不会崩溃

将上面提到的行替换为

if (counter >= 10) and (i == 1):

然后添加一个 try/except 块,在其中放置 if 块中的所有内容。所以这部分应该是这样的

if (counter >= 10) and (i == 1):
        try:
            if pts[-10] is not None:
                dX = pts[-10][0] - pts[i][0]
                dY = pts[-10][1] - pts[i][1]
                (dirX, dirY) = ("", "")

                if np.abs(dX) > 20:
                    dirX = "East" if np.sign(dX) == 1 else "West"

                if np.abs(dY) > 20:
                    dirY = "North" if np.sign(dY) == 1 else "South"

                if dirX != "" and dirY != "":
                    direction = "{}-{}".format(dirY, dirX)

            else:
                direction = dirX if dirX != "" else dirY

        except:
            direction = "error"

【讨论】:

    【解决方案2】:

    检测轮廓后重置帧计数器:

    # only proceed if at least one contour was found
    if len(cnts) > 0:
        # find the largest contour in the mask, then use
        # it to compute the minimum enclosing circle and
        # centroid
        c = max(cnts, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
    
    
    if len(pts) >= 60:
           cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2019-12-07
      • 2017-04-25
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多