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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import onnxruntime
|
| 3 |
+
from src.face_judgement_align import IDphotos_create
|
| 4 |
+
from hivisionai.hycv.vision import add_background
|
| 5 |
+
from src.layoutCreate import generate_layout_photo, generate_layout_image
|
| 6 |
+
import pathlib
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
size_list_dict = {"一寸": (413, 295), "二寸": (626, 413),
|
| 10 |
+
"教师资格证": (413, 295), "国家公务员考试": (413, 295), "初级会计考试": (413, 295)}
|
| 11 |
+
color_list_dict = {"蓝色": (86, 140, 212), "白色": (255, 255, 255), "红色": (233, 51, 35)}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# 设置Gradio examples
|
| 15 |
+
def set_example_image(example: list) -> dict:
|
| 16 |
+
return gr.Image.update(value=example[0])
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# 检测RGB是否超出范围,如果超出则约束到0~255之间
|
| 20 |
+
def range_check(value, min_value=0, max_value=255):
|
| 21 |
+
value = int(value)
|
| 22 |
+
if value <= min_value:
|
| 23 |
+
value = min_value
|
| 24 |
+
elif value > max_value:
|
| 25 |
+
value = max_value
|
| 26 |
+
return value
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def idphoto_inference(input_image,
|
| 30 |
+
mode_option,
|
| 31 |
+
size_list_option,
|
| 32 |
+
color_option,
|
| 33 |
+
render_option,
|
| 34 |
+
custom_color_R,
|
| 35 |
+
custom_color_G,
|
| 36 |
+
custom_color_B,
|
| 37 |
+
custom_size_height,
|
| 38 |
+
custom_size_width,
|
| 39 |
+
head_measure_ratio=0.2,
|
| 40 |
+
head_height_ratio=0.45,
|
| 41 |
+
top_distance_max=0.12,
|
| 42 |
+
top_distance_min=0.10):
|
| 43 |
+
|
| 44 |
+
idphoto_json = {
|
| 45 |
+
"size_mode": mode_option,
|
| 46 |
+
"color_mode": color_option,
|
| 47 |
+
"render_mode": render_option,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# 如果尺寸模式选择的是尺寸列表
|
| 51 |
+
if idphoto_json["size_mode"] == "尺寸列表":
|
| 52 |
+
idphoto_json["size"] = size_list_dict[size_list_option]
|
| 53 |
+
# 如果尺寸模式选择的是自定义尺寸
|
| 54 |
+
elif idphoto_json["size_mode"] == "自定义尺寸":
|
| 55 |
+
id_height = int(custom_size_height)
|
| 56 |
+
id_width = int(custom_size_width)
|
| 57 |
+
if id_height < id_width or min(id_height, id_width) < 100 or max(id_height, id_width) > 1800:
|
| 58 |
+
return {
|
| 59 |
+
img_output_standard: gr.update(value=None),
|
| 60 |
+
img_output_standard_hd: gr.update(value=None),
|
| 61 |
+
notification: gr.update(value="宽度应不大于长度;长宽不应小于100,大于1800", visible=True)}
|
| 62 |
+
idphoto_json["size"] = (id_height, id_width)
|
| 63 |
+
else:
|
| 64 |
+
idphoto_json["size"] = (None, None)
|
| 65 |
+
|
| 66 |
+
# 如果颜色模式选择的是自定义底色
|
| 67 |
+
if idphoto_json["color_mode"] == "自定义底色":
|
| 68 |
+
idphoto_json["color_bgr"] = (range_check(custom_color_R),
|
| 69 |
+
range_check(custom_color_G),
|
| 70 |
+
range_check(custom_color_B))
|
| 71 |
+
else:
|
| 72 |
+
idphoto_json["color_bgr"] = color_list_dict[color_option]
|
| 73 |
+
|
| 74 |
+
result_image_hd, result_image_standard, typography_arr, typography_rotate, \
|
| 75 |
+
_, _, _, _, status = IDphotos_create(input_image,
|
| 76 |
+
mode=idphoto_json["size_mode"],
|
| 77 |
+
size=idphoto_json["size"],
|
| 78 |
+
head_measure_ratio=head_measure_ratio,
|
| 79 |
+
head_height_ratio=head_height_ratio,
|
| 80 |
+
align=False,
|
| 81 |
+
beauty=False,
|
| 82 |
+
fd68=None,
|
| 83 |
+
human_sess=sess,
|
| 84 |
+
IS_DEBUG=False,
|
| 85 |
+
top_distance_max=top_distance_max,
|
| 86 |
+
top_distance_min=top_distance_min)
|
| 87 |
+
|
| 88 |
+
# 如果检测到人脸数量不等于1
|
| 89 |
+
if status == 0:
|
| 90 |
+
result_messgae = {
|
| 91 |
+
img_output_standard: gr.update(value=None),
|
| 92 |
+
img_output_standard_hd: gr.update(value=None),
|
| 93 |
+
notification: gr.update(value="人脸数量不等于1", visible=True)
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
# 如果检测到人脸数量等于1
|
| 97 |
+
else:
|
| 98 |
+
if idphoto_json["render_mode"] == "纯色":
|
| 99 |
+
result_image_standard = np.uint8(
|
| 100 |
+
add_background(result_image_standard, bgr=idphoto_json["color_bgr"]))
|
| 101 |
+
result_image_hd = np.uint8(add_background(result_image_hd, bgr=idphoto_json["color_bgr"]))
|
| 102 |
+
elif idphoto_json["render_mode"] == "上下渐变(白)":
|
| 103 |
+
result_image_standard = np.uint8(
|
| 104 |
+
add_background(result_image_standard, bgr=idphoto_json["color_bgr"], mode="updown_gradient"))
|
| 105 |
+
result_image_hd = np.uint8(
|
| 106 |
+
add_background(result_image_hd, bgr=idphoto_json["color_bgr"], mode="updown_gradient"))
|
| 107 |
+
else:
|
| 108 |
+
result_image_standard = np.uint8(
|
| 109 |
+
add_background(result_image_standard, bgr=idphoto_json["color_bgr"], mode="center_gradient"))
|
| 110 |
+
result_image_hd = np.uint8(
|
| 111 |
+
add_background(result_image_hd, bgr=idphoto_json["color_bgr"], mode="center_gradient"))
|
| 112 |
+
|
| 113 |
+
if idphoto_json["size_mode"] == "只换底":
|
| 114 |
+
result_layout_image = gr.update(visible=False)
|
| 115 |
+
else:
|
| 116 |
+
typography_arr, typography_rotate = generate_layout_photo(input_height=idphoto_json["size"][0],
|
| 117 |
+
input_width=idphoto_json["size"][1])
|
| 118 |
+
|
| 119 |
+
result_layout_image = generate_layout_image(result_image_standard, typography_arr,
|
| 120 |
+
typography_rotate,
|
| 121 |
+
height=idphoto_json["size"][0],
|
| 122 |
+
width=idphoto_json["size"][1])
|
| 123 |
+
|
| 124 |
+
result_messgae = {
|
| 125 |
+
img_output_standard: result_image_standard,
|
| 126 |
+
img_output_standard_hd: result_image_hd,
|
| 127 |
+
img_output_layout: result_layout_image,
|
| 128 |
+
notification: gr.update(visible=False)}
|
| 129 |
+
|
| 130 |
+
return result_messgae
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
if __name__ == "__main__":
|
| 134 |
+
HY_HUMAN_MATTING_WEIGHTS_PATH = "./hivision_modnet.onnx"
|
| 135 |
+
sess = onnxruntime.InferenceSession(HY_HUMAN_MATTING_WEIGHTS_PATH)
|
| 136 |
+
size_mode = ["尺寸列表", "只换底", "自定义尺寸"]
|
| 137 |
+
size_list = ["一寸", "二寸", "教师资格证", "国家公务员考试", "初级会计考试"]
|
| 138 |
+
colors = ["蓝色", "白色", "红色", "自定义底色"]
|
| 139 |
+
render = ["纯色", "上下渐变(白)", "中心渐变(白)"]
|
| 140 |
+
|
| 141 |
+
title = "<h1 id='title'>HivisionIDPhotos</h1>"
|
| 142 |
+
description = "<h3>😎6.20更新:新增尺寸选择列表</h3>"
|
| 143 |
+
css = '''
|
| 144 |
+
h1#title, h3 {
|
| 145 |
+
text-align: center;
|
| 146 |
+
}
|
| 147 |
+
'''
|
| 148 |
+
|
| 149 |
+
demo = gr.Blocks(css=css)
|
| 150 |
+
|
| 151 |
+
with demo:
|
| 152 |
+
gr.Markdown(title)
|
| 153 |
+
gr.Markdown(description)
|
| 154 |
+
with gr.Row():
|
| 155 |
+
with gr.Column():
|
| 156 |
+
img_input = gr.Image().style(height=350)
|
| 157 |
+
mode_options = gr.Radio(choices=size_mode, label="证件照尺寸选项", value="尺寸列表", elem_id="size")
|
| 158 |
+
# 预设尺寸下拉菜单
|
| 159 |
+
with gr.Row(visible=True) as size_list_row:
|
| 160 |
+
size_list_options = gr.Dropdown(choices=size_list, label="预设尺寸", value="一寸", elem_id="size_list")
|
| 161 |
+
|
| 162 |
+
with gr.Row(visible=False) as custom_size:
|
| 163 |
+
custom_size_height = gr.Number(value=413, label="height", interactive=True)
|
| 164 |
+
custom_size_wdith = gr.Number(value=295, label="width", interactive=True)
|
| 165 |
+
|
| 166 |
+
color_options = gr.Radio(choices=colors, label="背景色", value="蓝色", elem_id="color")
|
| 167 |
+
with gr.Row(visible=False) as custom_color:
|
| 168 |
+
custom_color_R = gr.Number(value=0, label="R", interactive=True)
|
| 169 |
+
custom_color_G = gr.Number(value=0, label="G", interactive=True)
|
| 170 |
+
custom_color_B = gr.Number(value=0, label="B", interactive=True)
|
| 171 |
+
|
| 172 |
+
render_options = gr.Radio(choices=render, label="渲染方式", value="纯色", elem_id="render")
|
| 173 |
+
|
| 174 |
+
img_but = gr.Button('开始制作')
|
| 175 |
+
# 案例图片
|
| 176 |
+
example_images = gr.Dataset(components=[img_input],
|
| 177 |
+
samples=[[path.as_posix()]
|
| 178 |
+
for path in sorted(pathlib.Path('images').rglob('*.jpg'))])
|
| 179 |
+
|
| 180 |
+
with gr.Column():
|
| 181 |
+
notification = gr.Text(label="状态", visible=False)
|
| 182 |
+
with gr.Row():
|
| 183 |
+
img_output_standard = gr.Image(label="标准照").style(height=350)
|
| 184 |
+
img_output_standard_hd = gr.Image(label="高清照").style(height=350)
|
| 185 |
+
img_output_layout = gr.Image(label="六寸排版照").style(height=350)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def change_color(colors):
|
| 189 |
+
if colors == "自定义底色":
|
| 190 |
+
return {custom_color: gr.update(visible=True)}
|
| 191 |
+
else:
|
| 192 |
+
return {custom_color: gr.update(visible=False)}
|
| 193 |
+
|
| 194 |
+
def change_size_mode(size_option_item):
|
| 195 |
+
if size_option_item == "自定义尺寸":
|
| 196 |
+
return {custom_size: gr.update(visible=True),
|
| 197 |
+
size_list_row: gr.update(visible=False)}
|
| 198 |
+
elif size_option_item == "只换底":
|
| 199 |
+
return {custom_size: gr.update(visible=False),
|
| 200 |
+
size_list_row: gr.update(visible=False)}
|
| 201 |
+
else:
|
| 202 |
+
return {custom_size: gr.update(visible=False),
|
| 203 |
+
size_list_row: gr.update(visible=True)}
|
| 204 |
+
|
| 205 |
+
color_options.input(change_color, inputs=[color_options], outputs=[custom_color])
|
| 206 |
+
mode_options.input(change_size_mode, inputs=[mode_options], outputs=[custom_size, size_list_row])
|
| 207 |
+
|
| 208 |
+
img_but.click(idphoto_inference,
|
| 209 |
+
inputs=[img_input, mode_options, size_list_options, color_options, render_options,
|
| 210 |
+
custom_color_R, custom_color_G, custom_color_B,
|
| 211 |
+
custom_size_height, custom_size_wdith],
|
| 212 |
+
outputs=[img_output_standard, img_output_standard_hd, img_output_layout, notification])
|
| 213 |
+
example_images.click(fn=set_example_image, inputs=[example_images], outputs=[img_input])
|
| 214 |
+
|
| 215 |
+
demo.launch()
|