Superlang/ImageComposition
2
1import importlib2import os3from PIL import Image4import gradio as gr5from omegaconf import OmegaConf6 7config = OmegaConf.load("config/annotator.yaml")8 9package_annotator = "processor"10 11 12def 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 result21 return [result]22 23 24def process(cls):25 def process_fc(img, res, *args):26 return process_image(cls, img, res, *args)27 28 return process_fc29 30 31block = gr.Blocks().queue()32examples = [[os.path.join(os.path.dirname(__file__), "example/product.png"),33 os.path.join(os.path.dirname(__file__), "example/bg.png")]]34with 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 57block.launch()58 