forked from ZidanMusk/experimenting-with-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correlation_tracker.py
49 lines (39 loc) · 1.46 KB
/
correlation_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
"""
@author: Mahmoud I.Zidan
"""
from dlib import correlation_tracker, rectangle
'''Appearance Model'''
class CorrelationTracker:
count = 0
def __init__(self,bbox,img):
self.tracker = correlation_tracker()
self.tracker.start_track(img,rectangle(long(bbox[0]),long(bbox[1]),long(bbox[2]),long(bbox[3])))
self.confidence = 0. # measures how confident the tracker is! (a.k.a. correlation score)
self.time_since_update = 0
self.id = CorrelationTracker.count
CorrelationTracker.count += 1
self.hits = 0
self.hit_streak = 0
self.age = 0
def predict(self,img):
self.confidence = self.tracker.update(img)
self.age += 1
if (self.time_since_update > 0):
self.hit_streak = 0
self.time_since_update += 1
return self.get_state()
def update(self,bbox,img):
self.time_since_update = 0
self.hits += 1
self.hit_streak += 1
'''re-start the tracker with detected positions (it detector was active)'''
if bbox != []:
self.tracker.start_track(img, rectangle(long(bbox[0]), long(bbox[1]), long(bbox[2]), long(bbox[3])))
'''
Note: another approach is to re-start the tracker only when the correlation score fall below some threshold
i.e.: if bbox !=[] and self.confidence < 10.
but this will reduce the algo. ability to track objects through longer periods of occlusions.
'''
def get_state(self):
pos = self.tracker.get_position()
return [pos.left(), pos.top(),pos.right(),pos.bottom()]