def update(self, dets, img=None):
        """
    Params:
      dets - a numpy array of detections in the format [[x,y,w,h,score],[x,y,w,h,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):
            pos = self.trackers[t].predict(img)  #for kal!
            #print(pos)
            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)
        if dets != []:
            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],
                               img)  ## for dlib re-intialize the trackers ?!

            #create and initialise new trackers for unmatched detections
            for i in unmatched_dets:
                if not self.use_dlib:
                    trk = KalmanBoxTracker(dets[i, :])
                else:
                    # print(dets[i,:])
                    trk = CorrelationTracker(dets[i, :], img)
                self.trackers.append(trk)

        i = len(self.trackers)
        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([], img)
            d = trk.get_state()
            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))
Ejemplo n.º 2
0
    def update(self, dets, img=None):
        """
    Params:
      dets - a numpy array of detections in the format [[x,y,w,h,score],[x,y,w,h,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
        trks = np.zeros((len(self.trackers), 5))
        """
    trks   : array of trackers' position with the score of each tracker
    to_del : array that contain indices of trackers need to be deleted as they are invalid 
    ret    : array of returned trackers [pos,id]
    colors : list of colors of the bounding boxes
    """
        to_del = []
        ret = []
        colors = []

        #get predicted locations from existing trackers.
        for t, trk in enumerate(trks):
            pos = self.trackers[t].predict(img)
            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)

        #Compare detections to trackers and fil the matched, unmatched_dets, unmatched_trks lists

        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],
                           img)  ## for dlib re-intialize the trackers ?!

        #create and initialise new trackers for unmatched detections
        for i in unmatched_dets:
            trk = CorrelationTracker(dets[i, :], img)
            self.trackers.append(trk)

        i = len(self.trackers)
        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([], img)
            d = trk.get_state()
            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))
                colors.append(trk.color)
            i -= 1
            #remove dead tracklet
            if (trk.time_since_update > self.max_age):
                self.trackers.pop(i)
            else:
                ret.append(np.concatenate((d, [trk.id + 1])).reshape(1, -1))
                colors.append(trk.color)

        if (len(ret) > 0):
            return np.concatenate(ret), colors
        return np.empty((0, 5)), []
Ejemplo n.º 3
0
    def update(self,
               dets,
               img_size,
               root_dic,
               addtional_attribute_list,
               img=None):
        """
        Params:
          dets - a numpy array of detections in the format [[x,y,w,h,score],[x,y,w,h,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:as in practical realtime MOT, the detector doesn't run on every single frame
        """
        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):
            pos = self.trackers[t].predict(img)  # for kal!
            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)
        if dets != []:
            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],
                               img)  ## for dlib re-intialize the trackers ?!
                    #print('update dets[d, :]: ' + str(dets[d, :][0]))
                    #logger.info('update trk: {0}'.format(trk.id + 1) + str(dets[d, :][0]))
                    trk.face_addtional_attribute.append(
                        addtional_attribute_list[d[0]])

            # create and initialise new trackers for unmatched detections
            for i in unmatched_dets:
                if not self.use_dlib:
                    #print('dets[i, :] : ' + str(i) + str(dets[i, :]) + '  ' + str(dets[i, :][0]))
                    trk = KalmanBoxTracker(dets[i, :])
                    trk.face_addtional_attribute.append(
                        addtional_attribute_list[i])
                    logger.info("new Tracker: {0}".format(trk.id + 1))
                else:
                    trk = CorrelationTracker(dets[i, :], img)
                self.trackers.append(trk)

        i = len(self.trackers)
        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([], img)
            d = trk.get_state()
            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
            #ret.append(np.concatenate((d, [trk.id + 1])).reshape(1, -1))
            i -= 1
            # remove dead tracklet
            #if (trk.time_since_update >= self.max_age or d[0] < 0 or d[1] < 0 or d[2] > img_size[1] or d[3] > img_size[0]):
            if (trk.time_since_update >= self.max_age or d[2] < 0 or d[3] < 0
                    or d[0] > img_size[1] or d[1] > img_size[0]):
                if (len(trk.face_addtional_attribute) >= 15):
                    #utils.save_to_file(root_dic, trk)
                    utils._save_to_file(root_dic, trk)
                logger.info('remove tracker: {0}'.format(trk.id + 1) +
                            '  time_since_update:' +
                            str(trk.time_since_update) + str(d))
                self.trackers.pop(i)
        if (len(ret) > 0):
            return np.concatenate(ret)
        return np.empty((0, 5))
Ejemplo n.º 4
0
font = cv2.FONT_HERSHEY_PLAIN

# create sequence object
sequence = VOTSequence(dataset_path, sequence)
init_frame = 0
n_failures = 0
# create parameters and tracker objects
# parameters = NCCParams()
# tracker = NCCTracker(parameters)

# Meanshift
parameters = MSParams()
# tracker = MeanShiftTracker(parameters)

# Correlation
tracker = CorrelationTracker(parameters)

time_all = 0

# initialize visualization window
sequence.initialize_window(win_name)
# tracking loop - goes over all frames in the video sequence
frame_idx = 0
while frame_idx < sequence.length():
    img = cv2.imread(sequence.frame(frame_idx))
    # initialize or track
    if frame_idx == init_frame:
        # initialize tracker (at the beginning of the sequence or after tracking failure)
        t_ = time.time()
        tracker.initialize(
            img, sequence.get_annotation(frame_idx, type='rectangle'))
Ejemplo n.º 5
0
 def add(self, dets, img, cowids):
     for i, det in enumerate(dets):
         trk = CorrelationTracker(det, img, cowids[i])
         self.trackers.append(trk)
Ejemplo n.º 6
0
 def begin(self, dets, img, cowids):
     self.trackers = []
     #   print(dets)
     for i, det in enumerate(dets):
         trk = CorrelationTracker(det, img, cowids[i])
         self.trackers.append(trk)