dxue321's picture
modify title
f398833
import gradio as gr
from PIL import Image
from multi_image_process import compute_inp_palette, recolor_single_image, recolor_group_images
example_images = ["./examples/flower/001.jpg",
"./examples/flower/002.jpg",
"./examples/flower/003.jpg",
"./examples/flower/004.jpg",
"./examples/flower/005.jpg",
]
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
def show_palette(*colors):
# create HTML,each color is a block with a border
color_blocks = ""
for color in colors:
if color: # if choose a color
color_blocks += f'<div style="width:40px;height:40px;background:{color};display:inline-block;margin:5px;border:1px solid #000;"></div>'
return color_blocks
def load_example_images():
images = [Image.open(p) for p in example_images]
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(value=example_images, visible=False)
if __name__ == "__main__":
with gr.Blocks() as demo:
with gr.Row():
gr.Markdown("# Palette-based Multi-image Recoloring")
with gr.Row():
gr.Markdown("A demo of paper [Integrating High‐Level Features for Consistent Palette‐based Multi‐image Recoloring](https://onlinelibrary.wiley.com/doi/pdf/10.1111/cgf.14964).")
# multiple image recoloring for color consistency
with gr.Row():
with gr.Column():
gr.Markdown("### Inputs")
image_input = gr.File(
label="Drag (Select) more than one photos",
file_types=["image"],
file_count="multiple"
)
uploaded_files = gr.Gallery(label="Input images", visible=False, columns=7, rows=1, height=250)
with gr.Column(visible=False) as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=image_input, size="sm")
image_input.upload(fn=swap_to_gallery, inputs=image_input, outputs=[uploaded_files, clear_button, image_input])
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, image_input])
gr.Markdown("### Select the parameters for recoloring")
with gr.Group():
with gr.Group():
gr.Markdown("### Recoloring without other techiques")
num_center_grp = gr.Dropdown(choices=[1, 2, 3, 4, 5, 6, 7], value=3, label="Number of group palettes")
# num_center_grp = gr.Slider(label="Number of group palettes", minimum=1, maximum=7, value=3, step=1)
with gr.Group():
gr.Markdown("### Recoloring with other techiques")
gr.Markdown("")
with gr.Group():
gr.Markdown("<center>white balance<center>")
checkbox_input_wb = gr.Checkbox(value=False, label="Apply white balance correction")
with gr.Group():
gr.Markdown("<center>saliency detection<center>")
with gr.Row():
checkbox_input_sal = gr.Checkbox(value=False, label="Apply saliency")
sal_method = gr.Dropdown(choices=['LDF (accurate)','ft (fast)','rbd (fast)'], value='LDF (accurate)', label="Saliency detection method")
with gr.Row():
num_center_sal = gr.Dropdown(choices=[1, 2, 3, 4], value=1, label="Number of salient palettes")
num_center_nonsal = gr.Dropdown(choices=[1, 2, 3, 4], value=1, label="Number of non-salient palettes")
# num_center_sal = gr.Slider(label="Number of salient palettes", minimum=1, maximum=4, value=1, step=1)
# num_center_nonsal = gr.Slider(label="Number of non-salient palettes", minimum=1, maximum=4, value=1, step=1)
with gr.Row():
checkbox_input_recolor_sal = gr.Checkbox(value=False, label="Recolor salient colors only")
checkbox_input_recolor_nonsal = gr.Checkbox(value=False, label="Recolor non-salient colors only")
with gr.Group():
gr.Markdown("<center>color naming<center>")
with gr.Row():
checkbox_input_cn = gr.Checkbox(value=False, label="Apply color naming")
naming_thres = gr.Textbox(value=0.8, label="Threshold of color naming", placeholder=0.8)
with gr.Column():
gr.Markdown("### Outputs")
output_gallery_palette_in = gr.Gallery(label="Input image palettes", columns=7, rows=1, height=100)
output_gallery_palette_group= gr.Gallery(label="Group palette", columns=2, rows=1, height=100)
output_gallery_palette_out = gr.Gallery(label="Output image palettes", columns=7, rows=1, height=100)
output_gallery_recolor = gr.Gallery(label="Recolored images", columns=7, rows=1, height=300)
with gr.Row():
example_btn = gr.Button("Load Example Images")
example_btn.click(fn=load_example_images, outputs=[uploaded_files, clear_button, image_input])
palette_btn = gr.Button("Compute palette").click(
compute_inp_palette,
inputs=[image_input],
outputs=[output_gallery_palette_in]
)
# recoloring_multi_btn = gr.Button("Recoloring multiple images").click(
# multi_img_color_consist,
# inputs=[image_input, num_center_grp, checkbox_input_wb, checkbox_input_sal, checkbox_input_cn, naming_thres],
# outputs=[output_gallery_recolor, output_gallery_palette_in, output_gallery_palette_out, output_gallery_palette_group]
# )
recoloring_multi_btn = gr.Button("Recoloring multiple images").click(
recolor_group_images,
inputs=[image_input, num_center_grp, num_center_sal, num_center_nonsal,
checkbox_input_wb,
checkbox_input_sal, sal_method, checkbox_input_recolor_sal, checkbox_input_recolor_nonsal,
checkbox_input_cn, naming_thres],
outputs=[output_gallery_recolor, output_gallery_palette_in, output_gallery_palette_out, output_gallery_palette_group]
)
# # single image recoloring with user-defined palette
# with gr.Row():
# with gr.Column():
# gr.Markdown("### Image color categorization")
# with gr.Row():
# color1 = gr.ColorPicker(label="Color 1", value="#ff0000")
# color2 = gr.ColorPicker(label="Color 2", value="#00ff00")
# color3 = gr.ColorPicker(label="Color 3", value="#0000ff")
# color4 = gr.ColorPicker(label="Color 4", value=None)
# color5 = gr.ColorPicker(label="Color 5", value=None)
# palette = gr.HTML()
# with gr.Row():
# btn = gr.Button("Show Picked Palette")
# btn.click(fn=show_palette, inputs=[color1, color2, color3, color4, color5], outputs=palette)
# with gr.Column():
# gr.Markdown("### Output image")
# output_gallery_recolor_single = gr.Gallery(label="Recolored image", columns=1, rows=1, height=100)
# recoloring_single_btn = gr.Button("Recoloring single images").click(
# recolor_single_image,
# inputs=[image_input, color1, color2, color3, color4, color5],
# outputs=[output_gallery_recolor_single]
# )
demo.launch()