1

我正在尝试在我自己的自定义对象检测器上实现排序算法,但我遇到了一些关于空检测情况的问题(因为初始帧还没有对象)

我已经从https://github.com/abewley/sort克隆了原始排序存储库

这是引发错误的代码(来自 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[i])
        #if len(dets[i]) > 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))

从这段代码实例化一个新的 KalmanBoxTracker 时:

    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))

可能的错误是什么或如何克服这一点,如果没有检测到,我一直在尝试不进行更新,但这样做我无法估计新位置。

此外,评论表明即使没有检测到也必须调用更新。

任何帮助将不胜感激,问候。

4

1 回答 1

1

我不知道这是否有帮助。解决方法之一是在调用跟踪器的地方使用它。但track ID会不断更新。创建一个单独的 Track ID 函数以获得平滑的轨道变化。

       if box is not None:
            x, y, w, h = box
            detections = np.array([[x,y,w,h,score]])
            trackers = tracker.update(detections,infer_image)
            try:
                if len(trackers[0]>0):
                    print(trackers[0][4])
            except:
                print("nothing to track")

        else:
            detections = np.array([[0,0,0,0,0]])
            trackers = tracker.update(detections,infer_image)

同样,这只是一种解决方法。为我必须做的事情工作得很好。

更新:替代解决方案: https ://github.com/abewley/sort/issues/73#issuecomment-627327850

于 2020-07-11T09:39:55.043 回答