Superlang commited on
Commit
047d71d
1 Parent(s): be8e1a1
config/annotator.yaml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ alpha:
2
+ process: AlphaComposite
3
+ input: [ ]
example/00019-1224130474.png ADDED
example/bg.png ADDED
example/product.png ADDED
main.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib
2
+ import os
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from omegaconf import OmegaConf
6
+
7
+ config = OmegaConf.load("config/annotator.yaml")
8
+
9
+ package_annotator = "processor"
10
+
11
+
12
+ def process_image(cls: str, fg: Image.Image, bg: Image.Image, *kwargs):
13
+ if fg.size != bg.size:
14
+ fg = fg.resize(bg.size)
15
+ module_imp = importlib.import_module(package_annotator)
16
+ model = getattr(module_imp, cls)
17
+ image_processor = model()
18
+ result = image_processor(fg, bg, *kwargs)
19
+ if type(result) == tuple:
20
+ return result
21
+ return [result]
22
+
23
+
24
+ def process(cls):
25
+ def process_fc(img, res, *args):
26
+ return process_image(cls, img, res, *args)
27
+
28
+ return process_fc
29
+
30
+
31
+ block = gr.Blocks().queue()
32
+ examples = [[os.path.join(os.path.dirname(__file__), "example/product.png"),
33
+ os.path.join(os.path.dirname(__file__), "example/bg.png")]]
34
+ with block:
35
+ for key in config.keys():
36
+ cls, input_element = config[key]["process"], config[key].get("input")
37
+ input_append = []
38
+ with gr.Tab(key):
39
+ with gr.Row():
40
+ gr.Markdown("## " + key)
41
+ with gr.Row():
42
+ with gr.Column():
43
+ input_image = gr.Image(label="foreground", source='upload', type="pil", image_mode="RGBA")
44
+ bg_image = gr.Image(label="background", source='upload', type="pil", image_mode="RGBA")
45
+ if input_element is not None:
46
+ for item in input_element:
47
+ input_append.append(getattr(gr, item["attr"])(**item["args"]))
48
+ run_button = gr.Button(label="Run")
49
+ gr.Examples(examples, [input_image, bg_image])
50
+ with gr.Column():
51
+ gallery = gr.Gallery(label="Generated images", show_label=False).style(height="auto")
52
+
53
+ run_button.click(fn=process(cls),
54
+ inputs=[input_image, bg_image] + input_append,
55
+ outputs=[gallery])
56
+
57
+ block.launch(server_port=7867)
processor/AlphaProcessor.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+
3
+
4
+ class AlphaComposite:
5
+ def __call__(self, im_rgb, bg_img):
6
+ bg_img = bg_img.convert("RGBA")
7
+ im_rgb = im_rgb.convert("RGBA")
8
+ result = Image.alpha_composite(bg_img, im_rgb)
9
+ return result
10
+
11
+
processor/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .AlphaProcessor import AlphaComposite
2
+ from .OriginalProcessor import OriginalComposite
3
+ from .QuadtreeProcessor import QuadtreeComposite
4
+ from .PoissonProcessor import PoissonComposite
5
+
6
+ __all__ = [
7
+ AlphaComposite,
8
+ OriginalComposite,
9
+ QuadtreeComposite,
10
+ PoissonComposite
11
+ ]