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.
Object tracking using OpenCV is a popular method that is extensively used in the domain. OpenCV has a number of built-in functions specifically designed for the purpose of object tracking. Some object trackers in OpenCV include MIL, CSRT, GOTURN, and MediandFlow.
For tracking, we pass the current frame to the existing multi-tracker object. If tracking is successful, we extract the new box coordinates with which to draw rectangles around the previously detected objects and print the corresponding class labels, on the current frame.
Two main approaches for tracking: Single Object tracking (SOT) and Multiple Object Tracking (MOT) In SOT, the bounding box of the target in the first frame is given to the tracker. The goal of the tracker is then to locate the same target in all the other frames.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With