9

I'm trying to use openCV tracking API in Python for object tracking. I tried the code in this link and don't have any problem running the code. But, looking at the openCV documentation here, I realized there are parameters for each of the tracking algorithms. How can I set these parameters in python? Had a hard time trying to figure this out, but no success.

Miranda
  • 565
  • 1
  • 10
  • 27

1 Answers1

2

An update, 4 years later, for anyone that might still be looking. For opencv-python-contrib versions around 3.x you can use sturkmen's answer (from here)

tracker = cv2.TrackerCSRT_create()
tracker.save("default_csrt.xml") // saves default values of the Tracker
                                    you can rename default_csrt.xml-> custom_csrt.xml 
                                    and change values in it and use it load params

fs = cv2.FileStorage("custom_csrt.xml",cv2.FILE_STORAGE_READ)
fn = fs.getFirstTopLevelNode()
tracker.read(fn)

For opencv 4.x (I'm on 4.5, and haven't gone through to check when the change happened), most of the trackers have a Tracker*_Params() method. If you want to find what parameters are available you can run e.g.

list(filter(lambda x: x.find('__') < 0, dir(cv2.TrackerCSRT_Params)))

In all, if you want to set the parameters for a tracker in Python opencv you could do something like this (note, I fudged the version compatibilities)

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
def set_CSRT_Params():
    # Don't modify
    default_params = {
        'padding': 3.,
        'template_size': 200.,
        'gsl_sigma': 1.,
        'hog_orientations': 9.,
        'num_hog_channels_used': 18,
        'hog_clip': 2.0000000298023224e-01,
        'use_hog': 1,
        'use_color_names': 1,
        'use_gray': 1,
        'use_rgb': 0,
        'window_function': 'hann',
        'kaiser_alpha': 3.7500000000000000e+00,
        'cheb_attenuation': 45.,
        'filter_lr': 1.9999999552965164e-02,
        'admm_iterations': 4,
        'number_of_scales': 100,
        'scale_sigma_factor': 0.25,
        'scale_model_max_area': 512.,
        'scale_lr': 2.5000000372529030e-02,
        'scale_step': 1.02,
        'use_channel_weights': 1,
        'weights_lr': 1.9999999552965164e-02,
        'use_segmentation': 1,
        'histogram_bins': 16,
        'background_ratio': 2,
        'histogram_lr': 3.9999999105930328e-02,
        'psr_threshold': 3.5000000149011612e-02,
    }
    # modify
    params = {
        'scale_lr': 0.5,
        'number_of_scales': 200
    }
    params = {**default_params, **params}
    tracker = None
    if int(major_ver) == 3 and 3 <= int(minor_ver) <= 4:
        import json
        import os
        with open('tmp.json', 'w') as fid:
            json.dump(params, fid)
        fs_settings = cv2.FileStorage("tmp.json", cv2.FILE_STORAGE_READ)
        tracker = cv2.TrackerCSRT_create()
        tracker.read(fs_settings.root())
        os.remove('tmp.json')
    elif int(major_ver) >= 4:
        param_handler = cv2.TrackerCSRT_Params()
        for key, val in params.items():
            setattr(param_handler, key, val)
        tracker = cv2.TrackerCSRT_create(param_handler)
    else:
        print("Cannot set parameters, using defaults")
        tracker = cv2.TrackerCSRT_create()
    return tracker
Andrew Holmgren
  • 1,225
  • 1
  • 11
  • 18