forked from Zeyi-Lin/HivisionIDPhotos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
306 lines (289 loc) · 12.1 KB
/
processor.py
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
import numpy as np
from hivision import IDCreator
from hivision.error import FaceError, APIError
from hivision.utils import add_background, resize_image_to_kb, add_watermark
from hivision.creator.layout_calculator import (
generate_layout_photo,
generate_layout_image,
)
from hivision.creator.choose_handler import choose_handler
from demo.utils import range_check
import gradio as gr
import os
import time
from demo.locals import LOCALES
class IDPhotoProcessor:
def process(
self,
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,
top_distance_max=0.12,
whitening_strength=0,
):
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,
"custom_image_kb": None,
}
# 如果尺寸模式选择的是尺寸列表
if idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][0]:
idphoto_json["size"] = LOCALES["size_list"][language]["develop"][
size_list_option
]
# 如果尺寸模式选择的是自定义尺寸
elif idphoto_json["size_mode"] == LOCALES["size_mode"][language]["choices"][2]:
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 [
gr.update(value=None), # img_output_standard
gr.update(value=None), # img_output_standard_hd
gr.update(value=None), # img_output_standard_png
gr.update(value=None), # img_output_standard_hd_png
None, # img_output_layout (assuming it should be None or not updated)
gr.update( # notification
value=LOCALES["size_mode"][language]["custom_size_eror"],
visible=True,
),
None, # file_download (assuming it should be None or not updated)
]
idphoto_json["size"] = (id_height, id_width)
else:
idphoto_json["size"] = (None, None)
# 如果颜色模式选择的是自定义底色
if idphoto_json["color_mode"] == LOCALES["bg_color"][language]["choices"][-1]:
idphoto_json["color_bgr"] = (
range_check(custom_color_R),
range_check(custom_color_G),
range_check(custom_color_B),
)
else:
hex_color = idphoto_json["color_bgr"] = LOCALES["bg_color"][language][
"develop"
][color_option]
# 转为 RGB
idphoto_json["color_bgr"] = tuple(
int(hex_color[i : i + 2], 16) for i in (0, 2, 4)
)
# 如果输出 KB 大小选择的是自定义
if (
idphoto_json["image_kb_mode"]
== LOCALES["image_kb"][language]["choices"][-1]
):
idphoto_json["custom_image_kb"] = custom_image_kb
creator = IDCreator()
choose_handler(creator, matting_model_option, face_detect_option)
# 是否只换底
change_bg_only = (
idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]
)
# 生成证件照
try:
result = creator(
input_image,
change_bg_only=change_bg_only,
size=idphoto_json["size"],
head_measure_ratio=head_measure_ratio,
head_top_range=(top_distance_max, top_distance_min),
whitening_strength=whitening_strength,
)
# 如果检测到人脸数量不等于1
except FaceError:
return [
gr.update(value=None), # img_output_standard
gr.update(value=None), # img_output_standard_hd
gr.update(value=None), # img_output_standard_png
gr.update(value=None), # img_output_standard_hd_png
gr.update(visible=False), # img_output_layout
gr.update( # notification
value=LOCALES["notification"][language]["face_error"],
visible=True,
),
None, # file_download (assuming it should be None or have no update)
]
# 如果 API 错误
except APIError as e:
return [
gr.update(value=None), # img_output_standard
gr.update(value=None), # img_output_standard_hd
gr.update(value=None), # img_output_standard_png
gr.update(value=None), # img_output_standard_hd_png
gr.update(visible=False), # img_output_layout
gr.update( # notification
value=LOCALES["notification"][language]["face_error"],
visible=True,
),
None, # file_download (assuming it should be None or have no update)
]
# 证件照生成正常
else:
(result_image_standard, result_image_hd, _, _, _, _) = result
result_image_standard_png = np.uint8(result_image_standard)
result_image_hd_png = np.uint8(result_image_hd)
# 纯色渲染
if (
idphoto_json["render_mode"]
== LOCALES["render_mode"][language]["choices"][0]
):
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"]
== LOCALES["render_mode"][language]["choices"][1]
):
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 change_bg_only:
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
== LOCALES["watermark_switch"][language]["choices"][1]
):
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,
)
# 如果添加水印
if watermark_option == LOCALES["watermark_switch"][language]["choices"][1]:
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,
)
# 如果输出 KB 大小选择的是自定义
if idphoto_json["custom_image_kb"]:
print("调整 kb 大小到", idphoto_json["custom_image_kb"], "kb")
output_image_path = f"{os.path.join(os.path.dirname(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:
return [
result_image_standard, # img_output_standard
result_image_hd, # img_output_standard_hd
result_image_standard_png, # img_output_standard_png
result_image_hd_png, # img_output_standard_hd_png
result_layout_image, # img_output_layout
gr.update(visible=False), # notification
gr.update(visible=True, value=output_image_path), # file_download
]
else:
return [
result_image_standard, # img_output_standard
result_image_hd, # img_output_standard_hd
result_image_standard_png, # img_output_standard_png
result_image_hd_png, # img_output_standard_hd_png
result_layout_image, # img_output_layout
gr.update(visible=False), # notification
gr.update(visible=False), # file_download
]