import os import gradio as gr from hivision import IDCreator from hivision.error import FaceError from hivision.utils import add_background, resize_image_to_kb from hivision.creator.layout_calculator import ( generate_layout_photo, generate_layout_image, ) import pathlib import numpy as np from demo.utils import csv_to_size_list import argparse # 获取尺寸列表 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), } color_list_dict_EN = { "Blue": (86, 140, 212), "White": (255, 255, 255), "Red": (233, 51, 35), } # 检测 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, head_measure_ratio=0.2, head_height_ratio=0.45, top_distance_max=0.12, top_distance_min=0.10, ): 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": "人脸数量不等于 1", "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": "The number of faces is not equal to 1", "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() 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, ) except FaceError: result_message = { img_output_standard: gr.update(value=None), img_output_standard_hd: gr.update(value=None), notification: gr.update( value=text_lang_map[language]["The number of faces is not equal to 1"], visible=True, ), } else: (result_image_hd, result_image_standard, _, _) = 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], ) 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 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 if __name__ == "__main__": language = ["中文", "English"] 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", "Custom Color"] render_CN = ["纯色", "上下渐变 (白)", "中心渐变 (白)"] render_EN = ["Solid Color", "Up-Down Gradient (White)", "Center Gradient (White)"] image_kb_CN = ["不设置", "自定义"] image_kb_EN = ["Not Set", "Custom"] # title = "