-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.py
221 lines (160 loc) · 5.86 KB
/
tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import inspect
from abc import ABC, abstractmethod
from typing import Callable, List, Tuple
import cv2
import numpy
from shapely import Polygon
from utils.tracker_test import test_tracker
from utils.utils import polygon_to_tuple
class Tracker(ABC):
"""
Wrapper for tracker that provides unified interface for later use in tracker_test.py
"""
@property
@abstractmethod
def name(self) -> str:
pass
@abstractmethod
def init(self, ground_truth_polygon: Polygon, frame: numpy.ndarray):
"""
Should be used to initialize tracker with first frame.
:param ground_truth_polygon: ground truth position of object in frame to track
:param frame: frame of video with object to track
"""
pass
@abstractmethod
def eval(self, frame: numpy.ndarray) -> (bool, list[float]):
"""
Supplies a frame with an object to track.
:param frame:
:return:
"""
pass
def test(
self,
dataset_dir: str,
show_tracking=False,
iou_threshold_for_correction=.0,
listener: Callable[[int, int], None] = None,
frame_listener: Callable[[numpy.ndarray], None] = None
) -> tuple[str, list[int], list[Polygon|None]]:
"""
Method used to perform test of a tracker. listener and frame_listener are used in streamlit to nicely show
tracking progress. Returned value consists of date string, list of times in milliseconds tracker needed to
finish its work, list of trajectories which are polygons defined by Polygon from shapely.
:param dataset_dir: path to directory with dataset
:param show_tracking: shows window with current state of tracker
:param iou_threshold_for_correction: threshold when tracker should be reinitialized
:param listener: used for progress bar - returns current frame number and number of all frames
:param frame_listener: returns current frame with tracker bounding box, ground truth box, IoU and few other informations
:return: date when test was performed, time for detection, trajectories returned by tracker
"""
return test_tracker(
self,
dataset_dir,
show_tracking,
iou_threshold_for_correction,
listener,
frame_listener
)
class TrackerCV2(Tracker, ABC):
_tracker: cv2.Tracker | None = None
@property
@abstractmethod
def tracker(self) -> cv2.Tracker:
"""
Convenience method for OpenCV tracker object. Should only return object.
:return: cv2 Tracker object
"""
pass
def init(self, ground_truth_polygon: Polygon, frame: numpy.ndarray):
self._tracker = self.tracker
_minimum_bounding_rectangle = polygon_to_tuple(ground_truth_polygon)
if _minimum_bounding_rectangle is not None:
# Initialize tracker with first frame and bounding box
self._tracker.init(frame, _minimum_bounding_rectangle)
def eval(self, frame: numpy.ndarray) -> (bool, list[float]):
if self._tracker is None:
raise Exception("Execute init() first")
return self._tracker.update(frame)
class TrackerMedianFlow(TrackerCV2):
@property
def name(self) -> str:
return "MedianFlow"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerMedianFlow().create()
class TrackerBoosting(TrackerCV2):
@property
def name(self) -> str:
return "Boosting"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerBoosting().create()
class TrackerMIL(TrackerCV2):
@property
def name(self) -> str:
return "MIL"
@property
def tracker(self) -> cv2.Tracker:
return cv2.TrackerMIL().create()
class TrackerKCF(TrackerCV2):
@property
def name(self) -> str:
return "KCF"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerKCF().create()
class TrackerTLD(TrackerCV2):
@property
def name(self) -> str:
return "TLD"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerTLD().create()
class TrackerGOTURN(TrackerCV2):
@property
def name(self) -> str:
return "GOTURN"
@property
def tracker(self) -> cv2.Tracker:
return cv2.TrackerGOTURN().create()
class TrackerMOSSE(TrackerCV2):
@property
def name(self) -> str:
return "MOSSE"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerMOSSE().create()
class TrackerCSRT(TrackerCV2):
@property
def name(self) -> str:
return "CSRT"
@property
def tracker(self) -> cv2.Tracker:
return cv2.legacy.TrackerCSRT().create()
class DummyTracker(Tracker):
"""
Tracker always returning first bounding box it received. Useful for making sure everything works.
"""
_ground_truth: Tuple[int, int, int, int] | None = None
@property
def name(self) -> str:
return "Dummy"
def init(self, ground_truth_polygon: Polygon, frame: numpy.ndarray):
_minimum_bounding_rectangle = polygon_to_tuple(ground_truth_polygon)
if _minimum_bounding_rectangle is not None:
# Initialize tracker with first frame and bounding box
self._ground_truth = _minimum_bounding_rectangle
def eval(self, frame: numpy.ndarray) -> (bool, List[float]):
if self._ground_truth is None:
raise Exception("Execute init() first")
return True, self._ground_truth
def get_concrete_classes(cls):
for subclass in cls.__subclasses__():
yield from get_concrete_classes(subclass)
if not inspect.isabstract(subclass):
yield subclass
if __name__ == '__main__':
for _tracker in get_concrete_classes(Tracker):
tracker = _tracker()