import gradio as gr import onnxruntime from src.face_judgement_align import IDphotos_create from hivisionai.hycv.vision import add_background from src.layoutCreate import generate_layout_photo, generate_layout_image import pathlib import numpy as np from image_utils import resize_image_to_kb size_list_dict = { "一寸": (413, 295), "二寸": (626, 413), "教师资格证": (413, 295), "国家公务员考试": (413, 295), "初级会计考试": (413, 295), } color_list_dict = { "蓝色": (86, 140, 212), "白色": (255, 255, 255), "红色": (233, 51, 35), } # 设置Gradio examples def set_example_image(example: list) -> dict: return gr.Image.update(value=example[0]) # 检测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, 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, } # 如果尺寸模式选择的是尺寸列表 if idphoto_json["size_mode"] == "尺寸列表": idphoto_json["size"] = size_list_dict[size_list_option] # 如果尺寸模式选择的是自定义尺寸 elif idphoto_json["size_mode"] == "自定义尺寸": 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="宽度应不大于长度;长宽不应小于100,大于1800", visible=True ), } idphoto_json["size"] = (id_height, id_width) else: idphoto_json["size"] = (None, None) # 如果颜色模式选择的是自定义底色 if idphoto_json["color_mode"] == "自定义底色": idphoto_json["color_bgr"] = ( range_check(custom_color_R), range_check(custom_color_G), range_check(custom_color_B), ) else: idphoto_json["color_bgr"] = color_list_dict[color_option] # 如果输出KB大小选择的是自定义 if idphoto_json["image_kb_mode"] == "自定义": idphoto_json["custom_image_kb"] = custom_image_kb else: idphoto_json["custom_image_kb"] = None # 生成证件照 ( result_image_hd, result_image_standard, typography_arr, typography_rotate, _, _, _, _, status, ) = IDphotos_create( input_image, mode=idphoto_json["size_mode"], size=idphoto_json["size"], head_measure_ratio=head_measure_ratio, head_height_ratio=head_height_ratio, align=False, beauty=False, fd68=None, human_sess=sess, IS_DEBUG=False, top_distance_max=top_distance_max, top_distance_min=top_distance_min, ) # 如果检测到人脸数量不等于1 if status == 0: result_messgae = { img_output_standard: gr.update(value=None), img_output_standard_hd: gr.update(value=None), notification: gr.update(value="人脸数量不等于1", visible=True), } # 如果检测到人脸数量等于1 else: if idphoto_json["render_mode"] == "纯色": 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"] == "上下渐变(白)": 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"] == "只换底": 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 = generate_layout_image( result_image_standard, typography_arr, typography_rotate, height=idphoto_json["size"][0], width=idphoto_json["size"][1], ) # 如果输出KB大小选择的是自定义 if idphoto_json["custom_image_kb"]: # 将标准照大小调整至目标大小 print("调整kb大小到", idphoto_json["custom_image_kb"], "kb") # 输出路径为一个根据时间戳+哈希值生成的随机文件名 import time output_image_path = f"./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_messgae = { 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_messgae = { 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_messgae if __name__ == "__main__": # 预加载ONNX模型 HY_HUMAN_MATTING_WEIGHTS_PATH = "./hivision_modnet.onnx" sess = onnxruntime.InferenceSession(HY_HUMAN_MATTING_WEIGHTS_PATH) size_mode = ["尺寸列表", "只换底", "自定义尺寸"] size_list = ["一寸", "二寸", "教师资格证", "国家公务员考试", "初级会计考试"] colors = ["蓝色", "白色", "红色", "自定义底色"] render = ["纯色", "上下渐变(白)", "中心渐变(白)"] image_kb = ["不设置", "自定义"] title = "