-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.py
67 lines (48 loc) · 2.19 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
from lib import *
from deep_sort.deep_sort import nn_matching
from deep_sort.deep_sort.detection import Detection
from deep_sort.deep_sort.tracker import Tracker
from deep_sort.tools import generate_detections as gdet
from config_app.config import get_config
config_app = get_config()
model_path = config_app['DEEP_SORT']['MODEL_PATH']
max_cosine_distance = config_app['DEEP_SORT']['MAX_COSINE_DISTANCE']
classes = config_app['DEEP_SORT']['CLASSES']
nn_budget = None
class tracker:
def __init__(self):
# trích xuất đặc trưng từ bbox
self.encoder = gdet.create_box_encoder(model_filename=model_path, batch_size=1)
# độ đo
self.metric = nn_matching.NearestNeighborDistanceMetric(
'cosine', matching_threshold=max_cosine_distance,
budget=nn_budget
)
# quản lí các track, thực hiện các bước liên kết và cập nhật
self.tracker = Tracker(self.metric)
key_list = []
val_list = []
for ID, class_name in enumerate(classes):
key_list.append(ID)
val_list.append(class_name)
self.key_list = key_list
self.val_list = val_list
def tracking(self, origin_frame, bboxes, scores, class_ids):
features = self.encoder(origin_frame, bboxes)
detections = [Detection(bbox, score, class_id, feature)
for bbox, score, class_id, feature in zip(bboxes, scores, class_ids, features)]
self.tracker.predict() # dự đoán vị trí track
self.tracker.update(detections=detections) # phát hiện các track hoặc tạo các track mới
tracked_bboxes = []
for track in self.tracker.tracks:
if not track.is_confirmed() or track.time_since_update > 5:
continue
bbox = track.to_tlbr() # top left bottom right
class_id = track.get_class()
conf_score = track.get_conf_score()
tracking_id = track.track_id
tracked_bboxes.append(
bbox.tolist() + [class_id, conf_score, tracking_id]
)
tracked_bboxes = np.array(tracked_bboxes)
return tracked_bboxes