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

MergerConfig.py 13 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
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
  1. import numpy as np
  2. import copy
  3. from facelib import FaceType
  4. from core.interact import interact as io
  5. class MergerConfig(object):
  6. TYPE_NONE = 0
  7. TYPE_MASKED = 1
  8. TYPE_FACE_AVATAR = 2
  9. ####
  10. TYPE_IMAGE = 3
  11. TYPE_IMAGE_WITH_LANDMARKS = 4
  12. def __init__(self, type=0,
  13. sharpen_mode=0,
  14. blursharpen_amount=0,
  15. **kwargs
  16. ):
  17. self.type = type
  18. self.sharpen_dict = {0:"None", 1:'box', 2:'gaussian'}
  19. #default changeable params
  20. self.sharpen_mode = sharpen_mode
  21. self.blursharpen_amount = blursharpen_amount
  22. def copy(self):
  23. return copy.copy(self)
  24. #overridable
  25. def ask_settings(self):
  26. s = """Choose sharpen mode: \n"""
  27. for key in self.sharpen_dict.keys():
  28. s += f"""({key}) {self.sharpen_dict[key]}\n"""
  29. io.log_info(s)
  30. self.sharpen_mode = io.input_int ("", 0, valid_list=self.sharpen_dict.keys(), help_message="Enhance details by applying sharpen filter.")
  31. if self.sharpen_mode != 0:
  32. self.blursharpen_amount = np.clip ( io.input_int ("Choose blur/sharpen amount", 0, add_info="-100..100"), -100, 100 )
  33. def toggle_sharpen_mode(self):
  34. a = list( self.sharpen_dict.keys() )
  35. self.sharpen_mode = a[ (a.index(self.sharpen_mode)+1) % len(a) ]
  36. def add_blursharpen_amount(self, diff):
  37. self.blursharpen_amount = np.clip ( self.blursharpen_amount+diff, -100, 100)
  38. #overridable
  39. def get_config(self):
  40. d = self.__dict__.copy()
  41. d.pop('type')
  42. return d
  43. #overridable
  44. def __eq__(self, other):
  45. #check equality of changeable params
  46. if isinstance(other, MergerConfig):
  47. return self.sharpen_mode == other.sharpen_mode and \
  48. self.blursharpen_amount == other.blursharpen_amount
  49. return False
  50. #overridable
  51. def to_string(self, filename):
  52. r = ""
  53. r += f"sharpen_mode : {self.sharpen_dict[self.sharpen_mode]}\n"
  54. r += f"blursharpen_amount : {self.blursharpen_amount}\n"
  55. return r
  56. mode_dict = {0:'original',
  57. 1:'overlay',
  58. 2:'hist-match',
  59. 3:'seamless',
  60. 4:'seamless-hist-match',
  61. 5:'raw-rgb',
  62. 6:'raw-predict'}
  63. mode_str_dict = { mode_dict[key] : key for key in mode_dict.keys() }
  64. mask_mode_dict = {0:'full',
  65. 1:'dst',
  66. 2:'learned-prd',
  67. 3:'learned-dst',
  68. 4:'learned-prd*learned-dst',
  69. 5:'learned-prd+learned-dst',
  70. 6:'XSeg-prd',
  71. 7:'XSeg-dst',
  72. 8:'XSeg-prd*XSeg-dst',
  73. 9:'learned-prd*learned-dst*XSeg-prd*XSeg-dst'
  74. }
  75. ctm_dict = { 0: "None", 1:"rct", 2:"lct", 3:"mkl", 4:"mkl-m", 5:"idt", 6:"idt-m", 7:"sot-m", 8:"mix-m" }
  76. ctm_str_dict = {None:0, "rct":1, "lct":2, "mkl":3, "mkl-m":4, "idt":5, "idt-m":6, "sot-m":7, "mix-m":8 }
  77. class MergerConfigMasked(MergerConfig):
  78. def __init__(self, face_type=FaceType.FULL,
  79. default_mode = 'overlay',
  80. mode='overlay',
  81. masked_hist_match=True,
  82. hist_match_threshold = 238,
  83. mask_mode = 4,
  84. erode_mask_modifier = 0,
  85. blur_mask_modifier = 0,
  86. motion_blur_power = 0,
  87. output_face_scale = 0,
  88. super_resolution_power = 0,
  89. color_transfer_mode = ctm_str_dict['rct'],
  90. image_denoise_power = 0,
  91. bicubic_degrade_power = 0,
  92. color_degrade_power = 0,
  93. **kwargs
  94. ):
  95. super().__init__(type=MergerConfig.TYPE_MASKED, **kwargs)
  96. self.face_type = face_type
  97. if self.face_type not in [FaceType.HALF, FaceType.MID_FULL, FaceType.FULL, FaceType.WHOLE_FACE, FaceType.HEAD ]:
  98. raise ValueError("MergerConfigMasked does not support this type of face.")
  99. self.default_mode = default_mode
  100. #default changeable params
  101. if mode not in mode_str_dict:
  102. mode = mode_dict[1]
  103. self.mode = mode
  104. self.masked_hist_match = masked_hist_match
  105. self.hist_match_threshold = hist_match_threshold
  106. self.mask_mode = mask_mode
  107. self.erode_mask_modifier = erode_mask_modifier
  108. self.blur_mask_modifier = blur_mask_modifier
  109. self.motion_blur_power = motion_blur_power
  110. self.output_face_scale = output_face_scale
  111. self.super_resolution_power = super_resolution_power
  112. self.color_transfer_mode = color_transfer_mode
  113. self.image_denoise_power = image_denoise_power
  114. self.bicubic_degrade_power = bicubic_degrade_power
  115. self.color_degrade_power = color_degrade_power
  116. def copy(self):
  117. return copy.copy(self)
  118. def set_mode (self, mode):
  119. self.mode = mode_dict.get (mode, self.default_mode)
  120. def toggle_masked_hist_match(self):
  121. if self.mode == 'hist-match':
  122. self.masked_hist_match = not self.masked_hist_match
  123. def add_hist_match_threshold(self, diff):
  124. if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
  125. self.hist_match_threshold = np.clip ( self.hist_match_threshold+diff , 0, 255)
  126. def toggle_mask_mode(self):
  127. a = list( mask_mode_dict.keys() )
  128. self.mask_mode = a[ (a.index(self.mask_mode)+1) % len(a) ]
  129. def add_erode_mask_modifier(self, diff):
  130. self.erode_mask_modifier = np.clip ( self.erode_mask_modifier+diff , -400, 400)
  131. def add_blur_mask_modifier(self, diff):
  132. self.blur_mask_modifier = np.clip ( self.blur_mask_modifier+diff , 0, 400)
  133. def add_motion_blur_power(self, diff):
  134. self.motion_blur_power = np.clip ( self.motion_blur_power+diff, 0, 100)
  135. def add_output_face_scale(self, diff):
  136. self.output_face_scale = np.clip ( self.output_face_scale+diff , -50, 50)
  137. def toggle_color_transfer_mode(self):
  138. self.color_transfer_mode = (self.color_transfer_mode+1) % ( max(ctm_dict.keys())+1 )
  139. def add_super_resolution_power(self, diff):
  140. self.super_resolution_power = np.clip ( self.super_resolution_power+diff , 0, 100)
  141. def add_color_degrade_power(self, diff):
  142. self.color_degrade_power = np.clip ( self.color_degrade_power+diff , 0, 100)
  143. def add_image_denoise_power(self, diff):
  144. self.image_denoise_power = np.clip ( self.image_denoise_power+diff, 0, 500)
  145. def add_bicubic_degrade_power(self, diff):
  146. self.bicubic_degrade_power = np.clip ( self.bicubic_degrade_power+diff, 0, 100)
  147. def ask_settings(self):
  148. s = """Choose mode: \n"""
  149. for key in mode_dict.keys():
  150. s += f"""({key}) {mode_dict[key]}\n"""
  151. io.log_info(s)
  152. mode = io.input_int ("", mode_str_dict.get(self.default_mode, 1) )
  153. self.mode = mode_dict.get (mode, self.default_mode )
  154. if 'raw' not in self.mode:
  155. if self.mode == 'hist-match':
  156. self.masked_hist_match = io.input_bool("Masked hist match?", True)
  157. if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
  158. self.hist_match_threshold = np.clip ( io.input_int("Hist match threshold", 255, add_info="0..255"), 0, 255)
  159. s = """Choose mask mode: \n"""
  160. for key in mask_mode_dict.keys():
  161. s += f"""({key}) {mask_mode_dict[key]}\n"""
  162. io.log_info(s)
  163. self.mask_mode = io.input_int ("", 1, valid_list=mask_mode_dict.keys() )
  164. if 'raw' not in self.mode:
  165. self.erode_mask_modifier = np.clip ( io.input_int ("Choose erode mask modifier", 0, add_info="-400..400"), -400, 400)
  166. self.blur_mask_modifier = np.clip ( io.input_int ("Choose blur mask modifier", 0, add_info="0..400"), 0, 400)
  167. self.motion_blur_power = np.clip ( io.input_int ("Choose motion blur power", 0, add_info="0..100"), 0, 100)
  168. self.output_face_scale = np.clip (io.input_int ("Choose output face scale modifier", 0, add_info="-50..50" ), -50, 50)
  169. if 'raw' not in self.mode:
  170. self.color_transfer_mode = io.input_str ( "Color transfer to predicted face", None, valid_list=list(ctm_str_dict.keys())[1:] )
  171. self.color_transfer_mode = ctm_str_dict[self.color_transfer_mode]
  172. super().ask_settings()
  173. self.super_resolution_power = np.clip ( io.input_int ("Choose super resolution power", 0, add_info="0..100", help_message="Enhance details by applying superresolution network."), 0, 100)
  174. if 'raw' not in self.mode:
  175. self.image_denoise_power = np.clip ( io.input_int ("Choose image degrade by denoise power", 0, add_info="0..500"), 0, 500)
  176. self.bicubic_degrade_power = np.clip ( io.input_int ("Choose image degrade by bicubic rescale power", 0, add_info="0..100"), 0, 100)
  177. self.color_degrade_power = np.clip ( io.input_int ("Degrade color power of final image", 0, add_info="0..100"), 0, 100)
  178. io.log_info ("")
  179. def __eq__(self, other):
  180. #check equality of changeable params
  181. if isinstance(other, MergerConfigMasked):
  182. return super().__eq__(other) and \
  183. self.mode == other.mode and \
  184. self.masked_hist_match == other.masked_hist_match and \
  185. self.hist_match_threshold == other.hist_match_threshold and \
  186. self.mask_mode == other.mask_mode and \
  187. self.erode_mask_modifier == other.erode_mask_modifier and \
  188. self.blur_mask_modifier == other.blur_mask_modifier and \
  189. self.motion_blur_power == other.motion_blur_power and \
  190. self.output_face_scale == other.output_face_scale and \
  191. self.color_transfer_mode == other.color_transfer_mode and \
  192. self.super_resolution_power == other.super_resolution_power and \
  193. self.image_denoise_power == other.image_denoise_power and \
  194. self.bicubic_degrade_power == other.bicubic_degrade_power and \
  195. self.color_degrade_power == other.color_degrade_power
  196. return False
  197. def to_string(self, filename):
  198. r = (
  199. f"""MergerConfig {filename}:\n"""
  200. f"""Mode: {self.mode}\n"""
  201. )
  202. if self.mode == 'hist-match':
  203. r += f"""masked_hist_match: {self.masked_hist_match}\n"""
  204. if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
  205. r += f"""hist_match_threshold: {self.hist_match_threshold}\n"""
  206. r += f"""mask_mode: { mask_mode_dict[self.mask_mode] }\n"""
  207. if 'raw' not in self.mode:
  208. r += (f"""erode_mask_modifier: {self.erode_mask_modifier}\n"""
  209. f"""blur_mask_modifier: {self.blur_mask_modifier}\n"""
  210. f"""motion_blur_power: {self.motion_blur_power}\n""")
  211. r += f"""output_face_scale: {self.output_face_scale}\n"""
  212. if 'raw' not in self.mode:
  213. r += f"""color_transfer_mode: {ctm_dict[self.color_transfer_mode]}\n"""
  214. r += super().to_string(filename)
  215. r += f"""super_resolution_power: {self.super_resolution_power}\n"""
  216. if 'raw' not in self.mode:
  217. r += (f"""image_denoise_power: {self.image_denoise_power}\n"""
  218. f"""bicubic_degrade_power: {self.bicubic_degrade_power}\n"""
  219. f"""color_degrade_power: {self.color_degrade_power}\n""")
  220. r += "================"
  221. return r
  222. class MergerConfigFaceAvatar(MergerConfig):
  223. def __init__(self, temporal_face_count=0,
  224. add_source_image=False):
  225. super().__init__(type=MergerConfig.TYPE_FACE_AVATAR)
  226. self.temporal_face_count = temporal_face_count
  227. #changeable params
  228. self.add_source_image = add_source_image
  229. def copy(self):
  230. return copy.copy(self)
  231. #override
  232. def ask_settings(self):
  233. self.add_source_image = io.input_bool("Add source image?", False, help_message="Add source image for comparison.")
  234. super().ask_settings()
  235. def toggle_add_source_image(self):
  236. self.add_source_image = not self.add_source_image
  237. #override
  238. def __eq__(self, other):
  239. #check equality of changeable params
  240. if isinstance(other, MergerConfigFaceAvatar):
  241. return super().__eq__(other) and \
  242. self.add_source_image == other.add_source_image
  243. return False
  244. #override
  245. def to_string(self, filename):
  246. return (f"MergerConfig {filename}:\n"
  247. f"add_source_image : {self.add_source_image}\n") + \
  248. super().to_string(filename) + "================"
Tip!

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

Comments

Loading...