Ejemplo n.º 1
0
def analyze_movie(
    video_handle: io.IOBase) -> {
        "results": io.IOBase,
        "video_results": io.IOBase
    }:
    """
    Args:
        video_handle: file object for the movie to be analyzed.
        progress_hook: function that is called with two integer arguments. the first one represents the current frame index
            the second represents the final index.
    Return:
          dictionary containing path to the .csv file and to .mp4 file
    """
    model = create_model_efficient(
        model_creation_func=partial(create_model, max_operating_res=320))

    video_handle.close()
    video_file = video_handle.name
    frame_ids = p2p_load('frame_ids',
                         loading_func=lambda filepath: np.load(filepath))
    if frame_ids is None:
        frame_ids = movement_frames_indexes(video_file,
                                            progress_hook=p2p_progress_hook)
        p2p_save("frame_ids",
                 frame_ids,
                 saving_func=lambda filepath, item: np.save(filepath, item),
                 filesuffix=".npy")
    image_gen = framedatapoint_generator_by_frame_ids2(video_file,
                                                       frame_ids,
                                                       reason="motionmap")

    # TODO set batchsize by the available VRAM
    pred_gen = compute(image_gen, model=model, batch_size=5)
    filtered_pred = filter_pred_detections(pred_gen)
    if p2p_progress_hook is not None:
        filtered_pred = generator_hook(video_file, filtered_pred,
                                       p2p_progress_hook)
    df = pred_iter_to_pandas(filtered_pred)
    destination = os.path.splitext(video_file)[0] + '.csv'
    df.to_csv(destination)
    if p2p_progress_hook is not None:
        # call one more time the hook. this is just for clean ending of the processing. it may happen in case where the
        # skip is 5 that the final index is not reached, and in percentage it will look like 99.9% finished
        size = get_video_file_size(video_file) - 1
        p2p_progress_hook(size, size)

    visual_destination = os.path.splitext(video_file)[0] + '_results.mp4'
    visual_destination_good_codec = os.path.splitext(
        video_file)[0] + '_results_good_codec.mp4'
    compare_multiple_dataframes(video_file, visual_destination, df)

    subprocess.call(
        ["ffmpeg", "-i", visual_destination, visual_destination_good_codec])
    return {
        "results": open(destination, 'rb'),
        "video_results": open(visual_destination_good_codec, 'rb')
    }
Ejemplo n.º 2
0
def html_imgs_generator(video_path, csv_path):
    pred_gen_from_df = pandas_to_pred_iter(pd.read_csv(csv_path))
    filtered_pred = filter_pred_detections(pred_gen_from_df)
    filtered_dataframe = pred_iter_to_pandas(filtered_pred)

    important_frames, important_df = get_important_frames(filtered_dataframe)
    image_gen = framedatapoint_generator_by_frame_ids2(video_path, important_frames)

    pred_from_important = pandas_to_pred_iter(important_df)

    for fdp in plot_detections(image_gen, pred_from_important):
        yield image2htmlstr(fdp.image)
Ejemplo n.º 3
0
def test_TruckDetector_pred_iter_to_pandas():
    auu_data_root = r'D:\aau-rainsnow\Hjorringvej\Hjorringvej-2'
    video_file = [
        osp.join(r, f) for (r, _, fs) in os.walk(auu_data_root) for f in fs
        if 'avi' in f or 'mkv' in f
    ][0]
    #file 'Hjorringvej\\Hjorringvej-2\\cam1.mkv' has 6000 frames
    model = create_model(max_operating_res=320)
    image_gen = framedatapoint_generator(video_file, skip=6000 // 30)
    image_gen1, image_gen2 = tee(image_gen)

    pred_gen = compute(image_gen1, model)

    df = pred_iter_to_pandas(pred_gen)

    pred_gen_from_df = pandas_to_pred_iter(df)

    for idx, fdp in enumerate(plot_detections(image_gen2, pred_gen_from_df)):
        cv2.imshow("image", fdp.image)
        cv2.waitKey(1)
        if idx == 5: break
def test_dataframe_filtered():
    csv_file_path = osp.join(osp.dirname(__file__), 'data', 'cut.csv')
    pred_gen_from_df = pandas_to_pred_iter(pd.read_csv(csv_file_path))
    filtered_pred = filter_pred_detections(pred_gen_from_df)
    filtered_dataframe = pred_iter_to_pandas(filtered_pred)
def test_estimate_next_frame_ids():
    frame_num_24_hours = 30 * 3600 * 24
    frame_num_24_hours = 30 * 3600 * 1

    def dummy_generator():
        for i in range(frame_num_24_hours):
            if 10 < i < 50 or 200 < i < 500 or 800 < i < 900 or 90000 < i < 95000:
                #simulate detection
                dp = PredictionDatapoint(pred={
                    "boxes":
                    np.array([[0, 0, 0, 0]]),
                    "scores":
                    np.array([0]),
                    "labels":
                    np.array([model_class_names.index("truck")]),
                    "obj_id":
                    np.array([0])
                },
                                         frame_id=i)
            else:
                #simulate no detection
                dp = PredictionDatapoint(pred={
                    "boxes": np.array([[]]).reshape(-1, 4),
                    "scores": np.array([]),
                    "labels": np.array([]),
                    "obj_id": np.array([])
                },
                                         frame_id=i)
            yield dp

    dummy_df = pred_iter_to_pandas(pdp_iterable=dummy_generator())
    cols = list(dummy_df.columns)
    cols.remove("reason")
    dummy_df = dummy_df.dropna(subset=cols)
    plt.hist(dummy_df['img_id'], range=(0, frame_num_24_hours))
    # plt.show()

    kde = gaussian_kde(dummy_df['img_id'])
    # these are the values over wich your kernel will be evaluated
    dist_space = np.linspace(0, frame_num_24_hours, 1000)
    # plot the results
    plt.plot(dist_space, kde(dist_space))
    # plt.show()

    import scipy.stats as st

    class TrafficPDF(st.rv_continuous):
        def __init__(self, dataframe):
            assert 'img_id' in dataframe.columns
            super(TrafficPDF, self).__init__(a=dataframe['img_id'].min(),
                                             b=dataframe['img_id'].max(),
                                             name="TrafficPDF")
            self.kde = gaussian_kde(dataframe['img_id'])

        # def _pdf(self, x):
        #     return self.kde(x)
        def _cdf(self, x):
            return self.kde.integrate_box_1d(-np.Inf, x)

    my_cv = TrafficPDF(dummy_df)

    generated_data = my_cv.rvs(size=100).astype(np.int32)
    generated_data = sorted(generated_data.tolist())
    plt.hist(generated_data, bins=100)
    # plt.show()

    detected_frames = set(dummy_df['img_id'].unique())

    frames_to_search = set()
    for i in generated_data:
        while i in frames_to_search or i in detected_frames:
            i += 1
        frames_to_search.add(i)

    assert len(set(detected_frames) & set(frames_to_search)) == 0
    pass
def get_raw_df_from_movie(movie_path, model):
    g1 = framedatapoint_generator(movie_path, skip=0, max_frames=200)
    g2 = compute(g1, model, filter_classes=['train', 'truck', 'bus'])
    df = pred_iter_to_pandas(g2)
    return df
def get_tracked_df_from_df(df):
    g1 = filter_pred_detections(pandas_to_pred_iter(df))
    filtered_df = pred_iter_to_pandas(g1)
    return filtered_df