Example #1
0
 def __init__(self, class_id, names, setofpoint, multi, MOI, ROI, info,
              frame_delay):
     self.max_cosine_distance = 0.9
     self.nms_max_overlap = 0.5
     self.nn_budget = None
     self.model_filename = 'model_data/market1501.pb'
     self.detections = []
     self.id = class_id
     self.boxes_tracked = []
     self.color = (255, 255, 255)
     #self.encoder = gdet.create_box_encoder(self.model_filename,batch_size=1)
     self.class_names = names
     metric = nn_matching.NearestNeighborDistanceMetric(
         "cosine", self.max_cosine_distance, self.nn_budget)
     self.obtracker = Tracker(metric, id_cam=int(info[0]))
     self.pts = [deque(maxlen=1000) for _ in range(999999)]
     #self.point_first = [deque(maxlen=2) for _ in range(999999)]
     self.vis = visualization.Visualization(img_shape=(960, 1280, 3),
                                            update_ms=2000)
     COLORS = np.random.randint(100, 255, size=(255, 3), dtype="uint8")
     self.color = [int(c) for c in COLORS[self.id]]
     self.setofpoint = setofpoint
     self.multi = multi
     self.MOI = MOI
     self.ROI = ROI
     self.frame_track = 1
     self.info = info
     self.frame_delay = frame_delay
     self.begin_time = 0
     self.shape_img = None
Example #2
0
def run(img, det, viewer, min_confidence, nms_max_overlap, tracker, display):
    def frame_callback(vis, frame_idx):
        print("Processing frame {:05d}".format(int(frame_idx) + 1))

        detections = create_detections(det, frame_idx)
        detections = [d for d in detections if d.confidence >= min_confidence]

        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]
        tracker.predict()
        tracker.update(detections)

        if display:
            vis.set_image(img.copy())
            vis.draw_trackers(tracker.tracks)

    min_frame_idx = int(det[:, 0].min())
    max_frame_idx = int(det[:, 0].max())
    seq_info = {
        "image_size": img.shape[:2],
        "min_frame_idx": min_frame_idx,
        "max_frame_idx": max_frame_idx
    }
    if display:
        visualizer = visualization.Visualization(seq_info, viewer)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback, vedio_file='deep_sort1.avi')
Example #3
0
 def __init__(self, class_id, names):
     self.max_cosine_distance = 0.9
     self.nms_max_overlap = 0.5
     self.nn_budget = None
     self.model_filename = 'model_data/market1501.pb'
     self.detections = []
     self.id = class_id
     self.boxes_tracked = []
     self.color = (255, 255, 255)
     #self.encoder = gdet.create_box_encoder(self.model_filename,batch_size=1)
     self.class_names = names
     metric = nn_matching.NearestNeighborDistanceMetric("cosine", self.max_cosine_distance, self.nn_budget)
     self.obtracker = Tracker(metric)
     self.pts = [deque(maxlen=100) for _ in range(999999)]
     self.point_first = [deque(maxlen=2) for _ in range(999999)]
     self.vis=visualization.Visualization(img_shape=(960,1280,3), update_ms=2000)
     COLORS = np.random.randint(100, 255, size=(255, 3), dtype="uint8")
     self.color = [int(c) for c in COLORS[self.id]]
def run(sequence_dir, det, output_file, min_confidence, nms_max_overlap,
        max_cosine_distance, nn_budget, display):
    seq_info = gather_sequence_info(sequence_dir, det)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        detections = create_detections(seq_info["detections"], frame_idx)
        detections = [d for d in detections if d.confidence >= min_confidence]

        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        tracker.predict()
        tracker.update(detections)

        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            # vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

    if display:
        viewer = ImageViewer(40, seq_info["image_size"][::-1],
                             "Figure KITTI-16")
        visualizer = visualization.Visualization(seq_info, viewer)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback, vedio_file='deep_sort1.avi')
Example #5
0
def run(sequence_dir, det_dir, checkpoint, output_file, max_age,
        context_amount, iou, occlusion_thres, association_thres, display):

    seq_info = gather_sequence_info(sequence_dir, det_dir)
    tracker = MOT_Tracker(max_age, occlusion_thres, association_thres)

    conf = configparser.ConfigParser()
    conf.read(os.path.join(sequence_dir, 'seqinfo.ini'))
    tracker.frame_rate = int(conf.get('Sequence', 'frameRate'))

    if display:
        gt = seq_info['groundtruth'][np.where(
            seq_info['groundtruth'][:, -2] == 1)[0], :]

    results = []
    runtime = []
    detections = seq_info["detections"]

    def frame_callback(vis, detections, frame_idx, iou):

        # Load image and generate detections.
        print("Processing frame %05d" % frame_idx)
        frame_path = seq_info['image_filenames'][int(frame_idx)]
        detections = create_detections(detections, frame_idx)

        # Update tracker.
        before_time = time.time()
        trackers = tracker.update(frame_path, checkpoint, context_amount,
                                  detections, iou)  # tracking
        runtime.append(time.time() - before_time)
        # Store results.
        for d in trackers:
            results.append([frame_idx, d[4], d[0], d[1], d[2], d[3]])

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_groundtruth(gt[np.where(gt[:,0] == frame_idx)[0], 1], \
                                gt[np.where(gt[:,0] == frame_idx)[0], 2: 6])
            record_trks = [
                t for t in tracker.tracks
                if (t.is_tracked() and t.time_since_update <= 5)
            ]
            vis.draw_trackers(record_trks)

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info,
                                                 detections,
                                                 iou,
                                                 update_ms=100)
    else:
        visualizer = visualization.NoVisualization(
            seq_info,
            detections,
            iou,
        )

    visualizer.run(frame_callback)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)

    return sum(runtime) / len(runtime) * 1000
Example #6
0
def run(sequence_dir,
        result_file,
        show_false_alarms=False,
        detection_file=None,
        update_ms=None,
        video_filename=None):
    """Run tracking result visualization.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    result_file : str
        Path to the tracking output file in MOTChallenge ground truth format.
    show_false_alarms : Optional[bool]
        If True, false alarms are highlighted as red boxes.
    detection_file : Optional[str]
        Path to the detection file.
    update_ms : Optional[int]
        Number of milliseconds between cosecutive frames. Defaults to (a) the
        frame rate specifid in the seqinfo.ini file or DEFAULT_UDPATE_MS ms if
        seqinfo.ini is not available.
    video_filename : Optional[Str]
        If not None, a video of the tracking results is written to this file.

    """
    seq_info = deep_sort_app.gather_sequence_info(sequence_dir, detection_file)
    results = np.loadtxt(result_file, delimiter=',')

    if show_false_alarms and seq_info["groundtruth"] is None:
        raise ValueError("No groundtruth available. Cannot show false alarms.")

    def frame_callback(vis, frame_idx):
        print("Frame idx", frame_idx)
        image = cv2.imread(seq_info["image_filenames"][frame_idx],
                           cv2.IMREAD_COLOR)

        vis.set_image(image.copy())

        if seq_info["detections"] is not None:
            detections = deep_sort_app.create_detections(
                seq_info["detections"], frame_idx)
            vis.draw_detections(detections)

        mask = results[:, 0].astype(np.int) == frame_idx
        track_ids = results[mask, 1].astype(np.int)
        boxes = results[mask, 2:6]
        vis.draw_groundtruth(track_ids, boxes)

        if show_false_alarms:
            groundtruth = seq_info["groundtruth"]
            mask = groundtruth[:, 0].astype(np.int) == frame_idx
            gt_boxes = groundtruth[mask, 2:6]
            for box in boxes:
                # NOTE(nwojke): This is not strictly correct, because we don't
                # solve the assignment problem here.
                min_iou_overlap = 0.5
                if iou(box, gt_boxes).max() < min_iou_overlap:
                    vis.viewer.color = 0, 0, 255
                    vis.viewer.thickness = 4
                    vis.viewer.rectangle(*box.astype(np.int))

    if update_ms is None:
        update_ms = seq_info["update_ms"]
    if update_ms is None:
        update_ms = DEFAULT_UPDATE_MS
    visualizer = visualization.Visualization(seq_info, update_ms)
    if video_filename is not None:
        visualizer.viewer.enable_videowriter(video_filename)
    visualizer.run(frame_callback)
def run(video_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance,
        nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    video_file : str
        Path to the video file.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """

    cfg_file = "yolo3/cfg/yolov3.cfg"
    weight_file = "yolo3/yolov3.weights"
    #weight_file = 'yolo3/backup/MOT17Det/yolov3-mot17det_10000.weights'
    use_cuda = 1


    det_model = create_model(cfg_file, weight_file, use_cuda)

    seq_info = gather_video_info(video_file)
    metric = nn_matching.NearestNeighborDistanceMetric(
        "cosine", max_cosine_distance, nn_budget)

    tracker = Tracker(metric)
    # just for warming up
    img = cv2.imread('./000001.jpg')

    sized = cv2.resize(img, (det_model.width, det_model.height))
    sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
    boxes = do_detect(det_model, sized, 0.5, 0.4, use_cuda)


    def frame_callback(vis, frame_idx):
        #print("Processing frame %05d" % frame_idx)
        #global total_frames, total_time
        ret, img = seq_info['video_cap'].read()
        if not ret:
            print('there is no frame!')
            sys.exit(1)

        #time_0 = time.time()
        # Load image and generate detections.
        detections = create_det_from_model(det_model, img, 0.5, 0.4, min_detection_height, use_cuda)

        #detections = create_detections(
        #    seq_info["detections"], frame_idx, min_detection_height)
        #if seq_info['groundtruth'] is not None:
        #    gts = create_gts(seq_info['groundtruth'], frame_idx)

        #detections = create_detections(
        #    seq_info["detections"], frame_idx, seq_info["image_filenames"][frame_idx], encoder, min_detection_height)


        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)
        #time_1 = time.time()
        #total_time += time_1 - time_0
        #total_frames += 1
        # Update visualization.
        if display:
            #image = cv2.imread(
            #    seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            #vis.set_image(image.copy())
            vis.set_image(img.copy())
            #vis.draw_detections(detections)
            #vis.draw_detections(gts)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        # NOTE: store from n_init frame(1-based index)


        # for track in tracker.tracks:
        #     # NOTE: the condition is different from that in drawing tracks
        #     if not track.is_confirmed() or track.time_since_update > 1:
        #         continue
        #     # NOTE: store estimated state instead of observation
        #     bbox = track.to_tlwh()
        #     results.append([
        #         frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]])


    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=1)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)
Example #8
0
def run_multiple(sequence_dir, detection_dir, output_dir, min_confidence,
                 nms_max_overlap, min_detection_height, max_cosine_distance,
                 max_age, nn_budget, display, save_images_dir):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_dir : str
        Path to the detections file.
    output_dir : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.
    save_images_dir : string
        If not None, save the tracking result to indicated directories

    """
    all_sequences = sorted(glob.glob(os.path.join(sequence_dir, '*')))
    if len(all_sequences) == 0:
        raise ValueError("There is no folder in " + sequence_dir)
    for sequence_dir in all_sequences:
        video_name = sequence_dir.split('/')[-1]
        output_file = os.path.join(output_dir, video_name + '.npy')  #'.txt')
        print(video_name)
        detection_file = os.path.join(detection_dir, video_name + '.npy')
        try:
            os.stat(detection_file)
            os.stat(sequence_dir)
        except:
            raise NameError(detection_file + ' or ' + sequence_dir +
                            " doesn't exist!")
        seq_info = gather_sequence_info(sequence_dir, detection_file)
        metric = nn_matching.NearestNeighborDistanceMetric(
            "cosine", max_cosine_distance, nn_budget)
        tracker = Tracker(metric, max_age=max_age)
        results = []

        def frame_callback(vis, frame_idx):
            # print("Processing frame %05d" % frame_idx)

            # Load image and generate detections.
            detections = create_detections(seq_info["detections"], frame_idx,
                                           min_detection_height)
            detections = [
                d for d in detections if d.confidence >= min_confidence
            ]

            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            scores = np.array([d.confidence for d in detections])
            indices = preprocessing.non_max_suppression(
                boxes, nms_max_overlap, scores)
            detections = [detections[i] for i in indices]

            # Update tracker.
            tracker.predict()
            tracker.update(detections)

            # Update visualization.
            if display:
                image = cv2.imread(seq_info["image_filenames"][frame_idx],
                                   cv2.IMREAD_COLOR)
                image_name = seq_info["image_filenames"][frame_idx].split(
                    '/')[-1]
                vis.set_image(image.copy(), image_name)
                # vis.draw_detections(detections)
                vis.draw_trackers(tracker.tracks)

            # Store results.
            for track in tracker.tracks:
                if not track.is_confirmed() or track.time_since_update > 1:
                    continue
                bbox = track.to_tlwh()
                # print("track._class:",track._class)
                # print("track.confidence:",track.confidence)
                # print("track.feature_to_save:",track.feature_to_save.shape)
                results.append(
                    np.hstack([
                        frame_idx, track.track_id, bbox[0], bbox[1], bbox[2],
                        bbox[3], track._class, track.confidence,
                        track.feature_to_save
                    ]))

        # Run tracker.
        if display:
            visualizer = visualization.Visualization(
                seq_info, update_ms=5, save_images_dir=save_images_dir)
        else:
            visualizer = visualization.NoVisualization(seq_info)
        visualizer.run(frame_callback)

        # Store results.
        np.save(output_file, np.array(results))

        # f = open(output_file, 'w')
        # for row in results:
        #     print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' % (
        #         row[0], row[1], row[2], row[3], row[4], row[5]),file=f)
        # f.close()
        # shutdown the window
        if display:
            cv2.destroyWindow(visualizer.viewer._caption)
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance,
        nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    print("init model....")
    use_gpu = True
    exact_list = ['7']
    model = models.init_model(name='resnet50', num_classes=751, loss={'softmax', 'metric'}, use_gpu=use_gpu,
                              aligned=True)
    checkpoint = torch.load("./checkpoint_ep300.pth.tar")
    
    #global id
    global_next_id = []
    global_next_id.append(1)
    # tmp2 = {}
    # for item in checkpoint.items():
    #     key = item[0]
    #     values = item[1]
    #     newkey = key[7:]
    #     tmp2[newkey] = values
    # model.load_state_dict(tmp2)
    model.load_state_dict(checkpoint['state_dict'])

##sex and age
    transform_test = transforms.Compose([
        # transforms.Scale(224,224),
        transforms.ToTensor(),
        transforms.Normalize((0.429, 0.410, 0.382), (0.287, 0.285, 0.435))
    ])
    model_sex_age = sex_age.ResNet50(sex_classes=2, age_classes=5)
    tmp = torch.load('./sex_age/epoch5.pkl')
    tmp2 = {}
    for item in tmp.items():
        key = item[0]
        values = item[1]
        newkey = key[7:]
        tmp2[newkey] = values

    model_sex_age.load_state_dict(tmp2)
    model_sex_age.cuda()
    model_sex_age.eval()


    myexactor = FeatureExtractor(model, exact_list)

    model.eval()
    model.cuda()

    seq_info_list = []
    metric_list = []
    tracker = []
    angle_length = len(sequence_dir)
    for index in range(0, angle_length):
        seq_info_list.append(gather_sequence_info(sequence_dir[index], detection_file[index]))
        metric_list.append(nn_matching.NearestNeighborDistanceMetric(
        "cosine", max_cosine_distance, nn_budget))
        tracker.append(Tracker(metric_list[index]))
    results1,results2,results3 = [],[],[]

    #define feature gallery
    dic_feature = {}
    def frame_callback(vis, frame_idx, seq_info, viewer,angle):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(
            seq_info["detections"], frame_idx, min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        image = cv2.imread(
            seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
        # Update tracker.
        tracker[angle-1].predict()
        tracker[angle-1].update(detections, image,  myexactor, dic_feature, frame_idx, global_next_id, angle)
        id_dic = {}
        for index, track in enumerate(tracker[angle-1].tracks):
            str_id = str(track.track_id)
            if str_id in id_dic.keys():
                track.track_id = global_next_id[0]
                global_next_id[0] += 1
            else:
                id_dic[str_id] = (index, track.state)

        # Update visualization.
        if display:
            vis.set_image(image.copy(),viewer,angle)
            #print("deep_sort angle: "+str(angle))
            vis.draw_detections(detections,viewer,angle)
            vis.draw_trackers(tracker[angle-1].tracks,viewer,angle)

        # Store results.
        for track in tracker[angle-1].tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            print(angle)
            if angle == 1:
                print("angle1")
                results1.append([
                    frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3], track.sex, track.person_age])
            if angle == 2:
                print("angle2")
                results2.append([
                    frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3], track.sex, track.person_age])
            if angle == 3:
                print("angle3")
                results3.append([
                    frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3], track.sex, track.person_age])
            ### save gallery
            if (frame_idx) % 4 == 0:
                for i in range(4):
                    if bbox[i] < 0 :
                        bbox[i] = 0
                img = image[int(bbox[1]):int(bbox[1] + bbox[3]), int(bbox[0]):int(bbox[0] + bbox[2])]
                img = cv2.resize(img, (128, 256), interpolation=cv2.INTER_CUBIC)
                temp = img.copy()
                transform_test = T.Compose([
                    T.ToTensor(),
                    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
                ])

                img = transform_test(img)
                img = torch.unsqueeze(img, 0)
##sex age forward
                track_id = track.track_id
                img = img.cuda()
                sex_output, age_output = model_sex_age(img)
                pred1 = sex_output.data.max(1)[1]
                pred2 = age_output.data.max(1)[1]
                age = age_dict[str(int(pred2))]
                sex = sex_dict[str(int(pred1))]
                track.person_age = age
                track.sex = sex
##end
                f1 = myexactor(img)
                a1 = normalize(pool2d(f1[0], type='max'))
                if str(track_id) not in dic_feature.keys():
                    dic_feature[str(track_id)] = []
                    dic_feature[str(track_id)].append((a1, angle, (bbox[0]+bbox[2]*0.5, bbox[1]+bbox[3])))
                else:
                    if len(dic_feature[str(track_id)]) > 100:
                        del(dic_feature[str(track_id)][0])
                    dic_feature[str(track_id)].append((a1, angle, (bbox[0]+bbox[2]*0.5, bbox[1]+bbox[3])))
    if display:
        visualizer = visualization.Visualization(seq_info_list, update_ms=5,angle_length=angle_length)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback,angle_length)

    # Store results.
    f = open("./hypothese1.txt", 'w')
    for row in results1:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,%s,%s,1,-1,-1,-1' % (
            row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]),file=f)
    f2 = open("./hypothese2.txt", 'w')
    for row in results2:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,%s,%s,1,-1,-1,-1' % (
            row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]), file=f2)
    f3 = open("./hypothese3.txt", 'w')
    for row in results3:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,%s, %s, 1,-1,-1,-1' % (
            row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]), file=f3)
Example #10
0
def run(image_dir, groundtruth, detections, min_confidence, nms_max_overlap,
        min_detection_height, max_cosine_distance, nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    image_dir : str
        Path to the MOTChallenge sequence directory.
    detection_dir : str
        Path to the detections file.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    seq_info = gather_sequence_info(image_dir, groundtruth, detections)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    out = []
    curr_idx = -1
    img = None
    for row in results:
        if curr_idx != row[0]:
            if img != None:
                out += [img]
            img = {
                'videoName': video_name,
                'name': '{}-{}.jpg'.format(video_name,
                                           str(row[0]).zfill(6)),
                'index': row[0],
                'labels': []
            }
            curr_idx = row[0]

        img['labels'] += [{
            'category': None,
            'box2d': {
                'x1': row[2],
                'y1': row[3],
                'x2': row[2] + row[4],
                'y2': row[3] + row[5]
            },
            'id': row[1]
        }]

    out += [img]

    return out
Example #11
0
def run(video_path,
        track_path,
        feat_path,
        save_output,
        out_dir,
        cap_dir,
        concat,
        smoothing,
        save_fig,
        is_plot,
        lag=30):
    video_name = os.path.basename(video_path)
    seq_info = gather_sequence_info(video_name, video_path, feat_path)

    if (concat): track_path = track_path[:-4] + '_join.csv'

    df = pd.read_csv(track_path, header=None)

    if (smoothing): results = smooth(df, smooth_method='golay')
    else: results = df.values

    if (save_fig):
        capture(video_path, cap_dir, results, seq_info, is_plot=is_plot)
        return

    cap = cv2.VideoCapture(video_path)
    print('Video Path:', video_path, '\tFeatures:', feat_path)

    print(lag)
    points = {}

    def frame_callback(vis, frame_idx):
        # print("Frame idx", frame_idx)
        image_np = np.array(cap.read()[1])
        vis.set_image(image_np)

        mask = results[:, 0].astype(np.int) == frame_idx
        track_ids = results[mask, 1].astype(np.int)
        boxes = results[mask, 2:6]

        points[frame_idx] = []
        for track_id, box in zip(track_ids, boxes):
            l, t, w, h = np.array(box).astype(int)
            x, y = int(l + w / 2), int(t + h)
            points[frame_idx].append([track_id, x, y])

        if (frame_idx > lag):
            remove_idx = frame_idx - lag
            if remove_idx in points:
                del points[remove_idx]

        vis.draw_groundtruth(track_ids, boxes, points)

    visualizer = visualization.Visualization(seq_info, update_ms=50)

    if save_output:
        if (concat):
            visualizer.viewer.enable_videowriter(
                os.path.join(out_dir, video_name[:-4] + '_opt.mp4'))
        else:
            visualizer.viewer.enable_videowriter(
                os.path.join(out_dir, video_name[:-4] + '_reg.mp4'))

    visualizer.run(frame_callback)

    cap.release()
    cv2.destroyAllWindows()
Example #12
0
def run(model, sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    model: protobuffr
        Path to tensorflow feature model
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    #encoder = create_box_encoder(model, batch_size=64)

    seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    #metric = nn_matching.NearestNeighborDistanceMetric(
    #    "euclidean", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)
    results = []

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        #if seq_info['groundtruth'] is not None:
        #    gts = create_gts(seq_info['groundtruth'], frame_idx)

        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        """
        if display:
            image = cv2.imread(
                seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            #vis.draw_detections(detections)
            #vis.viewer.color = 0, 255, 0
            #vis.draw_detections(gts)
            #vis.draw_trackers(tracker.tracks)
        """
        tracker.update(detections)
        # if display:
        #     vis.viewer.color = 255, 0, 0
        #     vis.draw_trackers(tracker.tracks)

        # Update visualization.
        #"""
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            #vis.draw_detections(detections)
            #vis.draw_detections(gts)
            vis.draw_trackers(tracker.tracks)
        #"""
        # Store results.
        # NOTE: store from n_init frame(1-based index)
        """
        for track in tracker.tracks:
            # NOTE: the condition is different from that in drawing tracks
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            # NOTE: store estimated state instead of observation
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]])
        """

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=10)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # Store results.
    #f = open(output_file, 'w')
    #for row in results:
    #    print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' % (
    #        row[0], row[1], row[2], row[3], row[4], row[5]),file=f)
    """
Example #13
0
def run(sequence_dir, output_file, min_confidence, nms_max_overlap,
        min_detection_height, max_cosine_distance, nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """

    global total_frames, total_time

    cfg_file = "yolo3/cfg/yolov3.cfg"
    #weight_file = "yolo3/yolov3.weights"
    weight_file = 'yolo3/backup/MOT17Det/yolov3-mot17det_10000.weights'
    use_cuda = 1

    det_model = create_model(cfg_file, weight_file, use_cuda)

    seq_info = gather_sequence_info(sequence_dir, None)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    #metric = nn_matching.NearestNeighborDistanceMetric(
    #    "euclidean", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)
    results = []

    img = cv2.imread(seq_info["image_filenames"][1])

    sized = cv2.resize(img, (det_model.width, det_model.height))
    sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
    boxes = do_detect(det_model, sized, 0.5, 0.4, use_cuda)

    print("processing: %s" % os.path.basename(sequence_dir))

    def frame_callback(vis, frame_idx):
        #print("Processing frame %05d" % frame_idx)
        global total_frames, total_time
        img = cv2.imread(seq_info["image_filenames"][frame_idx])

        time_0 = time.time()
        # Load image and generate detections.
        detections = create_det_from_model(det_model, img, 0.5, 0.4,
                                           min_detection_height, use_cuda)

        #detections = create_detections(
        #    seq_info["detections"], frame_idx, min_detection_height)
        #if seq_info['groundtruth'] is not None:
        #    gts = create_gts(seq_info['groundtruth'], frame_idx)

        #detections = create_detections(
        #    seq_info["detections"], frame_idx, seq_info["image_filenames"][frame_idx], encoder, min_detection_height)

        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        #tracker.predict()
        #tracker.update(detections)
        time_1 = time.time()
        total_time += time_1 - time_0
        total_frames += 1
        # Update visualization.
        if display:
            #image = cv2.imread(
            #    seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            #vis.set_image(image.copy())
            vis.set_image(img.copy())
            vis.draw_detections(detections)
            #vis.draw_detections(gts)
            #vis.draw_trackers(tracker.tracks)

        # Store results.
        # NOTE: store from n_init frame(1-based index)

        for track in tracker.tracks:
            # NOTE: the condition is different from that in drawing tracks
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            # NOTE: store estimated state instead of observation
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=1)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    print("average time for one frame: %.5f, FPS: %.5f" %
          (total_time / total_frames, total_frames / total_time))
    # Store results.
    #f = open(output_file, 'w')
    #for row in results:
    #    print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' % (
    #        row[0], row[1], row[2], row[3], row[4], row[5]),file=f)
    """
Example #14
0
def run_ghma(sequence_dir, detection_file, output_file,
            min_confidence, nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
            display, run_type):

    seq_info     = gather_sequence_info(sequence_dir, detection_file)
    image_shape  = seq_info["image_size"][::-1]
    aspect_ratio = float(image_shape[1]) / image_shape[0]
    image_shape  = 1024, int(aspect_ratio * 1024)

    first_idx    = seq_info["min_frame_idx"]
    last_idx     = seq_info["max_frame_idx"]
    #if pre_computed:
    if run_type == "pre_computed":
        print(" *********** Pre_computed mode ***********")
        if os.path.isfile('0130/01/det/det.txt'):
            if not os.path.getsize('0130/01/det/det.txt'):
                detFlag = False
            else:
                detFlag = True
        else:
            detFlag = False

        if not detFlag:
            net  = load_net(cfg_path, weight_path, 0)
            meta = load_meta(meta_path)
            det_file = open('0130/01/det/det.txt', 'w')
            for idx in range(first_idx, last_idx+1):
                print(idx)
                result = detect_(net, meta,
                            seq_info["image_filenames"][idx].encode('utf-8'),
                            11,
                            target=range(8),
                            thresh=min_confidence)
                for j in range(len(result)):
                    det_file.write("%d,%d,%f,%f,%f,%f,%f,-1,-1,-1\n" %
                        (idx, result[j][0], result[j][2], result[j][3], result[j][4], result[j][5], result[j][1]))
            det_file.close()
        else:
            print(">> Detections already exsits, skip yolo detection step")

        if os.path.isfile('./temp/01.npy'):
            if not os.path.getsize('./temp/01.npy'):
                extFlag = False
            else:
                extFlag = True
        else:
            extFlag = False

        if not extFlag:
            f = create_box_encoder("resources/networks/mars-small128.ckpt-68577", batch_size=32, loss_mode="cosine")
            generate_detections(f, "./0130/", "./temp/", None)
        else:
            print(">> Features already exists, skip extraction step")
        seq_info = gather_sequence_info(sequence_dir, "./temp/01.npy")
        metric   = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
        tracker  = Tracker(metric)
        results  = []
    elif run_type == "instant":
        print(" *********** Instant Mode ***********")
        encoder = create_box_encoder("resources/networks/mars-small128.ckpt-68577",
                                        batch_size=32, loss_mode="cosine")
        net     = load_net(cfg_path, weight_path, 0)
        meta    = load_meta(meta_path)
        metric  = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
        tracker = Tracker(metric)
        results = []
    else:
        raise Exception(" Unknown run type ")

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        if run_type == "pre_computed":
            detections = create_detections(seq_info["detections"], frame_idx, min_detection_height)
            detections = [d for d in detections if d.confidence >= min_confidence]
        elif run_type == "instant":
            detections = calc_cur_det(net, meta, encoder, seq_info, frame_idx, min_confidence)

        # Run non-maxima suppression.
        boxes      = np.array([d.tlwh for d in detections])
        scores     = np.array([d.confidence for d in detections])
        indices    = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)
            vis.save_image("./frame/{}.jpg".format(frame_idx))

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=100)
    else:
        visualizer = visualization.NoVisualization(seq_info)

    visualizer.run(frame_callback)
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance,
        nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric(
        "cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)
    results = []

    """
    seq_info['detections'] --> array of shape (detections, 10 + feat vect size)
    
    Every row corresponds to a single detection (every frame can have various detections and therefore various rows)
    
    For each detection, store:
        - The 10 first columns correspond to the MOT challenge notations:
        
            [<frame>, <id>, <bb_left>, <bb_top>, <bb_width>, <bb_height>, <conf>, <x>, <y>, <z>]
            OBS. The <id>, <x>, <y>, <z> params are set to -1.
        
        - The rest of columns correspond to the feature vectors.
    """

    det = seq_info["detections"]
    images = sorted(seq_info['image_filenames'].keys())

    def frame_callback(vis, frame_idx):

        image_id = images.index(frame_idx)
        det = seq_info["detections"][image_id:]

        if image_id==366:
            print()

        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(
            seq_info["detections"], image_id, min_detection_height)

        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(
                seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            #if not track.is_confirmed() or track.time_since_update > 1:
            #    continue
            bbox = track.to_tlwh()
            results.append([
                image_id, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3], 1])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)  # 5
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # Store results.

    #def results2json(results, output_file):
    data = list()
    for i, row in enumerate(results):

        print(i)
        if i == 412:
            print()

        d = dict()
        d['image_id'] = row[0]
        d['bbox'] = [row[2], row[3], row[4], row[5]]
        d['score'] = 1
        d['category_id'] = 1
        data.append(d)

    with open(output_file, 'w') as outfile:
        json.dump(data, outfile)

    """
Example #16
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """Run multi-target tracker on a particular sequence.
    """
    # 初始化tracker
    seq_info = gather_sequence_info(
        sequence_dir, detection_file)  # 加载检测文件detection_file:.npy
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    # 调用回调函数
    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections. 加载图像并生成检测。
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)  #生成检测
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression. 运行非极大值抑制。
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.  进行跟踪
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #17
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance,
        nn_budget, display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric(
        "cosine", max_cosine_distance, nn_budget)
    tracker = Tracker(metric)
    results = []
    
    # Create label
    # classs = []
    # 
    # for i in range(len(tri)):
    #   classs.append(label[tri[i]])
    # print(classs)
    #

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        #input()

        # Load image and generate detections.
        detections = create_detections(
            seq_info["detections"], frame_idx, min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # print("Detections: ", detections)
        # input()

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        label =  np.array([d.label for d in detections])
        #print('boxes',boxes)
        #print('scores',scores)
        indices = preprocessing.non_max_suppression(
            boxes, nms_max_overlap, scores)
        detections = [detections[i] for i in indices]

        # build label
        label_name = ['Di bo','Xe dap','Xe may',
                'Xe hang rong','Xe ba gac','Xe tac xi',
                'Xe hoi','Xe ban tai','Xe cuu thuong',
                'Xe khach','Xe buyt',
                'Xe tai','Container','Xe cuu hoa']
                
        label_actual = []
        for i in label:
            i = int(i)
            #print('i',i)
            label_actual.append(label_name[i])
            
        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(
                seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            #print(boxes)
            
            # vis.draw_detections(detections,label_actual)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue

            bbox = track.to_tlwh()
            #print('label',track.label)
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3],track.label])
        #print('Doneee')

    # Run tracker.
    if display:
        print('seq_info',seq_info)
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    
    cam = sequence_dir.split("/")[-1]

    #output_path_frames = os.path.join("saved_frames", cam)
  

    visualizer.run(frame_callback)


    #print("Sequence dir: ", sequence_dir)
    print("Cam: ", cam)
    #print(output_path_frames) 
    # Store results.
   
    # for k,row in enumerate(results):
    #    print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1'.format(
    #             row[0], row[1], row[2], row[3], row[4], row[5]))
    
    #print("Ahihi")

    f = open(output_file, 'w')
    #print("The abs path: ", os.path.join(os.path.abspath(output_file), cam + ".txt"))
    for k,row in enumerate(results):
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,%d,-1,-1' % (
            row[0], row[1], row[2], row[3], row[4], row[5],row[6]),file=f)
Example #18
0
def run(sequence_dir,
        detection_file,
        output_file,
        min_confidence,
        nms_max_overlap,
        min_detection_height,
        max_cosine_distance,
        nn_budget,
        display,
        video_dir=None,
        max_age=None,
        world_viewer_threshold=None):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    sequence_dir_list = []
    video_dir_list = []
    if video_dir:
        video_dir_list = video_dir.split(",")
    else:
        sequence_dir_list = sequence_dir.split(",")
    detection_file_list = detection_file.split(",")
    camera_num = len(detection_file_list)
    seq_info_dic = {}
    tracker_dic = {}
    global_track = []
    global_id = [0]
    for i in range(camera_num):
        if video_dir:
            seq_info_dic[i] = gather_video_info(video_dir_list[i],
                                                detection_file_list[i])
        else:
            seq_info_dic[i] = gather_sequence_info(sequence_dir_list[i],
                                                   detection_file_list[i])
    # print ("initial seq_info_dic is {}".format(seq_info_dic))
    for i in range(camera_num):
        tracker_dic[i] = Tracker(nn_matching.NearestNeighborDistanceMetric(
            "cosine", max_cosine_distance, nn_budget),
                                 i,
                                 camera_num,
                                 max_age=max_age)
    results = []

    tracking_file = open('tracking.txt', 'w+')

    def video_frame_processing(vis, frame_idx, index, image):
        # print("Processing frame %05d" % frame_idx)

        frame_indices = seq_info_dic[index]["detections"][:, 0].astype(np.int)
        mask = frame_indices == frame_idx
        detection_list = []
        for row in seq_info_dic[index]["detections"][mask]:
            bbox, confidence, feature = row[1:5], row[5], row[6:]
            # if bbox[3] < min_height:
            #     continue
            detection_list.append(Detection(bbox, confidence, feature, index))
        print('input nums: ', len(detection_list))
        # filter detections by confidence
        detections = [
            d for d in detection_list if d.confidence >= min_confidence
        ]

        #input()
        # extract detection info
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        # filter detection by NMS
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]
        print('filtered nums: ', len(detections))
        tracker_dic[index].predict()
        matching_results = tracker_dic[index].update(detections, tracker_dic,
                                                     global_id, global_track,
                                                     world_viewer_threshold)
        print('matching results: ', matching_results)
        for res in matching_results:
            det = detections[res[1]]
            tracking_file.write('{} {} {} {} {} {} {}\n'.format(
                frame_idx, det.tlwh[0], det.tlwh[1], det.tlwh[2], det.tlwh[3],
                det.confidence, res[0]))
            tracking_file.flush()

        if display:
            if index == 0:
                vis.reset_image()
            vis.append_image(image.copy())
            vis.draw_trackers(tracker_dic[index].tracks, index)
        # Store results.
        # for i in range(camera_num):
        #     for track in tracker_dic[i].tracks:
        #         if not track.is_confirmed() or track.time_since_update > 1:
        #             continue
        #         bbox = track.to_tlwh()
        #         results.append([
        #             frame_idx, track.global_id, bbox[0], bbox[1], bbox[2], bbox[3]])

    def frame_callback(vis, frame_idx, index):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info_dic[index]["detections"],
                                       frame_idx, index, min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]
        # print ("the camera_index is {}, the coordiantes of detections is {}".format(index,[detection.tlwh for detection in detections]))
        # Update tracker.
        tracker_dic[index].predict()
        tracker_dic[index].update(detections, tracker_dic, global_id,
                                  global_track, world_viewer_threshold)

        # Update visualization.
        if display:
            if index == 0:
                vis.reset_image()
            image = cv2.imread(
                seq_info_dic[index]["image_filenames"][frame_idx],
                cv2.IMREAD_COLOR)
            vis.append_image(image.copy())
            vis.draw_trackers(tracker_dic[index].tracks, index)
        # Store results.
        # for i in range(camera_num):
        #     for track in tracker_dic[i].tracks:
        #         if not track.is_confirmed() or track.time_since_update > 1:
        #             continue
        #         bbox = track.to_tlwh()
        #         results.append([
        #             frame_idx, track.global_id, bbox[0], bbox[1], bbox[2], bbox[3]])

    # find the maximun frame id
    last_frame = {}
    max_frame_idx = 0
    for i in range(camera_num):
        last_frame[i] = seq_info_dic[i]["max_frame_idx"]
        if last_frame[i] > max_frame_idx:
            max_frame_idx = last_frame[i]

    if video_dir == None:
        visualizer = visualization.Visualization(seq_info_dic, global_id,
                                                 camera_num, 5, last_frame)
        visualizer.run(frame_callback, tracker_dic, camera_num, max_frame_idx)
    else:
        # for video sequence

        # print ("seq_info_dic is {}".format(seq_info_dic))
        visualizer = visualization.Visualization(seq_info_dic, global_id,
                                                 camera_num, 5, last_frame,
                                                 video_dir_list)
        visualizer.run(video_frame_processing, tracker_dic, camera_num,
                       max_frame_idx)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #19
0
def cam_detection(encoder, cam_id, min_confidence, max_cosine_distance,
                  nms_max_overlap, nn_budget):
    cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        print("Camera {} open failed.".format(cam_id))
        return

    ret, frame = cap.read()
    seq_info = gather_sequence_info(cam_id, frame)

    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []
    jump_step = 3
    print('Start frame_callback')

    def frame_callback(vis, frame_idx):
        print('Processing frame: {:d}'.format(frame_idx))
        if not cap.isOpened(): return
        st_time = time.time()
        # Load image and generate detections.
        ret, frame = cap.read()

        # Object detection -- SSD
        rclasses, rscores, rbboxes = process_image(
            frame,
            select_threshold=min_confidence,
            nms_threshold=nms_max_overlap)  #boxes in (x1, y1, x2, y2) format

        # Select only person
        cls_ind = 15

        inds = np.where(rclasses == cls_ind)[0]
        if len(inds) == 0:
            dets = []
        else:
            cls_boxes = rbboxes[inds, :]
            cls_boxes_v = cls_boxes.copy()
            #convert normalized (ny1, nx1, ny2, nx2) to pixel coordinate (x1, y1, x2, y2)
            cls_boxes_v[:, 1] = cls_boxes[:, 0] * frame.shape[0]
            cls_boxes_v[:, 0] = cls_boxes[:, 1] * frame.shape[1]
            cls_boxes_v[:, 3] = cls_boxes[:, 2] * frame.shape[0]
            cls_boxes_v[:, 2] = cls_boxes[:, 3] * frame.shape[1]

            cls_boxes_v[:, 2:] -= cls_boxes_v[:, :
                                              2]  #transfer to (x,y,w,h) format
            cls_scores = rscores[inds]

            dets = np.hstack(
                (cls_boxes_v, cls_scores[:, np.newaxis])).astype(np.float32)

            print('Filtered detection size: {}'.format(np.asarray(dets).shape))
        # Feature extraction
        detections = feature_extraction(encoder, frame, dets)

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        vis.set_image(frame.copy())
        vis.draw_detections(detections)
        vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

        end_time = time.time()
        print('Processing time: {:.3f}s'.format(end_time - st_time))

    # Run tracker.
    visualizer = visualization.Visualization(seq_info, update_ms=5)

    visualizer.run(frame_callback)

    cap.release()
Example #20
0
def run(sequence_dir, result_file, show_false_alarms=False, detection_file=None,
        update_ms=None, video_filename=None, IDnum=0):
    """Run tracking result visualization.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    result_file : str
        Path to the tracking output file in MOTChallenge ground truth format.
    show_false_alarms : Optional[bool]
        If True, false alarms are highlighted as red boxes.
    detection_file : Optional[str]
        Path to the detection file.
    update_ms : Optional[int]
        Number of milliseconds between cosecutive frames. Defaults to (a) the
        frame rate specifid in the seqinfo.ini file or DEFAULT_UDPATE_MS ms if
        seqinfo.ini is not available.
    video_filename : Optional[Str]
        If not None, a video of the tracking results is written to this file.
    track_id : currently track id
    IDnum : not 0 is foot tracking display

    """



    seq_info = deep_sort_app.gather_sequence_info(sequence_dir, detection_file)
    results = np.loadtxt(result_file, delimiter=',')


    if show_false_alarms and seq_info["groundtruth"] is None:
        raise ValueError("No groundtruth available. Cannot show false alarms.")

    def frame_callback(vis, frame_idx):
        #프레임별로 처리
        print("Frame idx", frame_idx)
        image = cv2.imread(
            seq_info["image_filenames"][frame_idx], cv2.IMREAD_COLOR)

        vis.set_image(image.copy())

        if seq_info["detections"] is not None:
            detections = deep_sort_app.create_detections(
                seq_info["detections"], frame_idx)
            vis.draw_detections(detections)

        mask = results[:, 0].astype(np.int) == frame_idx
        track_ids = results[mask, 1].astype(np.int)         #해당 frame_id인 mask값들 중 [1]들인 id값 추출
        boxes = results[mask, 2:6]
        vis.draw_groundtruth(track_ids, boxes)

        # 발위치 10개 중 y값만 빼기
        h_file = os.path.dirname(result_file)  # result/text/

        with open(h_file + '/ID_h.txt', 'r') as f_hi:
            line_splits = [int(l.split(',')[1]) for l in f_hi.read().splitlines()[1:]]
        i = 0
        #print(line_splits)



        if show_false_alarms:
            groundtruth = seq_info["groundtruth"]
            mask = groundtruth[:, 0].astype(np.int) == frame_idx
            gt_boxes = groundtruth[mask, 2:6]
            for box in boxes:
                # NOTE(nwojke): This is not strictly correct, because we don't
                # solve the assignment problem here.
                min_iou_overlap = 0.5
                if iou(box, gt_boxes).max() < min_iou_overlap:
                    vis.viewer.color = 0, 0, 255
                    vis.viewer.thickness = 4
                    vis.viewer.rectangle(*box.astype(np.int))


                if IDnum != 0:                            # Tracking 하는 ID만 보여주고 발 표시 !!!!!!!


                    vis.viewer.circle(
                    box[0] + box[2] / 2, box[1] + box[3], 3)

                    #if int(box[1] + box[3]) > line_splits[i] - 3 and int(bbox[1] + bbox[3]) < line_splits[i] + 3:
                    #    vis.viewer.circle(
                    #        box[0] + box[2] / 2, box[1] + box[3], 3)
                    #    i += 1


    if update_ms is None:
        update_ms = seq_info["update_ms"]
    if update_ms is None:
        update_ms = DEFAULT_UPDATE_MS
    visualizer = visualization.Visualization(seq_info, update_ms)
    if video_filename is not None:
        visualizer.viewer.enable_videowriter(video_filename)
    visualizer.run(frame_callback)
Example #21
0
def run(video_name, video_path, feat_path, track_path, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):

    seq_info = gather_sequence_info(video_name, video_path, feat_path)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric, max_age=50, n_init=5)
    results = []
    cap = cv2.VideoCapture(video_path)
    print('Video Path:', video_path, '\tFeatures:', feat_path)

    def frame_callback(vis, frame_idx):
        # print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            vis.set_image(cap.read()[1])

            # vis.draw_detections(detections)
            count_human = vis.draw_trackers(tracker.tracks)
        else:
            count_human = vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()

            ID = count_human.index(track.track_id) + 1
            results.append([frame_idx, ID, bbox[0], bbox[1], bbox[2], bbox[3]])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=50)
    else:
        visualizer = visualization.NoVisualization(seq_info)

    visualizer.run(frame_callback)

    cap.release()
    # Store results.
    f = open(track_path, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #22
0
def run(sequence_dir,
        detection_file,
        output_file,
        min_confidence,
        nms_max_overlap,
        min_detection_height,
        max_cosine_distance,
        nn_budget,
        display,
        stock=False,
        track_class=None,
        **kwargs):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    if track_class is not None:
        seq_info = gather_sequence_info(sequence_dir, detection_file,
                                        track_class)
    else:
        seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    if stock:
        print('initializing a stock tracker')
        tracker = Tracker(metric,
                          max_age=kwargs['max_age'],
                          max_iou_distance=1.0 - kwargs['min_iou_overlap'])
    else:
        print('initializing a modified tracker')
        # the tracker now has the class as an optional argument
        tracker = MyTracker(
            metric,
            max_age=kwargs['max_age'],
            max_iou_distance=1.0 - kwargs['min_iou_overlap'],
            tracker_type=kwargs["tracker_type"],
            flow_dir=kwargs["flow_dir"],
            update_kf=kwargs["update_kf"],
            update_hit=kwargs["update_hit"]
        )  # the IOU is inverted as 1 - IOU in the cost matrix

    results = []
    if kwargs["track_subset_file"] is not None:
        good_frames = np.loadtxt(kwargs["track_subset_file"])
    else:
        good_frames = None

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # this is is what should be called detections
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)

        # this is the high confidence detections
        high_confidence_detections = [
            d for d in detections if d.confidence >= min_confidence
        ]
        # These are the low confidences ones and we don't need to run NMS on them because the goal is to retain as much information as possible
        # these should be cmpletely disjoint
        low_confidence_detections = [
            d for d in detections if d.confidence < min_confidence
        ]

        #HACK temporarily set the value of detections to None to make sure it's not used
        detections = None

        # Run non-maxima suppression.
        # these should only be from high conf detections
        # hc = high_conf
        hc_boxes = np.array([d.tlwh for d in high_confidence_detections])
        hc_scores = np.array(
            [d.confidence for d in high_confidence_detections])

        indices = preprocessing.non_max_suppression(hc_boxes, nms_max_overlap,
                                                    hc_scores)

        hc_nms_positive_detections = [
            high_confidence_detections[i] for i in indices
        ]  # I think you can just do this by indexing
        # this should negate the value from the line above
        # there might be a cleaner way to do this with sets
        hc_nms_negative_detections = [
            high_confidence_detections[i]
            for i in range(len(high_confidence_detections)) if i not in indices
        ]
        assert len(hc_nms_positive_detections) + len(
            hc_nms_negative_detections
        ) == len(
            high_confidence_detections
        ), "This operation should just be partitioning detections into two subsets"

        # Update tracker.
        tracker.predict()
        # read the next image because we will actually be using it now
        image = cv2.imread(seq_info["image_filenames"][frame_idx],
                           cv2.IMREAD_COLOR)
        assert image is not None, "the image now needs to be properly read"
        if stock:
            tracker.update(hc_nms_positive_detections)
        else:
            tracker.update(hc_nms_positive_detections,
                           bad_detections=hc_nms_negative_detections +
                           low_confidence_detections,
                           image=image,
                           use_unmatched=kwargs["use_unmatched"],
                           frame_idx=frame_idx)

        # Update visualization.
        if display:
            vis.set_image(image.copy())
            if seq_info['groundtruth'] is not None:
                vis.draw_groundtruth(
                    *create_groundtruth(seq_info['groundtruth'], frame_idx))
            #HACK for showing detections
            #vis.draw_detections(hc_nms_positive_detections)
            vis.draw_trackers(
                tracker.tracks,
                create_groundtruth(seq_info['groundtruth'], frame_idx)
            )  # clean up the double use of create_groundtruht

        # Store results.
        for track in tracker.tracks:
            #MOD write all the tracks, even if not detected recently
            if not track.is_confirmed():  # or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(
            seq_info,
            update_ms=5,
            video_output_file=kwargs["video_output_file"],
            vis_method=kwargs["vis_method"])
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback, good_frames)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)

    # score results
    if seq_info['groundtruth'] is not None:
        scorer = Scorer()
        # I think the groundtruths might be off by a factor of 2
        scores = scorer.score_lists(results, seq_info['groundtruth'],
                                    good_frames)
        assert type(
            scores
        ) == pd.DataFrame, "The results are supposed to be a dataframe"
        last_slash_idx = output_file.rfind("/")
        scores_output_file = "{}scores{}".format(
            output_file[:last_slash_idx + 1], output_file[last_slash_idx + 1:])
        argv_output_file = "{}argv{}".format(output_file[:last_slash_idx + 1],
                                             output_file[last_slash_idx + 1:])
        # write the scores to a (very short) file
        current_name = scores.index[0]
        argv = re.sub("[',]", "", str(kwargs["argv"]))
        scores = scores.rename({current_name: argv})
        scores.to_csv(scores_output_file)
        with open(argv_output_file, 'w') as outfile:
            outfile.write(str(kwargs["argv"]))
    else:
        print("There are no groundtruths so no scoring can be done")
        last_slash_idx = output_file.rfind("/")
        argv_output_file = "{}argv{}".format(output_file[:last_slash_idx + 1],
                                             output_file[last_slash_idx + 1:])
        with open(argv_output_file, 'w') as outfile:
            outfile.write(str(kwargs["argv"]))
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.
    IDnum   : int
        Tracking ID_num

    """

    # 프레임 사진 있는 위치, 영상번호 (seq_info["image_filenames"])
    # 프레임번호 (seq_info["image_filenames"][frame_idx])
    # 다 gather_sequence_info에 있음

    if 'y' == input("is there ID [y/n] : "):
        IDnum = int(input("ID_num for tracking : "))  #########
        #range_down = input("ID tracking range_down : ")             #########
        #range_up = input("ID tracking range_up : ")                 #########
    else:
        IDnum = 0

    if 'y' == input("foot display [y/n] : "):  ############# y값 함수 구하고나면 발 위치보기
        foot_dis = True
    else:
        foot_dis = False

    seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []
    target_h = []

    update_ms = seq_info["update_ms"]
    max_frame_idx = seq_info["max_frame_idx"]

    i = 0

    def frame_callback(vis, frame_idx):
        #print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])  # (x, y, w, h)
        scores = np.array([d.confidence
                           for d in detections])  # Detector confidence score
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display or foot_dis:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            if IDnum == 0:
                vis.draw_detections(detections)
                vis.draw_trackers(tracker.tracks)
            else:  #찾는 ID 있을 때
                vis.draw_target_trackers(tracker.tracks, IDnum)
            if foot_dis:  # Tracking 하는 ID만 보여주고 발 표시 !!!!!!!
                vis.draw_foot(tracker.tracks, IDnum)

        # h 저장
        h_file = os.path.dirname(output_file)  # result/text/
        with open(h_file + '/ID_h.txt', 'r') as f_hi:
            line_splits = [
                int(l.split(',')[1]) for l in f_hi.read().splitlines()[1:]
            ]

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            ############################################################### tracking 대신 bbox만 넣어주고 계산하기
            bbox = track.to_tlwh()

            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])
            if int(track.track_id) == int(IDnum):
                #print("find ID-01")
                if bbox[1] + bbox[3] < seq_info["image_size"][0]:
                    target_h.append(
                        [track.track_id, bbox[1] + bbox[3],
                         bbox[3]])  # id의 y값 별 h

                if (
                        frame_idx >= 40
                ):  # start frame 이걸로 설정    ################################
                    # MOT16-02에서 할머니 멈추기 전까지가 260frame
                    endT = 117 * update_ms / 1000
                    # endT = 5
                    vel_py.foot_world(frame_idx, track.track_id, bbox, IDnum,
                                      update_ms, max_frame_idx, endT)
                    # velocity 추정 끝나면 count = 0으로 계산 그만시킴

                # foot 10 보기
                for i in range(10):
                    if int(bbox[1] + bbox[3]) > line_splits[i] - 1 and int(
                            bbox[1] + bbox[3]) < line_splits[i] + 1:
                        print(int(bbox[1] + bbox[3]))
                        vis.draw_foot(tracker.tracks, IDnum)

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)

    ##### 영상 저장 ###########
    #video_output_dir = os.path.curdir + '/result/video'
    #video_filename = os.path.join(video_output_dir, "%s_all_tracking.avi" % (os.path.basename(sequence_dir)))       # video name은 seq_info["sequence_name"]
    #video_filename = os.path.join(video_output_dir, "%s_ID%s_tracking.avi" % (os.path.basename(sequence_dir), IDnum))
    #video_filename = os.path.join(video_output_dir, "%s_ID%s_foot 10.avi" % (os.path.basename(sequence_dir), IDnum))
    #if video_filename is not None:
    #    visualizer.viewer.enable_videowriter(video_filename)
    #########################
    visualizer.run(frame_callback)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
        # [frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]]
    f.close()
Example #24
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
        MOT
        MOTChanllenge 序列文件所在的目录
    detection_file : str
        Path to the detections file.
        检测的文件目录
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
        追踪结果输出的文件目录
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
        最小置信度阈值
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
        最小检测框高度
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
        余弦距离门控阈值
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
        外观描述库的最大大小,如果大于该值,后边的外观描述信息会将旧的外观描述信息覆盖掉
    display : bool
        If True, show visualization of intermediate tracking results.
        是否将追踪结果进行可视化的展示
    """
    # 收集流信息,包括图片名称,检测结果以及置信度等
    seq_info = gather_sequence_info(sequence_dir, detection_file)
    # metic实例化nn_matching的NearestNeighborDistanceMetric类,输入的初始距离度量函数是cosine,此时可以传入euclidean
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    # 将度量方式对象传入到追踪器对象中,创建一个追踪器对象
    tracker = Tracker(metric)
    results = []

    # 循环帧:frame_idx < last_idx则运行frame_callback,且每次循环frame_idx += 1
    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        # 筛选小于min_confidence的目标,并构造一个Detection对象构成的列表
        # Detection是一个存储图中一个bbox结果
        # 需要:1. bbox(tlwh形式) 2. 对应置信度 3. 对应embedding

        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        # 筛选检测框
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        # 使用非极大抑制,找出局部范围内得分最大的框
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        # 更新检测框列表
        detections = [detections[i] for i in indices]

        # Update tracker.
        # tracker给出一个预测结果,然后将detection传入,进行卡尔曼滤波操作
        tracker.predict()
        tracker.update(detections)

        # Update visualization,
        # 进行可是化操作,如果选择了跟踪结果可视化,图片框中展示的图片

        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    # 根据display选择不同的可视化对象
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    # 调用对应的run方法开始进行追踪。
    visualizer.run(frame_callback)

    # 将追踪的结果存储到output_file中
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #25
0
def main():
    start = time.time()
    counter = []
    writeVideo_flag = False
    fps = 0.0
    filename_path = os.path.join(result_path, 'submission.txt')
    list_video, list_ids = load_list_video(list_video_path, id_path)
    result_file = open(filename_path, 'w')

    max_cosine_distance=0.8
    nn_budget = 100
    nms_max_overlap = 1.0
    display = True
    for video in list_video: 
        path = os.path.join(video_path, video)
        ROI = load_roi(zones_path, video)
        vis=visualization.Visualization(img_shape=(960,1280,3), update_ms=2000)

        metric = nn_matching.NearestNeighborDistanceMetric(
        "cosine", max_cosine_distance, nn_budget)
        tracker = Tracker(metric)
        results = []
        print("Processing video: ", video )
        video_capture = cv2.VideoCapture(path)

        pause_display = False
        frame_num = 0
        while True:
            
            start = time.time()
            # print(count)
            video_capture.set(cv2.CAP_PROP_POS_FRAMES, frame_num)
            ret, frame = video_capture.read()  # frame shape 640*480*3
            # gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
            # frame = np.zeros_like(frame1)
            # frame[:,:,0] = gray
            # frame[:,:,1] = gray
            # frame[:,:,2] = gray
            
            if ret != True:
                break
             #   print(count1)
            #print(frame.shape)
            w = int(video_capture.get(3))
            h = int(video_capture.get(4))   
            result = []
            t1 = time.time()
            
            img = letterbox(frame, new_shape=img_size)[0]
            img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
            img = np.ascontiguousarray(img)
            dets = run_detect(model,img,device,frame)

            detectionss=[]
            for det in dets:
                feature = gdet.HOG_feature(frame, det[:4])
                detectionss.append(Detection(det[:4], det[4], feature, det[-1]))   
            #detectionss.append(Detection(det[:4], det[4], det[-1]) for det in dets)
            img = np.zeros((h, w, 3), np.uint8)
            img = frame.copy()
            min_confidence = 0.4
            detections = [d for d in detectionss if d.confidence >= min_confidence]

            # Run non-maxima suppression.
            boxes = np.array([d.tlwh for d in detections])
            scores = np.array([d.confidence for d in detections])
            indices = preprocessing.non_max_suppression(
                boxes, nms_max_overlap, scores)
            detections = [detections[i] for i in indices]

            # Update tracker.
            tracker.predict()
            tracker.update(detections)

            if display:
                vis.set_image(frame.copy())
                vis.draw_detections(detections)
                vis.draw_trackers(tracker.tracks)
            res = vis.return_img()
            draw_roi(ROI, res)
            cv2.imshow('frame', res)
            print('frame_num', frame_num)
            if not pause_display:
                key = cv2.waitKey(2)
                if key == ord('q'):
                    break
                if key == ord(' '):
                    pause_display = not pause_display
                frame_num += 1
            else:
                key = cv2.waitKey(0)
                if key == ord('q'):
                    break
                if key == ord(' '):
                    pause_display = not pause_display
            
        print(" ")
        print("[Finish]")
        

    video_capture.release()

    if writeVideo_flag:
        out.release()
        #list_file.close()
    result_file.close()
    cv2.destroyAllWindows()
Example #26
0
def run(min_confidence, nms_max_overlap, min_detection_height,
        max_cosine_distance, nn_budget, max_time, feature_model):
    encoder = create_box_encoder(feature_model, batch_size=32)
    detector = Detector()
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    cap = cv2.VideoCapture(0)

    seq_info = {
        "sequence_name":
        "real-time",
        # "image_filenames": image_filenames,
        # "detections": detections,
        # "groundtruth": groundtruth,
        "image_size": (int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
                       int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))),
        "min_frame_idx":
        0,
        "max_frame_idx":
        max_time * 20,
        "feature_dim":
        127,
        "update_ms":
        500
    }

    def frame_callback(vis, frame_idx):
        # read image from camera
        ret, frame = cap.read()
        frame = np.array(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            exit(0)

        # generate bounding box
        detections = detector.detect(frame.copy(), frame_idx)
        # only display image if no detection
        if detections.shape[0] <= 0:
            vis.set_image(frame)
            return

        # generate deep feature
        features = encoder(frame.copy(), detections[:, 2:6].copy())
        detections = np.array([
            np.r_[(row, feature)]
            for row, feature in zip(detections, features)
        ])
        detections = create_detections(detections, min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)
        # Update visualization.
        vis.set_image(frame)
        vis.draw_detections(detections)
        vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    visualizer = visualization.Visualization(seq_info, update_ms=5)
    visualizer.run(frame_callback)

    cap.release()
    cv2.destroyAllWindows()
Example #27
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """
    multi-target tracker 실행

    Parameters
    ----------
    sequence_dir : str
        시퀀스 데이터 경로
    detection_file : str
        검출 파일
    output_file : str
        output file 경로 / 추적 결과를 포함
    min_confidence : float
        confidence 임계값 / 이보다 낮은 confidence는 무시하게 된다.
    nms_max_overlap: float
        Maximum detection overlap (NMS 임계값)
    min_detection_height : int
        height 임계값 / 이보다 낮은 height는 무시하게 된다.
    max_cosine_distance : float
        cosine distance metric 임계값
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget is enforced.
    display : bool
        True : 시각화

    """
    # 시퀀스 정보수
    seq_info = gather_sequence_info(sequence_dir, detection_file)
    # cosine distance metric 사용
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    # 프레임 마다 호출
    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # detection 생성
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # NMS 실행
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # tracker 업데이트 이 부분에서 detection을 업데이트 해서 객체를 추적한다.
        # 내가 내 코드에 결합하고 싶을 때 사용하는 방법을 이해하기 위해 포스트를 작성했기 떄문에
        # 자세하게 보고싶으면 SORT 포스트를 보자 비슷한거 같다.
        tracker.predict()
        tracker.update(detections)

        # 시각화
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # 결과를 저장한다.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # 객체를 추적하는 시작부분 frame_callback을 매 프레임 호출한다.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # 결과를 저장한다.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #28
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display, clss):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    capture = cv2.VideoCapture(sequence_dir)
    seq_info = gather_sequence_info(sequence_dir, detection_file, capture)
    capture.set(1, seq_info['min_frame_idx'])
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       seq_info['image_size'], clss,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            #capture.set(1, frame_idx)
            _, image = capture.read()
            #pdb.set_trace()
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 15:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # Post-process results.
    results_array = np.array(results)
    track_idxs = results_array[:, 1]
    unique, counts = np.unique(track_idxs, return_counts=True)
    #pdb.set_trace()
    # for u, c in zip(unique, counts):
    #     if c < 100:
    #         results_array = results_array[track_idxs!=u]
    #         track_idxs = track_idxs[track_idxs!=u]

    foo, unique_idxs = np.unique(track_idxs, return_inverse=True)
    results_array[:, 1] = unique_idxs
    results_processed = results_array.tolist()

    # Store results.
    f = open(output_file + '_' + clss + '.txt', 'w')
    for row in results_processed:
        print('%.2f,%.2f,%.2f,%.2f,%.2f,%.2f' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #29
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    seq_info = gather_sequence_info(sequence_dir, detection_file)
    metric = nn_matching.NearestNeighborDistanceMetric("cosine",
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric)
    results = []

    def frame_callback(vis, frame_idx):
        print("Processing frame %05d" % frame_idx)

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]

        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3]
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    # Store results.
    f = open(output_file, 'w')
    for row in results:
        print('%d,%d,%.2f,%.2f,%.2f,%.2f,1,-1,-1,-1' %
              (row[0], row[1], row[2], row[3], row[4], row[5]),
              file=f)
Example #30
0
def run(sequence_dir, detection_file, output_file, min_confidence,
        nms_max_overlap, min_detection_height, max_cosine_distance, nn_budget,
        display, offset, n_frames, max_iou_distance, max_age, n_init,
        max_tracks, metric_param, alpha_ds):
    """Run multi-target tracker on a particular sequence.

    Parameters
    ----------
    sequence_dir : str
        Path to the MOTChallenge sequence directory.
    detection_file : str
        Path to the detections file.
    output_file : str
        Path to the tracking output file. This file will contain the tracking
        results on completion.
    min_confidence : float
        Detection confidence threshold. Disregard all detections that have
        a confidence lower than this value.
    nms_max_overlap: float
        Maximum detection overlap (non-maxima suppression threshold).
    min_detection_height : int
        Detection height threshold. Disregard all detections that have
        a height lower than this value.
    max_cosine_distance : float
        Gating threshold for cosine distance metric (object appearance).
    nn_budget : Optional[int]
        Maximum size of the appearance descriptor gallery. If None, no budget
        is enforced.
    display : bool
        If True, show visualization of intermediate tracking results.

    """
    seq_info = gather_sequence_info(sequence_dir, detection_file, offset,
                                    n_frames)
    metric = nn_matching.NearestNeighborDistanceMetric(alpha_ds,
                                                       max_cosine_distance,
                                                       nn_budget)

    tracker = Tracker(metric,
                      max_iou_distance=max_iou_distance,
                      max_age=max_age,
                      n_init=n_init,
                      max_tracks=max_tracks,
                      metric_param=metric_param)
    results = []

    def frame_callback(vis, frame_idx):
        aaa = n_frames / 10
        if frame_idx % aaa == 0:
            print("Processing frame {} / {}".format(frame_idx, n_frames))

        # Load image and generate detections.
        detections = create_detections(seq_info["detections"], frame_idx,
                                       min_detection_height)
        detections = [d for d in detections if d.confidence >= min_confidence]

        # Run non-maxima suppression.
        boxes = np.array([d.tlwh for d in detections])
        scores = np.array([d.confidence for d in detections])
        indices = preprocessing.non_max_suppression(boxes, nms_max_overlap,
                                                    scores)
        detections = [detections[i] for i in indices]
        #print(len(detections))
        # Update tracker.
        tracker.predict()
        tracker.update(detections)

        # Update visualization.
        if display:
            image = cv2.imread(seq_info["image_filenames"][frame_idx],
                               cv2.IMREAD_COLOR)
            vis.set_image(image.copy())
            #vis.draw_detections(detections)
            vis.draw_trackers(tracker.tracks)

        # Store results.
        for track in tracker.tracks:
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            bbox = track.to_tlwh()
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3],
                1, -1, -1, -1
            ])

    # Run tracker.
    if display:
        visualizer = visualization.Visualization(seq_info, update_ms=5)
    else:
        visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    #Reorder from 1 to n_tracks:
    ids = list(np.unique(np.array(results)[:, 1]))
    results = np.array(results)
    results[:, 1] = [ids.index(x) for x in results[:, 1]]

    # Store results.
    np.save(output_file, results)