File size: 16,243 Bytes
402b504
 
 
f8cafb8
 
 
 
 
 
402b504
 
 
 
 
7173af9
402b504
 
 
1d213d9
402b504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7173af9
f8cafb8
 
 
 
 
 
9b2289b
402b504
f8cafb8
402b504
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402b504
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2289b
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402b504
 
 
 
 
f8cafb8
402b504
 
f8cafb8
 
 
 
 
 
 
 
 
402b504
 
 
 
 
f8cafb8
402b504
 
 
 
 
f8cafb8
402b504
 
 
 
f8cafb8
 
 
 
 
 
 
 
 
 
402b504
f8cafb8
 
402b504
 
f8cafb8
402b504
 
 
 
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2289b
f8cafb8
 
402b504
 
 
f8cafb8
 
 
 
 
 
 
 
 
 
 
9b2289b
f8cafb8
402b504
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d213d9
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402b504
f8cafb8
 
 
1d213d9
 
 
 
f8cafb8
 
 
 
 
 
 
1d213d9
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d213d9
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d213d9
 
 
 
 
 
 
 
 
 
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d213d9
 
 
 
 
 
 
f8cafb8
1d213d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8cafb8
 
1d213d9
 
 
f8cafb8
1d213d9
 
f8cafb8
1d213d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8cafb8
1d213d9
 
 
f8cafb8
b2cb163
1d213d9
 
 
f8cafb8
b2cb163
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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,
    save_image_dpi_to_bytes,
)
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.locales 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,
        image_dpi_option=False,
        custom_image_dpi=None,
        brightness_strength=0,
        contrast_strength=0,
        sharpen_strength=0,
        saturation_strength=0,
        face_alignment_option=False,
    ):
        # 初始化参数
        top_distance_min = top_distance_max - 0.02
        # 得到render_option在LOCALES["render_mode"][language]["choices"]中的索引
        render_option_index = LOCALES["render_mode"][language]["choices"].index(
            render_option
        )
        idphoto_json = self._initialize_idphoto_json(
            mode_option, color_option, render_option_index, image_kb_options
        )

        # 处理尺寸模式
        size_result = self._process_size_mode(
            idphoto_json,
            language,
            size_list_option,
            custom_size_height,
            custom_size_width,
        )
        if isinstance(size_result, list):
            return size_result  # 返回错误信息

        # 处理颜色模式
        self._process_color_mode(
            idphoto_json,
            language,
            color_option,
            custom_color_R,
            custom_color_G,
            custom_color_B,
        )

        # 如果设置了自定义KB大小
        if (
            idphoto_json["image_kb_mode"]
            == LOCALES["image_kb"][language]["choices"][-1]
        ):
            idphoto_json["custom_image_kb"] = custom_image_kb

        # 如果设置了自定义DPI大小
        if image_dpi_option == LOCALES["image_dpi"][language]["choices"][-1]:
            idphoto_json["custom_image_dpi"] = custom_image_dpi

        # 创建IDCreator实例并设置处理器
        creator = IDCreator()
        choose_handler(creator, matting_model_option, face_detect_option)

        # 生成证件照
        try:
            result = self._generate_id_photo(
                creator,
                input_image,
                idphoto_json,
                language,
                head_measure_ratio,
                top_distance_max,
                top_distance_min,
                whitening_strength,
                brightness_strength,
                contrast_strength,
                sharpen_strength,
                saturation_strength,
                face_alignment_option,
            )
        except (FaceError, APIError):
            return self._handle_photo_generation_error(language)

        # 后处理生成的照片
        return self._process_generated_photo(
            result,
            idphoto_json,
            language,
            watermark_option,
            watermark_text,
            watermark_text_size,
            watermark_text_opacity,
            watermark_text_angle,
            watermark_text_space,
            watermark_text_color,
        )

    def _initialize_idphoto_json(
        self,
        mode_option,
        color_option,
        render_option,
        image_kb_options,
    ):
        """初始化idphoto_json字典"""
        return {
            "size_mode": mode_option,
            "color_mode": color_option,
            "render_mode": render_option,
            "image_kb_mode": image_kb_options,
            "custom_image_kb": None,
            "custom_image_dpi": None,
        }

    def _process_size_mode(
        self,
        idphoto_json,
        language,
        size_list_option,
        custom_size_height,
        custom_size_width,
    ):
        """处理尺寸模式"""
        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, id_width = int(custom_size_height), int(custom_size_width)
            if (
                id_height < id_width
                or min(id_height, id_width) < 100
                or max(id_height, id_width) > 1800
            ):
                return self._create_error_response(language)
            idphoto_json["size"] = (id_height, id_width)
        else:
            idphoto_json["size"] = (None, None)

    def _process_color_mode(
        self,
        idphoto_json,
        language,
        color_option,
        custom_color_R,
        custom_color_G,
        custom_color_B,
    ):
        """处理颜色模式"""
        if idphoto_json["color_mode"] == LOCALES["bg_color"][language]["choices"][-1]:
            idphoto_json["color_bgr"] = tuple(
                map(range_check, [custom_color_R, custom_color_G, custom_color_B])
            )
        else:
            hex_color = LOCALES["bg_color"][language]["develop"][color_option]
            idphoto_json["color_bgr"] = tuple(
                int(hex_color[i : i + 2], 16) for i in (0, 2, 4)
            )

    def _generate_id_photo(
        self,
        creator: IDCreator,
        input_image,
        idphoto_json,
        language,
        head_measure_ratio,
        top_distance_max,
        top_distance_min,
        whitening_strength,
        brightness_strength,
        contrast_strength,
        sharpen_strength,
        saturation_strength,
        face_alignment_option,
    ):
        """生成证件照"""
        change_bg_only = (
            idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]
        )
        return 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,
            brightness_strength=brightness_strength,
            contrast_strength=contrast_strength,
            sharpen_strength=sharpen_strength,
            saturation_strength=saturation_strength,
            face_alignment=face_alignment_option,
        )

    def _handle_photo_generation_error(self, language):
        """处理照片生成错误"""
        return [gr.update(value=None) for _ in range(4)] + [
            gr.update(visible=False),
            gr.update(
                value=LOCALES["notification"][language]["face_error"], visible=True
            ),
            None,
        ]

    def _process_generated_photo(
        self,
        result,
        idphoto_json,
        language,
        watermark_option,
        watermark_text,
        watermark_text_size,
        watermark_text_opacity,
        watermark_text_angle,
        watermark_text_space,
        watermark_text_color,
    ):
        """处理生成的照片"""
        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)

        # 渲染背景
        result_image_standard, result_image_hd = self._render_background(
            result_image_standard, result_image_hd, idphoto_json
        )

        # 生成排版照片
        result_image_layout, result_image_layout_gr = self._generate_image_layout(
            idphoto_json,
            result_image_standard,
            language,
            watermark_option,
            watermark_text,
            watermark_text_size,
            watermark_text_opacity,
            watermark_text_angle,
            watermark_text_space,
            watermark_text_color,
        )

        # 添加水印
        if watermark_option == LOCALES["watermark_switch"][language]["choices"][1]:
            result_image_standard, result_image_hd = self._add_watermark(
                result_image_standard,
                result_image_hd,
                watermark_text,
                watermark_text_size,
                watermark_text_opacity,
                watermark_text_angle,
                watermark_text_space,
                watermark_text_color,
            )

        # 调整图片大小
        output_image_path = self._resize_image_if_needed(
            result_image_standard,
            result_image_hd,
            result_image_layout,
            idphoto_json,
        )

        return self._create_response(
            result_image_standard,
            result_image_hd,
            result_image_standard_png,
            result_image_hd_png,
            result_image_layout_gr,
            output_image_path,
        )

    def _render_background(self, result_image_standard, result_image_hd, idphoto_json):
        """渲染背景"""
        render_modes = {0: "pure_color", 1: "updown_gradient", 2: "center_gradient"}
        render_mode = render_modes[idphoto_json["render_mode"]]

        result_image_standard = np.uint8(
            add_background(
                result_image_standard, bgr=idphoto_json["color_bgr"], mode=render_mode
            )
        )
        result_image_hd = np.uint8(
            add_background(
                result_image_hd, bgr=idphoto_json["color_bgr"], mode=render_mode
            )
        )

        return result_image_standard, result_image_hd

    def _generate_image_layout(
        self,
        idphoto_json,
        result_image_standard,
        language,
        watermark_option,
        watermark_text,
        watermark_text_size,
        watermark_text_opacity,
        watermark_text_angle,
        watermark_text_space,
        watermark_text_color,
    ):
        """生成排版照片"""
        if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
            return gr.update(visible=False)

        typography_arr, typography_rotate = generate_layout_photo(
            input_height=idphoto_json["size"][0],
            input_width=idphoto_json["size"][1],
        )

        image = result_image_standard
        if watermark_option == LOCALES["watermark_switch"][language]["choices"][1]:
            image = add_watermark(
                image=image,
                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_layout = generate_layout_image(
            image,
            typography_arr,
            typography_rotate,
            height=idphoto_json["size"][0],
            width=idphoto_json["size"][1],
        )

        return result_image_layout, gr.update(
            value=result_image_layout,
            visible=True,
        )

    def _add_watermark(
        self,
        result_image_standard,
        result_image_hd,
        watermark_text,
        watermark_text_size,
        watermark_text_opacity,
        watermark_text_angle,
        watermark_text_space,
        watermark_text_color,
    ):
        """添加水印"""
        watermark_params = {
            "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_standard = add_watermark(
            image=result_image_standard, **watermark_params
        )
        result_image_hd = add_watermark(image=result_image_hd, **watermark_params)
        return result_image_standard, result_image_hd

    def _resize_image_if_needed(
        self,
        result_image_standard,
        result_image_hd,
        result_image_layout,
        idphoto_json,
    ):
        """如果需要,调整图片大小"""
        # 设置输出路径
        base_path = os.path.join(
            os.path.dirname(os.path.dirname(__file__)), "demo/kb_output"
        )
        timestamp = int(time.time())
        output_paths = {
            "standard": f"{base_path}/{timestamp}_standard",
            "hd": f"{base_path}/{timestamp}_hd",
            "layout": f"{base_path}/{timestamp}_layout",
        }

        # 获取自定义的KB和DPI值
        custom_kb = idphoto_json.get("custom_image_kb")
        custom_dpi = idphoto_json.get("custom_image_dpi", 300)

        # 处理同时有自定义KB和DPI的情况
        if custom_kb and custom_dpi:
            # 为所有输出路径添加DPI信息
            for key in output_paths:
                output_paths[key] += f"_{custom_dpi}dpi.jpg"
            # 为标准图像添加KB信息
            output_paths["standard"] = output_paths["standard"].replace(
                ".jpg", f"_{custom_kb}kb.jpg"
            )

            # 调整标准图像大小并保存
            resize_image_to_kb(
                result_image_standard,
                output_paths["standard"],
                custom_kb,
                dpi=custom_dpi,
            )
            # 保存高清图像和排版图像
            save_image_dpi_to_bytes(result_image_hd, output_paths["hd"], dpi=custom_dpi)
            save_image_dpi_to_bytes(
                result_image_layout, output_paths["layout"], dpi=custom_dpi
            )

            return list(output_paths.values())

        # 只有自定义DPI的情况
        elif custom_dpi:
            for key in output_paths:
                output_paths[key] += f"_{custom_dpi}dpi.jpg"
                # 保存所有图像,使用自定义DPI
                save_image_dpi_to_bytes(
                    locals()[f"result_image_{key}"], output_paths[key], dpi=custom_dpi
                )

            return list(output_paths.values())

        # 只有自定义KB的情况
        elif custom_kb:
            output_paths["standard"] += f"_{custom_kb}kb.jpg"
            # 只调整标准图像大小并保存
            resize_image_to_kb(
                result_image_standard,
                output_paths["standard"],
                custom_kb,
                dpi=300,
            )

            return [output_paths["standard"]]

        # 如果没有自定义设置,返回None
        return None

    def _create_response(
        self,
        result_image_standard,
        result_image_hd,
        result_image_standard_png,
        result_image_hd_png,
        result_layout_image,
        output_image_path,
    ):
        """创建响应"""
        response = [
            result_image_standard,
            result_image_hd,
            result_image_standard_png,
            result_image_hd_png,
            result_layout_image,
            gr.update(visible=False),
        ]
        if output_image_path:
            response.append(gr.update(visible=True, value=output_image_path))
        else:
            response.append(gr.update(visible=False))
        return response

    def _create_error_response(self, language):
        """创建错误响应"""
        return [gr.update(value=None) for _ in range(4)] + [
            None,
            gr.update(
                value=LOCALES["size_mode"][language]["custom_size_eror"], visible=True
            ),
            None,
        ]