我正在使用对象跟踪代码。 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的位置,但这不是我想要的。可能是什么问题?
答案 0 :(得分:0)
相同的问题(至少我是这样认为的)
一旦检测到此行,它就会崩溃
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"
答案 1 :(得分:0)
检测轮廓后重置帧计数器:
# 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)