コード例 #1
0
ファイル: st_video.py プロジェクト: tpmp-inra/ipso_phen
def build_single_plant_video(args):
    plant, dst_folder_, db, dates_, experiment_, angles_ = args

    p_output = os.path.join(dst_folder_, f"{plant}.mp4")
    if os.path.isfile(p_output):
        return f"Plant {plant} already handled"

    ret = db.query(
        command="SELECT",
        columns="filepath",
        additional="ORDER BY date_time ASC",
        date=dict(operator="IN", values=dates_),
        experiment=experiment_,
        plant=plant,
        angle=angles_[0],
    )
    main_angle_image_list_ = [item[0] for item in ret]

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    out = cv2.VideoWriter(p_output, fourcc, 24.0, (video_width, video_height))
    fnt = cv2.FONT_HERSHEY_DUPLEX

    for main_angle_image_ in main_angle_image_list_:
        main_angle_wrapper_side = BaseImageProcessor(
            main_angle_image_,
            db,
        )
        try:
            img_main_angle = build_image(main_angle_wrapper_side)
            if img_main_angle is None:
                continue
            main_angle_wrapper_side.store_image(image=img_main_angle,
                                                text=angles_[0],
                                                force_store=True)
        except Exception as e:
            print(
                f'Exception "{repr(e)}" while handling {str(main_angle_wrapper_side)}'
            )

        current_date_time = main_angle_wrapper_side.date_time

        for secondary_angle in angles_[1:]:
            secondary_angle_image_ = db.query_one(
                command="SELECT",
                columns="filepath",
                additional="ORDER BY date_time ASC",
                experiment=experiment_,
                plant=plant,
                angle=secondary_angle,
                date_time=dict(
                    operator="BETWEEN",
                    date_min=current_date_time - datetime.timedelta(hours=1),
                    date_max=current_date_time + datetime.timedelta(hours=1),
                ),
            )
            if secondary_angle_image_:
                secondary_angle_image_ = secondary_angle_image_[0]
            if secondary_angle_image_ and os.path.isfile(
                    secondary_angle_image_):
                secondary_angle_wrapper = BaseImageProcessor(
                    secondary_angle_image_)
                try:
                    secondary_angle_img = build_image(secondary_angle_wrapper)
                    main_angle_wrapper_side.store_image(
                        image=secondary_angle_img,
                        text=secondary_angle,
                        force_store=True)
                except Exception as e:
                    print(
                        f'Exception "{repr(e)}" while handling {str(secondary_angle_wrapper)}'
                    )

        mosaic = main_angle_wrapper_side.build_mosaic(
            (video_height, video_width, 3), angles_)
        cv2.putText(
            mosaic,
            current_date_time.strftime("%d/%m/%Y - %H:%M:%S"),
            (10, 1000),
            fnt,
            1,
            (255, 0, 255),
            2,
            cv2.LINE_AA,
        )

        # cv2.imwrite(
        #     os.path.join(os.path.dirname(p_output), main_angle_wrapper_side.luid + ".jpg"), mosaic
        # )

        def write_image_times(out_writer, img_, times=24):
            for _ in range(0, times):
                out_writer.write(img_)

        # Print source image
        write_image_times(out, mosaic)

    # Release everything if job is finished
    out.release()
    cv2.destroyAllWindows()

    return None
コード例 #2
0
ファイル: st_video.py プロジェクト: tpmp-inra/ipso_phen
def build_sbs_video():
    p_output = os.path.join(dst_folder, f'output_{"_".join(plants)}_.mp4')
    if os.path.isfile(p_output):
        return f"Plants {'_'.join(plants)} already handled"

    ret = current_database.query(
        command="SELECT",
        columns="filepath",
        additional="ORDER BY date_time ASC",
        date=dict(operator="IN", values=dates),
        experiment=experiment,
        plant=plants[0],
        angle=angle,
    )
    file_list_ = [item[0] for item in ret]

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    out = cv2.VideoWriter(p_output, fourcc, 24.0, (video_width, video_height))
    fnt = cv2.FONT_HERSHEY_DUPLEX

    total = len(file_list_)
    current_progress = st.progress(0)
    time_counter = 1
    for i, source_vis_plant in enumerate(file_list_):
        if not (source_vis_plant and os.path.isfile(source_vis_plant)):
            continue
        # Handle first image
        main_wrapper = BaseImageProcessor(source_vis_plant)
        try:
            img_main = build_image(main_wrapper)
            main_wrapper.store_image(image=img_main,
                                     text=plants[0],
                                     force_store=True)
        except Exception as e:
            print(
                f'Exception "{repr(e)}" while handling {str(source_vis_plant)}'
            )

        current_date_time = main_wrapper.date_time

        # Handle the rest
        has_missing_ = False
        for counter, ancillary_plant in enumerate(plants[1:]):
            file_name_ = current_database.query_one(
                command="SELECT",
                columns="filepath",
                additional="ORDER BY date_time ASC",
                experiment=experiment,
                plant=ancillary_plant,
                angle=angle,
                date_time=dict(
                    operator="BETWEEN",
                    date_min=current_date_time - datetime.timedelta(hours=10),
                    date_max=current_date_time + datetime.timedelta(hours=10),
                ),
            )
            if file_name_:
                file_name_ = file_name_[0]
            if file_name_ and os.path.isfile(file_name_):
                try:
                    main_wrapper.store_image(
                        image=build_image(BaseImageProcessor(file_name_)),
                        text=plants[counter + 1],
                        force_store=True,
                    )
                except Exception as e:
                    print(
                        f'Exception "{repr(e)}" while handling {str(file_name_)}'
                    )
            else:
                has_missing_ = True

        mosaic = main_wrapper.build_mosaic((video_height, video_width, 3),
                                           plants)

        # cv2.imwrite(
        #     os.path.join(os.path.dirname(p_output), f"{main_wrapper.plant}_{time_counter}.jpg"),
        #     mosaic,
        # )
        time_counter += 1

        def write_image_times(out_writer, img_, times=12):
            for _ in range(0, times):
                out_writer.write(img_)

        # Print source image
        write_image_times(out, mosaic)

        current_progress.progress((i + 1) / total)

    # Release everything if job is finished
    out.release()
    cv2.destroyAllWindows()