Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ConverterMasked.py 14 KB

You have to be logged in to leave a comment. Sign In
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
  1. from models import ConverterBase
  2. from facelib import LandmarksProcessor
  3. from facelib import FaceType
  4. import cv2
  5. import numpy as np
  6. from utils import image_utils
  7. from utils.console_utils import *
  8. class ConverterMasked(ConverterBase):
  9. #override
  10. def __init__(self, predictor,
  11. predictor_input_size=0,
  12. output_size=0,
  13. face_type=FaceType.FULL,
  14. base_erode_mask_modifier = 0,
  15. base_blur_mask_modifier = 0,
  16. **in_options):
  17. super().__init__(predictor)
  18. self.predictor_input_size = predictor_input_size
  19. self.output_size = output_size
  20. self.face_type = face_type
  21. self.TFLabConverter = None
  22. mode = input_int ("Choose mode: (1) overlay, (2) hist match, (3) hist match bw, (4) seamless (default), (5) seamless hist match, (6) raw : ", 4)
  23. self.mode = {1:'overlay',
  24. 2:'hist-match',
  25. 3:'hist-match-bw',
  26. 4:'seamless',
  27. 5:'seamless-hist-match',
  28. 6:'raw'}.get (mode, 'seamless')
  29. if self.mode == 'raw':
  30. mode = input_int ("Choose raw mode: (1) rgb, (2) rgb+mask (default), (3) mask only, (4) predicted only : ", 2)
  31. self.raw_mode = {1:'rgb',
  32. 2:'rgb-mask',
  33. 3:'mask-only',
  34. 4:'predicted-only'}.get (mode, 'rgb-mask')
  35. if self.mode != 'raw':
  36. if self.mode == 'hist-match' or self.mode == 'hist-match-bw':
  37. self.masked_hist_match = input_bool("Masked hist match? (y/n skip:y) : ", True)
  38. if self.mode == 'hist-match' or self.mode == 'hist-match-bw' or self.mode == 'seamless-hist-match':
  39. self.hist_match_threshold = np.clip ( input_int("Hist match threshold [0..255] (skip:255) : ", 255), 0, 255)
  40. self.use_predicted_mask = input_bool("Use predicted mask? (y/n skip:y) : ", True)
  41. if self.mode != 'raw':
  42. self.erode_mask_modifier = base_erode_mask_modifier + np.clip ( input_int ("Choose erode mask modifier [-200..200] (skip:0) : ", 0), -200, 200)
  43. self.blur_mask_modifier = base_blur_mask_modifier + np.clip ( input_int ("Choose blur mask modifier [-200..200] (skip:0) : ", 0), -200, 200)
  44. self.seamless_erode_mask_modifier = 0
  45. if self.mode == 'seamless' or self.mode == 'seamless-hist-match':
  46. self.seamless_erode_mask_modifier = np.clip ( input_int ("Choose seamless erode mask modifier [-100..100] (skip:0) : ", 0), -100, 100)
  47. self.output_face_scale = np.clip ( 1.0 + input_int ("Choose output face scale modifier [-50..50] (skip:0) : ", 0)*0.01, 0.5, 1.5)
  48. if self.mode != 'raw':
  49. self.transfercolor = input_bool("Transfer color from dst face to converted final face? (y/n skip:n) : ", False)
  50. self.final_image_color_degrade_power = np.clip ( input_int ("Degrade color power of final image [0..100] (skip:0) : ", 0), 0, 100)
  51. self.alpha = input_bool("Export png with alpha channel? (y/n skip:n) : ", False)
  52. print ("")
  53. #override
  54. def get_mode(self):
  55. return ConverterBase.MODE_FACE
  56. #override
  57. def dummy_predict(self):
  58. self.predictor ( np.zeros ( (self.predictor_input_size,self.predictor_input_size,4), dtype=np.float32 ) )
  59. #override
  60. def convert_face (self, img_bgr, img_face_landmarks, debug):
  61. if debug:
  62. debugs = [img_bgr.copy()]
  63. img_size = img_bgr.shape[1], img_bgr.shape[0]
  64. img_face_mask_a = LandmarksProcessor.get_image_hull_mask (img_bgr, img_face_landmarks)
  65. face_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, self.output_size, face_type=self.face_type)
  66. face_output_mat = LandmarksProcessor.get_transform_mat (img_face_landmarks, self.output_size, face_type=self.face_type, scale=self.output_face_scale)
  67. dst_face_bgr = cv2.warpAffine( img_bgr , face_mat, (self.output_size, self.output_size), flags=cv2.INTER_LANCZOS4 )
  68. dst_face_mask_a_0 = cv2.warpAffine( img_face_mask_a, face_mat, (self.output_size, self.output_size), flags=cv2.INTER_LANCZOS4 )
  69. predictor_input_bgr = cv2.resize (dst_face_bgr, (self.predictor_input_size,self.predictor_input_size))
  70. predictor_input_mask_a_0 = cv2.resize (dst_face_mask_a_0, (self.predictor_input_size,self.predictor_input_size))
  71. predictor_input_mask_a = np.expand_dims (predictor_input_mask_a_0, -1)
  72. predicted_bgra = self.predictor ( np.concatenate( (predictor_input_bgr, predictor_input_mask_a), -1) )
  73. prd_face_bgr = np.clip (predicted_bgra[:,:,0:3], 0, 1.0 )
  74. prd_face_mask_a_0 = np.clip (predicted_bgra[:,:,3], 0.0, 1.0)
  75. if not self.use_predicted_mask:
  76. prd_face_mask_a_0 = predictor_input_mask_a_0
  77. prd_face_mask_a_0[ prd_face_mask_a_0 < 0.001 ] = 0.0
  78. prd_face_mask_a = np.expand_dims (prd_face_mask_a_0, axis=-1)
  79. prd_face_mask_aaa = np.repeat (prd_face_mask_a, (3,), axis=-1)
  80. img_prd_face_mask_aaa = cv2.warpAffine( prd_face_mask_aaa, face_output_mat, img_size, np.zeros(img_bgr.shape, dtype=float), flags=cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4 )
  81. img_prd_face_mask_aaa = np.clip (img_prd_face_mask_aaa, 0.0, 1.0)
  82. img_face_mask_aaa = img_prd_face_mask_aaa
  83. if debug:
  84. debugs += [img_face_mask_aaa.copy()]
  85. img_face_mask_aaa [ img_face_mask_aaa <= 0.1 ] = 0.0
  86. img_face_mask_flatten_aaa = img_face_mask_aaa.copy()
  87. img_face_mask_flatten_aaa[img_face_mask_flatten_aaa > 0.9] = 1.0
  88. maxregion = np.argwhere(img_face_mask_flatten_aaa==1.0)
  89. out_img = img_bgr.copy()
  90. if self.mode == 'raw':
  91. if self.raw_mode == 'rgb' or self.raw_mode == 'rgb-mask':
  92. out_img = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, out_img, cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
  93. if self.raw_mode == 'rgb-mask':
  94. out_img = np.concatenate ( [out_img, np.expand_dims (img_face_mask_aaa[:,:,0],-1)], -1 )
  95. if self.raw_mode == 'mask-only':
  96. out_img = img_face_mask_aaa
  97. if self.raw_mode == 'predicted-only':
  98. out_img = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, np.zeros(out_img.shape), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
  99. else:
  100. if maxregion.size != 0:
  101. miny,minx = maxregion.min(axis=0)[:2]
  102. maxy,maxx = maxregion.max(axis=0)[:2]
  103. if debug:
  104. print ("maxregion.size: %d, minx:%d, maxx:%d miny:%d, maxy:%d" % (maxregion.size, minx, maxx, miny, maxy ) )
  105. lenx = maxx - minx
  106. leny = maxy - miny
  107. if lenx >= 4 and leny >= 4:
  108. masky = int(minx+(lenx//2))
  109. maskx = int(miny+(leny//2))
  110. lowest_len = min (lenx, leny)
  111. if debug:
  112. print ("lowest_len = %f" % (lowest_len) )
  113. img_mask_blurry_aaa = img_face_mask_aaa
  114. if self.erode_mask_modifier != 0:
  115. ero = int( lowest_len * ( 0.126 - lowest_len * 0.00004551365 ) * 0.01*self.erode_mask_modifier )
  116. if debug:
  117. print ("erode_size = %d" % (ero) )
  118. if ero > 0:
  119. img_mask_blurry_aaa = cv2.erode(img_mask_blurry_aaa, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(ero,ero)), iterations = 1 )
  120. elif ero < 0:
  121. img_mask_blurry_aaa = cv2.dilate(img_mask_blurry_aaa, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(-ero,-ero)), iterations = 1 )
  122. if self.seamless_erode_mask_modifier != 0:
  123. ero = int( lowest_len * ( 0.126 - lowest_len * 0.00004551365 ) * 0.01*self.seamless_erode_mask_modifier )
  124. if debug:
  125. print ("seamless_erode_size = %d" % (ero) )
  126. if ero > 0:
  127. img_face_mask_flatten_aaa = cv2.erode(img_face_mask_flatten_aaa, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(ero,ero)), iterations = 1 )
  128. elif ero < 0:
  129. img_face_mask_flatten_aaa = cv2.dilate(img_face_mask_flatten_aaa, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(-ero,-ero)), iterations = 1 )
  130. if self.blur_mask_modifier > 0:
  131. blur = int( lowest_len * 0.10 * 0.01*self.blur_mask_modifier )
  132. if debug:
  133. print ("blur_size = %d" % (blur) )
  134. if blur > 0:
  135. img_mask_blurry_aaa = cv2.blur(img_mask_blurry_aaa, (blur, blur) )
  136. img_mask_blurry_aaa = np.clip( img_mask_blurry_aaa, 0, 1.0 )
  137. if self.mode == 'hist-match-bw':
  138. prd_face_bgr = cv2.cvtColor(prd_face_bgr, cv2.COLOR_BGR2GRAY)
  139. prd_face_bgr = np.repeat( np.expand_dims (prd_face_bgr, -1), (3,), -1 )
  140. if self.mode == 'hist-match' or self.mode == 'hist-match-bw':
  141. if debug:
  142. debugs += [ cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, np.zeros(img_bgr.shape, dtype=np.float32), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT ) ]
  143. hist_mask_a = np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=prd_face_bgr.dtype)
  144. if self.masked_hist_match:
  145. hist_mask_a *= prd_face_mask_a
  146. hist_match_1 = prd_face_bgr*hist_mask_a + (1.0-hist_mask_a)* np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=prd_face_bgr.dtype)
  147. hist_match_1[ hist_match_1 > 1.0 ] = 1.0
  148. hist_match_2 = dst_face_bgr*hist_mask_a + (1.0-hist_mask_a)* np.ones ( prd_face_bgr.shape[:2] + (1,) , dtype=prd_face_bgr.dtype)
  149. hist_match_2[ hist_match_1 > 1.0 ] = 1.0
  150. prd_face_bgr = image_utils.color_hist_match(hist_match_1, hist_match_2, self.hist_match_threshold )
  151. if self.mode == 'hist-match-bw':
  152. prd_face_bgr = prd_face_bgr.astype(np.float32)
  153. out_img = cv2.warpAffine( prd_face_bgr, face_output_mat, img_size, out_img, cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
  154. if debug:
  155. debugs += [out_img.copy()]
  156. debugs += [img_mask_blurry_aaa.copy()]
  157. if self.mode == 'overlay':
  158. pass
  159. if self.mode == 'seamless' or self.mode == 'seamless-hist-match':
  160. out_img = np.clip( img_bgr*(1-img_face_mask_aaa) + (out_img*img_face_mask_aaa) , 0, 1.0 )
  161. if debug:
  162. debugs += [out_img.copy()]
  163. out_img = cv2.seamlessClone( (out_img*255).astype(np.uint8), (img_bgr*255).astype(np.uint8), (img_face_mask_flatten_aaa*255).astype(np.uint8), (masky,maskx) , cv2.NORMAL_CLONE )
  164. out_img = out_img.astype(np.float32) / 255.0
  165. if debug:
  166. debugs += [out_img.copy()]
  167. out_img = np.clip( img_bgr*(1-img_mask_blurry_aaa) + (out_img*img_mask_blurry_aaa) , 0, 1.0 )
  168. if self.mode == 'seamless-hist-match':
  169. out_face_bgr = cv2.warpAffine( out_img, face_mat, (self.output_size, self.output_size) )
  170. new_out_face_bgr = image_utils.color_hist_match(out_face_bgr, dst_face_bgr, self.hist_match_threshold)
  171. new_out = cv2.warpAffine( new_out_face_bgr, face_mat, img_size, img_bgr.copy(), cv2.WARP_INVERSE_MAP | cv2.INTER_LANCZOS4, cv2.BORDER_TRANSPARENT )
  172. out_img = np.clip( img_bgr*(1-img_mask_blurry_aaa) + (new_out*img_mask_blurry_aaa) , 0, 1.0 )
  173. if self.transfercolor:
  174. if self.TFLabConverter is None:
  175. self.TFLabConverter = image_utils.TFLabConverter()
  176. img_lab_l, img_lab_a, img_lab_b = np.split ( self.TFLabConverter.bgr2lab (img_bgr), 3, axis=-1 )
  177. out_img_lab_l, out_img_lab_a, out_img_lab_b = np.split ( self.TFLabConverter.bgr2lab (out_img), 3, axis=-1 )
  178. out_img = self.TFLabConverter.lab2bgr ( np.concatenate([out_img_lab_l, img_lab_a, img_lab_b], axis=-1) )
  179. if self.final_image_color_degrade_power != 0:
  180. if debug:
  181. debugs += [out_img.copy()]
  182. out_img_reduced = image_utils.reduce_colors(out_img, 256)
  183. if self.final_image_color_degrade_power == 100:
  184. out_img = out_img_reduced
  185. else:
  186. alpha = self.final_image_color_degrade_power / 100.0
  187. out_img = (out_img*(1.0-alpha) + out_img_reduced*alpha)
  188. if self.alpha:
  189. out_img = np.concatenate ( [out_img, np.expand_dims (img_mask_blurry_aaa[:,:,0],-1)], -1 )
  190. out_img = np.clip (out_img, 0.0, 1.0 )
  191. if debug:
  192. debugs += [out_img.copy()]
  193. return debugs if debug else out_img
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...