How to add "Tracker" in openCV python 2.7

Elahe picture Elahe · Feb 22, 2017 · Viewed 21.2k times · Source

I’m working with python 2.7 and opencv 3.1 I want to run a code for tracking objects by this:

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("videos/chaplin.mp4")

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

but when I run it, I face with this error:

AttributeError: 'module' object has no attribute 'Tracker_create'

Here is the source code : http://www.learnopencv.com/object-tracking-using-opencv-cpp-python/ I’m searching for solutions but I can’t find anything useful… what can I do to add this module to my opencv library?

Answer

Manish S picture Manish S · Feb 12, 2018

Just install opencv-contrib-python

pip install opencv-contrib-python

and it will work !