0

Traceback (most recent call last):
 File "test.py", line 10, in <module>
  tracker = cv2.Tracker_create("MIL")
AttributeError: module 'cv2.cv2' has no attribute 'Tracker_create

I get the above error when I try to run:

import cv2
import sys

if __name__ == '__main__' :

# Set up tracker.
# Instead of MIL, you can also use
# BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN

tracker = cv2.Tracker_create("MIL")

# Read video
video = cv2.VideoCapture(0)

# Exit if video not opened.
if not video.isOpened():
    print ("Could not open video")
    sys.exit()

# Read first frame.
ok, frame = video.read()
if not ok:
    print ('Cannot read video file')
    sys.exit()

# Define an initial bounding box
bbox = (287, 23, 86, 320)

# Uncomment the line below to select a different bounding box
# bbox = cv2.selectROI(frame, False)

# Initialize tracker with first frame and bounding box
ok = tracker.init(frame, bbox)

while True:
    # Read a new frame
    ok, frame = video.read()
    if not ok:
        break

    # Update tracker
    ok, bbox = tracker.update(frame)

    # Draw bounding box
    if ok:
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (0,0,255))

    # Display result
    cv2.imshow("Tracking", frame)

    # Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27 : break

I found an answer here: How to add "Tracker" in openCV python 2.7

But this confused me more. I'm on MacOSX and I'm just getting started with OpenCV and I'm not really sure how to recompile OpenCV with the correct modules.

Thanks in advance, and sorry if I'm missing something obvious.

1
  • Did you install opencv_contrib module? tracker is under tracking module which is not available in opencv core. Commented Oct 5, 2017 at 16:24

1 Answer 1

1

So it wasn't a case of the installation, but the constructor name had changed.

tracker = cv2.Tracker_create("MIL")

Should be:

tracker = cv2.TrackerMIL_create()
Sign up to request clarification or add additional context in comments.

1 Comment

This answer should be accepted, hope the person who ask this question accept the answer,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.