[Python] SORT tracking algorithm not working when no objects are detected

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement the sort algorithm on my own custom object detector, but i'm having some issues regarding to the case of empty detection (since initial frames don't have objects yet)

    I've cloned the original sort repository from https://github.com/abewley/sort

    This is the code raising the error (from sort.py):

    class Sort(object):
    def __init__(self,max_age=1,min_hits=3):
    """
    Sets key parameters for SORT
    """
    self.max_age = max_age
    self.min_hits = min_hits
    self.trackers = []
    self.frame_count = 0

    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.
    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 = []
    for t,trk in enumerate(trks):
    #print("[INFO] trk before:", trk)
    pos = self.trackers[t].predict()[0]
    trk[:] = [pos[0], pos[1], pos[2], pos[3], 0]
    #print("[INFO] trk after: ", trk)
    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)

    #update matched trackers with assigned detections
    for t,trk in enumerate(self.trackers):
    if(t not in unmatched_trks):
    d = matched[np.where(matched[:,1]==t)[0],0]
    trk.update(dets[d,:][0])

    #create and initialise new trackers for unmatched detections
    for i in unmatched_dets:
    print("[INFO] Detection ", i, " from tracker initialization: ", dets)
    #if len(dets) > 0:
    trk = KalmanBoxTracker(dets[i,:])
    self.trackers.append(trk)
    i = len(self.trackers)
    for trk in reversed(self.trackers):
    d = trk.get_state()[0]
    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
    i -= 1
    #remove dead tracklet
    if(trk.time_since_update > self.max_age):
    self.trackers.pop(i)
    if(len(ret)>0):
    return np.concatenate(ret)
    return np.empty((0,5))


    when instantiating a new KalmanBoxTracker from this code:

    def convert_bbox_to_z(bbox):
    """
    Takes a bounding box in the form [x1,y1,x2,y2] and returns z in the form
    [x,y,s,r] where x,y is the centre of the box and s is the scale/area and r is
    the aspect ratio
    """
    w = bbox[2]-bbox[0]
    h = bbox[3]-bbox[1]
    x = bbox[0]+w/2.
    y = bbox[1]+h/2.
    s = w*h #scale is just area
    r = w/float(h)
    return np.array([x,y,s,r]).reshape((4,1))

    def convert_x_to_bbox(x,score=None):
    """
    Takes a bounding box in the centre form [x,y,s,r] and returns it in the form
    [x1,y1,x2,y2] where x1,y1 is the top left and x2,y2 is the bottom right
    """
    w = np.sqrt(x[2]*x[3])
    h = x[2]/w
    if(score==None):
    return np.array([x[0]-w/2.,x[1]-h/2.,x[0]+w/2.,x[1]+h/2.]).reshape((1,4))
    else:
    return np.array([x[0]-w/2.,x[1]-h/2.,x[0]+w/2.,x[1]+h/2.,score]).reshape((1,5))


    What are the possible errors or how to overcome this, i've been trying to not making updates if there is no detection but by doing that I can't get an estimate of the new position.

    Also the comments indicates that update must be called even with no detections.

    Any help would be apreciated, Regards.

    Continue reading...

Compartilhe esta Página