Exemplo n.º 1
0
def run_detection(video_source, detection_source):
    """Runs detection with MoVe Extrapolation based on a av.container video source and
    a detection source that implements `poll` behaviour."""

    mv_filter_method = C.move_config_aggregation_method
    acc = Accumulator()
    acc.register(extract_mvs)
    acc.register(apply_queue_of_mvs)
    acc.register(apply_mvs)
    run_data = {
        "results": [],
        "statistics": {
            "loop_time_samples": [],
            "debugging": acc,
        },
    }

    first_frame = True
    curr_mvs = None
    curr_bboxes = None
    mvs_buffer = queue.Queue()
    for i, frame in tqdm(enumerate(video_source.decode(video=0))):
        img = unpack_frame_to_img(frame)

        loop_last = time.perf_counter()

        if C.move_config_bypass:
            detections = detection_source.wait(i)
            curr_bboxes = detections[0]

        else:
            curr_mvs = extract_mvs(frame)
            mvs_buffer.put(curr_mvs)

            detections = detection_source.poll(i)
            if first_frame and detections is None:
                print(
                    "Skipping loop iteration until first detections come in.")
                continue

            elif not first_frame and detections is None:
                curr_bboxes = apply_mvs(curr_bboxes, curr_mvs,
                                        mv_filter_method)

            else:
                first_frame = False
                bboxes = detections[0]
                bboxes = apply_queue_of_mvs(mvs_buffer, bboxes,
                                            mv_filter_method)
                curr_bboxes = bboxes.copy()

        loop_now = time.perf_counter()
        run_data["statistics"]["loop_time_samples"].append(loop_now -
                                                           loop_last)

        h, w, _ = img.shape
        track_ids = [-1] * len(curr_bboxes)
        for j in range(len(curr_bboxes)):
            bbox = clamp_bbox_to_frame(curr_bboxes[j], (h, w))
            run_data["results"].append([
                i,
                track_ids[j],
                bbox[0],
                bbox[1],
                bbox[2],
                bbox[3],
            ])

    try:
        run_data["statistics"] = post_process_statistics(
            run_data["statistics"])

    except Exception as e:
        print("ERROR: ", e)
        run_data["statistics"] = None

    return run_data