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))
Exemplo n.º 2
0
    def update(self, dets, img_size, root_dic, addtional_attribute_list, predict_num):
        """
        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()  # kalman predict ,very fast ,<1ms
            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])
                    trk.face_addtional_attribute.append(addtional_attribute_list[d[0]])

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

        i = len(self.trackers)
        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([])
            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 or trk.predict_num >= predict_num 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) >= 5:
                    utils.save_to_file(root_dic, trk)
                logger.info('remove tracker: {0}'.format(trk.id + 1))
                self.trackers.pop(i)
        if len(ret) > 0:
            return np.concatenate(ret)
        return np.empty((0, 5))
  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 = []
    colors=[]
    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)

    #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:
      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 )):
          ret.append(np.concatenate((d,[trk.id+1])).reshape(1,-1)) 
          colors.append(trk.color)
        
        #remove dead tracklet
        elif(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)),[]
Exemplo n.º 4
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)), []
Exemplo n.º 5
0
    def run(self, dets, img, frame_width, frame_height, timestamp=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.
    """

        detections = np.empty((0, 6))

        for obj, score, bounds in dets:
            x, y, w, h = bounds

            if obj.decode('utf-8') == 'person':
                name = 0
            else:
                name = 1

            detections = np.vstack(
                (detections, (int(x - w / 2), int(y - h / 2), int(x + w / 2),
                              int(y + h / 2), score, name)))

        dets = detections

        self.timestamp = timestamp
        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):
            if len(self.trackers) > 0:
                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]

                    features = fm.calculatePersonHistograms(
                        dets[d, :][0][0], dets[d, :][0][1], dets[d, :][0][2],
                        dets[d, :][0][3], img)

                    trk.update(dets[d, :][0], features,
                               img)  ## for dlib re-intialize the trackers ?!

            #create and initialise new trackers for unmatched detections
            for i in unmatched_dets:
                features = fm.calculatePersonHistograms(
                    dets[i][0], dets[i][1], dets[i][2], dets[i][3], img)
                self.trackers.append(
                    KalmanBoxTracker(dets[i, :], features, dets[i, 5]))

        to_be_deleted = []
        for area in self.areas:
            if area.closed:
                area.inside = 0
            for idx, trk in enumerate(reversed(self.trackers)):
                if dets == []:
                    trk.update([], img)
                d = trk.get_state()

                ret.append(np.concatenate((d, [trk.id + 1])).reshape(
                    1, -1))  # +1 as MOT benchmark requires positive
                bottom_mid_x, bottom_mid_y = self.get_bottom_mid_point(
                    ret[-1][-1][0], ret[-1][-1][2], ret[-1][-1][3])
                trk.location_history.append(Point(bottom_mid_x, bottom_mid_y))

                if trk.history_limit >= self.keep_history:
                    if len(self.trackers) > 0:
                        trk.location_history.pop(0)
                else:
                    trk.history_limit += 1

                if area.closed == True:
                    if len(trk.location_history) > 1:
                        self.change_area_count(trk, area)
                else:
                    self.is_inside(trk, area)

                #remove dead tracklet
                if (trk.time_since_update > self.max_age):
                    pos = trk.predict(img)
                    features = fm.calculatePersonHistograms(
                        pos[0], pos[1], pos[2], pos[3], img)

                    closeness = fm.calculateCloseness(trk.last_features,
                                                      features)

                    # if distance between last time we saw this object and current estimation is more than 25. Kill it.
                    if closeness > 0.25:
                        if len(self.trackers) > 0:
                            to_be_deleted.append(idx)

            print('Name =', area.name, 'Inside =', area.inside, 'Outside =',
                  area.outside, 'Counters =', area.counters)

        to_be_deleted = self.unique(to_be_deleted)

        self.delete_objs(to_be_deleted)

        if (len(ret) > 0):
            return self.areas, self.trackers
        return self.areas, self.trackers
Exemplo n.º 6
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.
        """
        #get predicted locations from existing trackers.
        trks = np.zeros((len(self.trackers), 5))
        for t, trk in enumerate(trks):
            pos = self.trackers[t].predict(img)
            trk[:] = [pos[0], pos[1], pos[2], pos[3], 0]
        trks = np.ma.compress_rows(np.ma.masked_invalid(trks))
        if dets != []:
            dets_inflated = np.copy(dets)
            for i in range(dets_inflated.shape[0]):
                r1 = dets_inflated[i, :]
                r1 = (r1[0], r1[1], r1[2] - r1[0], r1[3] - r1[1])
                r1 = model.rect_inflate2(r1, r1[2] * self.inflate_ratio,
                                         r1[3] * self.inflate_ratio)
                dets_inflated[i][0] = r1[0]
                dets_inflated[i][1] = r1[1]
                dets_inflated[i][2] = r1[2] + r1[0]
                dets_inflated[i][3] = r1[3] + r1[1]
            matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(
                dets_inflated, 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)

            #create and initialize new trackers for unmatched detections
            for i in unmatched_dets:
                skip = False
                for tr in self.trackers:
                    r1 = tr.get_state()
                    r2 = dets[i, :]
                    r1 = (r1[0], r1[1], r1[2] - r1[0], r1[3] - r1[1])
                    r2 = (r2[0], r2[1], r2[2] - r2[0], r2[3] - r2[1])
                    isect = model.rect_intersection(r1, r2)
                    if isect:
                        area_factor = (isect[2] * isect[3]) / min(
                            r1[2] * r1[3], r2[2] * r2[3])
                        if area_factor > self.mix_threshold:
                            tr.time_since_update = 0
                            skip = True
                            break
                if skip:
                    continue
                new_id = None
                for tr in self.trackers:
                    r2 = dets[i, :]
                    r2 = (r2[0], r2[1], r2[2] - r2[0], r2[3] - r2[1])
                    if tr.mixed_ids:
                        r3 = tr.get_state()
                        r3 = (r3[0], r3[1], r3[2] - r3[0], r3[3] - r3[1])
                        r3 = model.rect_inflate2(r3, max(r3[2], r3[3]),
                                                 max(r3[2], r3[3]))
                        isect = model.rect_intersection(r2, r3)
                        if isect:
                            new_id = tr.mixed_ids[0]
                            del tr.mixed_ids[0]
                            break
                trk = KCFTracker(dets[i, :], img, new_id, self.use_4patch,
                                 self.stop_on_4patch_break)
                self.trackers.append(trk)

        i = len(self.trackers)
        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([], img)
            i -= 1
            #remove dead tracklet
            if (trk.time_since_update > self.max_age):
                self.trackers.pop(i)

        i = len(self.trackers)
        for trk1 in reversed(self.trackers):
            i -= 1
            if trk1.life < self.mix_life_threshold:
                continue
            r1 = trk1.get_state()
            r1 = (r1[0], r1[1], r1[2] - r1[0], r1[3] - r1[1])
            for trk2 in self.trackers:
                if trk1 == trk2:
                    continue
                if trk2.life < self.mix_life_threshold:
                    continue
                r2 = trk2.get_state()
                r2 = (r2[0], r2[1], r2[2] - r2[0], r2[3] - r2[1])
                isect = model.rect_intersection(r1, r2)
                if isect:
                    area_factor = (isect[2] * isect[3]) / min(
                        r1[2] * r1[3], r2[2] * r2[3])
                    if area_factor > self.mix_threshold:
                        trk2.mixed_ids += trk1.mixed_ids
                        trk2.mixed_ids.append(trk1.id)
                        self.trackers.pop(i)
                        break
        ret = []
        for trk in reversed(self.trackers):
            d = trk.get_state()
            ret.append(list(d) + [trk.id, trk.points, trk.mixed_ids])

        return ret
Exemplo n.º 7
0
    def update(self, dets, obj_classes):
        """
        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()
            # print(pos)
            trk[:] = [pos[0], pos[1], pos[2], pos[3], 0]
            if np.any(np.isnan(pos)):
                print('isnan')
                to_del.append(t)
        trks = np.ma.compress_rows(np.ma.masked_invalid(trks))
        for t in reversed(to_del):
            self.trackers.pop(t)
            print(t)
        if dets != []:
            matched, unmatched_dets, unmatched_trks = associate_detections_to_trackers(
                dets, trks, iou_threshold=0.25)

            # 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])
                    trk.tracklet_class = int(obj_classes[d])

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

        # update the variables used for MR tasks
        for trk in reversed(self.trackers):
            trk.statistics_update()

        i = len(self.trackers)

        for trk in reversed(self.trackers):
            if dets == []:
                trk.update([])

            if trk.hit_streak >= self.min_hits or self.frame_count <= self.min_hits:
                ret.append(trk)
            i -= 1
            # remove dead tracklet and counted dead tracklet
            if (trk.time_since_update >
                    self.max_age) or (trk.count_flag and trk.time_since_counted
                                      > self.time_since_counted_threshold):
                self.trajectory_database.append(trk.trajectory)
                self.trackers.pop(i)

        if len(ret) > 0:
            return True, ret
        else:
            return False, ret