-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_tracker.py
84 lines (60 loc) · 2.85 KB
/
object_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
import numpy as np
from scipy.spatial import distance as dist
from collections import OrderedDict
class ObjectTracker():
def __init__(self, max_frames_disappeared=50):
self.next_object_id = 0
self.objects = OrderedDict()
self.disappeared = OrderedDict()
self.max_frames_disappeared = max_frames_disappeared
def register(self, centroid):
self.objects[self.next_object_id] = centroid
self.disappeared[self.next_object_id] = 0
self.next_object_id += 1
def unregister(self, object_id):
del self.objects[object_id]
del self.disappeared[object_id]
def update(self, rects):
if len(rects) == 0:
for object_id in list(self.disappeared.keys()):
self.disappeared[object_id] += 1
if self.disappeared[object_id] > self.max_frames_disappeared:
self.unregister(object_id)
return self.objects
else:
input_centroids = np.zeros((len(rects), 2), dtype="int")
for (i, (x1, y1, x2, y2)) in enumerate(rects):
cx = int((x1 + x2) / 2.0)
cy = int((y1 + y2) / 2.0)
input_centroids[i] = (cx, cy)
if len(self.objects) == 0:
for i in range(0, len(input_centroids)):
self.register(input_centroids[i])
else:
object_ids = list(self.objects.keys())
object_centroids = list(self.objects.values())
distance = dist.cdist(np.array(object_centroids), input_centroids)
rows = distance.min(axis=1).argsort()
cols = distance.argmin(axis=1)[rows]
used_rows = set()
used_cols = set()
for (row, col) in zip(rows, cols):
if row in used_rows or col in used_cols:
continue
object_id = object_ids[row]
self.objects[object_id] = input_centroids[col]
self.disappeared[object_id] = 0
used_rows.add(row)
used_cols.add(col)
unused_rows = set(range(0, distance.shape[0])).difference(used_rows)
unused_cols = set(range(0, distance.shape[1])).difference(used_cols)
if distance.shape[0] >= distance.shape[1]:
for row in unused_rows:
object_id = object_ids[row]
self.disappeared[object_id] += 1
if self.disappeared[object_id] > self.max_frames_disappeared:
self.unregister(object_id)
else:
for col in unused_cols:
self.register(input_centroids[col])
return self.objects