How to use tracker_builder method in tempest

Best Python code snippet using tempest_python

pysot_trackers.py

Source:pysot_trackers.py Github

copy

Full Screen

1import sys2sys.path.append("backends/pysot")34from argparse import Namespace5import torch6from pathlib import Path7import numpy as np8import cv29import sys1011import pysot.models.model_builder12import pysot.tracker.tracker_builder13from pysot.core.config import cfg14#from pysot.models.model_builder import ModelBuilder15#from pysot.tracker.tracker_builder import build_tracker161718class SiamRPNTracker:19 def __init__(self, is_cpu=True):20 is_cpu = not torch.cuda.is_available() and is_cpu21 cfgarg = Namespace(config="backends/pysot/experiments/siamrpn_alex_dwxcorr/config.yaml",22 snapshot="backends/pysot/experiments/siamrpn_alex_dwxcorr/model.pth",23 )24 # try: # 初期化が出来ない謎仕様のため25 # del sys.modules['pysot.core.config']26 # del sys.modules['pysot.models.model_builder']27 # del sys.modules['pysot.tracker.tracker_builder']28 # except Exception as e:29 # print("cannot re import")30 # from pysot.core.config import cfg31 # import pysot.models.model_builder32 # import pysot.tracker.tracker_builder3334 cfg.merge_from_file(cfgarg.config)35 cfg.CUDA = not is_cpu36 #cfg.CUDA = False37 device = torch.device("cuda" if not is_cpu else "cpu")38 model = pysot.models.model_builder.ModelBuilder()39 model.load_state_dict(torch.load(cfgarg.snapshot,40 map_location=lambda storage, loc: storage.cpu()))41 model.eval().to(device)42 self.tracker = pysot.tracker.tracker_builder.build_tracker(model)43 44 def set_bbox(self, image, xyxy_dict=None, polygon_list=None):45 if xyxy_dict is None:46 raise Exception("SiameseMaskTracker set_bbox need xyxy_dict")47 x1 = xyxy_dict["x1"]48 y1 = xyxy_dict["y1"]49 x2 = xyxy_dict["x2"]50 y2 = xyxy_dict["y2"]51 w = x2-x152 h = y2-y153 54 self.tracker.init(image, (x1,y1,w,h))55 56 def get_bbox(self, image):57 outputs = self.tracker.track(image)58 59 xyxy_dict = {"x1":int(outputs["bbox"][0]),60 "y1":int(outputs["bbox"][1]),61 "x2":int(outputs["bbox"][0]+outputs["bbox"][2]),62 "y2":int(outputs["bbox"][1]+outputs["bbox"][3])63 }64 65 out_dict = {"bbox_dict":xyxy_dict, "polygon":None}66 return out_dict676869class SiamRPNTrackerV2:70 def __init__(self, is_cpu=True):71 is_cpu = not torch.cuda.is_available() and is_cpu72 cfgarg = Namespace(config="backends/pysot/experiments/siamrpn_mobilev2_l234_dwxcorr/config.yaml",73 snapshot="backends/pysot/experiments/siamrpn_mobilev2_l234_dwxcorr/model.pth",74 )7576 # try: # 初期化が出来ない謎仕様のため77 # del sys.modules['pysot.core.config']78 # del sys.modules['pysot.models.model_builder']79 # del sys.modules['pysot.tracker.tracker_builder']80 # except Exception as e:81 # print("cannot re import")82 # from pysot.core.config import cfg83 # import pysot.models.model_builder84 # import pysot.tracker.tracker_builder8586 # cfg.merge_from_file(cfgarg.config)87 cfg.CUDA = not is_cpu88 #cfg.CUDA = False89 device = torch.device("cuda" if not is_cpu else "cpu")90 model = pysot.models.model_builder.ModelBuilder()91 model.load_state_dict(torch.load(cfgarg.snapshot,92 map_location=lambda storage, loc: storage.cpu()))93 model.eval().to(device)94 self.tracker = pysot.tracker.tracker_builder.build_tracker(model)95 96 def set_bbox(self, image, xyxy_dict=None, polygon_list=None):97 if xyxy_dict is None:98 raise Exception("SiameseMaskTracker set_bbox need xyxy_dict")99 x1 = xyxy_dict["x1"]100 y1 = xyxy_dict["y1"]101 x2 = xyxy_dict["x2"]102 y2 = xyxy_dict["y2"]103 w = x2-x1104 h = y2-y1105 106 self.tracker.init(image, (x1,y1,w,h))107 108 def get_bbox(self, image):109 outputs = self.tracker.track(image)110 111 xyxy_dict = {"x1":int(outputs["bbox"][0]),112 "y1":int(outputs["bbox"][1]),113 "x2":int(outputs["bbox"][0]+outputs["bbox"][2]),114 "y2":int(outputs["bbox"][1]+outputs["bbox"][3])115 }116 117 out_dict = {"bbox_dict":xyxy_dict, "polygon":None}118 return out_dict119120121class SiamMaskTrackerV2():122 def __init__(self, is_cpu=True, epsilon=0.01):123 is_cpu = not torch.cuda.is_available() and is_cpu124 cfgarg = Namespace(config="backends/pysot/experiments/siammask_r50_l3/config.yaml",125 snapshot="backends/pysot/experiments/siammask_r50_l3/model.pth",126 )127128 # try: # 初期化が出来ない謎仕様のため129 # del sys.modules['pysot.core.config']130 # del sys.modules['pysot.models.model_builder']131 # del sys.modules['pysot.tracker.tracker_builder']132 # except Exception as e:133 # print("cannot re import")134 # from pysot.core.config import cfg135 # import pysot.models.model_builder136 # import pysot.tracker.tracker_builder137138 cfg.merge_from_file(cfgarg.config)139 cfg.CUDA = not is_cpu140 #cfg.CUDA = False141 device = torch.device("cuda" if not is_cpu else "cpu")142 model = pysot.models.model_builder.ModelBuilder()143 model.load_state_dict(torch.load(cfgarg.snapshot,144 map_location=lambda storage, loc: storage.cpu()))145 model.eval().to(device)146 self.tracker = pysot.tracker.tracker_builder.build_tracker(model)147 self.epsilon = epsilon148 149 def set_bbox(self, image, xyxy_dict=None, polygon_list=None):150 if xyxy_dict is None:151 raise Exception("SiameseMaskTracker set_bbox need xyxy_dict")152 x1 = xyxy_dict["x1"]153 y1 = xyxy_dict["y1"]154 x2 = xyxy_dict["x2"]155 y2 = xyxy_dict["y2"]156 w = x2-x1157 h = y2-y1158 159 self.tracker.init(image, (x1,y1,w,h))160 161 def get_bbox(self, image):162 outputs = self.tracker.track(image)163 mask_img = (outputs["mask"]*255).astype(np.uint8)164 _, img_otsu = cv2.threshold(mask_img, 0, 255, cv2.THRESH_OTSU)165 #_, contours, _ = cv2.findContours(img_otsu, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)166 if cv2.__version__[-5] == '4':167 contours, _ = cv2.findContours(img_otsu, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)168 else:169 _, contours, _ = cv2.findContours(img_otsu, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)170 out_dict = {}171 172 cnt_area = [cv2.contourArea(cnt) for cnt in contours]173 if len(contours) != 0 and np.max(cnt_area) > 100:174 contour = contours[np.argmax(cnt_area)] # use max area polygon175 polygon = contour.reshape(-1, 2)176 pbox = cv2.boundingRect(polygon) # Min Max Rectangle177178 out_dict["bbox_dict"] = {"x1":pbox[0],179 "y1":pbox[1],180 "x2":pbox[0]+pbox[2],181 "y2":pbox[1]+pbox[3]182 } 183 epsilon = self.epsilon*cv2.arcLength(polygon, True)184 approx = cv2.approxPolyDP(polygon, epsilon, True).squeeze()185 out_dict["polygon"] = [{"x":int(one_point[0]), "y":int(one_point[1])} for one_point in approx]186 187 else:188 out_dict["polygon"] = None189 out_dict["bbox_dict"] = {"x1":None,"y1":None,"x2":None,"y2":None}190 191 return out_dict192193 194if __name__ == "__main__": ...

Full Screen

Full Screen

builder.py

Source:builder.py Github

copy

Full Screen

1# -*- coding: utf-8 -*2from typing import Dict3from yacs.config import CfgNode4from .tracker import builder as tracker_builder5from .tracker.tracker_base import TRACK_PIPELINES6# from .segmenter import builder as segmenter_builder7def build_pipeline(task: str, cfg: CfgNode):8 """9 Builder function.10 Arguments11 ---------12 task: str13 builder task name (track|vos)14 cfg: CfgNode15 buidler configuration16 Returns17 -------18 torch.nn.Module19 module built by builder20 """21 if task == "track":22 track_pipeline = tracker_builder.build(cfg)23 return track_pipeline24 else:25 print("model for task {} is not complted".format(task))26 exit(-1)27def get_config() -> Dict[str, CfgNode]:28 """29 Get available component list config30 Returns31 -------32 CfgNode33 config with list of available components34 """35 cfg_dict = {"track": CfgNode()}36 for cfg_name, module in zip([37 "track",38 ], [39 TRACK_PIPELINES,40 ]):41 cfg = cfg_dict[cfg_name]42 cfg["name"] = "unknown"43 for name in module:44 cfg[name] = CfgNode()45 tester = module[name]46 hps = tester.default_hyper_params47 for hp_name in hps:48 cfg[name][hp_name] = hps[hp_name]...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful