Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# less than "track_max_gap" away are connected
graph = nx.Graph()
for i in range(len(tracks)):
graph.add_node(i)
for i, j in itertools.combinations(range(len(tracks)), 2):
# only try to match tracks with a short gap between them
ti = tracks[i][-1][0]
tj = tracks[j][0][0]
if (tj < ti) or (tj - ti > self.track_max_gap):
continue
# match tracks whose last and first position match
rectangle1 = dlib.drectangle(*tracks[i][-1][1])
rectangle2 = dlib.drectangle(*tracks[j][0][1])
if self._match(rectangle1, rectangle2):
graph.add_edge(i, j)
# merge tracks that are in the same connected component
merged_tracks = []
for group in nx.connected_components(graph):
track = [item for t in sorted(group) for item in tracks[t]]
merged_tracks.append(track)
return merged_tracks
# build graph where nodes are tracks and where matching tracks
# less than "track_max_gap" away are connected
graph = nx.Graph()
for i in range(len(tracks)):
graph.add_node(i)
for i, j in itertools.combinations(range(len(tracks)), 2):
# only try to match tracks with a short gap between them
ti = tracks[i][-1][0]
tj = tracks[j][0][0]
if (tj < ti) or (tj - ti > self.track_max_gap):
continue
# match tracks whose last and first position match
rectangle1 = dlib.drectangle(*tracks[i][-1][1])
rectangle2 = dlib.drectangle(*tracks[j][0][1])
if self._match(rectangle1, rectangle2):
graph.add_edge(i, j)
# merge tracks that are in the same connected component
merged_tracks = []
for group in nx.connected_components(graph):
track = [item for t in sorted(group) for item in tracks[t]]
merged_tracks.append(track)
return merged_tracks
def getFaceGenerator(tracking, frame_width, frame_height, double=True):
"""Parse precomputed face file and generate timestamped faces"""
# load tracking file and sort it by timestamp
names = ['t', 'track', 'left', 'top', 'right', 'bottom', 'status']
dtype = {'left': np.float32, 'top': np.float32,
'right': np.float32, 'bottom': np.float32}
tracking = read_table(tracking, delim_whitespace=True, header=None,
names=names, dtype=dtype)
tracking = tracking.sort_values('t')
# t is the time sent by the frame generator
t = yield
rectangle = dlib.drectangle if double else dlib.rectangle
faces = []
currentT = None
for _, (T, identifier, left, top, right, bottom, status) in tracking.iterrows():
left = int(left * frame_width)
right = int(right * frame_width)
top = int(top * frame_height)
bottom = int(bottom * frame_height)
face = rectangle(left, top, right, bottom)
# load all faces from current frame and only those faces
if T == currentT or currentT is None:
faces.append((identifier, face, status))
position.bottom()
)
current = (t, position, direction)
self._tracking_graph.add_edge(
self._previous[identifier], current,
confidence=self._confidences[identifier])
# save current position of the tracker for next iteration
self._previous[identifier] = current
# start new trackers for all detections
for d, detection in enumerate(detections):
# start new tracker
new_tracker = dlib.correlation_tracker()
new_tracker.start_track(frame, dlib.drectangle(*detection))
self._trackers[new_identifier] = new_tracker
# save previous (t, position, status) tuple
current = (t, detection, DETECTION)
self._previous[new_identifier] = current
# increment tracker identifier
new_identifier = new_identifier + 1
def _merge_trajs(traj_1, traj_2):
try:
assert traj_1.pend > traj_2.pstart and traj_1.pstart < traj_2.pend
except AssertionError:
print('{}-{} {}-{}'.format(traj_1.pstart, traj_1.pend, traj_2.pstart, traj_2.pend))
overlap_length = max(traj_1.pend - traj_2.pstart, 0)
for i in range(overlap_length):
roi_1 = traj_1.rois[traj_1.length() - overlap_length + i]
roi_2 = traj_2.rois[i]
left = (roi_1.left() + roi_2.left()) / 2
top = (roi_1.top() + roi_2.top()) / 2
right = (roi_1.right() + roi_2.right()) / 2
bottom = (roi_1.bottom() + roi_2.bottom()) / 2
traj_1.rois[traj_1.length() - overlap_length + i] = drectangle(left, top, right, bottom)
for i in range(overlap_length, traj_2.length()):
traj_1.predict(traj_2.rois[i])
return traj_1
def _fix(self, track):
"""Fix track by merging matching forward/backward tracklets"""
fixed_track = []
for t, group in itertools.groupby(sorted(track), key=lambda x: x[0]):
group = list(group)
# make sure all positions are overlap enough
error = False
for (_, pos1, _), (_, pos2, _) in itertools.combinations(group, 2):
rectangle1 = dlib.drectangle(*pos1)
rectangle2 = dlib.drectangle(*pos2)
if self._match(rectangle1, rectangle2) == 0:
error = True
break
# status
status = "+".join(
sorted((status for _, _, status in group),
key=lambda s: {DETECTION: 2,
FORWARD: 1,
BACKWARD: 3}[s]))
if error:
status = "error({0})".format(status)
# average position
pos = tuple(int(round(v))
def _fix(self, track):
"""Fix track by merging matching forward/backward tracklets"""
fixed_track = []
for t, group in itertools.groupby(sorted(track), key=lambda x: x[0]):
group = list(group)
# make sure all positions are overlap enough
error = False
for (_, pos1, _), (_, pos2, _) in itertools.combinations(group, 2):
rectangle1 = dlib.drectangle(*pos1)
rectangle2 = dlib.drectangle(*pos2)
if self._match(rectangle1, rectangle2) == 0:
error = True
break
# status
status = "+".join(
sorted((status for _, _, status in group),
key=lambda s: {DETECTION: 2,
FORWARD: 1,
BACKWARD: 3}[s]))
if error:
status = "error({0})".format(status)
# average position
"""
n_trackers, n_detections = len(trackers), len(detections)
if n_trackers < 1 or n_detections < 1:
return dict()
n = max(n_trackers, n_detections)
overlap_area = np.zeros((n, n))
# list of (identifier, tracker) tuple
trackers_ = list(trackers.items())
for t, (identifier, tracker) in enumerate(trackers_):
position = tracker.get_position()
for d, detection in enumerate(detections):
rectangle = dlib.drectangle(*detection)
overlap_area[t, d] = self._match(position, rectangle)
# find the best one-to-one mapping
match = {}
mapping = self._hungarian.compute(np.max(overlap_area) - overlap_area)
for t, d in mapping:
if t >= n_trackers or d >= n_detections:
continue
if overlap_area[t, d] > 0.:
identifier, _ = trackers_[t]
match[d] = identifier
return match