Spaces:
Running
Running
File size: 37,100 Bytes
cc3e5bb d031867 ca46a75 23cd1cf ca46a75 1c25fe3 d031867 ca46a75 cc3e5bb 929aa8b cc3e5bb 72049af cc3e5bb ca46a75 5ea080f cc3e5bb 5ea080f 796a686 5ea080f d031867 cc3e5bb 796a686 cc3e5bb d031867 cc3e5bb d031867 5ea080f cc3e5bb 59bff44 929aa8b 23cd1cf 5ea080f 3cdc8a1 5ea080f 3cdc8a1 d031867 5ea080f d031867 cc3e5bb 23cd1cf cc3e5bb 23cd1cf cc3e5bb d031867 cc3e5bb d031867 cc3e5bb d031867 5ea080f d031867 5ea080f cc3e5bb 5ea080f d031867 cc3e5bb 5ea080f d031867 cc3e5bb d031867 cc3e5bb 5ea080f ca46a75 1c25fe3 23cd1cf ca46a75 5ea080f ca46a75 3cdc8a1 ca46a75 d031867 23cd1cf cc3e5bb 23cd1cf cc3e5bb d031867 f232fd6 cc3e5bb d031867 5ea080f cc3e5bb d031867 5ea080f d031867 5ea080f d031867 5ea080f d031867 5ea080f d031867 929aa8b cc3e5bb d031867 929aa8b d031867 5ea080f 929aa8b 5ea080f cc3e5bb 5ea080f cc3e5bb 5ea080f ca46a75 5ea080f 929aa8b 5ea080f ca46a75 5ea080f ca46a75 5ea080f d031867 ca46a75 d031867 929aa8b d031867 1c25fe3 59bff44 cc3e5bb 59bff44 1c25fe3 59bff44 23cd1cf 59bff44 cc3e5bb 796a686 cc3e5bb 929aa8b cc3e5bb d031867 5ea080f ca46a75 d031867 1c25fe3 d031867 ca46a75 d031867 cc3e5bb d031867 ca46a75 59bff44 23cd1cf 59bff44 23cd1cf 59bff44 cc3e5bb 3cdc8a1 5ea080f d031867 3cdc8a1 5ea080f 3cdc8a1 5ea080f 3cdc8a1 5ea080f 3cdc8a1 5ea080f 3cdc8a1 06fbec3 3cdc8a1 5ea080f 3cdc8a1 06fbec3 3cdc8a1 5ea080f 929aa8b ca46a75 d031867 ca46a75 5ea080f cc3e5bb ca46a75 cc3e5bb 5ea080f cc3e5bb d031867 ca46a75 d031867 bc95f5c ca46a75 d031867 5ea080f cc3e5bb ca46a75 cc3e5bb 23cd1cf cc3e5bb 3cdc8a1 929aa8b cc3e5bb 59bff44 cc3e5bb ca46a75 cc3e5bb 23cd1cf cc3e5bb 3cdc8a1 929aa8b cc3e5bb d031867 cc3e5bb d031867 cc3e5bb 5ea080f cc3e5bb 5ea080f cc3e5bb 5ea080f d031867 5ea080f cc3e5bb 23cd1cf cc3e5bb 3cdc8a1 929aa8b cc3e5bb 5ea080f cc3e5bb 59bff44 929aa8b 23cd1cf 3cdc8a1 5ea080f 23cd1cf 1c25fe3 23cd1cf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
import os
import gradio as gr
from hivision import IDCreator
from hivision.error import FaceError, APIError
from hivision.utils import add_background, resize_image_to_kb
from hivision.creator.layout_calculator import (
generate_layout_photo,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
import pathlib
import numpy as np
from demo.utils import csv_to_size_list
import argparse
from PIL import Image
from hivision.plugin.watermark import Watermarker, WatermarkerStyles
# 获取尺寸列表
root_dir = os.path.dirname(os.path.abspath(__file__))
size_list_dict_CN = csv_to_size_list(os.path.join(root_dir, "demo/size_list_CN.csv"))
size_list_dict_EN = csv_to_size_list(os.path.join(root_dir, "demo/size_list_EN.csv"))
color_list_dict_CN = {
"蓝色": (86, 140, 212),
"白色": (255, 255, 255),
"红色": (233, 51, 35),
"黑色": (0, 0, 0),
"深蓝色": (69, 98, 148),
}
color_list_dict_EN = {
"Blue": (86, 140, 212),
"White": (255, 255, 255),
"Red": (233, 51, 35),
"Black": (0, 0, 0),
"Dark blue": (69, 98, 148),
}
# 检测 RGB 是否超出范围,如果超出则约束到 0~255 之间
def range_check(value, min_value=0, max_value=255):
value = int(value)
if value <= min_value:
value = min_value
elif value > max_value:
value = max_value
return value
def idphoto_inference(
input_image,
mode_option,
size_list_option,
color_option,
render_option,
image_kb_options,
custom_color_R,
custom_color_G,
custom_color_B,
custom_size_height,
custom_size_width,
custom_image_kb,
language,
matting_model_option,
watermark_option,
watermark_text,
watermark_text_color,
watermark_text_size,
watermark_text_opacity,
watermark_text_angle,
watermark_text_space,
face_detect_option,
head_measure_ratio=0.2,
# head_height_ratio=0.45,
top_distance_max=0.12,
top_distance_min=0.10,
):
top_distance_min = top_distance_max - 0.02
idphoto_json = {
"size_mode": mode_option,
"color_mode": color_option,
"render_mode": render_option,
"image_kb_mode": image_kb_options,
}
text_lang_map = {
"中文": {
"Size List": "尺寸列表",
"Custom Size": "自定义尺寸",
"The width should not be greater than the length; the length and width should not be less than 100, and no more than 1800.": "宽度应不大于长度;长宽不应小于 100,大于 1800",
"Custom Color": "自定义底色",
"Custom": "自定义",
"The number of faces is not equal to 1, please upload an image with a single face. If the actual number of faces is 1, it may be an issue with the accuracy of the detection model. Please switch to a different face detection model on the left or raise a Github Issue to notify the author.": "人脸数量不等于 1,请上传单张人脸的图像。如果实际人脸数量为 1,可能是检测模型精度的问题,请在左边更换人脸检测模型或给作者提Github Issue。",
"Solid Color": "纯色",
"Up-Down Gradient (White)": "上下渐变 (白)",
"Center Gradient (White)": "中心渐变 (白)",
"Set KB size (Download in the bottom right)": "设置 KB 大小(结果在右边最底的组件下载)",
"Not Set": "不设置",
"Only Change Background": "只换底",
},
"English": {
"Size List": "Size List",
"Custom Size": "Custom Size",
"The width should not be greater than the length; the length and width should not be less than 100, and no more than 1800.": "The width should not be greater than the length; the length and width should not be less than 100, and no more than 1800.",
"Custom Color": "Custom Color",
"Custom": "Custom",
"The number of faces is not equal to 1, please upload an image with a single face. If the actual number of faces is 1, it may be an issue with the accuracy of the detection model. Please switch to a different face detection model on the left or raise a Github Issue to notify the author.": "The number of faces is not equal to 1, please upload an image with a single face. If the actual number of faces is 1, it may be an issue with the accuracy of the detection model. Please switch to a different face detection model on the left or raise a Github Issue to notify the author.",
"Solid Color": "Solid Color",
"Up-Down Gradient (White)": "Up-Down Gradient (White)",
"Center Gradient (White)": "Center Gradient (White)",
"Set KB size (Download in the bottom right)": "Set KB size (Download in the bottom right)",
"Not Set": "Not Set",
"Only Change Background": "Only Change Background",
},
}
# 如果尺寸模式选择的是尺寸列表
if idphoto_json["size_mode"] == text_lang_map[language]["Size List"]:
if language == "中文":
idphoto_json["size"] = size_list_dict_CN[size_list_option]
else:
idphoto_json["size"] = size_list_dict_EN[size_list_option]
# 如果尺寸模式选择的是自定义尺寸
elif idphoto_json["size_mode"] == text_lang_map[language]["Custom Size"]:
id_height = int(custom_size_height)
id_width = int(custom_size_width)
if (
id_height < id_width
or min(id_height, id_width) < 100
or max(id_height, id_width) > 1800
):
return {
img_output_standard: gr.update(value=None),
img_output_standard_hd: gr.update(value=None),
notification: gr.update(
value=text_lang_map[language][
"The width should not be greater than the length; the length and width should not be less than 100, and no more than 1800."
],
visible=True,
),
}
idphoto_json["size"] = (id_height, id_width)
else:
idphoto_json["size"] = (None, None)
# 如果颜色模式选择的是自定义底色
if idphoto_json["color_mode"] == text_lang_map[language]["Custom Color"]:
idphoto_json["color_bgr"] = (
range_check(custom_color_R),
range_check(custom_color_G),
range_check(custom_color_B),
)
else:
if language == "中文":
idphoto_json["color_bgr"] = color_list_dict_CN[color_option]
else:
idphoto_json["color_bgr"] = color_list_dict_EN[color_option]
# 如果输出 KB 大小选择的是自定义
if idphoto_json["image_kb_mode"] == text_lang_map[language]["Custom"]:
idphoto_json["custom_image_kb"] = custom_image_kb
else:
idphoto_json["custom_image_kb"] = None
creator = IDCreator()
choose_handler(creator, matting_model_option, face_detect_option)
change_bg_only = idphoto_json["size_mode"] in ["只换底", "Only Change Background"]
# 生成证件照
try:
result = creator(
input_image,
change_bg_only=change_bg_only,
size=idphoto_json["size"],
head_measure_ratio=head_measure_ratio,
# head_height_ratio=head_height_ratio,
head_top_range=(top_distance_max, top_distance_min),
)
except FaceError:
result_message = {
img_output_standard: gr.update(value=None),
img_output_standard_hd: gr.update(value=None),
img_output_layout: gr.update(visible=False),
notification: gr.update(
value=text_lang_map[language][
"The number of faces is not equal to 1, please upload an image with a single face. If the actual number of faces is 1, it may be an issue with the accuracy of the detection model. Please switch to a different face detection model on the left or raise a Github Issue to notify the author."
],
visible=True,
),
}
except APIError as e:
result_message = {
img_output_standard: gr.update(value=None),
img_output_standard_hd: gr.update(value=None),
img_output_layout: gr.update(visible=False),
notification: gr.update(
value=f"Please make sure you have correctly set up the Face++ API Key and Secret.\nAPI Error\nStatus Code is {e.status_code}\nPossible errors are: {e}\n",
visible=True,
),
}
else:
(result_image_standard, result_image_hd, _, _) = result
if idphoto_json["render_mode"] == text_lang_map[language]["Solid Color"]:
result_image_standard = np.uint8(
add_background(result_image_standard, bgr=idphoto_json["color_bgr"])
)
result_image_hd = np.uint8(
add_background(result_image_hd, bgr=idphoto_json["color_bgr"])
)
elif (
idphoto_json["render_mode"]
== text_lang_map[language]["Up-Down Gradient (White)"]
):
result_image_standard = np.uint8(
add_background(
result_image_standard,
bgr=idphoto_json["color_bgr"],
mode="updown_gradient",
)
)
result_image_hd = np.uint8(
add_background(
result_image_hd,
bgr=idphoto_json["color_bgr"],
mode="updown_gradient",
)
)
else:
result_image_standard = np.uint8(
add_background(
result_image_standard,
bgr=idphoto_json["color_bgr"],
mode="center_gradient",
)
)
result_image_hd = np.uint8(
add_background(
result_image_hd,
bgr=idphoto_json["color_bgr"],
mode="center_gradient",
)
)
# 如果只换底,就不生成排版照
if (
idphoto_json["size_mode"]
== text_lang_map[language]["Only Change Background"]
):
result_layout_image = gr.update(visible=False)
# 如果是尺寸列表或自定义尺寸,则生成排版照
else:
typography_arr, typography_rotate = generate_layout_photo(
input_height=idphoto_json["size"][0],
input_width=idphoto_json["size"][1],
)
if watermark_option == "添加" or watermark_option == "Add":
result_layout_image = gr.update(
value=generate_layout_image(
add_watermark(
image=result_image_standard,
text=watermark_text,
size=watermark_text_size,
opacity=watermark_text_opacity,
angle=watermark_text_angle,
space=watermark_text_space,
color=watermark_text_color,
),
typography_arr,
typography_rotate,
height=idphoto_json["size"][0],
width=idphoto_json["size"][1],
),
visible=True,
)
else:
result_layout_image = gr.update(
value=generate_layout_image(
result_image_standard,
typography_arr,
typography_rotate,
height=idphoto_json["size"][0],
width=idphoto_json["size"][1],
),
visible=True,
)
# 如果输出 KB 大小选择的是自定义
if idphoto_json["custom_image_kb"]:
# 将标准照大小调整至目标大小
print("调整 kb 大小到", idphoto_json["custom_image_kb"], "kb")
# 输出路径为一个根据时间戳 + 哈希值生成的随机文件名
import time
output_image_path = f"{os.path.join(os.path.dirname(__file__), 'demo/kb_output')}/{int(time.time())}.jpg"
resize_image_to_kb(
result_image_standard,
output_image_path,
idphoto_json["custom_image_kb"],
)
else:
output_image_path = None
# Add watermark if selected
if watermark_option == "添加" or watermark_option == "Add":
result_image_standard = add_watermark(
image=result_image_standard,
text=watermark_text,
size=watermark_text_size,
opacity=watermark_text_opacity,
angle=watermark_text_angle,
space=watermark_text_space,
color=watermark_text_color,
)
result_image_hd = add_watermark(
image=result_image_hd,
text=watermark_text,
size=watermark_text_size,
opacity=watermark_text_opacity,
angle=watermark_text_angle,
space=watermark_text_space,
color=watermark_text_color,
)
if output_image_path:
result_message = {
img_output_standard: result_image_standard,
img_output_standard_hd: result_image_hd,
img_output_layout: result_layout_image,
notification: gr.update(visible=False),
file_download: gr.update(visible=True, value=output_image_path),
}
else:
result_message = {
img_output_standard: result_image_standard,
img_output_standard_hd: result_image_hd,
img_output_layout: result_layout_image,
notification: gr.update(visible=False),
file_download: gr.update(visible=False),
}
return result_message
# Add this function to handle watermark addition
def add_watermark(
image: np.ndarray, text, size=50, opacity=0.5, angle=45, color="#8B8B1B", space=75
):
image = Image.fromarray(image)
# 创建 Watermarker 实例
watermarker = Watermarker(
input_image=image,
text=text,
style=WatermarkerStyles.STRIPED,
angle=angle,
color=color,
opacity=opacity,
size=size,
space=space,
)
# 返回带水印的图片
return np.array(watermarker.image.convert("RGB"))
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--port", type=int, default=7860, help="The port number of the server"
)
argparser.add_argument(
"--host", type=str, default="127.0.0.1", help="The host of the server"
)
argparser.add_argument(
"--root_path",
type=str,
default=None,
help="The root path of the server, default is None (='/'), e.g. '/myapp'",
)
args = argparser.parse_args()
language = ["中文", "English"]
matting_model_list = [
os.path.splitext(file)[0]
for file in os.listdir(os.path.join(root_dir, "hivision/creator/weights"))
if file.endswith(".onnx") or file.endswith(".mnn")
]
DEFAULT_MATTING_MODEL = "modnet_photographic_portrait_matting"
if DEFAULT_MATTING_MODEL in matting_model_list:
matting_model_list.remove(DEFAULT_MATTING_MODEL)
matting_model_list.insert(0, DEFAULT_MATTING_MODEL)
face_detect_model_list = ["mtcnn", "face++ (联网API)"]
size_mode_CN = ["尺寸列表", "只换底", "自定义尺寸"]
size_mode_EN = ["Size List", "Only Change Background", "Custom Size"]
size_list_CN = list(size_list_dict_CN.keys())
size_list_EN = list(size_list_dict_EN.keys())
colors_CN = ["蓝色", "白色", "红色", "黑色", "深蓝色", "自定义底色"]
colors_EN = ["Blue", "White", "Red", "Black", "Dark blue", "Custom Color"]
watermark_CN = ["不添加", "添加"]
watermark_EN = ["Not Add", "Add"]
render_CN = ["纯色", "上下渐变 (白)", "中心渐变 (白)"]
render_EN = ["Solid Color", "Up-Down Gradient (White)", "Center Gradient (White)"]
image_kb_CN = ["不设置", "自定义"]
image_kb_EN = ["Not Set", "Custom"]
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;
}
"""
def load_description(fp):
with open(fp, "r", encoding="utf-8") as f:
content = f.read()
return content
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_options = gr.Dropdown(
choices=language,
label="Language",
value="中文",
elem_id="language",
)
face_detect_model_options = gr.Dropdown(
choices=face_detect_model_list,
label="人脸检测模型",
value=face_detect_model_list[0],
elem_id="matting_model",
)
matting_model_options = gr.Dropdown(
choices=matting_model_list,
label="抠图模型",
value=matting_model_list[0],
elem_id="matting_model",
)
with gr.Tab("核心参数") as key_parameter_tab:
mode_options = gr.Radio(
choices=size_mode_CN,
label="证件照尺寸选项",
value="尺寸列表",
elem_id="size",
)
# 预设尺寸下拉菜单
with gr.Row(visible=True) as size_list_row:
size_list_options = gr.Dropdown(
choices=size_list_CN,
label="预设尺寸",
value=size_list_CN[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_wdith = gr.Number(
value=295, label="width", interactive=True
)
# 左:背景色选项
color_options = gr.Radio(
choices=colors_CN, label="背景色", value="蓝色", elem_id="color"
)
# 左:如果选择「自定义底色」,显示 RGB 输入框
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=render_CN,
label="渲染方式",
value="纯色",
elem_id="render",
)
with gr.Tab("高级参数") as advance_parameter_tab:
# 面部占照片总比例
head_measure_ratio_option = gr.Slider(
minimum=0.1,
maximum=0.5,
value=0.2,
step=0.01,
label="面部比例",
interactive=True,
)
# 人像头顶距离照片顶部的比例
top_distance_option = gr.Slider(
minimum=0.02,
maximum=0.5,
value=0.12,
step=0.01,
label="头距顶距离",
interactive=True,
)
# 输出照片的KB值
image_kb_options = gr.Radio(
choices=image_kb_CN,
label="设置 KB 大小(结果在右边最底的组件下载)",
value="不设置",
elem_id="image_kb",
)
# 自定义 KB 大小,滑动条,最小 10KB,最大 200KB
with gr.Row(visible=False) as custom_image_kb:
custom_image_kb_size = gr.Slider(
minimum=10,
maximum=1000,
value=50,
label="KB 大小",
interactive=True,
)
with gr.Tab("水印") as watermark_parameter_tab:
# 左: 水印
watermark_options = gr.Radio(
choices=watermark_CN,
label="水印",
value="不添加",
elem_id="watermark",
)
with gr.Row():
# 左:水印文字
watermark_text_options = gr.Text(
max_length=10,
label="水印文字",
value="Hello",
placeholder="请输入水印文字(最多10个字符)",
elem_id="watermark_text",
interactive=False,
)
# 水印颜色
watermark_text_color = gr.ColorPicker(
label="水印颜色",
interactive=False,
value="#FFFFFF",
)
# 文字大小
watermark_text_size = gr.Slider(
minimum=10,
maximum=100,
value=20,
label="文字大小",
interactive=False,
step=1,
)
# 文字透明度
watermark_text_opacity = gr.Slider(
minimum=0,
maximum=1,
value=0.15,
label="水印透明度",
interactive=False,
step=0.01,
)
# 文字角度
watermark_text_angle = gr.Slider(
minimum=0,
maximum=360,
value=30,
label="水印角度",
interactive=False,
step=1,
)
# 文字间距
watermark_text_space = gr.Slider(
minimum=10,
maximum=200,
value=25,
label="水印间距",
interactive=False,
step=1,
)
def update_watermark_text_visibility(choice):
return [
gr.update(interactive=(choice == "添加" or choice == "Add"))
] * 6
watermark_options.change(
fn=update_watermark_text_visibility,
inputs=[watermark_options],
outputs=[
watermark_text_options,
watermark_text_color,
watermark_text_size,
watermark_text_opacity,
watermark_text_angle,
watermark_text_space,
],
)
img_but = gr.Button("开始制作")
# 案例图片
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="状态", visible=False)
with gr.Row():
img_output_standard = gr.Image(
label="标准照", height=350, format="jpeg"
)
img_output_standard_hd = gr.Image(
label="高清照", height=350, format="jpeg"
)
img_output_layout = gr.Image(
label="六寸排版照", height=350, format="jpeg"
)
file_download = gr.File(label="下载调整 KB 大小后的照片", visible=False)
# ---------------- 设置隐藏/显示组件 ----------------
def change_language(language):
# 将Gradio组件中的内容改为中文或英文
if language == "中文":
return {
size_list_options: gr.update(
label="预设尺寸",
choices=size_list_CN,
value=size_list_CN[0],
),
mode_options: gr.update(
label="证件照尺寸选项",
choices=size_mode_CN,
value="尺寸列表",
),
color_options: gr.update(
label="背景色",
choices=colors_CN,
value="蓝色",
),
img_but: gr.update(value="开始制作"),
render_options: gr.update(
label="渲染方式",
choices=render_CN,
value="纯色",
),
image_kb_options: gr.update(
label="设置 KB 大小(结果在右边最底的组件下载)",
choices=image_kb_CN,
value="不设置",
),
matting_model_options: gr.update(label="抠图模型"),
face_detect_model_options: gr.update(label="人脸检测模型"),
custom_image_kb_size: gr.update(label="KB 大小"),
notification: gr.update(label="状态"),
img_output_standard: gr.update(label="标准照"),
img_output_standard_hd: gr.update(label="高清照"),
img_output_layout: gr.update(label="六寸排版照"),
file_download: gr.update(label="下载调整 KB 大小后的照片"),
head_measure_ratio_option: gr.update(label="面部比例"),
top_distance_option: gr.update(label="头距顶距离"),
key_parameter_tab: gr.update(label="核心参数"),
advance_parameter_tab: gr.update(label="高级参数"),
watermark_parameter_tab: gr.update(label="水印"),
watermark_text_options: gr.update(label="水印文字"),
watermark_text_color: gr.update(label="水印颜色"),
watermark_text_size: gr.update(label="文字大小"),
watermark_text_opacity: gr.update(label="水印透明度"),
watermark_text_angle: gr.update(label="水印角度"),
watermark_text_space: gr.update(label="水印间距"),
watermark_options: gr.update(
label="水印", value="不添加", choices=watermark_CN
),
}
elif language == "English":
return {
size_list_options: gr.update(
label="Default size",
choices=size_list_EN,
value=size_list_EN[0],
),
mode_options: gr.update(
label="ID photo size options",
choices=size_mode_EN,
value="Size List",
),
color_options: gr.update(
label="Background color",
choices=colors_EN,
value="Blue",
),
img_but: gr.update(value="Start"),
render_options: gr.update(
label="Rendering mode",
choices=render_EN,
value="Solid Color",
),
image_kb_options: gr.update(
label="Set KB size (Download in the bottom right)",
choices=image_kb_EN,
value="Not Set",
),
matting_model_options: gr.update(label="Matting model"),
face_detect_model_options: gr.update(label="Face detect model"),
custom_image_kb_size: gr.update(label="KB size"),
notification: gr.update(label="Status"),
img_output_standard: gr.update(label="Standard photo"),
img_output_standard_hd: gr.update(label="HD photo"),
img_output_layout: gr.update(label="Layout photo"),
file_download: gr.update(
label="Download the photo after adjusting the KB size"
),
head_measure_ratio_option: gr.update(label="Head ratio"),
top_distance_option: gr.update(label="Top distance"),
key_parameter_tab: gr.update(label="Key Parameters"),
advance_parameter_tab: gr.update(label="Advance Parameters"),
watermark_parameter_tab: gr.update(label="Watermark"),
watermark_text_options: gr.update(label="Text"),
watermark_text_color: gr.update(label="Color"),
watermark_text_size: gr.update(label="Size"),
watermark_text_opacity: gr.update(label="Opacity"),
watermark_text_angle: gr.update(label="Angle"),
watermark_text_space: gr.update(label="Space"),
watermark_options: gr.update(
label="Watermark", value="Not Add", choices=watermark_EN
),
}
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(
idphoto_inference,
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_wdith,
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,
],
)
demo.launch(
# server_name=args.host,
# server_port=args.port,
show_api=False,
# favicon_path=os.path.join(root_dir, "assets/hivision_logo.png"),
# root_path=args.root_path,
)
|