forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle2paints_utils.py
230 lines (186 loc) · 6.16 KB
/
style2paints_utils.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
import numpy as np
import cv2
from skimage.measure import block_reduce
__all__ = [
'k_resize',
'sk_resize',
'd_resize',
'min_resize',
'go_passline',
'min_k_down',
'min_k_down_c',
'mini_norm',
'hard_norm',
'sensitive',
'min_black',
'eye_black',
'cal_std',
'opreate_normal_hint',
's_enhance',
'emph_line',
'ini_hint',
'de_line',
'blur_line',
'clip_15',
'cv_denoise',
]
def k_resize(x, k):
if x.shape[0] < x.shape[1]:
s0 = k
s1 = int(x.shape[1] * (k / x.shape[0]))
s1 = s1 - s1 % 64
_s0 = 16 * s0
_s1 = int(x.shape[1] * (_s0 / x.shape[0]))
_s1 = (_s1 + 32) - (_s1 + 32) % 64
else:
s1 = k
s0 = int(x.shape[0] * (k / x.shape[1]))
s0 = s0 - s0 % 64
_s1 = 16 * s1
_s0 = int(x.shape[0] * (_s1 / x.shape[1]))
_s0 = (_s0 + 32) - (_s0 + 32) % 64
new_min = min(_s1, _s0)
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (_s1, _s0), interpolation=interpolation)
return y
def sk_resize(x, k):
if x.shape[0] < x.shape[1]:
s0 = k
s1 = int(x.shape[1] * (k / x.shape[0]))
s1 = s1 - s1 % 16
_s0 = 4 * s0
_s1 = int(x.shape[1] * (_s0 / x.shape[0]))
_s1 = (_s1 + 8) - (_s1 + 8) % 16
else:
s1 = k
s0 = int(x.shape[0] * (k / x.shape[1]))
s0 = s0 - s0 % 16
_s1 = 4 * s1
_s0 = int(x.shape[0] * (_s1 / x.shape[1]))
_s0 = (_s0 + 8) - (_s0 + 8) % 16
new_min = min(_s1, _s0)
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (_s1, _s0), interpolation=interpolation)
return y
def d_resize(x, d, fac=1.0):
new_min = min(int(d[1] * fac), int(d[0] * fac))
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (int(d[1] * fac), int(d[0] * fac)), interpolation=interpolation)
return y
def min_resize(x, m):
if x.shape[0] < x.shape[1]:
s0 = m
s1 = int(float(m) / float(x.shape[0]) * float(x.shape[1]))
else:
s0 = int(float(m) / float(x.shape[1]) * float(x.shape[0]))
s1 = m
new_max = max(s1, s0)
raw_max = max(x.shape[0], x.shape[1])
if new_max < raw_max:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (s1, s0), interpolation=interpolation)
return y
def go_passline(img):
o = img.astype(np.float32)
b = cv2.GaussianBlur(img, (7, 7), 0).astype(np.float32)
r = np.max(b - o, axis=2, keepdims=True)
r /= np.max(cv2.resize(r.clip(0, 255).astype(np.uint8), (64, 64), cv2.INTER_AREA))
r = (1 - r).clip(0, 1)
return np.tile((r * 255.0).clip(0, 255).astype(np.uint8), [1, 1, 3])
def min_k_down(x, k):
y = 255 - x.astype(np.float32)
y = block_reduce(y, (k, k), np.max)
y = 255 - y
return y.clip(0, 255).astype(np.uint8)
def min_k_down_c(x, k):
y = 255 - x.astype(np.float32)
y = block_reduce(y, (k, k, 1), np.max)
y = 255 - y
return y.clip(0, 255).astype(np.uint8)
def mini_norm(x):
y = x.astype(np.float32)
y = 1 - y / 255.0
y -= np.min(y)
y /= np.max(y)
return (255.0 - y * 80.0).astype(np.uint8)
def hard_norm(x):
o = x.astype(np.float32)
b = cv2.GaussianBlur(x, (3, 3), 0).astype(np.float32)
y = (o - b + 255.0).clip(0, 255)
y = 1 - y / 255.0
y -= np.min(y)
y /= np.max(y)
y[y < np.mean(y)] = 0
y[y > 0] = 1
return (255.0 - y * 255.0).astype(np.uint8)
def sensitive(x, s=15.0):
y = x.astype(np.float32)
y -= s
y /= 255.0 - s * 2.0
y *= 255.0
return y.clip(0, 255).astype(np.uint8)
def min_black(x):
return np.tile(np.min(x, axis=2, keepdims=True), [1, 1, 3])
def eye_black(x):
return cv2.cvtColor(cv2.cvtColor(x, cv2.COLOR_RGB2GRAY), cv2.COLOR_GRAY2RGB)
def cal_std(x):
y = (cv2.resize(x, (128, 128), cv2.INTER_AREA)).astype(np.float32)
return np.mean(np.var(y, axis=2))
def opreate_normal_hint(gird, points, type, length):
h = gird.shape[0]
w = gird.shape[1]
for point in points:
x, y, r, g, b, t = point
if t == type:
x = int(x * w)
y = int(y * h)
l_ = max(0, x - length)
b_ = max(0, y - length)
r_ = min(w, x + length + 1)
t_ = min(h, y + length + 1)
gird[b_:t_, l_:r_, 2] = r
gird[b_:t_, l_:r_, 1] = g
gird[b_:t_, l_:r_, 0] = b
gird[b_:t_, l_:r_, 3] = 255.0
return gird
def s_enhance(x, k=2.0):
p = cv2.cvtColor(x, cv2.COLOR_RGB2HSV).astype(np.float)
p[:, :, 1] *= k
p = p.clip(0, 255).astype(np.uint8)
return cv2.cvtColor(p, cv2.COLOR_HSV2RGB).clip(0, 255)
def emph_line(x, y, c):
a = x.astype(np.float32)
b = y.astype(np.float32)[:, :, None] / 255.0
c = np.tile(c[None, None, ::-1], [a.shape[0], a.shape[1], 1])
return (a * b + c * (1 - b)).clip(0, 255).astype(np.uint8)
def ini_hint(x):
r = np.zeros(shape=(x.shape[0], x.shape[1], 4), dtype=np.float32)
return r
def de_line(x, y):
a = x.astype(np.float32)
b = y.astype(np.float32)[:, :, None] / 255.0
c = np.tile(np.array([255, 255, 255])[None, None, ::-1], [a.shape[0], a.shape[1], 1])
return (a * b + c * (1 - b)).clip(0, 255).astype(np.uint8)
def blur_line(x, y):
o = x.astype(np.float32)
b = cv2.GaussianBlur(x, (3, 3), 0).astype(np.float32)
k = y.astype(np.float32)[:, :, None] / 255.0
return (o * k + b * (1 - k)).clip(0, 255).astype(np.uint8)
def clip_15(x, s=15.0):
return ((x - s) / (255.0 - s - s)).clip(0, 1) * 255.0
def cv_denoise(x):
return cv2.fastNlMeansDenoisingColored(x, None, 3, 3, 7, 21)