Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add "Tracker" in openCV python 2.7

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?

like image 924
Elahe Avatar asked Feb 22 '17 09:02

Elahe


People also ask

How do I import a tracker into Python?

Move the module to any folder in Python Path: Python path can be found out by entering import sys;print sys. path in Python terminal. It will print out many locations. Move /usr/local/lib/python2.


4 Answers

Just install opencv-contrib-python

pip install opencv-contrib-python

and it will work !

like image 178
Manish S Avatar answered Oct 17 '22 17:10

Manish S


I think the easiest and fastest method is to install via the .whl files. @foobar gives the answer in the post @kyjanond links to, but you can obtain the .whl files from the following links.

OpenCV: https://pypi.python.org/pypi/opencv-python/3.3.0.10

OpenCV Contrib: https://pypi.python.org/pypi/opencv-contrib-python/3.3.0.10

I installed OpenCV 3.3.0 on Python 2.7, so I downloaded:

  • opencv_python-3.3.0.10-cp27-cp27m-win32.whl
  • opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

To install, I ran:

  • python -m pip install opencv_python-3.3.0.10-cp27-cp27m-win32.whl
  • python -m pip install opencv_contrib_python-3.3.0.10-cp27-cp27m-win32.whl

This worked, but in the updated version of OpenCV, the way the tracker functions are called have changed.

The original code in the GitHub repository was:


tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']

tracker_type = tracker_types[1]

tracker = cv2.Tracker_create(tracker_type)

I changed this to


tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN']

tracker_type = tracker_types[1]

if tracker_type == tracker_types[0]:
    tracker = cv2.TrackerBoosting_create()
elif tracker_type == tracker_types[1]:
    tracker = cv2.TrackerMIL_create()
elif tracker_type == tracker_types[2]:
    tracker = cv2.TrackerKCF_create()
elif tracker_type == tracker_types[3]:
    tracker = cv2.TrackerTLD_create()
elif tracker_type == tracker_types[4]:
    tracker = cv2.TrackerMedianFlow_create()
elif tracker_type == tracker_types[5]:
    tracker = cv2.TrackerGOTURN_create()

This approach seemed to work well for me.

like image 45
solarflare Avatar answered Oct 17 '22 16:10

solarflare


It looks like you didn't compile your OpenCV with opencv_contrib modules. You have to recompile it. You can find a very good step-by-step tutorial how to do that in this blogpost.

EDIT:

If you need to compile it on Windows you can use this great tutorial by @Osama

Hope it helps.

like image 3
kyjanond Avatar answered Oct 17 '22 17:10

kyjanond


Once Installation is over. All files are installed in /usr/local/ folder.
But to use it, your Python should be able to find OpenCV module.

You have two options for that.

  1. Move the module to any folder in Python Path: Python path can be found out by entering import sys;print sys.path in Python terminal. It will print out many locations. Move /usr/local/lib/python2.7/site-packages/cv2.so to any of this folder. For example, su mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/ → site-packages. But you will have to do this every time you install OpenCV.

  2. Add /usr/local/lib/python2.7/site-packages to the PYTHON_PATH: It is to be done only once. Just open ~/.bashrc and add following line to it, then log out and come back. export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages. Thus OpenCV installation is finished. Open a terminal and try import cv2.

like image 1
Deepak Avatar answered Oct 17 '22 18:10

Deepak