File size: 4,647 Bytes
ca46a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7173af9
ca46a75
 
 
7173af9
 
f8cafb8
 
 
 
9b2289b
ca46a75
 
 
7173af9
ca46a75
 
 
7173af9
 
f8cafb8
 
 
 
9b2289b
ca46a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7173af9
 
 
 
 
 
 
 
 
 
 
 
f8cafb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2289b
 
 
 
ca46a75
 
 
 
 
 
7173af9
ca46a75
 
7173af9
ca46a75
 
 
7173af9
ca46a75
 
 
 
 
 
 
 
7173af9
ca46a75
 
 
7173af9
 
 
 
 
 
 
 
ca46a75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b2289b
ca46a75
 
9b2289b
 
ca46a75
 
 
 
 
9b2289b
 
 
 
ca46a75
 
 
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
r"""
@DATE: 2024/9/5 19:20
@File: context.py
@IDE: pycharm
@Description:
    证件照创建上下文类,用于同步信息
"""
from typing import Optional, Callable, Tuple
import numpy as np


class Params:
    def __init__(
        self,
        size: Tuple[int, int] = (413, 295),
        change_bg_only: bool = False,
        crop_only: bool = False,
        head_measure_ratio: float = 0.2,
        head_height_ratio: float = 0.45,
        head_top_range: float = (0.12, 0.1),
        face: Tuple[int, int, int, int] = None,
        whitening_strength: int = 0,
        brightness_strength: int = 0,
        contrast_strength: int = 0,
        sharpen_strength: int = 0,
        saturation_strength: int = 0,
        face_alignment: bool = False,
    ):
        self.__size = size
        self.__change_bg_only = change_bg_only
        self.__crop_only = crop_only
        self.__head_measure_ratio = head_measure_ratio
        self.__head_height_ratio = head_height_ratio
        self.__head_top_range = head_top_range
        self.__face = face
        self.__whitening_strength = whitening_strength
        self.__brightness_strength = brightness_strength
        self.__contrast_strength = contrast_strength
        self.__sharpen_strength = sharpen_strength
        self.__saturation_strength = saturation_strength
        self.__face_alignment = face_alignment

    @property
    def size(self):
        return self.__size

    @property
    def change_bg_only(self):
        return self.__change_bg_only

    @property
    def head_measure_ratio(self):
        return self.__head_measure_ratio

    @property
    def head_height_ratio(self):
        return self.__head_height_ratio

    @property
    def head_top_range(self):
        return self.__head_top_range

    @property
    def crop_only(self):
        return self.__crop_only

    @property
    def face(self):
        return self.__face

    @property
    def whitening_strength(self):
        return self.__whitening_strength

    @property
    def brightness_strength(self):
        return self.__brightness_strength

    @property
    def contrast_strength(self):
        return self.__contrast_strength

    @property
    def sharpen_strength(self):
        return self.__sharpen_strength

    @property
    def saturation_strength(self):
        return self.__saturation_strength

    @property
    def face_alignment(self):
        return self.__face_alignment


class Result:
    def __init__(
        self,
        standard: np.ndarray,
        hd: np.ndarray,
        matting: np.ndarray,
        clothing_params: Optional[dict],
        typography_params: Optional[dict],
        face: Optional[Tuple[int, int, int, int, float]],
    ):
        self.standard = standard
        self.hd = hd
        self.matting = matting
        self.clothing_params = clothing_params
        """
        服装参数,仅换底时为 None
        """
        self.typography_params = typography_params
        """
        排版参数,仅换底时为 None
        """
        self.face = face

    def __iter__(self):
        return iter(
            [
                self.standard,
                self.hd,
                self.matting,
                self.clothing_params,
                self.typography_params,
                self.face,
            ]
        )


class Context:
    def __init__(self, params: Params):
        self.params: Params = params
        """
        证件照处理参数
        """
        self.origin_image: Optional[np.ndarray] = None
        """
        输入的原始图像,处理时会进行resize,长宽不一定等于输入图像
        """
        self.processing_image: Optional[np.ndarray] = None
        """
        当前正在处理的图像
        """
        self.matting_image: Optional[np.ndarray] = None
        """
        人像抠图结果
        """
        self.face: dict = dict(rectangle=None, roll_angle=None)
        """
        人脸检测结果,大于一个人脸时已在上层抛出异常
        rectangle: 人脸矩形框,包含 x1, y1, width, height 的坐标, x1, y1 为左上角坐标, width, height 为矩形框的宽度和高度
        roll_angle: 人脸偏转角度,以眼睛为标准,计算的人脸偏转角度,用于人脸矫正
        """
        self.result: Optional[Result] = None
        """
        证件照处理结果
        """
        self.align_info: Optional[dict] = None
        """
        人脸矫正信息,仅当 align_face 为 True 时存在
        """


ContextHandler = Optional[Callable[[Context], None]]