Beispiel #1
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')
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')
Beispiel #3
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)
def run(detections, **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.

    """

    min_confidence = kwargs['min_confidence']
    nms_max_overlap = kwargs['nms_max_overlap']
    max_cosine_distance = kwargs['max_cosine_distance']
    nn_budget = kwargs['nn_budget']
    max_age = kwargs['max_age']
    n_init = kwargs['n_init']
    seq_info = gather_sequence_info(detections)
    metric = nn_matching.NearestNeighborDistanceMetric('cosine',
                                                       max_cosine_distance,
                                                       nn_budget)
    tracker = Tracker(metric, max_age=max_age, n_init=n_init)
    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)
        detections = [d for d in detections if d.confidence >= min_confidence]
        # Run non-maxima suppression.
        if nms_max_overlap >= 0:
            boxes = np.array([d.to_tlbr() for d in detections])
            scores = np.array([d.confidence for d in detections])

            nms_keep = nms(torch.from_numpy(boxes),
                           torch.from_numpy(scores),
                           iou_threshold=nms_max_overlap).numpy()

            detections = [detections[i] for i in nms_keep]

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

        # Store results.
        for track in tracker.tracks:
            #only output the detection results, no prediction
            if not track.is_confirmed() or track.time_since_update >= 1:
                continue
            bbox = track.last_tlwh
            feature = track.last_feature
            confidence = track.last_confidence
            results.append([
                frame_idx, track.track_id, bbox[0], bbox[1], bbox[2], bbox[3],
                confidence
            ] + feature.tolist())

    # Run tracker.
    visualizer = visualization.NoVisualization(seq_info)
    visualizer.run(frame_callback)

    return results
Beispiel #5
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)
Beispiel #6
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
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)
Beispiel #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)
Beispiel #9
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
Beispiel #10
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)
    """
Beispiel #11
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 = []
    pose_frame_list = []

    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)

        image = cv2.imread(seq_info["image_filenames"][frame_idx],
                           cv2.IMREAD_COLOR)
        print(image.shape[0])
        print(image.shape[1])

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

        # Add library to draw image
        image_img = Image.fromarray(image)

        # Prepare saving image with points of pose
        draw = ImageDraw.Draw(image_img)

        # 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]
            ])
            bbox_top = int(bbox[0])
            bbox_left = int(bbox[1])
            bbox_width = int(bbox[2])
            bbox_height = int(bbox[3])
            bbox_right = bbox_left + bbox_width
            bbox_bottom = bbox_top + bbox_height
            draw.rectangle([bbox_left, bbox_top, bbox_right, bbox_bottom],
                           outline='red')
            print([int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])])

        image_img_numpy = np.asarray(image_img)
        pose_frame_list.append(image_img_numpy)

    # Run tracker.
    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)

    video_pose = ImageSequenceClip(pose_frame_list, fps=24)
    video_pose.write_videofile("./results.mp4",
                               fps=24,
                               progress_bar=False,
                               codec="mpeg4")
Beispiel #12
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)
    """
Beispiel #13
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)

    """
Beispiel #15
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)
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)
Beispiel #17
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"]))
Beispiel #18
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)
Beispiel #19
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)
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()
Beispiel #21
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)
Beispiel #22
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)
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)