Spaces:
Running
Running
import gradio as gr | |
import os | |
import pathlib | |
from demo.locals import LOCALES | |
from hivision.creator.choose_handler import FACE_DETECT_MODELS | |
def load_description(fp): | |
with open(fp, "r", encoding="utf-8") as f: | |
content = f.read() | |
return content | |
def create_ui( | |
processor, root_dir, human_matting_models: list, face_detect_models: list | |
): | |
DEFAULT_LANG = "zh" | |
DEFAULT_HUMAN_MATTING_MODEL = "modnet_photographic_portrait_matting" | |
DEFAULT_FACE_DETECT_MODEL = "mtcnn" | |
if DEFAULT_HUMAN_MATTING_MODEL in human_matting_models: | |
human_matting_models.remove(DEFAULT_HUMAN_MATTING_MODEL) | |
human_matting_models.insert(0, DEFAULT_HUMAN_MATTING_MODEL) | |
css = """ | |
#col-left { | |
margin: 0 auto; | |
max-width: 430px; | |
} | |
#col-mid { | |
margin: 0 auto; | |
max-width: 430px; | |
} | |
#col-right { | |
margin: 0 auto; | |
max-width: 430px; | |
} | |
#col-showcase { | |
margin: 0 auto; | |
max-width: 1100px; | |
} | |
#button { | |
color: blue; | |
} | |
""" | |
demo = gr.Blocks(title="HivisionIDPhotos", css=css) | |
with demo: | |
gr.HTML(load_description(os.path.join(root_dir, "assets/title.md"))) | |
with gr.Row(): | |
# ------------ 左半边 UI ---------------- | |
with gr.Column(): | |
img_input = gr.Image(height=400) | |
with gr.Row(): | |
# 语言选择器 | |
language = ["zh", "en"] | |
language_options = gr.Dropdown( | |
choices=language, | |
label="Language", | |
value=DEFAULT_LANG, | |
) | |
face_detect_model_options = gr.Dropdown( | |
choices=face_detect_models, | |
label=LOCALES["face_model"][DEFAULT_LANG]["label"], | |
value=DEFAULT_FACE_DETECT_MODEL, | |
) | |
matting_model_options = gr.Dropdown( | |
choices=human_matting_models, | |
label=LOCALES["matting_model"][DEFAULT_LANG]["label"], | |
value=human_matting_models[0], | |
) | |
with gr.Tab( | |
LOCALES["key_param"][DEFAULT_LANG]["label"] | |
) as key_parameter_tab: | |
mode_options = gr.Radio( | |
choices=LOCALES["size_mode"][DEFAULT_LANG]["choices"], | |
label=LOCALES["size_mode"][DEFAULT_LANG]["label"], | |
value=LOCALES["size_mode"][DEFAULT_LANG]["choices"][0], | |
) | |
with gr.Row(visible=True) as size_list_row: | |
size_list_options = gr.Dropdown( | |
choices=LOCALES["size_list"][DEFAULT_LANG]["choices"], | |
label="预设尺寸", | |
value=LOCALES["size_list"][DEFAULT_LANG]["choices"][0], | |
elem_id="size_list", | |
) | |
with gr.Row(visible=False) as custom_size: | |
custom_size_height = gr.Number( | |
value=413, label="height", interactive=True | |
) | |
custom_size_width = gr.Number( | |
value=295, label="width", interactive=True | |
) | |
color_options = gr.Radio( | |
choices=LOCALES["bg_color"][DEFAULT_LANG]["choices"], | |
label=LOCALES["bg_color"][DEFAULT_LANG]["label"], | |
value=LOCALES["bg_color"][DEFAULT_LANG]["choices"][0], | |
) | |
with gr.Row(visible=False) as custom_color: | |
custom_color_R = gr.Number(value=0, label="R", interactive=True) | |
custom_color_G = gr.Number(value=0, label="G", interactive=True) | |
custom_color_B = gr.Number(value=0, label="B", interactive=True) | |
render_options = gr.Radio( | |
choices=LOCALES["render_mode"][DEFAULT_LANG]["choices"], | |
label=LOCALES["render_mode"][DEFAULT_LANG]["label"], | |
value=LOCALES["render_mode"][DEFAULT_LANG]["choices"][0], | |
) | |
with gr.Tab( | |
LOCALES["advance_param"][DEFAULT_LANG]["label"] | |
) as advance_parameter_tab: | |
head_measure_ratio_option = gr.Slider( | |
minimum=0.1, | |
maximum=0.5, | |
value=0.2, | |
step=0.01, | |
label=LOCALES["head_measure_ratio"][DEFAULT_LANG]["label"], | |
interactive=True, | |
) | |
top_distance_option = gr.Slider( | |
minimum=0.02, | |
maximum=0.5, | |
value=0.12, | |
step=0.01, | |
label=LOCALES["top_distance"][DEFAULT_LANG]["label"], | |
interactive=True, | |
) | |
image_kb_options = gr.Radio( | |
choices=LOCALES["image_kb"][DEFAULT_LANG]["choices"], | |
label=LOCALES["image_kb"][DEFAULT_LANG]["label"], | |
value=LOCALES["image_kb"][DEFAULT_LANG]["choices"][0], | |
) | |
with gr.Row(visible=False) as custom_image_kb: | |
custom_image_kb_size = gr.Slider( | |
minimum=10, | |
maximum=1000, | |
value=50, | |
label=LOCALES["image_kb_size"][DEFAULT_LANG]["label"], | |
interactive=True, | |
) | |
with gr.Tab( | |
LOCALES["watermark_tab"][DEFAULT_LANG]["label"] | |
) as watermark_parameter_tab: | |
watermark_options = gr.Radio( | |
choices=LOCALES["watermark_switch"][DEFAULT_LANG]["choices"], | |
label=LOCALES["watermark_switch"][DEFAULT_LANG]["label"], | |
value=LOCALES["watermark_switch"][DEFAULT_LANG]["choices"][0], | |
) | |
with gr.Row(): | |
watermark_text_options = gr.Text( | |
max_length=20, | |
label=LOCALES["watermark_text"][DEFAULT_LANG]["label"], | |
value=LOCALES["watermark_text"][DEFAULT_LANG]["value"], | |
placeholder=LOCALES["watermark_text"][DEFAULT_LANG][ | |
"placeholder" | |
], | |
interactive=False, | |
) | |
watermark_text_color = gr.ColorPicker( | |
label=LOCALES["watermark_color"][DEFAULT_LANG]["label"], | |
interactive=False, | |
value="#FFFFFF", | |
) | |
watermark_text_size = gr.Slider( | |
minimum=10, | |
maximum=100, | |
value=20, | |
label=LOCALES["watermark_size"][DEFAULT_LANG]["label"], | |
interactive=False, | |
step=1, | |
) | |
watermark_text_opacity = gr.Slider( | |
minimum=0, | |
maximum=1, | |
value=0.15, | |
label=LOCALES["watermark_opacity"][DEFAULT_LANG]["label"], | |
interactive=False, | |
step=0.01, | |
) | |
watermark_text_angle = gr.Slider( | |
minimum=0, | |
maximum=360, | |
value=30, | |
label=LOCALES["watermark_angle"][DEFAULT_LANG]["label"], | |
interactive=False, | |
step=1, | |
) | |
watermark_text_space = gr.Slider( | |
minimum=10, | |
maximum=200, | |
value=25, | |
label=LOCALES["watermark_space"][DEFAULT_LANG]["label"], | |
interactive=False, | |
step=1, | |
) | |
def update_watermark_text_visibility(choice, language): | |
return [ | |
gr.update( | |
interactive=( | |
choice | |
== LOCALES["watermark_switch"][language]["choices"][ | |
1 | |
] | |
) | |
) | |
] * 6 | |
watermark_options.change( | |
fn=update_watermark_text_visibility, | |
inputs=[watermark_options, language_options], | |
outputs=[ | |
watermark_text_options, | |
watermark_text_color, | |
watermark_text_size, | |
watermark_text_opacity, | |
watermark_text_angle, | |
watermark_text_space, | |
], | |
) | |
img_but = gr.Button(LOCALES["button"][DEFAULT_LANG]["label"]) | |
example_images = gr.Examples( | |
inputs=[img_input], | |
examples=[ | |
[path.as_posix()] | |
for path in sorted( | |
pathlib.Path(os.path.join(root_dir, "demo/images")).rglob( | |
"*.jpg" | |
) | |
) | |
], | |
) | |
# ---------------- 右半边 UI ---------------- | |
with gr.Column(): | |
notification = gr.Text( | |
label=LOCALES["notification"][DEFAULT_LANG]["label"], visible=False | |
) | |
with gr.Row(): | |
img_output_standard = gr.Image( | |
label=LOCALES["standard_photo"][DEFAULT_LANG]["label"], | |
height=350, | |
format="jpeg", | |
) | |
img_output_standard_hd = gr.Image( | |
label=LOCALES["hd_photo"][DEFAULT_LANG]["label"], | |
height=350, | |
format="jpeg", | |
) | |
img_output_layout = gr.Image( | |
label=LOCALES["layout_photo"][DEFAULT_LANG]["label"], | |
height=350, | |
format="jpeg", | |
) | |
file_download = gr.File( | |
label=LOCALES["download"][DEFAULT_LANG]["label"], visible=False | |
) | |
# ---------------- 设置隐藏/显示组件 ---------------- | |
def change_language(language): | |
return { | |
face_detect_model_options: gr.update( | |
label=LOCALES["face_model"][language]["label"] | |
), | |
matting_model_options: gr.update( | |
label=LOCALES["matting_model"][language]["label"] | |
), | |
size_list_options: gr.update( | |
label=LOCALES["size_list"][language]["label"], | |
choices=LOCALES["size_list"][language]["choices"], | |
value=LOCALES["size_list"][language]["choices"][0], | |
), | |
mode_options: gr.update( | |
label=LOCALES["size_mode"][language]["label"], | |
choices=LOCALES["size_mode"][language]["choices"], | |
value=LOCALES["size_mode"][language]["choices"][0], | |
), | |
color_options: gr.update( | |
label=LOCALES["bg_color"][language]["label"], | |
choices=LOCALES["bg_color"][language]["choices"], | |
value=LOCALES["bg_color"][language]["choices"][0], | |
), | |
img_but: gr.update(value=LOCALES["button"][language]["label"]), | |
render_options: gr.update( | |
label=LOCALES["render_mode"][language]["label"], | |
choices=LOCALES["render_mode"][language]["choices"], | |
value=LOCALES["render_mode"][language]["choices"][0], | |
), | |
image_kb_options: gr.update( | |
label=LOCALES["image_kb_size"][language]["label"], | |
choices=LOCALES["image_kb"][language]["choices"], | |
value=LOCALES["image_kb"][language]["choices"][0], | |
), | |
custom_image_kb_size: gr.update( | |
label=LOCALES["image_kb"][language]["label"] | |
), | |
notification: gr.update( | |
label=LOCALES["notification"][language]["label"] | |
), | |
img_output_standard: gr.update( | |
label=LOCALES["standard_photo"][language]["label"] | |
), | |
img_output_standard_hd: gr.update( | |
label=LOCALES["hd_photo"][language]["label"] | |
), | |
img_output_layout: gr.update( | |
label=LOCALES["layout_photo"][language]["label"] | |
), | |
file_download: gr.update( | |
label=LOCALES["download"][language]["label"] | |
), | |
head_measure_ratio_option: gr.update( | |
label=LOCALES["head_measure_ratio"][language]["label"] | |
), | |
top_distance_option: gr.update( | |
label=LOCALES["top_distance"][language]["label"] | |
), | |
key_parameter_tab: gr.update( | |
label=LOCALES["key_param"][language]["label"] | |
), | |
advance_parameter_tab: gr.update( | |
label=LOCALES["advance_param"][language]["label"] | |
), | |
watermark_parameter_tab: gr.update( | |
label=LOCALES["watermark_tab"][language]["label"] | |
), | |
watermark_text_options: gr.update( | |
label=LOCALES["watermark_text"][language]["label"], | |
placeholder=LOCALES["watermark_text"][language]["placeholder"], | |
), | |
watermark_text_color: gr.update( | |
label=LOCALES["watermark_color"][language]["label"] | |
), | |
watermark_text_size: gr.update( | |
label=LOCALES["watermark_size"][language]["label"] | |
), | |
watermark_text_opacity: gr.update( | |
label=LOCALES["watermark_opacity"][language]["label"] | |
), | |
watermark_text_angle: gr.update( | |
label=LOCALES["watermark_angle"][language]["label"] | |
), | |
watermark_text_space: gr.update( | |
label=LOCALES["watermark_space"][language]["label"] | |
), | |
watermark_options: gr.update( | |
label=LOCALES["watermark_switch"][language]["label"], | |
choices=LOCALES["watermark_switch"][language]["choices"], | |
value=LOCALES["watermark_switch"][language]["choices"][0], | |
), | |
} | |
def change_color(colors): | |
if colors == "自定义底色" or colors == "Custom Color": | |
return {custom_color: gr.update(visible=True)} | |
else: | |
return {custom_color: gr.update(visible=False)} | |
def change_size_mode(size_option_item): | |
if ( | |
size_option_item == "自定义尺寸" | |
or size_option_item == "Custom Size" | |
): | |
return { | |
custom_size: gr.update(visible=True), | |
size_list_row: gr.update(visible=False), | |
} | |
elif ( | |
size_option_item == "只换底" | |
or size_option_item == "Only Change Background" | |
): | |
return { | |
custom_size: gr.update(visible=False), | |
size_list_row: gr.update(visible=False), | |
} | |
else: | |
return { | |
custom_size: gr.update(visible=False), | |
size_list_row: gr.update(visible=True), | |
} | |
def change_image_kb(image_kb_option): | |
if image_kb_option == "自定义" or image_kb_option == "Custom": | |
return {custom_image_kb: gr.update(visible=True)} | |
else: | |
return {custom_image_kb: gr.update(visible=False)} | |
# ---------------- 绑定事件 ---------------- | |
language_options.input( | |
change_language, | |
inputs=[language_options], | |
outputs=[ | |
size_list_options, | |
mode_options, | |
color_options, | |
img_but, | |
render_options, | |
image_kb_options, | |
matting_model_options, | |
face_detect_model_options, | |
custom_image_kb_size, | |
notification, | |
img_output_standard, | |
img_output_standard_hd, | |
img_output_layout, | |
file_download, | |
head_measure_ratio_option, | |
top_distance_option, | |
key_parameter_tab, | |
advance_parameter_tab, | |
watermark_parameter_tab, | |
watermark_text_options, | |
watermark_text_color, | |
watermark_text_size, | |
watermark_text_opacity, | |
watermark_text_angle, | |
watermark_text_space, | |
watermark_options, | |
], | |
) | |
color_options.input( | |
change_color, inputs=[color_options], outputs=[custom_color] | |
) | |
mode_options.input( | |
change_size_mode, | |
inputs=[mode_options], | |
outputs=[custom_size, size_list_row], | |
) | |
image_kb_options.input( | |
change_image_kb, inputs=[image_kb_options], outputs=[custom_image_kb] | |
) | |
img_but.click( | |
processor.process, | |
inputs=[ | |
img_input, | |
mode_options, | |
size_list_options, | |
color_options, | |
render_options, | |
image_kb_options, | |
custom_color_R, | |
custom_color_G, | |
custom_color_B, | |
custom_size_height, | |
custom_size_width, | |
custom_image_kb_size, | |
language_options, | |
matting_model_options, | |
watermark_options, | |
watermark_text_options, | |
watermark_text_color, | |
watermark_text_size, | |
watermark_text_opacity, | |
watermark_text_angle, | |
watermark_text_space, | |
face_detect_model_options, | |
head_measure_ratio_option, | |
top_distance_option, | |
], | |
outputs=[ | |
img_output_standard, | |
img_output_standard_hd, | |
img_output_layout, | |
notification, | |
file_download, | |
], | |
) | |
return demo | |