refactor: methods

This commit is contained in:
Artemy 2023-04-18 13:16:16 +03:00
parent 3f1d13c1c0
commit 09346cb114
6 changed files with 35 additions and 24 deletions

View file

@ -2,6 +2,8 @@ from PIL import Image, ImageFilter
from skimage import feature from skimage import feature
import numpy as np import numpy as np
methods = ["Sharpen", "Edge detection", "Canny edge detection"]
def edge(file_name): def edge(file_name):
image = Image.open(file_name) image = Image.open(file_name)
@ -22,3 +24,10 @@ def canny_edge(file_name):
def sharpen(file_name): def sharpen(file_name):
image = Image.open(file_name) image = Image.open(file_name)
return image.filter(ImageFilter.SHARPEN) return image.filter(ImageFilter.SHARPEN)
methods_funcs = {
"Sharpen": sharpen,
"Edge detection": edge,
"Canny edge detection": canny_edge,
}

View file

@ -2,6 +2,8 @@ from PIL import Image, ImageChops
from tqdm import tqdm from tqdm import tqdm
import numpy as np import numpy as np
methods = ["Denoise", "StarTracks", "Noise extractor", "Untrack"]
def denoise(files): def denoise(files):
images = [np.asarray(Image.open(file)) for file in tqdm(files)] images = [np.asarray(Image.open(file)) for file in tqdm(files)]
@ -36,3 +38,11 @@ def untrack(files):
image = im3 image = im3
return image return image
methods_funcs = {
"Denoise": denoise,
"StarTracks": startracks,
"Noise extractor": noise_extractor,
"Untrack": untrack,
}

View file

@ -1,7 +1,7 @@
import ffmpeg import ffmpeg
import os import os
from processing.utils import generate_name, get_date_text, generate_name_with_file_name from processing.utils import generate_name, get_date_text, generate_name_with_file_name
from methods.bulk_methods import edge, sharpen, canny_edge from methods.bulk_methods import methods_funcs
def images_to_video( def images_to_video(
@ -31,12 +31,7 @@ def video_to_images(video_path, img_ext):
def bulk_processing(directory, out_directory, method): def bulk_processing(directory, out_directory, method):
date = get_date_text() date = get_date_text()
if method == "Edge detection": run_bulk(methods_funcs[method], directory, out_directory, date)
run_bulk(edge, directory, out_directory, date)
elif method == "Canny edge detection":
run_bulk(canny_edge, directory, out_directory, date)
elif method == "Sharpen":
run_bulk(sharpen, directory, out_directory, date)
def run_bulk(func, directory, out_directory, date): def run_bulk(func, directory, out_directory, date):

View file

@ -1,4 +1,6 @@
from methods.stack_methods import denoise, startracks, noise_extractor, untrack from methods.stack_methods import (
methods_funcs,
)
import os import os
from processing.utils import generate_name from processing.utils import generate_name
@ -6,16 +8,8 @@ from processing.utils import generate_name
def stacking(dir, method): def stacking(dir, method):
files = os.listdir(dir) files = os.listdir(dir)
files = list(map(lambda x: os.path.join(dir, x), files)) files = list(map(lambda x: os.path.join(dir, x), files))
files = list(filter(lambda x: x.endswith(".png"), files))
if method == "denoise": img = methods_funcs[method](files)
img = denoise(files)
elif method == "startracks":
img = startracks(files)
elif method == "noise extractor":
img = noise_extractor(files)
elif method == "untrack":
img = untrack(files)
name = generate_name() name = generate_name()
img.save(name) img.save(name)

View file

@ -1,5 +1,6 @@
import gradio as gr import gradio as gr
from processing.bulk import bulk_processing from processing.bulk import bulk_processing
from methods.bulk_methods import methods
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown( gr.Markdown(
@ -11,8 +12,8 @@ with gr.Blocks() as app:
placeholder="A directory with many images.", lines=1, label="Directory" placeholder="A directory with many images.", lines=1, label="Directory"
) )
method = gr.Dropdown( method = gr.Dropdown(
choices=["Edge detection", "Canny edge detection", "sharpen"], choices=methods,
value="Sharpen", value=methods[0],
label="Method", label="Method",
) )

View file

@ -1,18 +1,20 @@
import gradio as gr import gradio as gr
from processing.stacking import stacking from processing.stacking import stacking
from methods.stack_methods import methods
with gr.Blocks() as app: with gr.Blocks() as app:
gr.Markdown("Stacking images.") gr.Markdown("Stacking images.")
with gr.Row(): with gr.Row():
with gr.Column(): with gr.Column():
directory = gr.Text( directory = gr.Text(
placeholder="A directory with many images of the same size", lines=1, label="Directory") placeholder="A directory with many images of the same size",
methods = gr.Dropdown( lines=1,
choices=["denoise", "startracks", "noise extractor", "untrack"], value="denoise", label="Method") label="Directory",
)
method = gr.Dropdown(choices=methods, value=methods[0], label="Method")
submit = gr.Button("Submit") submit = gr.Button("Submit")
with gr.Column(): with gr.Column():
output = gr.Gallery() output = gr.Gallery()
submit.click(stacking, inputs=[ submit.click(stacking, inputs=[directory, method], outputs=[output])
directory, methods], outputs=[output])