简体   繁体   中英

python opencv - Object Tracking and coordinates

I am using an object tracking code. https://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/ . For the couple of times it worked fine. It tracked the ball just fine. Then, the camera was opened with no problem but when I moved the tennis ball in camera's range of vision I got the following error

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 I delete if counter >= 10 and i == 1 and pts[-10] is not None:

the code works without showing the positions of x and y but that is not what I want. what could be the problem?

Same Problem (at least i think so)

It crashes as soon as the detection recognizes sth at this Line

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

My "Solution" is not good and its decreasing the accuracy pretty much but at least i can run it without chrashing

replace the line mentioned above with

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

and then add a try/except block in which you put everything that was in the if block. So the part should look like this

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"

Reset the frame counter after detect contour:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM