How to add “Tracker” in openCV python 2.7

前端 未结 5 1893
一个人的身影
一个人的身影 2020-12-20 14:05

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         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 14:42

    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.

提交回复
热议问题