abewley / sort

Simple, online, and realtime tracking of multiple objects in a video sequence.
GNU General Public License v3.0
3.82k stars 1.07k forks source link

How to return KF predicted bboxes w/o detection #126

Open ncrasta opened 3 years ago

ncrasta commented 3 years ago

For the visualization purpose, is it possible to return the predicted bounding boxes when the track is "alive" but lost the detection temporarily?

MaloM-CVision commented 2 years ago

in Sort.py : ` def update(self, dets): """ Params: dets - a numpy array of detections in the format [[x1,y1,x2,y2,score],[x1,y1,x2,y2,score],...] Requires: this method must be called once for each frame even with empty detections (use np.empty((0, 5)) for frames without detections). Returns the a similar array, where the last column is the object ID.

    NOTE: The number of objects returned may differ from the number of detections provided.
    """
    self.frame_count += 1
    # get predicted locations from existing trackers.
    trks = np.zeros((len(self.trackers), 5))
    to_del = []
    ret = []
    unmatched = []
    for t, trk in enumerate(trks):
        pos = self.trackers[t].predict()[0]
        trk[:] = [pos[0], pos[1], pos[2], pos[3], 0]
        if np.any(np.isnan(pos)):
            to_del.append(t)
    trks = np.ma.compress_rows(np.ma.masked_invalid(trks))
    for t in reversed(to_del):
        self.trackers.pop(t)
    matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(dets, trks, self.iou_threshold)

    # update matched trackers with assigned detections
    for m in matched:
        self.trackers[m[1]].update(dets[m[0], :])

    # create and initialise new trackers for unmatched detections
    for i in unmatched_dets:
        trk = KalmanBoxTracker(dets[i, :])
        self.trackers.append(trk)
    i = len(self.trackers)
    for trk in reversed(self.trackers):
        d = trk.get_state()[0]
        i -= 1
        if (trk.time_since_update < 1) and (trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits):
            ret.append(np.concatenate((d, [trk.id + 1])).reshape(1, -1))  # +1 as MOT benchmark requires positive

        # remove dead tracklet
        elif (trk.time_since_update > self.max_age):
            self.trackers.pop(i)
        elif (trk.time_since_update > 0):
            unmatched.append(np.concatenate((d, [trk.id + 1])).reshape(1, -1))

    return1 = np.concatenate(ret) if len(ret) > 0 else np.empty((0, 5))
    return2 = np.concatenate(unmatched) if len(unmatched) > 0 else np.empty((0, 5))

    return return1, return2

`