MohamedIFQ commited on
Commit
f9a80d8
1 Parent(s): 42e86a6

Upload 3 files

Browse files
Files changed (3) hide show
  1. scripts/download_models.sh +32 -0
  2. scripts/extension.py +189 -0
  3. scripts/test.sh +21 -0
scripts/download_models.sh ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mkdir ./checkpoints
2
+
3
+ # lagency download link
4
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/auido2exp_00300-model.pth -O ./checkpoints/auido2exp_00300-model.pth
5
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/auido2pose_00140-model.pth -O ./checkpoints/auido2pose_00140-model.pth
6
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/epoch_20.pth -O ./checkpoints/epoch_20.pth
7
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/facevid2vid_00189-model.pth.tar -O ./checkpoints/facevid2vid_00189-model.pth.tar
8
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/shape_predictor_68_face_landmarks.dat -O ./checkpoints/shape_predictor_68_face_landmarks.dat
9
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/wav2lip.pth -O ./checkpoints/wav2lip.pth
10
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/mapping_00229-model.pth.tar -O ./checkpoints/mapping_00229-model.pth.tar
11
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/mapping_00109-model.pth.tar -O ./checkpoints/mapping_00109-model.pth.tar
12
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/hub.zip -O ./checkpoints/hub.zip
13
+ # unzip -n ./checkpoints/hub.zip -d ./checkpoints/
14
+
15
+
16
+ #### download the new links.
17
+ wget -nc https://github.com/OpenTalker/SadTalker/releases/download/v0.0.2-rc/mapping_00109-model.pth.tar -O ./checkpoints/mapping_00109-model.pth.tar
18
+ wget -nc https://github.com/OpenTalker/SadTalker/releases/download/v0.0.2-rc/mapping_00229-model.pth.tar -O ./checkpoints/mapping_00229-model.pth.tar
19
+ wget -nc https://github.com/OpenTalker/SadTalker/releases/download/v0.0.2-rc/SadTalker_V0.0.2_256.safetensors -O ./checkpoints/SadTalker_V0.0.2_256.safetensors
20
+ wget -nc https://github.com/OpenTalker/SadTalker/releases/download/v0.0.2-rc/SadTalker_V0.0.2_512.safetensors -O ./checkpoints/SadTalker_V0.0.2_512.safetensors
21
+
22
+
23
+ # wget -nc https://github.com/Winfredy/SadTalker/releases/download/v0.0.2/BFM_Fitting.zip -O ./checkpoints/BFM_Fitting.zip
24
+ # unzip -n ./checkpoints/BFM_Fitting.zip -d ./checkpoints/
25
+
26
+ ### enhancer
27
+ mkdir -p ./gfpgan/weights
28
+ wget -nc https://github.com/xinntao/facexlib/releases/download/v0.1.0/alignment_WFLW_4HG.pth -O ./gfpgan/weights/alignment_WFLW_4HG.pth
29
+ wget -nc https://github.com/xinntao/facexlib/releases/download/v0.1.0/detection_Resnet50_Final.pth -O ./gfpgan/weights/detection_Resnet50_Final.pth
30
+ wget -nc https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -O ./gfpgan/weights/GFPGANv1.4.pth
31
+ wget -nc https://github.com/xinntao/facexlib/releases/download/v0.2.2/parsing_parsenet.pth -O ./gfpgan/weights/parsing_parsenet.pth
32
+
scripts/extension.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys
2
+ from pathlib import Path
3
+ import tempfile
4
+ import gradio as gr
5
+ from modules.call_queue import wrap_gradio_gpu_call, wrap_queued_call
6
+ from modules.shared import opts, OptionInfo
7
+ from modules import shared, paths, script_callbacks
8
+ import launch
9
+ import glob
10
+ from huggingface_hub import snapshot_download
11
+
12
+
13
+
14
+ def check_all_files_safetensor(current_dir):
15
+ kv = {
16
+ "SadTalker_V0.0.2_256.safetensors": "sadtalker-256",
17
+ "SadTalker_V0.0.2_512.safetensors": "sadtalker-512",
18
+ "mapping_00109-model.pth.tar" : "mapping-109" ,
19
+ "mapping_00229-model.pth.tar" : "mapping-229" ,
20
+ }
21
+
22
+ if not os.path.isdir(current_dir):
23
+ return False
24
+
25
+ dirs = os.listdir(current_dir)
26
+
27
+ for f in dirs:
28
+ if f in kv.keys():
29
+ del kv[f]
30
+
31
+ return len(kv.keys()) == 0
32
+
33
+ def check_all_files(current_dir):
34
+ kv = {
35
+ "auido2exp_00300-model.pth": "audio2exp",
36
+ "auido2pose_00140-model.pth": "audio2pose",
37
+ "epoch_20.pth": "face_recon",
38
+ "facevid2vid_00189-model.pth.tar": "face-render",
39
+ "mapping_00109-model.pth.tar" : "mapping-109" ,
40
+ "mapping_00229-model.pth.tar" : "mapping-229" ,
41
+ "wav2lip.pth": "wav2lip",
42
+ "shape_predictor_68_face_landmarks.dat": "dlib",
43
+ }
44
+
45
+ if not os.path.isdir(current_dir):
46
+ return False
47
+
48
+ dirs = os.listdir(current_dir)
49
+
50
+ for f in dirs:
51
+ if f in kv.keys():
52
+ del kv[f]
53
+
54
+ return len(kv.keys()) == 0
55
+
56
+
57
+
58
+ def download_model(local_dir='./checkpoints'):
59
+ REPO_ID = 'vinthony/SadTalker'
60
+ snapshot_download(repo_id=REPO_ID, local_dir=local_dir, local_dir_use_symlinks=False)
61
+
62
+ def get_source_image(image):
63
+ return image
64
+
65
+ def get_img_from_txt2img(x):
66
+ talker_path = Path(paths.script_path) / "outputs"
67
+ imgs_from_txt_dir = str(talker_path / "txt2img-images/")
68
+ imgs = glob.glob(imgs_from_txt_dir+'/*/*.png')
69
+ imgs.sort(key=lambda x:os.path.getmtime(os.path.join(imgs_from_txt_dir, x)))
70
+ img_from_txt_path = os.path.join(imgs_from_txt_dir, imgs[-1])
71
+ return img_from_txt_path, img_from_txt_path
72
+
73
+ def get_img_from_img2img(x):
74
+ talker_path = Path(paths.script_path) / "outputs"
75
+ imgs_from_img_dir = str(talker_path / "img2img-images/")
76
+ imgs = glob.glob(imgs_from_img_dir+'/*/*.png')
77
+ imgs.sort(key=lambda x:os.path.getmtime(os.path.join(imgs_from_img_dir, x)))
78
+ img_from_img_path = os.path.join(imgs_from_img_dir, imgs[-1])
79
+ return img_from_img_path, img_from_img_path
80
+
81
+ def get_default_checkpoint_path():
82
+ # check the path of models/checkpoints and extensions/
83
+ checkpoint_path = Path(paths.script_path) / "models"/ "SadTalker"
84
+ extension_checkpoint_path = Path(paths.script_path) / "extensions"/ "SadTalker" / "checkpoints"
85
+
86
+ if check_all_files_safetensor(checkpoint_path):
87
+ # print('founding sadtalker checkpoint in ' + str(checkpoint_path))
88
+ return checkpoint_path
89
+
90
+ if check_all_files_safetensor(extension_checkpoint_path):
91
+ # print('founding sadtalker checkpoint in ' + str(extension_checkpoint_path))
92
+ return extension_checkpoint_path
93
+
94
+ if check_all_files(checkpoint_path):
95
+ # print('founding sadtalker checkpoint in ' + str(checkpoint_path))
96
+ return checkpoint_path
97
+
98
+ if check_all_files(extension_checkpoint_path):
99
+ # print('founding sadtalker checkpoint in ' + str(extension_checkpoint_path))
100
+ return extension_checkpoint_path
101
+
102
+ return None
103
+
104
+
105
+
106
+ def install():
107
+
108
+ kv = {
109
+ "face_alignment": "face-alignment==1.3.5",
110
+ "imageio": "imageio==2.19.3",
111
+ "imageio_ffmpeg": "imageio-ffmpeg==0.4.7",
112
+ "librosa":"librosa==0.8.0",
113
+ "pydub":"pydub==0.25.1",
114
+ "scipy":"scipy==1.8.1",
115
+ "tqdm": "tqdm",
116
+ "yacs":"yacs==0.1.8",
117
+ "yaml": "pyyaml",
118
+ "av":"av",
119
+ "gfpgan": "gfpgan",
120
+ }
121
+
122
+ # # dlib is not necessary currently
123
+ # if 'darwin' in sys.platform:
124
+ # kv['dlib'] = "dlib"
125
+ # else:
126
+ # kv['dlib'] = 'dlib-bin'
127
+
128
+ # #### we need to have a newer version of imageio for our method.
129
+ # launch.run_pip("install imageio==2.19.3", "requirements for SadTalker")
130
+
131
+ for k,v in kv.items():
132
+ if not launch.is_installed(k):
133
+ print(k, launch.is_installed(k))
134
+ launch.run_pip("install "+ v, "requirements for SadTalker")
135
+
136
+ if os.getenv('SADTALKER_CHECKPOINTS'):
137
+ print('load Sadtalker Checkpoints from '+ os.getenv('SADTALKER_CHECKPOINTS'))
138
+
139
+ elif get_default_checkpoint_path() is not None:
140
+ os.environ['SADTALKER_CHECKPOINTS'] = str(get_default_checkpoint_path())
141
+ else:
142
+
143
+ print(
144
+ """"
145
+ SadTalker will not support download all the files from hugging face, which will take a long time.
146
+
147
+ please manually set the SADTALKER_CHECKPOINTS in `webui_user.bat`(windows) or `webui_user.sh`(linux)
148
+ """
149
+ )
150
+
151
+ # python = sys.executable
152
+
153
+ # launch.run(f'"{python}" -m pip uninstall -y huggingface_hub', live=True)
154
+ # launch.run(f'"{python}" -m pip install --upgrade git+https://github.com/huggingface/huggingface_hub@main', live=True)
155
+ # ### run the scripts to downlod models to correct localtion.
156
+ # # print('download models for SadTalker')
157
+ # # launch.run("cd " + paths.script_path+"/extensions/SadTalker && bash ./scripts/download_models.sh", live=True)
158
+ # # print('SadTalker is successfully installed!')
159
+ # download_model(paths.script_path+'/extensions/SadTalker/checkpoints')
160
+
161
+
162
+ def on_ui_tabs():
163
+ install()
164
+
165
+ sys.path.extend([paths.script_path+'/extensions/SadTalker'])
166
+
167
+ repo_dir = paths.script_path+'/extensions/SadTalker/'
168
+
169
+ result_dir = opts.sadtalker_result_dir
170
+ os.makedirs(result_dir, exist_ok=True)
171
+
172
+ from app_sadtalker import sadtalker_demo
173
+
174
+ if os.getenv('SADTALKER_CHECKPOINTS'):
175
+ checkpoint_path = os.getenv('SADTALKER_CHECKPOINTS')
176
+ else:
177
+ checkpoint_path = repo_dir+'checkpoints/'
178
+
179
+ audio_to_video = sadtalker_demo(checkpoint_path=checkpoint_path, config_path=repo_dir+'src/config', warpfn = wrap_queued_call)
180
+
181
+ return [(audio_to_video, "SadTalker", "extension")]
182
+
183
+ def on_ui_settings():
184
+ talker_path = Path(paths.script_path) / "outputs"
185
+ section = ('extension', "SadTalker")
186
+ opts.add_option("sadtalker_result_dir", OptionInfo(str(talker_path / "SadTalker/"), "Path to save results of sadtalker", section=section))
187
+
188
+ script_callbacks.on_ui_settings(on_ui_settings)
189
+ script_callbacks.on_ui_tabs(on_ui_tabs)
scripts/test.sh ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ### some test command before commit.
2
+ # python inference.py --preprocess crop --size 256
3
+ # python inference.py --preprocess crop --size 512
4
+
5
+ # python inference.py --preprocess extcrop --size 256
6
+ # python inference.py --preprocess extcrop --size 512
7
+
8
+ # python inference.py --preprocess resize --size 256
9
+ # python inference.py --preprocess resize --size 512
10
+
11
+ # python inference.py --preprocess full --size 256
12
+ # python inference.py --preprocess full --size 512
13
+
14
+ # python inference.py --preprocess extfull --size 256
15
+ # python inference.py --preprocess extfull --size 512
16
+
17
+ python inference.py --preprocess full --size 256 --enhancer gfpgan
18
+ python inference.py --preprocess full --size 512 --enhancer gfpgan
19
+
20
+ python inference.py --preprocess full --size 256 --enhancer gfpgan --still
21
+ python inference.py --preprocess full --size 512 --enhancer gfpgan --still