Send history of track to predictor

This commit is contained in:
Ruben van de Ven 2023-10-22 14:25:01 +02:00
parent b911caa5af
commit 3091557733

View file

@ -40,9 +40,21 @@ DETECTORS = [DETECTOR_RESNET, DETECTOR_YOLOv8]
@dataclass @dataclass
class Track: class Track:
"""A bit of an haphazardous wrapper around the 'real' tracker to provide
a history, with which the predictor can work, as we then can deduce velocity
and acceleration.
"""
track_id: str = None track_id: str = None
history: [Detection]= field(default_factory=lambda: []) history: [Detection]= field(default_factory=lambda: [])
def get_projected_history(self, H):
foot_coordinates = [d.get_foot_coords() for d in self.history]
if len(foot_coordinates):
return cv2.perspectiveTransform(np.array([foot_coordinates]),H)
return np.array([])
@dataclass @dataclass
class Detection: class Detection:
track_id: str track_id: str
@ -144,27 +156,27 @@ class Tracker:
# Store detections into tracklets # Store detections into tracklets
projected_coordinates = []
for detection in detections: for detection in detections:
track = self.tracks[detection.track_id] track = self.tracks[detection.track_id]
track.track_id = detection.track_id # for new tracks track.track_id = detection.track_id # for new tracks
track.history.append(detection) track.history.append(detection) # add to history
# projected_coordinates.append(track.get_projected_history(self.H)) # then get full history
# TODO: hadle occlusions, and dissappearance
# if len(track.history) > 30: # retain 90 tracks for 90 frames # if len(track.history) > 30: # retain 90 tracks for 90 frames
# track.history.pop(0) # track.history.pop(0)
foot_coordinates = np.array([[t.get_foot_coords()] for t in detections])
if len(foot_coordinates):
projected_coordinates = cv2.perspectiveTransform(foot_coordinates,self.H)
else:
projected_coordinates = []
# print(TEMP_proj_coords)
trajectories = {} trajectories = {}
for detection, coords in zip(detections, projected_coordinates): for detection in detections:
tid = str(detection.track_id) tid = str(detection.track_id)
track = self.tracks[detection.track_id]
coords = track.get_projected_history(self.H) # get full history
trajectories[tid] = { trajectories[tid] = {
"id": tid, "id": tid,
"history": [{"x":c[0], "y":c[1]} for c in coords] if not self.config.bypass_prediction else coords.tolist() # already doubles nested, fine for test "history": [{"x":c[0], "y":c[1]} for c in coords[0]] if not self.config.bypass_prediction else coords[0].tolist() # already doubles nested, fine for test
} }
# logger.info(f"{trajectories}") # logger.info(f"{trajectories}")
frame.trajectories = trajectories frame.trajectories = trajectories