andro1241 commited on
Commit
e13700e
·
verified ·
1 Parent(s): af0547d

Upload 4 files

Browse files
processors/modules/deep_swapper/choices.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Sequence
2
+
3
+ from facefusion.common_helper import create_int_range
4
+ from facefusion.filesystem import get_file_name, resolve_file_paths, resolve_relative_path
5
+ from facefusion.processors.modules.deep_swapper.types import DeepSwapperModel
6
+
7
+ deep_swapper_models : List[DeepSwapperModel] =\
8
+ [
9
+ 'druuzil/adam_levine_320',
10
+ 'druuzil/adrianne_palicki_384',
11
+ 'druuzil/agnetha_falskog_224',
12
+ 'druuzil/alan_ritchson_320',
13
+ 'druuzil/alicia_vikander_320',
14
+ 'druuzil/amber_midthunder_320',
15
+ 'druuzil/andras_arato_384',
16
+ 'druuzil/andrew_tate_320',
17
+ 'druuzil/angelina_jolie_384',
18
+ 'druuzil/anne_hathaway_320',
19
+ 'druuzil/anya_chalotra_320',
20
+ 'druuzil/arnold_schwarzenegger_320',
21
+ 'druuzil/benjamin_affleck_320',
22
+ 'druuzil/benjamin_stiller_384',
23
+ 'druuzil/bradley_pitt_224',
24
+ 'druuzil/brie_larson_384',
25
+ 'druuzil/bruce_campbell_384',
26
+ 'druuzil/bryan_cranston_320',
27
+ 'druuzil/catherine_blanchett_352',
28
+ 'druuzil/christian_bale_320',
29
+ 'druuzil/christopher_hemsworth_320',
30
+ 'druuzil/christoph_waltz_384',
31
+ 'druuzil/cillian_murphy_320',
32
+ 'druuzil/cobie_smulders_256',
33
+ 'druuzil/dwayne_johnson_384',
34
+ 'druuzil/edward_norton_320',
35
+ 'druuzil/elisabeth_shue_320',
36
+ 'druuzil/elizabeth_olsen_384',
37
+ 'druuzil/elon_musk_320',
38
+ 'druuzil/emily_blunt_320',
39
+ 'druuzil/emma_stone_384',
40
+ 'druuzil/emma_watson_320',
41
+ 'druuzil/erin_moriarty_384',
42
+ 'druuzil/eva_green_320',
43
+ 'druuzil/ewan_mcgregor_320',
44
+ 'druuzil/florence_pugh_320',
45
+ 'druuzil/freya_allan_320',
46
+ 'druuzil/gary_cole_224',
47
+ 'druuzil/gigi_hadid_224',
48
+ 'druuzil/harrison_ford_384',
49
+ 'druuzil/hayden_christensen_320',
50
+ 'druuzil/heath_ledger_320',
51
+ 'druuzil/henry_cavill_448',
52
+ 'druuzil/hugh_jackman_384',
53
+ 'druuzil/idris_elba_320',
54
+ 'druuzil/jack_nicholson_320',
55
+ 'druuzil/james_carrey_384',
56
+ 'druuzil/james_mcavoy_320',
57
+ 'druuzil/james_varney_320',
58
+ 'druuzil/jason_momoa_320',
59
+ 'druuzil/jason_statham_320',
60
+ 'druuzil/jennifer_connelly_384',
61
+ 'druuzil/jimmy_donaldson_320',
62
+ 'druuzil/jordan_peterson_384',
63
+ 'druuzil/karl_urban_224',
64
+ 'druuzil/kate_beckinsale_384',
65
+ 'druuzil/laurence_fishburne_384',
66
+ 'druuzil/lili_reinhart_320',
67
+ 'druuzil/luke_evans_384',
68
+ 'druuzil/mads_mikkelsen_384',
69
+ 'druuzil/mary_winstead_320',
70
+ 'druuzil/margaret_qualley_384',
71
+ 'druuzil/melina_juergens_320',
72
+ 'druuzil/michael_fassbender_320',
73
+ 'druuzil/michael_fox_320',
74
+ 'druuzil/millie_bobby_brown_320',
75
+ 'druuzil/morgan_freeman_320',
76
+ 'druuzil/patrick_stewart_224',
77
+ 'druuzil/rachel_weisz_384',
78
+ 'druuzil/rebecca_ferguson_320',
79
+ 'druuzil/scarlett_johansson_320',
80
+ 'druuzil/shannen_doherty_384',
81
+ 'druuzil/seth_macfarlane_384',
82
+ 'druuzil/thomas_cruise_320',
83
+ 'druuzil/thomas_hanks_384',
84
+ 'druuzil/william_murray_384',
85
+ 'druuzil/zoe_saldana_384',
86
+ 'edel/emma_roberts_224',
87
+ 'edel/ivanka_trump_224',
88
+ 'edel/lize_dzjabrailova_224',
89
+ 'edel/sidney_sweeney_224',
90
+ 'edel/winona_ryder_224',
91
+ 'iperov/alexandra_daddario_224',
92
+ 'iperov/alexei_navalny_224',
93
+ 'iperov/amber_heard_224',
94
+ 'iperov/dilraba_dilmurat_224',
95
+ 'iperov/elon_musk_224',
96
+ 'iperov/emilia_clarke_224',
97
+ 'iperov/emma_watson_224',
98
+ 'iperov/erin_moriarty_224',
99
+ 'iperov/jackie_chan_224',
100
+ 'iperov/james_carrey_224',
101
+ 'iperov/jason_statham_320',
102
+ 'iperov/keanu_reeves_320',
103
+ 'iperov/margot_robbie_224',
104
+ 'iperov/natalie_dormer_224',
105
+ 'iperov/nicolas_coppola_224',
106
+ 'iperov/robert_downey_224',
107
+ 'iperov/rowan_atkinson_224',
108
+ 'iperov/ryan_reynolds_224',
109
+ 'iperov/scarlett_johansson_224',
110
+ 'iperov/sylvester_stallone_224',
111
+ 'iperov/thomas_cruise_224',
112
+ 'iperov/thomas_holland_224',
113
+ 'iperov/vin_diesel_224',
114
+ 'iperov/vladimir_putin_224',
115
+ 'jen/angelica_trae_288',
116
+ 'jen/ella_freya_224',
117
+ 'jen/emma_myers_320',
118
+ 'jen/evie_pickerill_224',
119
+ 'jen/kang_hyewon_320',
120
+ 'jen/maddie_mead_224',
121
+ 'jen/nicole_turnbull_288',
122
+ 'mats/alica_schmidt_320',
123
+ 'mats/ashley_alexiss_224',
124
+ 'mats/billie_eilish_224',
125
+ 'mats/brie_larson_224',
126
+ 'mats/cara_delevingne_224',
127
+ 'mats/carolin_kebekus_224',
128
+ 'mats/chelsea_clinton_224',
129
+ 'mats/claire_boucher_224',
130
+ 'mats/corinna_kopf_224',
131
+ 'mats/florence_pugh_224',
132
+ 'mats/hillary_clinton_224',
133
+ 'mats/jenna_fischer_224',
134
+ 'mats/kim_jisoo_320',
135
+ 'mats/mica_suarez_320',
136
+ 'mats/shailene_woodley_224',
137
+ 'mats/shraddha_kapoor_320',
138
+ 'mats/yu_jimin_352',
139
+ 'rumateus/alison_brie_224',
140
+ 'rumateus/amber_heard_224',
141
+ 'rumateus/angelina_jolie_224',
142
+ 'rumateus/aubrey_plaza_224',
143
+ 'rumateus/bridget_regan_224',
144
+ 'rumateus/cobie_smulders_224',
145
+ 'rumateus/deborah_woll_224',
146
+ 'rumateus/dua_lipa_224',
147
+ 'rumateus/emma_stone_224',
148
+ 'rumateus/hailee_steinfeld_224',
149
+ 'rumateus/hilary_duff_224',
150
+ 'rumateus/jessica_alba_224',
151
+ 'rumateus/jessica_biel_224',
152
+ 'rumateus/john_cena_224',
153
+ 'rumateus/kim_kardashian_224',
154
+ 'rumateus/kristen_bell_224',
155
+ 'rumateus/lucy_liu_224',
156
+ 'rumateus/margot_robbie_224',
157
+ 'rumateus/megan_fox_224',
158
+ 'rumateus/meghan_markle_224',
159
+ 'rumateus/millie_bobby_brown_224',
160
+ 'rumateus/natalie_portman_224',
161
+ 'rumateus/nicki_minaj_224',
162
+ 'rumateus/olivia_wilde_224',
163
+ 'rumateus/shay_mitchell_224',
164
+ 'rumateus/sophie_turner_224',
165
+ 'rumateus/taylor_swift_224'
166
+ ]
167
+
168
+ custom_model_file_paths = resolve_file_paths(resolve_relative_path('../.assets/models/custom'))
169
+
170
+ if custom_model_file_paths:
171
+
172
+ for model_file_path in custom_model_file_paths:
173
+ model_id = '/'.join([ 'custom', get_file_name(model_file_path) ])
174
+ deep_swapper_models.append(model_id)
175
+
176
+ deep_swapper_morph_range : Sequence[int] = create_int_range(0, 100, 1)
processors/modules/deep_swapper/core.py ADDED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from argparse import ArgumentParser
2
+ from functools import lru_cache
3
+ from types import ModuleType
4
+ from typing import List, Tuple
5
+
6
+ import cv2
7
+ import numpy
8
+ from cv2.typing import Size
9
+
10
+ import facefusion.jobs.job_manager
11
+ import facefusion.jobs.job_store
12
+ from facefusion import config, content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer, inference_manager, logger, state_manager, translator, video_manager
13
+ from facefusion.common_helper import create_int_metavar, get_middle
14
+ from facefusion.download import conditional_download_hashes, conditional_download_sources, resolve_download_url_by_provider
15
+ from facefusion.face_creator import scale_face
16
+ from facefusion.face_helper import paste_back, warp_face_by_face_landmark_5
17
+ from facefusion.face_masker import create_area_mask, create_box_mask, create_occlusion_mask, create_region_mask
18
+ from facefusion.face_selector import select_faces
19
+ from facefusion.filesystem import get_file_name, in_directory, is_image, is_video, resolve_file_paths, resolve_relative_path, same_file_extension
20
+ from facefusion.processors.modules.deep_swapper import choices as deep_swapper_choices
21
+ from facefusion.processors.modules.deep_swapper.types import DeepSwapperInputs, DeepSwapperMorph
22
+ from facefusion.processors.types import ProcessorOutputs
23
+ from facefusion.program_helper import find_argument_group
24
+ from facefusion.thread_helper import thread_semaphore
25
+ from facefusion.types import ApplyStateItem, Args, DownloadScope, Face, InferencePool, Mask, ModelOptions, ModelSet, ProcessMode, VisionFrame
26
+ from facefusion.vision import conditional_match_frame_color, read_static_image, read_static_video_frame
27
+
28
+
29
+ @lru_cache()
30
+ def create_static_model_set(download_scope : DownloadScope) -> ModelSet:
31
+ model_config = []
32
+
33
+ if download_scope == 'full':
34
+ model_config.extend(
35
+ [
36
+ ('druuzil', 'adam_levine_320'),
37
+ ('druuzil', 'adrianne_palicki_384'),
38
+ ('druuzil', 'agnetha_falskog_224'),
39
+ ('druuzil', 'alan_ritchson_320'),
40
+ ('druuzil', 'alicia_vikander_320'),
41
+ ('druuzil', 'amber_midthunder_320'),
42
+ ('druuzil', 'andras_arato_384'),
43
+ ('druuzil', 'andrew_tate_320'),
44
+ ('druuzil', 'angelina_jolie_384'),
45
+ ('druuzil', 'anne_hathaway_320'),
46
+ ('druuzil', 'anya_chalotra_320'),
47
+ ('druuzil', 'arnold_schwarzenegger_320'),
48
+ ('druuzil', 'benjamin_affleck_320'),
49
+ ('druuzil', 'benjamin_stiller_384'),
50
+ ('druuzil', 'bradley_pitt_224'),
51
+ ('druuzil', 'brie_larson_384'),
52
+ ('druuzil', 'bruce_campbell_384'),
53
+ ('druuzil', 'bryan_cranston_320'),
54
+ ('druuzil', 'catherine_blanchett_352'),
55
+ ('druuzil', 'christian_bale_320'),
56
+ ('druuzil', 'christopher_hemsworth_320'),
57
+ ('druuzil', 'christoph_waltz_384'),
58
+ ('druuzil', 'cillian_murphy_320'),
59
+ ('druuzil', 'cobie_smulders_256'),
60
+ ('druuzil', 'dwayne_johnson_384'),
61
+ ('druuzil', 'edward_norton_320'),
62
+ ('druuzil', 'elisabeth_shue_320'),
63
+ ('druuzil', 'elizabeth_olsen_384'),
64
+ ('druuzil', 'elon_musk_320'),
65
+ ('druuzil', 'emily_blunt_320'),
66
+ ('druuzil', 'emma_stone_384'),
67
+ ('druuzil', 'emma_watson_320'),
68
+ ('druuzil', 'erin_moriarty_384'),
69
+ ('druuzil', 'eva_green_320'),
70
+ ('druuzil', 'ewan_mcgregor_320'),
71
+ ('druuzil', 'florence_pugh_320'),
72
+ ('druuzil', 'freya_allan_320'),
73
+ ('druuzil', 'gary_cole_224'),
74
+ ('druuzil', 'gigi_hadid_224'),
75
+ ('druuzil', 'harrison_ford_384'),
76
+ ('druuzil', 'hayden_christensen_320'),
77
+ ('druuzil', 'heath_ledger_320'),
78
+ ('druuzil', 'henry_cavill_448'),
79
+ ('druuzil', 'hugh_jackman_384'),
80
+ ('druuzil', 'idris_elba_320'),
81
+ ('druuzil', 'jack_nicholson_320'),
82
+ ('druuzil', 'james_carrey_384'),
83
+ ('druuzil', 'james_mcavoy_320'),
84
+ ('druuzil', 'james_varney_320'),
85
+ ('druuzil', 'jason_momoa_320'),
86
+ ('druuzil', 'jason_statham_320'),
87
+ ('druuzil', 'jennifer_connelly_384'),
88
+ ('druuzil', 'jimmy_donaldson_320'),
89
+ ('druuzil', 'jordan_peterson_384'),
90
+ ('druuzil', 'karl_urban_224'),
91
+ ('druuzil', 'kate_beckinsale_384'),
92
+ ('druuzil', 'laurence_fishburne_384'),
93
+ ('druuzil', 'lili_reinhart_320'),
94
+ ('druuzil', 'luke_evans_384'),
95
+ ('druuzil', 'mads_mikkelsen_384'),
96
+ ('druuzil', 'mary_winstead_320'),
97
+ ('druuzil', 'margaret_qualley_384'),
98
+ ('druuzil', 'melina_juergens_320'),
99
+ ('druuzil', 'michael_fassbender_320'),
100
+ ('druuzil', 'michael_fox_320'),
101
+ ('druuzil', 'millie_bobby_brown_320'),
102
+ ('druuzil', 'morgan_freeman_320'),
103
+ ('druuzil', 'patrick_stewart_224'),
104
+ ('druuzil', 'rachel_weisz_384'),
105
+ ('druuzil', 'rebecca_ferguson_320'),
106
+ ('druuzil', 'scarlett_johansson_320'),
107
+ ('druuzil', 'shannen_doherty_384'),
108
+ ('druuzil', 'seth_macfarlane_384'),
109
+ ('druuzil', 'thomas_cruise_320'),
110
+ ('druuzil', 'thomas_hanks_384'),
111
+ ('druuzil', 'william_murray_384'),
112
+ ('druuzil', 'zoe_saldana_384'),
113
+ ('edel', 'emma_roberts_224'),
114
+ ('edel', 'ivanka_trump_224'),
115
+ ('edel', 'lize_dzjabrailova_224'),
116
+ ('edel', 'sidney_sweeney_224'),
117
+ ('edel', 'winona_ryder_224')
118
+ ])
119
+ if download_scope in [ 'lite', 'full' ]:
120
+ model_config.extend(
121
+ [
122
+ ('iperov', 'alexandra_daddario_224'),
123
+ ('iperov', 'alexei_navalny_224'),
124
+ ('iperov', 'amber_heard_224'),
125
+ ('iperov', 'dilraba_dilmurat_224'),
126
+ ('iperov', 'elon_musk_224'),
127
+ ('iperov', 'emilia_clarke_224'),
128
+ ('iperov', 'emma_watson_224'),
129
+ ('iperov', 'erin_moriarty_224'),
130
+ ('iperov', 'jackie_chan_224'),
131
+ ('iperov', 'james_carrey_224'),
132
+ ('iperov', 'jason_statham_320'),
133
+ ('iperov', 'keanu_reeves_320'),
134
+ ('iperov', 'margot_robbie_224'),
135
+ ('iperov', 'natalie_dormer_224'),
136
+ ('iperov', 'nicolas_coppola_224'),
137
+ ('iperov', 'robert_downey_224'),
138
+ ('iperov', 'rowan_atkinson_224'),
139
+ ('iperov', 'ryan_reynolds_224'),
140
+ ('iperov', 'scarlett_johansson_224'),
141
+ ('iperov', 'sylvester_stallone_224'),
142
+ ('iperov', 'thomas_cruise_224'),
143
+ ('iperov', 'thomas_holland_224'),
144
+ ('iperov', 'vin_diesel_224'),
145
+ ('iperov', 'vladimir_putin_224')
146
+ ])
147
+ if download_scope == 'full':
148
+ model_config.extend(
149
+ [
150
+ ('jen', 'angelica_trae_288'),
151
+ ('jen', 'ella_freya_224'),
152
+ ('jen', 'emma_myers_320'),
153
+ ('jen', 'evie_pickerill_224'),
154
+ ('jen', 'kang_hyewon_320'),
155
+ ('jen', 'maddie_mead_224'),
156
+ ('jen', 'nicole_turnbull_288'),
157
+ ('mats', 'alica_schmidt_320'),
158
+ ('mats', 'ashley_alexiss_224'),
159
+ ('mats', 'billie_eilish_224'),
160
+ ('mats', 'brie_larson_224'),
161
+ ('mats', 'cara_delevingne_224'),
162
+ ('mats', 'carolin_kebekus_224'),
163
+ ('mats', 'chelsea_clinton_224'),
164
+ ('mats', 'claire_boucher_224'),
165
+ ('mats', 'corinna_kopf_224'),
166
+ ('mats', 'florence_pugh_224'),
167
+ ('mats', 'hillary_clinton_224'),
168
+ ('mats', 'jenna_fischer_224'),
169
+ ('mats', 'kim_jisoo_320'),
170
+ ('mats', 'mica_suarez_320'),
171
+ ('mats', 'shailene_woodley_224'),
172
+ ('mats', 'shraddha_kapoor_320'),
173
+ ('mats', 'yu_jimin_352'),
174
+ ('rumateus', 'alison_brie_224'),
175
+ ('rumateus', 'amber_heard_224'),
176
+ ('rumateus', 'angelina_jolie_224'),
177
+ ('rumateus', 'aubrey_plaza_224'),
178
+ ('rumateus', 'bridget_regan_224'),
179
+ ('rumateus', 'cobie_smulders_224'),
180
+ ('rumateus', 'deborah_woll_224'),
181
+ ('rumateus', 'dua_lipa_224'),
182
+ ('rumateus', 'emma_stone_224'),
183
+ ('rumateus', 'hailee_steinfeld_224'),
184
+ ('rumateus', 'hilary_duff_224'),
185
+ ('rumateus', 'jessica_alba_224'),
186
+ ('rumateus', 'jessica_biel_224'),
187
+ ('rumateus', 'john_cena_224'),
188
+ ('rumateus', 'kim_kardashian_224'),
189
+ ('rumateus', 'kristen_bell_224'),
190
+ ('rumateus', 'lucy_liu_224'),
191
+ ('rumateus', 'margot_robbie_224'),
192
+ ('rumateus', 'megan_fox_224'),
193
+ ('rumateus', 'meghan_markle_224'),
194
+ ('rumateus', 'millie_bobby_brown_224'),
195
+ ('rumateus', 'natalie_portman_224'),
196
+ ('rumateus', 'nicki_minaj_224'),
197
+ ('rumateus', 'olivia_wilde_224'),
198
+ ('rumateus', 'shay_mitchell_224'),
199
+ ('rumateus', 'sophie_turner_224'),
200
+ ('rumateus', 'taylor_swift_224')
201
+ ])
202
+ model_set : ModelSet = {}
203
+
204
+ for model_scope, model_name in model_config:
205
+ model_id = '/'.join([ model_scope, model_name ])
206
+
207
+ model_set[model_id] =\
208
+ {
209
+ 'hashes':
210
+ {
211
+ 'deep_swapper':
212
+ {
213
+ 'url': resolve_download_url_by_provider('huggingface', 'deepfacelive-models-' + model_scope, model_name + '.hash'),
214
+ 'path': resolve_relative_path('../.assets/models/' + model_scope + '/' + model_name + '.hash')
215
+ }
216
+ },
217
+ 'sources':
218
+ {
219
+ 'deep_swapper':
220
+ {
221
+ 'url': resolve_download_url_by_provider('huggingface', 'deepfacelive-models-' + model_scope, model_name + '.dfm'),
222
+ 'path': resolve_relative_path('../.assets/models/' + model_scope + '/' + model_name + '.dfm')
223
+ }
224
+ },
225
+ 'template': 'dfl_whole_face'
226
+ }
227
+
228
+ custom_model_file_paths = resolve_file_paths(resolve_relative_path('../.assets/models/custom'))
229
+
230
+ if custom_model_file_paths:
231
+
232
+ for model_file_path in custom_model_file_paths:
233
+ model_id = '/'.join([ 'custom', get_file_name(model_file_path) ])
234
+
235
+ model_set[model_id] =\
236
+ {
237
+ 'sources':
238
+ {
239
+ 'deep_swapper':
240
+ {
241
+ 'path': resolve_relative_path(model_file_path)
242
+ }
243
+ },
244
+ 'template': 'dfl_whole_face'
245
+ }
246
+
247
+ return model_set
248
+
249
+
250
+ def get_inference_pool() -> InferencePool:
251
+ model_names = [ state_manager.get_item('deep_swapper_model') ]
252
+ model_source_set = get_model_options().get('sources')
253
+
254
+ return inference_manager.get_inference_pool(__name__, model_names, model_source_set)
255
+
256
+
257
+ def clear_inference_pool() -> None:
258
+ model_names = [ state_manager.get_item('deep_swapper_model') ]
259
+ inference_manager.clear_inference_pool(__name__, model_names)
260
+
261
+
262
+ def get_model_options() -> ModelOptions:
263
+ model_name = state_manager.get_item('deep_swapper_model')
264
+ return create_static_model_set('full').get(model_name)
265
+
266
+
267
+ def get_model_size() -> Size:
268
+ deep_swapper = get_inference_pool().get('deep_swapper')
269
+
270
+ for deep_swapper_input in deep_swapper.get_inputs():
271
+ if deep_swapper_input.name == 'in_face:0':
272
+ return deep_swapper_input.shape[1:3]
273
+
274
+ return 0, 0
275
+
276
+
277
+ def register_args(program : ArgumentParser) -> None:
278
+ group_processors = find_argument_group(program, 'processors')
279
+ if group_processors:
280
+ group_processors.add_argument('--deep-swapper-model', help = translator.get('help.model', __package__), default = config.get_str_value('processors', 'deep_swapper_model', 'iperov/elon_musk_224'), choices = deep_swapper_choices.deep_swapper_models)
281
+ group_processors.add_argument('--deep-swapper-morph', help = translator.get('help.morph', __package__), type = int, default = config.get_int_value('processors', 'deep_swapper_morph', '100'), choices = deep_swapper_choices.deep_swapper_morph_range, metavar = create_int_metavar(deep_swapper_choices.deep_swapper_morph_range))
282
+ facefusion.jobs.job_store.register_step_keys([ 'deep_swapper_model', 'deep_swapper_morph' ])
283
+
284
+
285
+ def apply_args(args : Args, apply_state_item : ApplyStateItem) -> None:
286
+ apply_state_item('deep_swapper_model', args.get('deep_swapper_model'))
287
+ apply_state_item('deep_swapper_morph', args.get('deep_swapper_morph'))
288
+
289
+
290
+ def get_common_modules() -> List[ModuleType]:
291
+ return [ content_analyser, face_classifier, face_detector, face_landmarker, face_masker, face_recognizer ]
292
+
293
+
294
+ def pre_check() -> bool:
295
+ model_hash_set = get_model_options().get('hashes')
296
+ model_source_set = get_model_options().get('sources')
297
+
298
+ for common_module in get_common_modules():
299
+ if not common_module.pre_check():
300
+ return False
301
+
302
+ if model_hash_set and model_source_set:
303
+ return conditional_download_hashes(model_hash_set) and conditional_download_sources(model_source_set)
304
+ return True
305
+
306
+
307
+ def pre_process(mode : ProcessMode) -> bool:
308
+ if mode in [ 'output', 'preview' ] and not is_image(state_manager.get_item('target_path')) and not is_video(state_manager.get_item('target_path')):
309
+ logger.error(translator.get('choose_image_or_video_target') + translator.get('exclamation_mark'), __name__)
310
+ return False
311
+ if mode == 'output' and not in_directory(state_manager.get_item('output_path')):
312
+ logger.error(translator.get('specify_image_or_video_output') + translator.get('exclamation_mark'), __name__)
313
+ return False
314
+ if mode == 'output' and not same_file_extension(state_manager.get_item('target_path'), state_manager.get_item('output_path')):
315
+ logger.error(translator.get('match_target_and_output_extension') + translator.get('exclamation_mark'), __name__)
316
+ return False
317
+ return True
318
+
319
+
320
+ def post_process() -> None:
321
+ read_static_image.cache_clear()
322
+ read_static_video_frame.cache_clear()
323
+ video_manager.clear_video_pool()
324
+
325
+ if state_manager.get_item('video_memory_strategy') in [ 'strict', 'moderate' ]:
326
+ clear_inference_pool()
327
+
328
+ if state_manager.get_item('video_memory_strategy') == 'strict':
329
+ for common_module in get_common_modules():
330
+ common_module.clear_inference_pool()
331
+
332
+
333
+ def swap_face(target_face : Face, temp_vision_frame : VisionFrame) -> VisionFrame:
334
+ model_template = get_model_options().get('template')
335
+ model_size = get_model_size()
336
+ crop_vision_frame, affine_matrix = warp_face_by_face_landmark_5(temp_vision_frame, target_face.landmark_set.get('5/68'), model_template, model_size)
337
+ crop_vision_frame_raw = crop_vision_frame.copy()
338
+ box_mask = create_box_mask(crop_vision_frame, state_manager.get_item('face_mask_blur'), state_manager.get_item('face_mask_padding'))
339
+ crop_masks =\
340
+ [
341
+ box_mask
342
+ ]
343
+
344
+ if 'occlusion' in state_manager.get_item('face_mask_types'):
345
+ occlusion_mask = create_occlusion_mask(crop_vision_frame)
346
+ crop_masks.append(occlusion_mask)
347
+
348
+ crop_vision_frame = prepare_crop_frame(crop_vision_frame)
349
+ deep_swapper_morph = numpy.array([ numpy.interp(state_manager.get_item('deep_swapper_morph'), [ 0, 100 ], [ 0, 1 ]) ]).astype(numpy.float32)
350
+ crop_vision_frame, crop_source_mask, crop_target_mask = forward(crop_vision_frame, deep_swapper_morph)
351
+ crop_vision_frame = normalize_crop_frame(crop_vision_frame)
352
+ crop_vision_frame = conditional_match_frame_color(crop_vision_frame_raw, crop_vision_frame)
353
+ crop_masks.append(prepare_crop_mask(crop_source_mask, crop_target_mask))
354
+
355
+ if 'area' in state_manager.get_item('face_mask_types'):
356
+ face_landmark_68 = cv2.transform(target_face.landmark_set.get('68').reshape(1, -1, 2), affine_matrix).reshape(-1, 2)
357
+ area_mask = create_area_mask(crop_vision_frame, face_landmark_68, state_manager.get_item('face_mask_areas'))
358
+ crop_masks.append(area_mask)
359
+
360
+ if 'region' in state_manager.get_item('face_mask_types'):
361
+ region_mask = create_region_mask(crop_vision_frame, state_manager.get_item('face_mask_regions'))
362
+ crop_masks.append(region_mask)
363
+
364
+ crop_mask = numpy.minimum.reduce(crop_masks).clip(0, 1)
365
+ paste_vision_frame = paste_back(temp_vision_frame, crop_vision_frame, crop_mask, affine_matrix)
366
+ return paste_vision_frame
367
+
368
+
369
+ def forward(crop_vision_frame : VisionFrame, deep_swapper_morph : DeepSwapperMorph) -> Tuple[VisionFrame, Mask, Mask]:
370
+ deep_swapper = get_inference_pool().get('deep_swapper')
371
+ deep_swapper_inputs = {}
372
+
373
+ for deep_swapper_input in deep_swapper.get_inputs():
374
+ if deep_swapper_input.name == 'in_face:0':
375
+ deep_swapper_inputs[deep_swapper_input.name] = crop_vision_frame
376
+ if deep_swapper_input.name == 'morph_value:0':
377
+ deep_swapper_inputs[deep_swapper_input.name] = deep_swapper_morph
378
+
379
+ with thread_semaphore():
380
+ crop_target_mask, crop_vision_frame, crop_source_mask = deep_swapper.run(None, deep_swapper_inputs)
381
+
382
+ return crop_vision_frame[0], crop_source_mask[0], crop_target_mask[0]
383
+
384
+
385
+ def has_morph_input() -> bool:
386
+ deep_swapper = get_inference_pool().get('deep_swapper')
387
+
388
+ for deep_swapper_input in deep_swapper.get_inputs():
389
+ if deep_swapper_input.name == 'morph_value:0':
390
+ return True
391
+
392
+ return False
393
+
394
+
395
+ def prepare_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
396
+ crop_vision_frame = cv2.addWeighted(crop_vision_frame, 1.75, cv2.GaussianBlur(crop_vision_frame, (0, 0), 2), -0.75, 0)
397
+ crop_vision_frame = crop_vision_frame / 255.0
398
+ crop_vision_frame = numpy.expand_dims(crop_vision_frame, axis = 0).astype(numpy.float32)
399
+ return crop_vision_frame
400
+
401
+
402
+ def normalize_crop_frame(crop_vision_frame : VisionFrame) -> VisionFrame:
403
+ crop_vision_frame = (crop_vision_frame * 255.0).clip(0, 255)
404
+ crop_vision_frame = crop_vision_frame.astype(numpy.uint8)
405
+ return crop_vision_frame
406
+
407
+
408
+ def prepare_crop_mask(crop_source_mask : Mask, crop_target_mask : Mask) -> Mask:
409
+ model_size = get_model_size()
410
+ blur_size = 6.25
411
+ kernel_size = 3
412
+ crop_mask = numpy.minimum.reduce([ crop_source_mask, crop_target_mask ])
413
+ crop_mask = crop_mask.reshape(model_size).clip(0, 1)
414
+ crop_mask = cv2.erode(crop_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)), iterations = 2)
415
+ crop_mask = cv2.GaussianBlur(crop_mask, (0, 0), blur_size)
416
+ return crop_mask
417
+
418
+
419
+ def process_frame(inputs : DeepSwapperInputs) -> ProcessorOutputs:
420
+ reference_vision_frame = inputs.get('reference_vision_frame')
421
+ source_vision_frames = inputs.get('source_vision_frames')
422
+ target_vision_frames = inputs.get('target_vision_frames')
423
+ temp_vision_frame = inputs.get('temp_vision_frame')
424
+ temp_vision_mask = inputs.get('temp_vision_mask')
425
+
426
+ target_vision_frame = get_middle(target_vision_frames)
427
+ target_faces = select_faces(reference_vision_frame, source_vision_frames, target_vision_frames)
428
+
429
+ if target_faces:
430
+ for target_face in target_faces:
431
+ target_face = scale_face(target_face, target_vision_frame, temp_vision_frame)
432
+ temp_vision_frame = swap_face(target_face, temp_vision_frame)
433
+
434
+ return temp_vision_frame, temp_vision_mask
processors/modules/deep_swapper/locales.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from facefusion.types import Locales
2
+
3
+ LOCALES : Locales =\
4
+ {
5
+ 'en':
6
+ {
7
+ 'help':
8
+ {
9
+ 'model': 'choose the model responsible for swapping the face',
10
+ 'morph': 'morph between source face and target faces'
11
+ },
12
+ 'uis':
13
+ {
14
+ 'model_dropdown': 'DEEP SWAPPER MODEL',
15
+ 'morph_slider': 'DEEP SWAPPER MORPH'
16
+ }
17
+ }
18
+ }
processors/modules/deep_swapper/types.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, List, TypeAlias, TypedDict
2
+
3
+ from numpy.typing import NDArray
4
+
5
+ from facefusion.types import Mask, VisionFrame
6
+
7
+ DeepSwapperInputs = TypedDict('DeepSwapperInputs',
8
+ {
9
+ 'reference_vision_frame' : VisionFrame,
10
+ 'source_vision_frames' : List[VisionFrame],
11
+ 'target_vision_frames' : List[VisionFrame],
12
+ 'temp_vision_frame' : VisionFrame,
13
+ 'temp_vision_mask' : Mask
14
+ })
15
+
16
+ DeepSwapperModel : TypeAlias = str
17
+
18
+ DeepSwapperMorph : TypeAlias = NDArray[Any]