gordonchan commited on
Commit
86f4fde
1 Parent(s): 305876d

Update app.py

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