Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/merger/MergeMasked.py
628 views
1
import sys
2
import traceback
3
4
import cv2
5
import numpy as np
6
7
from core import imagelib
8
from core.cv2ex import *
9
from core.interact import interact as io
10
from facelib import FaceType, LandmarksProcessor
11
12
is_windows = sys.platform[0:3] == 'win'
13
xseg_input_size = 256
14
15
def MergeMaskedFace (predictor_func, predictor_input_shape,
16
face_enhancer_func,
17
xseg_256_extract_func,
18
cfg, frame_info, img_bgr_uint8, img_bgr, img_face_landmarks):
19
20
img_size = img_bgr.shape[1], img_bgr.shape[0]
21
img_face_mask_a = LandmarksProcessor.get_image_hull_mask (img_bgr.shape, img_face_landmarks)
22
23
input_size = predictor_input_shape[0]
24
mask_subres_size = input_size*4
25
output_size = input_size
26
if cfg.super_resolution_power != 0:
27
output_size *= 4
28
29
face_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, output_size, face_type=cfg.face_type)
30
face_output_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, output_size, face_type=cfg.face_type, scale= 1.0 + 0.01*cfg.output_face_scale)
31
32
if mask_subres_size == output_size:
33
face_mask_output_mat = face_output_mat
34
else:
35
face_mask_output_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, mask_subres_size, face_type=cfg.face_type, scale= 1.0 + 0.01*cfg.output_face_scale)
36
37
dst_face_bgr = cv2.warpAffine( img_bgr , face_mat, (output_size, output_size), flags=cv2.INTER_CUBIC )
38
dst_face_bgr = np.clip(dst_face_bgr, 0, 1)
39
40
dst_face_mask_a_0 = cv2.warpAffine( img_face_mask_a, face_mat, (output_size, output_size), flags=cv2.INTER_CUBIC )
41
dst_face_mask_a_0 = np.clip(dst_face_mask_a_0, 0, 1)
42
43
predictor_input_bgr = cv2.resize (dst_face_bgr, (input_size,input_size) )
44
45
predicted = predictor_func (predictor_input_bgr)
46
prd_face_bgr = np.clip (predicted[0], 0, 1.0)
47
prd_face_mask_a_0 = np.clip (predicted[1], 0, 1.0)
48
prd_face_dst_mask_a_0 = np.clip (predicted[2], 0, 1.0)
49
50
if cfg.super_resolution_power != 0:
51
prd_face_bgr_enhanced = face_enhancer_func(prd_face_bgr, is_tanh=True, preserve_size=False)
52
mod = cfg.super_resolution_power / 100.0
53
prd_face_bgr = cv2.resize(prd_face_bgr, (output_size,output_size))*(1.0-mod) + prd_face_bgr_enhanced*mod
54
prd_face_bgr = np.clip(prd_face_bgr, 0, 1)
55
56
if cfg.super_resolution_power != 0:
57
prd_face_mask_a_0 = cv2.resize (prd_face_mask_a_0, (output_size, output_size), interpolation=cv2.INTER_CUBIC)
58
prd_face_dst_mask_a_0 = cv2.resize (prd_face_dst_mask_a_0, (output_size, output_size), interpolation=cv2.INTER_CUBIC)
59
60
if cfg.mask_mode == 0: #full
61
wrk_face_mask_a_0 = np.ones_like(dst_face_mask_a_0)
62
elif cfg.mask_mode == 1: #dst
63
wrk_face_mask_a_0 = cv2.resize (dst_face_mask_a_0, (output_size,output_size), interpolation=cv2.INTER_CUBIC)
64
elif cfg.mask_mode == 2: #learned-prd
65
wrk_face_mask_a_0 = prd_face_mask_a_0
66
elif cfg.mask_mode == 3: #learned-dst
67
wrk_face_mask_a_0 = prd_face_dst_mask_a_0
68
elif cfg.mask_mode == 4: #learned-prd*learned-dst
69
wrk_face_mask_a_0 = prd_face_mask_a_0*prd_face_dst_mask_a_0
70
elif cfg.mask_mode == 5: #learned-prd+learned-dst
71
wrk_face_mask_a_0 = np.clip( prd_face_mask_a_0+prd_face_dst_mask_a_0, 0, 1)
72
elif cfg.mask_mode >= 6 and cfg.mask_mode <= 9: #XSeg modes
73
if cfg.mask_mode == 6 or cfg.mask_mode == 8 or cfg.mask_mode == 9:
74
# obtain XSeg-prd
75
prd_face_xseg_bgr = cv2.resize (prd_face_bgr, (xseg_input_size,)*2, interpolation=cv2.INTER_CUBIC)
76
prd_face_xseg_mask = xseg_256_extract_func(prd_face_xseg_bgr)
77
X_prd_face_mask_a_0 = cv2.resize ( prd_face_xseg_mask, (output_size, output_size), interpolation=cv2.INTER_CUBIC)
78
79
if cfg.mask_mode >= 7 and cfg.mask_mode <= 9:
80
# obtain XSeg-dst
81
xseg_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, xseg_input_size, face_type=cfg.face_type)
82
dst_face_xseg_bgr = cv2.warpAffine(img_bgr, xseg_mat, (xseg_input_size,)*2, flags=cv2.INTER_CUBIC )
83
dst_face_xseg_mask = xseg_256_extract_func(dst_face_xseg_bgr)
84
X_dst_face_mask_a_0 = cv2.resize (dst_face_xseg_mask, (output_size,output_size), interpolation=cv2.INTER_CUBIC)
85
86
if cfg.mask_mode == 6: #'XSeg-prd'
87
wrk_face_mask_a_0 = X_prd_face_mask_a_0
88
elif cfg.mask_mode == 7: #'XSeg-dst'
89
wrk_face_mask_a_0 = X_dst_face_mask_a_0
90
elif cfg.mask_mode == 8: #'XSeg-prd*XSeg-dst'
91
wrk_face_mask_a_0 = X_prd_face_mask_a_0 * X_dst_face_mask_a_0
92
elif cfg.mask_mode == 9: #learned-prd*learned-dst*XSeg-prd*XSeg-dst
93
wrk_face_mask_a_0 = prd_face_mask_a_0 * prd_face_dst_mask_a_0 * X_prd_face_mask_a_0 * X_dst_face_mask_a_0
94
95
wrk_face_mask_a_0[ wrk_face_mask_a_0 < (1.0/255.0) ] = 0.0 # get rid of noise
96
97
# resize to mask_subres_size
98
if wrk_face_mask_a_0.shape[0] != mask_subres_size:
99
wrk_face_mask_a_0 = cv2.resize (wrk_face_mask_a_0, (mask_subres_size, mask_subres_size), interpolation=cv2.INTER_CUBIC)
100
101
# process mask in local predicted space
102
if 'raw' not in cfg.mode:
103
# add zero pad
104
wrk_face_mask_a_0 = np.pad (wrk_face_mask_a_0, input_size)
105
106
ero = cfg.erode_mask_modifier
107
blur = cfg.blur_mask_modifier
108
109
if ero > 0:
110
wrk_face_mask_a_0 = cv2.erode(wrk_face_mask_a_0, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(ero,ero)), iterations = 1 )
111
elif ero < 0:
112
wrk_face_mask_a_0 = cv2.dilate(wrk_face_mask_a_0, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(-ero,-ero)), iterations = 1 )
113
114
# clip eroded/dilated mask in actual predict area
115
# pad with half blur size in order to accuratelly fade to zero at the boundary
116
clip_size = input_size + blur // 2
117
118
wrk_face_mask_a_0[:clip_size,:] = 0
119
wrk_face_mask_a_0[-clip_size:,:] = 0
120
wrk_face_mask_a_0[:,:clip_size] = 0
121
wrk_face_mask_a_0[:,-clip_size:] = 0
122
123
if blur > 0:
124
blur = blur + (1-blur % 2)
125
wrk_face_mask_a_0 = cv2.GaussianBlur(wrk_face_mask_a_0, (blur, blur) , 0)
126
127
wrk_face_mask_a_0 = wrk_face_mask_a_0[input_size:-input_size,input_size:-input_size]
128
129
wrk_face_mask_a_0 = np.clip(wrk_face_mask_a_0, 0, 1)
130
131
img_face_mask_a = cv2.warpAffine( wrk_face_mask_a_0, face_mask_output_mat, img_size, np.zeros(img_bgr.shape[0:2], dtype=np.float32), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC )[...,None]
132
img_face_mask_a = np.clip (img_face_mask_a, 0.0, 1.0)
133
img_face_mask_a [ img_face_mask_a < (1.0/255.0) ] = 0.0 # get rid of noise
134
135
if wrk_face_mask_a_0.shape[0] != output_size:
136
wrk_face_mask_a_0 = cv2.resize (wrk_face_mask_a_0, (output_size,output_size), interpolation=cv2.INTER_CUBIC)
137
138
wrk_face_mask_a = wrk_face_mask_a_0[...,None]
139
140
out_img = None
141
out_merging_mask_a = None
142
if cfg.mode == 'original':
143
return img_bgr, img_face_mask_a
144
145
elif 'raw' in cfg.mode:
146
if cfg.mode == 'raw-rgb':
147
out_img_face = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, np.empty_like(img_bgr), cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC)
148
out_img_face_mask = cv2.warpAffine( np.ones_like(prd_face_bgr), face_output_mat, img_size, np.empty_like(img_bgr), cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC)
149
out_img = img_bgr*(1-out_img_face_mask) + out_img_face*out_img_face_mask
150
out_merging_mask_a = img_face_mask_a
151
elif cfg.mode == 'raw-predict':
152
out_img = prd_face_bgr
153
out_merging_mask_a = wrk_face_mask_a
154
else:
155
raise ValueError(f"undefined raw type {cfg.mode}")
156
157
out_img = np.clip (out_img, 0.0, 1.0 )
158
else:
159
160
# Process if the mask meets minimum size
161
maxregion = np.argwhere( img_face_mask_a >= 0.1 )
162
if maxregion.size != 0:
163
miny,minx = maxregion.min(axis=0)[:2]
164
maxy,maxx = maxregion.max(axis=0)[:2]
165
lenx = maxx - minx
166
leny = maxy - miny
167
if min(lenx,leny) >= 4:
168
wrk_face_mask_area_a = wrk_face_mask_a.copy()
169
wrk_face_mask_area_a[wrk_face_mask_area_a>0] = 1.0
170
171
if 'seamless' not in cfg.mode and cfg.color_transfer_mode != 0:
172
if cfg.color_transfer_mode == 1: #rct
173
prd_face_bgr = imagelib.reinhard_color_transfer (prd_face_bgr, dst_face_bgr, target_mask=wrk_face_mask_area_a, source_mask=wrk_face_mask_area_a)
174
elif cfg.color_transfer_mode == 2: #lct
175
prd_face_bgr = imagelib.linear_color_transfer (prd_face_bgr, dst_face_bgr)
176
elif cfg.color_transfer_mode == 3: #mkl
177
prd_face_bgr = imagelib.color_transfer_mkl (prd_face_bgr, dst_face_bgr)
178
elif cfg.color_transfer_mode == 4: #mkl-m
179
prd_face_bgr = imagelib.color_transfer_mkl (prd_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
180
elif cfg.color_transfer_mode == 5: #idt
181
prd_face_bgr = imagelib.color_transfer_idt (prd_face_bgr, dst_face_bgr)
182
elif cfg.color_transfer_mode == 6: #idt-m
183
prd_face_bgr = imagelib.color_transfer_idt (prd_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
184
elif cfg.color_transfer_mode == 7: #sot-m
185
prd_face_bgr = imagelib.color_transfer_sot (prd_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a, steps=10, batch_size=30)
186
prd_face_bgr = np.clip (prd_face_bgr, 0.0, 1.0)
187
elif cfg.color_transfer_mode == 8: #mix-m
188
prd_face_bgr = imagelib.color_transfer_mix (prd_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
189
190
if cfg.mode == 'hist-match':
191
hist_mask_a = np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=np.float32)
192
193
if cfg.masked_hist_match:
194
hist_mask_a *= wrk_face_mask_area_a
195
196
white = (1.0-hist_mask_a)* np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=np.float32)
197
198
hist_match_1 = prd_face_bgr*hist_mask_a + white
199
hist_match_1[ hist_match_1 > 1.0 ] = 1.0
200
201
hist_match_2 = dst_face_bgr*hist_mask_a + white
202
hist_match_2[ hist_match_1 > 1.0 ] = 1.0
203
204
prd_face_bgr = imagelib.color_hist_match(hist_match_1, hist_match_2, cfg.hist_match_threshold ).astype(dtype=np.float32)
205
206
if 'seamless' in cfg.mode:
207
#mask used for cv2.seamlessClone
208
img_face_seamless_mask_a = None
209
for i in range(1,10):
210
a = img_face_mask_a > i / 10.0
211
if len(np.argwhere(a)) == 0:
212
continue
213
img_face_seamless_mask_a = img_face_mask_a.copy()
214
img_face_seamless_mask_a[a] = 1.0
215
img_face_seamless_mask_a[img_face_seamless_mask_a <= i / 10.0] = 0.0
216
break
217
218
out_img = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, np.empty_like(img_bgr), cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC )
219
out_img = np.clip(out_img, 0.0, 1.0)
220
221
if 'seamless' in cfg.mode:
222
try:
223
#calc same bounding rect and center point as in cv2.seamlessClone to prevent jittering (not flickering)
224
l,t,w,h = cv2.boundingRect( (img_face_seamless_mask_a*255).astype(np.uint8) )
225
s_maskx, s_masky = int(l+w/2), int(t+h/2)
226
out_img = cv2.seamlessClone( (out_img*255).astype(np.uint8), img_bgr_uint8, (img_face_seamless_mask_a*255).astype(np.uint8), (s_maskx,s_masky) , cv2.NORMAL_CLONE )
227
out_img = out_img.astype(dtype=np.float32) / 255.0
228
except Exception as e:
229
#seamlessClone may fail in some cases
230
e_str = traceback.format_exc()
231
232
if 'MemoryError' in e_str:
233
raise Exception("Seamless fail: " + e_str) #reraise MemoryError in order to reprocess this data by other processes
234
else:
235
print ("Seamless fail: " + e_str)
236
237
cfg_mp = cfg.motion_blur_power / 100.0
238
239
out_img = img_bgr*(1-img_face_mask_a) + (out_img*img_face_mask_a)
240
241
if ('seamless' in cfg.mode and cfg.color_transfer_mode != 0) or \
242
cfg.mode == 'seamless-hist-match' or \
243
cfg_mp != 0 or \
244
cfg.blursharpen_amount != 0 or \
245
cfg.image_denoise_power != 0 or \
246
cfg.bicubic_degrade_power != 0:
247
248
out_face_bgr = cv2.warpAffine( out_img, face_mat, (output_size, output_size), flags=cv2.INTER_CUBIC )
249
250
if 'seamless' in cfg.mode and cfg.color_transfer_mode != 0:
251
if cfg.color_transfer_mode == 1:
252
out_face_bgr = imagelib.reinhard_color_transfer (out_face_bgr, dst_face_bgr, target_mask=wrk_face_mask_area_a, source_mask=wrk_face_mask_area_a)
253
elif cfg.color_transfer_mode == 2: #lct
254
out_face_bgr = imagelib.linear_color_transfer (out_face_bgr, dst_face_bgr)
255
elif cfg.color_transfer_mode == 3: #mkl
256
out_face_bgr = imagelib.color_transfer_mkl (out_face_bgr, dst_face_bgr)
257
elif cfg.color_transfer_mode == 4: #mkl-m
258
out_face_bgr = imagelib.color_transfer_mkl (out_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
259
elif cfg.color_transfer_mode == 5: #idt
260
out_face_bgr = imagelib.color_transfer_idt (out_face_bgr, dst_face_bgr)
261
elif cfg.color_transfer_mode == 6: #idt-m
262
out_face_bgr = imagelib.color_transfer_idt (out_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
263
elif cfg.color_transfer_mode == 7: #sot-m
264
out_face_bgr = imagelib.color_transfer_sot (out_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a, steps=10, batch_size=30)
265
out_face_bgr = np.clip (out_face_bgr, 0.0, 1.0)
266
elif cfg.color_transfer_mode == 8: #mix-m
267
out_face_bgr = imagelib.color_transfer_mix (out_face_bgr*wrk_face_mask_area_a, dst_face_bgr*wrk_face_mask_area_a)
268
269
if cfg.mode == 'seamless-hist-match':
270
out_face_bgr = imagelib.color_hist_match(out_face_bgr, dst_face_bgr, cfg.hist_match_threshold)
271
272
if cfg_mp != 0:
273
k_size = int(frame_info.motion_power*cfg_mp)
274
if k_size >= 1:
275
k_size = np.clip (k_size+1, 2, 50)
276
if cfg.super_resolution_power != 0:
277
k_size *= 2
278
out_face_bgr = imagelib.LinearMotionBlur (out_face_bgr, k_size , frame_info.motion_deg)
279
280
if cfg.blursharpen_amount != 0:
281
out_face_bgr = imagelib.blursharpen ( out_face_bgr, cfg.sharpen_mode, 3, cfg.blursharpen_amount)
282
283
if cfg.image_denoise_power != 0:
284
n = cfg.image_denoise_power
285
while n > 0:
286
img_bgr_denoised = cv2.medianBlur(img_bgr, 5)
287
if int(n / 100) != 0:
288
img_bgr = img_bgr_denoised
289
else:
290
pass_power = (n % 100) / 100.0
291
img_bgr = img_bgr*(1.0-pass_power)+img_bgr_denoised*pass_power
292
n = max(n-10,0)
293
294
if cfg.bicubic_degrade_power != 0:
295
p = 1.0 - cfg.bicubic_degrade_power / 101.0
296
img_bgr_downscaled = cv2.resize (img_bgr, ( int(img_size[0]*p), int(img_size[1]*p ) ), interpolation=cv2.INTER_CUBIC)
297
img_bgr = cv2.resize (img_bgr_downscaled, img_size, interpolation=cv2.INTER_CUBIC)
298
299
new_out = cv2.warpAffine( out_face_bgr, face_mat, img_size, np.empty_like(img_bgr), cv2.WARP_INVERSE_MAP | cv2.INTER_CUBIC )
300
301
out_img = np.clip( img_bgr*(1-img_face_mask_a) + (new_out*img_face_mask_a) , 0, 1.0 )
302
303
if cfg.color_degrade_power != 0:
304
out_img_reduced = imagelib.reduce_colors(out_img, 256)
305
if cfg.color_degrade_power == 100:
306
out_img = out_img_reduced
307
else:
308
alpha = cfg.color_degrade_power / 100.0
309
out_img = (out_img*(1.0-alpha) + out_img_reduced*alpha)
310
out_merging_mask_a = img_face_mask_a
311
312
if out_img is None:
313
out_img = img_bgr.copy()
314
315
return out_img, out_merging_mask_a
316
317
318
def MergeMasked (predictor_func,
319
predictor_input_shape,
320
face_enhancer_func,
321
xseg_256_extract_func,
322
cfg,
323
frame_info):
324
img_bgr_uint8 = cv2_imread(frame_info.filepath)
325
img_bgr_uint8 = imagelib.normalize_channels (img_bgr_uint8, 3)
326
img_bgr = img_bgr_uint8.astype(np.float32) / 255.0
327
328
outs = []
329
for face_num, img_landmarks in enumerate( frame_info.landmarks_list ):
330
out_img, out_img_merging_mask = MergeMaskedFace (predictor_func, predictor_input_shape, face_enhancer_func, xseg_256_extract_func, cfg, frame_info, img_bgr_uint8, img_bgr, img_landmarks)
331
outs += [ (out_img, out_img_merging_mask) ]
332
333
#Combining multiple face outputs
334
final_img = None
335
final_mask = None
336
for img, merging_mask in outs:
337
h,w,c = img.shape
338
339
if final_img is None:
340
final_img = img
341
final_mask = merging_mask
342
else:
343
final_img = final_img*(1-merging_mask) + img*merging_mask
344
final_mask = np.clip (final_mask + merging_mask, 0, 1 )
345
346
final_img = np.concatenate ( [final_img, final_mask], -1)
347
348
return (final_img*255).astype(np.uint8)
349
350