feat: bulk processing

This commit is contained in:
Artemy 2023-04-16 10:36:29 +03:00
parent eb26452ad6
commit 45916cd320
4 changed files with 44 additions and 6 deletions

9
methods/bulk_methods.py Normal file
View file

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

View file

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

View file

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

View file

@ -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],
)