From 45916cd32006fccfc89d5701c815389d44748675 Mon Sep 17 00:00:00 2001 From: Artemy Date: Sun, 16 Apr 2023 10:36:29 +0300 Subject: [PATCH] feat: bulk processing --- methods/bulk_methods.py | 9 +++++++++ processing/bulk.py | 18 ++++++++++++++++-- processing/utils.py | 7 +++++++ tabs/Bulk processing/app.py | 16 ++++++++++++---- 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 methods/bulk_methods.py diff --git a/methods/bulk_methods.py b/methods/bulk_methods.py new file mode 100644 index 0000000..8c6ddc0 --- /dev/null +++ b/methods/bulk_methods.py @@ -0,0 +1,9 @@ +from PIL import Image, ImageFilter + + +def canny_edge(file_name): + image = Image.open(file_name) + + image = image.convert("L") + + return image.filter(ImageFilter.FIND_EDGES) diff --git a/processing/bulk.py b/processing/bulk.py index bc8ab79..40c4a0c 100644 --- a/processing/bulk.py +++ b/processing/bulk.py @@ -1,7 +1,8 @@ -from PIL import Image, ImageFilter + import ffmpeg import os -from processing.utils import generate_name, get_date_text +from processing.utils import generate_name, get_date_text, generate_name_with_file_name +from methods.bulk_methods import canny_edge def images_to_video(directory, fps, img_ext, img_name_format, video_name, video_ext, video_dir): @@ -23,3 +24,16 @@ def video_to_images(video_path, img_ext): extension=img_ext, name=f"%d", subfolder=os.path.join("images", get_date_text())) ffmpeg.input(video_path).output(images_pattern).run() + + +def bulk_processing(directory, out_directory, method): + date = get_date_text() + if method == "canny edge": + for file in os.listdir(directory): + img = canny_edge(os.path.join(directory, file)) + if out_directory: + img_out_path = os.path.join(out_directory, file) + else: + img_out_path = generate_name_with_file_name( + name=file, subfolder=os.path.join("images", date)) + img.save(img_out_path) diff --git a/processing/utils.py b/processing/utils.py index dd26018..8f75950 100644 --- a/processing/utils.py +++ b/processing/utils.py @@ -12,5 +12,12 @@ def generate_name(name=False, subfolder="stacked", extension="png", format='%Y-% return os.path.join(".", "output", subfolder, f"{name}.{extension}") +def generate_name_with_file_name(name, subfolder): + + os.makedirs(os.path.join(".", "output", subfolder), exist_ok=True) + + return os.path.join(".", "output", subfolder, name) + + def get_date_text(format='%Y-%m-%d_%H-%M-%S'): return datetime.datetime.now().strftime(format) diff --git a/tabs/Bulk processing/app.py b/tabs/Bulk processing/app.py index 8045c68..f1a86d0 100644 --- a/tabs/Bulk processing/app.py +++ b/tabs/Bulk processing/app.py @@ -1,5 +1,5 @@ import gradio as gr -from processing.bulk import images_to_video +from processing.bulk import bulk_processing with gr.Blocks() as app: gr.Markdown( @@ -7,7 +7,15 @@ with gr.Blocks() as app: with gr.Row(): with gr.Column(): directory = gr.Text( - placeholder="A directory with many images of the same size", lines=1, label="Directory") - methods = gr.Dropdown( - choices=["denoise", "startracks", "noise extractor", "untrack"], value="denoise", label="Method") + placeholder="A directory with many images.", lines=1, label="Directory") + method = gr.Dropdown( + choices=["canny edge"], value="canny edge", label="Method") + + with gr.Accordion("Advanced settings", open=False) as acc: + out_dir = gr.Text( + label="Output directory", placeholder="The directory where the processed photos will be saved. If not specified `./output/images`") submit = gr.Button("Submit") + submit.click( + fn=bulk_processing, + inputs=[directory, out_dir, method], + )