JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
demo.py
Go to the documentation of this file.
1# This file is part of OpenCV Zoo project.
2# It is subject to the license terms in the LICENSE file found in the same directory.
3#
4# Copyright (C) 2021, Shenzhen Institute of Artificial Intelligence and Robotics for Society, all rights reserved.
5# Third party copyrights are property of their respective owners.
6
7import argparse
8
9import numpy as np
10import cv2 as cv
11
12from dasiamrpn import DaSiamRPN
13
14def str2bool(v):
15 if v.lower() in ['on', 'yes', 'true', 'y', 't']:
16 return True
17 elif v.lower() in ['off', 'no', 'false', 'n', 'f']:
18 return False
19 else:
20 raise NotImplementedError
21
22parser = argparse.ArgumentParser(
23 description="Distractor-aware Siamese Networks for Visual Object Tracking (https://arxiv.org/abs/1808.06048)")
24parser.add_argument('--input', '-i', type=str, help='Path to the input video. Omit for using default camera.')
25parser.add_argument('--model_path', type=str, default='object_tracking_dasiamrpn_model_2021nov.onnx', help='Path to dasiamrpn_model.onnx.')
26parser.add_argument('--kernel_cls1_path', type=str, default='object_tracking_dasiamrpn_kernel_cls1_2021nov.onnx', help='Path to dasiamrpn_kernel_cls1.onnx.')
27parser.add_argument('--kernel_r1_path', type=str, default='object_tracking_dasiamrpn_kernel_r1_2021nov.onnx', help='Path to dasiamrpn_kernel_r1.onnx.')
28parser.add_argument('--save', '-s', type=str2bool, default=False, help='Set true to save results. This flag is invalid when using camera.')
29parser.add_argument('--vis', '-v', type=str2bool, default=True, help='Set true to open a window for result visualization. This flag is invalid when using camera.')
30args = parser.parse_args()
31
32def visualize(image, bbox, score, isLocated, fps=None, box_color=(0, 255, 0),text_color=(0, 255, 0), fontScale = 1, fontSize = 1):
33 output = image.copy()
34 h, w, _ = output.shape
35
36 if fps is not None:
37 cv.putText(output, 'FPS: {:.2f}'.format(fps), (0, 30), cv.FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize)
38
39 if isLocated and score >= 0.6:
40 # bbox: Tuple of length 4
41 x, y, w, h = bbox
42 cv.rectangle(output, (x, y), (x+w, y+h), box_color, 2)
43 cv.putText(output, '{:.2f}'.format(score), (x, y+20), cv.FONT_HERSHEY_DUPLEX, fontScale, text_color, fontSize)
44 else:
45 text_size, baseline = cv.getTextSize('Target lost!', cv.FONT_HERSHEY_DUPLEX, fontScale, fontSize)
46 text_x = int((w - text_size[0]) / 2)
47 text_y = int((h - text_size[1]) / 2)
48 cv.putText(output, 'Target lost!', (text_x, text_y), cv.FONT_HERSHEY_DUPLEX, fontScale, (0, 0, 255), fontSize)
49
50 return output
51
52if __name__ == '__main__':
53 # Instantiate DaSiamRPN
54 model = DaSiamRPN(
55 model_path=args.model_path,
56 kernel_cls1_path=args.kernel_cls1_path,
57 kernel_r1_path=args.kernel_r1_path
58 )
59
60 # Read from args.input
61 _input = args.input
62 if args.input is None:
63 device_id = 0
64 _input = device_id
65 video = cv.VideoCapture(_input)
66
67 # Select an object
68 has_frame, first_frame = video.read()
69 if not has_frame:
70 print('No frames grabbed!')
71 exit()
72 first_frame_copy = first_frame.copy()
73 cv.putText(first_frame_copy, "1. Drag a bounding box to track.", (0, 15), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
74 cv.putText(first_frame_copy, "2. Press ENTER to confirm", (0, 35), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))
75 roi = cv.selectROI('DaSiamRPN Demo', first_frame_copy)
76 print("Selected ROI: {}".format(roi))
77
78 # Init tracker with ROI
79 model.init(first_frame, roi)
80
81 # Track frame by frame
82 tm = cv.TickMeter()
83 while cv.waitKey(1) < 0:
84 has_frame, frame = video.read()
85 if not has_frame:
86 print('End of video')
87 break
88 # Inference
89 tm.start()
90 isLocated, bbox, score = model.infer(frame)
91 tm.stop()
92 # Visualize
93 frame = visualize(frame, bbox, score, isLocated, fps=tm.getFPS())
94 cv.imshow('DaSiamRPN Demo', frame)
95 tm.reset()
visualize(image, results, box_color=(0, 255, 0), text_color=(0, 0, 255), fps=None)
Definition demo.py:46
int
Definition demo.py:37