Yuantao Feng commited on
Commit
23d8387
·
1 Parent(s): 0199e9f

Add DaSiamRPN for object tracking (#15)

Browse files

* impl wrapper & demo

* add data for object tracking benchmark

* impl benchmark for DaSiamRPN

* update benchmark results for DaSiamRPN

README.md CHANGED
@@ -36,6 +36,7 @@ Hardware Setup:
36
  | [PP-ResNet](./models/image_classification_ppresnet) | 224x224 | 56.05 | 602.58 | 98.64 |
37
  | [PP-HumanSeg](./models/human_segmentation_pphumanseg) | 192x192 | 19.92 | 105.32 | 67.97 |
38
  | [WeChatQRCode](./models/qrcode_wechatqrcode) | 100x100 | 7.04 | 37.68 | --- |
 
39
 
40
  ## License
41
 
 
36
  | [PP-ResNet](./models/image_classification_ppresnet) | 224x224 | 56.05 | 602.58 | 98.64 |
37
  | [PP-HumanSeg](./models/human_segmentation_pphumanseg) | 192x192 | 19.92 | 105.32 | 67.97 |
38
  | [WeChatQRCode](./models/qrcode_wechatqrcode) | 100x100 | 7.04 | 37.68 | --- |
39
+ | [DaSiamRPN](./models/object_tracking_dasiamrpn) | 1280x720 | 36.15 | 705.48 | 76.82 |
40
 
41
  ## License
42
 
benchmark/benchmark.py CHANGED
@@ -85,14 +85,14 @@ class Benchmark:
85
  model.setBackend(self._backend)
86
  model.setTarget(self._target)
87
 
88
- if 'video' in self._dataloader.name.lower():
89
- model.init(self._dataloader.getROI())
90
-
91
- for data in self._dataloader:
92
- filename, img = data[:2]
93
- size = [img.shape[1], img.shape[0]]
94
  if filename not in self._benchmark_results:
95
  self._benchmark_results[filename] = dict()
 
 
 
 
96
  self._benchmark_results[filename][str(size)] = self._metric.forward(model, *data[1:])
97
 
98
  def printResults(self):
 
85
  model.setBackend(self._backend)
86
  model.setTarget(self._target)
87
 
88
+ for idx, data in enumerate(self._dataloader):
89
+ filename, input_data = data[:2]
 
 
 
 
90
  if filename not in self._benchmark_results:
91
  self._benchmark_results[filename] = dict()
92
+ if isinstance(input_data, np.ndarray):
93
+ size = [input_data.shape[1], input_data.shape[0]]
94
+ else:
95
+ size = input_data.getFrameSize()
96
  self._benchmark_results[filename][str(size)] = self._metric.forward(model, *data[1:])
97
 
98
  def printResults(self):
benchmark/config/object_tracking_dasiamrpn.yaml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Benchmark:
2
+ name: "Object Tracking Benchmark"
3
+ type: "Tracking"
4
+ data:
5
+ type: "TrackingVideoLoader"
6
+ path: "benchmark/data/object_tracking"
7
+ files: ["throw_cup.mp4"]
8
+ metric:
9
+ type: "Tracking"
10
+ reduction: "gmean"
11
+ backend: "default"
12
+ target: "cpu"
13
+
14
+ Model:
15
+ name: "DaSiamRPN"
16
+ model_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_model_2021nov.onnx"
17
+ kernel_cls1_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_kernel_cls1_2021nov.onnx"
18
+ kernel_r1_path: "models/object_tracking_dasiamrpn/object_tracking_dasiamrpn_kernel_r1_2021nov.onnx"
benchmark/download_data.py CHANGED
@@ -184,7 +184,11 @@ data_downloaders = dict(
184
  qrcode=Downloader(name='qrcode',
185
  url='https://drive.google.com/u/0/uc?id=1_OXB7eiCIYO335ewkT6EdAeXyriFlq_H&export=download',
186
  sha='ac01c098934a353ca1545b5266de8bb4f176d1b3',
187
- filename='qrcode.zip')
 
 
 
 
188
  )
189
 
190
  if __name__ == '__main__':
 
184
  qrcode=Downloader(name='qrcode',
185
  url='https://drive.google.com/u/0/uc?id=1_OXB7eiCIYO335ewkT6EdAeXyriFlq_H&export=download',
186
  sha='ac01c098934a353ca1545b5266de8bb4f176d1b3',
187
+ filename='qrcode.zip'),
188
+ object_tracking=Downloader(name='object_tracking',
189
+ url='https://drive.google.com/u/0/uc?id=1_cw5pUmTF-XmQVcQAI8fIp-Ewi2oMYIn&export=download',
190
+ sha='0bdb042632a245270013713bc48ad35e9221f3bb',
191
+ filename='object_tracking.zip')
192
  )
193
 
194
  if __name__ == '__main__':
benchmark/utils/dataloaders/base_dataloader.py CHANGED
@@ -34,7 +34,7 @@ class _BaseImageLoader:
34
  class _VideoStream:
35
  def __init__(self, filepath):
36
  self._filepath = filepath
37
- self._video = cv.VideoCapture(filepath)
38
 
39
  def __iter__(self):
40
  while True:
@@ -44,8 +44,21 @@ class _VideoStream:
44
  else:
45
  break
46
 
 
 
 
 
 
 
 
 
47
  def reload(self):
48
- self._video = cv.VideoCapture(filepath)
 
 
 
 
 
49
 
50
 
51
  class _BaseVideoLoader:
@@ -56,6 +69,10 @@ class _BaseVideoLoader:
56
  self._files = kwargs.pop('files', None)
57
  assert self._files,'Benchmark[\'data\'][\'files\'] cannot be empty.'
58
 
 
 
 
 
59
  @property
60
  def name(self):
61
  return self.__class__.__name__
@@ -64,4 +81,4 @@ class _BaseVideoLoader:
64
  return len(self._files)
65
 
66
  def __getitem__(self, idx):
67
- return self._files[idx], _VideoStream(os.path.join(self._path, self._files[idx]))
 
34
  class _VideoStream:
35
  def __init__(self, filepath):
36
  self._filepath = filepath
37
+ self._video = cv.VideoCapture(self._filepath)
38
 
39
  def __iter__(self):
40
  while True:
 
44
  else:
45
  break
46
 
47
+ def __next__(self):
48
+ while True:
49
+ has_frame, frame = self._video.read()
50
+ if has_frame:
51
+ return frame
52
+ else:
53
+ break
54
+
55
  def reload(self):
56
+ self._video = cv.VideoCapture(self._filepath)
57
+
58
+ def getFrameSize(self):
59
+ w = int(self._video.get(cv.CAP_PROP_FRAME_WIDTH))
60
+ h = int(self._video.get(cv.CAP_PROP_FRAME_HEIGHT))
61
+ return [w, h]
62
 
63
 
64
  class _BaseVideoLoader:
 
69
  self._files = kwargs.pop('files', None)
70
  assert self._files,'Benchmark[\'data\'][\'files\'] cannot be empty.'
71
 
72
+ self._streams = dict()
73
+ for filename in self._files:
74
+ self._streams[filename] = _VideoStream(os.path.join(self._path, filename))
75
+
76
  @property
77
  def name(self):
78
  return self.__class__.__name__
 
81
  return len(self._files)
82
 
83
  def __getitem__(self, idx):
84
+ return self._files[idx], self._streams[idx]
benchmark/utils/dataloaders/tracking.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import numpy as np
2
 
3
  from .base_dataloader import _BaseVideoLoader
@@ -8,18 +9,19 @@ class TrackingVideoLoader(_BaseVideoLoader):
8
  def __init__(self, **kwargs):
9
  super().__init__(**kwargs)
10
 
11
- self._rois = self._load_roi()
 
 
 
12
 
13
- unsupported_keys = []
14
- for k, _ in kwargs.items():
15
- unsupported_keys.append(k)
16
- print('Keys ({}) are not supported in Benchmark[\'data\'].'.format(str(unsupported_keys)))
17
 
18
  def _load_roi(self):
19
  rois = dict.fromkeys(self._files, None)
20
  for filename in self._files:
21
- rois[filename] = np.loadtxt(os.path.join(self._path, '{}.txt'.format(filename[:-4])), ndmin=2)
22
  return rois
23
 
24
- def getROI(self):
25
- return self._rois
 
 
1
+ import os
2
  import numpy as np
3
 
4
  from .base_dataloader import _BaseVideoLoader
 
9
  def __init__(self, **kwargs):
10
  super().__init__(**kwargs)
11
 
12
+ self._first_frames = dict()
13
+ for filename in self._files:
14
+ stream = self._streams[filename]
15
+ self._first_frames[filename] = next(stream)
16
 
17
+ self._rois = self._load_roi()
 
 
 
18
 
19
  def _load_roi(self):
20
  rois = dict.fromkeys(self._files, None)
21
  for filename in self._files:
22
+ rois[filename] = np.loadtxt(os.path.join(self._path, '{}.txt'.format(filename[:-4])), dtype=np.int32, ndmin=2)
23
  return rois
24
 
25
+ def __getitem__(self, idx):
26
+ filename = self._files[idx]
27
+ return filename, self._streams[filename], self._first_frames[filename], self._rois[filename]
benchmark/utils/metrics/__init__.py CHANGED
@@ -1,5 +1,6 @@
1
  from .base import Base
2
  from .detection import Detection
3
  from .recognition import Recognition
 
4
 
5
- __all__ = ['Base', 'Detection', 'Recognition']
 
1
  from .base import Base
2
  from .detection import Detection
3
  from .recognition import Recognition
4
+ from .tracking import Tracking
5
 
6
+ __all__ = ['Base', 'Detection', 'Recognition', 'Tracking']
benchmark/utils/metrics/tracking.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2 as cv
2
+
3
+ from .base_metric import BaseMetric
4
+ from ..factory import METRICS
5
+
6
+ @METRICS.register
7
+ class Tracking(BaseMetric):
8
+ def __init__(self, **kwargs):
9
+ super().__init__(**kwargs)
10
+
11
+ if self._warmup or self._repeat:
12
+ print('warmup and repeat in metric for tracking do not function.')
13
+
14
+ def forward(self, model, *args, **kwargs):
15
+ stream, first_frame, rois = args
16
+
17
+ for roi in rois:
18
+ stream.reload()
19
+ model.init(first_frame, tuple(roi))
20
+ self._timer.reset()
21
+ for frame in stream:
22
+ self._timer.start()
23
+ model.infer(frame)
24
+ self._timer.stop()
25
+
26
+ return self._getResult()
models/__init__.py CHANGED
@@ -5,6 +5,7 @@ from .face_recognition_sface.sface import SFace
5
  from .image_classification_ppresnet.ppresnet import PPResNet
6
  from .human_segmentation_pphumanseg.pphumanseg import PPHumanSeg
7
  from .qrcode_wechatqrcode.wechatqrcode import WeChatQRCode
 
8
 
9
  class Registery:
10
  def __init__(self, name):
@@ -24,4 +25,5 @@ MODELS.register(CRNN)
24
  MODELS.register(SFace)
25
  MODELS.register(PPResNet)
26
  MODELS.register(PPHumanSeg)
27
- MODELS.register(WeChatQRCode)
 
 
5
  from .image_classification_ppresnet.ppresnet import PPResNet
6
  from .human_segmentation_pphumanseg.pphumanseg import PPHumanSeg
7
  from .qrcode_wechatqrcode.wechatqrcode import WeChatQRCode
8
+ from .object_tracking_dasiamrpn.dasiamrpn import DaSiamRPN
9
 
10
  class Registery:
11
  def __init__(self, name):
 
25
  MODELS.register(SFace)
26
  MODELS.register(PPResNet)
27
  MODELS.register(PPHumanSeg)
28
+ MODELS.register(WeChatQRCode)
29
+ MODELS.register(DaSiamRPN)
models/object_tracking_dasiamrpn/LICENSE ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
models/object_tracking_dasiamrpn/README.md ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # DaSiamRPN
2
+
3
+ [Distractor-aware Siamese Networks for Visual Object Tracking](https://arxiv.org/abs/1808.06048)
4
+
5
+ Note:
6
+ - Model source: [opencv/samples/dnn/diasiamrpn_tracker.cpp](https://github.com/opencv/opencv/blob/ceb94d52a104c0c1287a43dfa6ba72705fb78ac1/samples/dnn/dasiamrpn_tracker.cpp#L5-L7)
7
+ - Visit https://github.com/foolwood/DaSiamRPN for training details.
8
+
9
+ ## Demo
10
+
11
+ Run the following command to try the demo:
12
+ ```shell
13
+ # track on camera input
14
+ python demo.py
15
+ # track on video input
16
+ python demo.py --input /path/to/video
17
+ ```
18
+
19
+ ## License
20
+
21
+ All files in this directory are licensed under [Apache 2.0 License](./LICENSE).
22
+
23
+ ## Reference:
24
+
25
+ - DaSiamRPN Official Repository: https://github.com/foolwood/DaSiamRPN
26
+ - Paper: https://arxiv.org/abs/1808.06048
27
+ - OpenCV API `TrackerDaSiamRPN` Doc: https://docs.opencv.org/4.x/de/d93/classcv_1_1TrackerDaSiamRPN.html
28
+ - OpenCV Sample: https://github.com/opencv/opencv/blob/4.x/samples/dnn/dasiamrpn_tracker.cpp
models/object_tracking_dasiamrpn/dasiamrpn.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
7
+ import numpy as np
8
+ import cv2 as cv
9
+
10
+ class DaSiamRPN:
11
+ def __init__(self, model_path, kernel_cls1_path, kernel_r1_path, backend_id=0, target_id=0):
12
+ self._model_path = model_path
13
+ self._kernel_cls1_path = kernel_cls1_path
14
+ self._kernel_r1_path = kernel_r1_path
15
+ self._backend_id = backend_id
16
+ self._target_id = target_id
17
+
18
+ self._param = cv.TrackerDaSiamRPN_Params()
19
+ self._param.model = self._model_path
20
+ self._param.kernel_cls1 = self._kernel_cls1_path
21
+ self._param.kernel_r1 = self._kernel_r1_path
22
+ self._param.backend = self._backend_id
23
+ self._param.target = self._target_id
24
+ self._model = cv.TrackerDaSiamRPN.create(self._param)
25
+
26
+ @property
27
+ def name(self):
28
+ return self.__class__.__name__
29
+
30
+ def setBackend(self, backend_id):
31
+ self._backend_id = backend_id
32
+ self._param = cv.TrackerDaSiamRPN_Params()
33
+ self._param.model = self._model_path
34
+ self._param.kernel_cls1 = self._kernel_cls1_path
35
+ self._param.kernel_r1 = self._kernel_r1_path
36
+ self._param.backend = self._backend_id
37
+ self._param.target = self._target_id
38
+ self._model = cv.TrackerDaSiamRPN.create(self._param)
39
+
40
+ def setTarget(self, target_id):
41
+ self._target_id = target_id
42
+ self._param = cv.TrackerDaSiamRPN_Params()
43
+ self._param.model = self._model_path
44
+ self._param.kernel_cls1 = self._kernel_cls1_path
45
+ self._param.kernel_r1 = self._kernel_r1_path
46
+ self._param.backend = self._backend_id
47
+ self._param.target = self._target_id
48
+ self._model = cv.TrackerDaSiamRPN.create(self._param)
49
+
50
+ def init(self, image, roi):
51
+ self._model.init(image, roi)
52
+
53
+ def infer(self, image):
54
+ isLocated, bbox = self._model.update(image)
55
+ score = self._model.getTrackingScore()
56
+ return isLocated, bbox, score
models/object_tracking_dasiamrpn/demo.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
7
+ import argparse
8
+
9
+ import numpy as np
10
+ import cv2 as cv
11
+
12
+ from dasiamrpn import DaSiamRPN
13
+
14
+ def 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
+
22
+ parser = argparse.ArgumentParser(
23
+ description="Distractor-aware Siamese Networks for Visual Object Tracking (https://arxiv.org/abs/1808.06048)")
24
+ parser.add_argument('--input', '-i', type=str, help='Path to the input video. Omit for using default camera.')
25
+ parser.add_argument('--model_path', type=str, default='object_tracking_dasiamrpn_model_2021nov.onnx', help='Path to dasiamrpn_model.onnx.')
26
+ parser.add_argument('--kernel_cls1_path', type=str, default='object_tracking_dasiamrpn_kernel_cls1_2021nov.onnx', help='Path to dasiamrpn_kernel_cls1.onnx.')
27
+ parser.add_argument('--kernel_r1_path', type=str, default='object_tracking_dasiamrpn_kernel_r1_2021nov.onnx', help='Path to dasiamrpn_kernel_r1.onnx.')
28
+ parser.add_argument('--save', '-s', type=str2bool, default=False, help='Set true to save results. This flag is invalid when using camera.')
29
+ parser.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.')
30
+ args = parser.parse_args()
31
+
32
+ def 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
+
52
+ if __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()