Spaces:
Runtime error
Runtime error
update
Browse files- app.py +102 -25
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -6,6 +6,11 @@ import subprocess
|
|
| 6 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
|
| 7 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-image==0.19.3"])
|
| 8 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "face-alignment==1.3.5"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Clone repo nếu chưa có
|
| 11 |
if not os.path.exists('first_order_model'):
|
|
@@ -16,6 +21,78 @@ if not os.path.exists('first_order_model'):
|
|
| 16 |
sys.path.append('.')
|
| 17 |
sys.path.append('first_order_model')
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Bây giờ import các module cần thiết
|
| 20 |
import gradio as gr
|
| 21 |
import numpy as np
|
|
@@ -23,28 +100,26 @@ import torch
|
|
| 23 |
import imageio
|
| 24 |
from skimage.transform import resize
|
| 25 |
from skimage import img_as_ubyte
|
|
|
|
| 26 |
|
| 27 |
-
# Import
|
| 28 |
-
from
|
| 29 |
-
from first_order_model.animate import normalize_kp
|
| 30 |
|
| 31 |
# Tải mô hình pre-trained
|
| 32 |
def download_model():
|
| 33 |
model_path = 'checkpoints/vox-cpk.pth.tar'
|
| 34 |
-
if not os.path.exists(
|
| 35 |
os.makedirs('checkpoints', exist_ok=True)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
])
|
| 40 |
|
| 41 |
config_path = 'first_order_model/config/vox-256.yaml'
|
| 42 |
-
if not os.path.exists(
|
| 43 |
os.makedirs('first_order_model/config', exist_ok=True)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
])
|
| 48 |
|
| 49 |
return config_path, model_path
|
| 50 |
|
|
@@ -53,6 +128,7 @@ def make_animation(source_image, driving_video, relative=True, adapt_movement_sc
|
|
| 53 |
config_path, checkpoint_path = download_model()
|
| 54 |
|
| 55 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
| 56 |
|
| 57 |
# Tải mô hình và cấu hình
|
| 58 |
generator, kp_detector = load_checkpoints(config_path, checkpoint_path, device=device)
|
|
@@ -89,17 +165,14 @@ def make_animation(source_image, driving_video, relative=True, adapt_movement_sc
|
|
| 89 |
kp_driving = kp_detector(driving_frame)
|
| 90 |
|
| 91 |
# Chuẩn hóa keypoints
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)
|
| 101 |
-
else:
|
| 102 |
-
kp_norm = kp_driving
|
| 103 |
|
| 104 |
# Tạo frame
|
| 105 |
out = generator(source, kp_source=kp_source, kp_driving=kp_norm)
|
|
@@ -107,7 +180,11 @@ def make_animation(source_image, driving_video, relative=True, adapt_movement_sc
|
|
| 107 |
|
| 108 |
# Lưu video kết quả
|
| 109 |
output_path = 'result.mp4'
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
return output_path
|
| 113 |
|
|
|
|
| 6 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-learn"])
|
| 7 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "scikit-image==0.19.3"])
|
| 8 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "face-alignment==1.3.5"])
|
| 9 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "ffmpeg-python"])
|
| 10 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "PyYAML==5.3.1"])
|
| 11 |
+
|
| 12 |
+
# Cài đặt ffmpeg trong môi trường Ubuntu
|
| 13 |
+
os.system("apt-get update && apt-get install -y ffmpeg")
|
| 14 |
|
| 15 |
# Clone repo nếu chưa có
|
| 16 |
if not os.path.exists('first_order_model'):
|
|
|
|
| 21 |
sys.path.append('.')
|
| 22 |
sys.path.append('first_order_model')
|
| 23 |
|
| 24 |
+
# Sửa code để truy cập trực tiếp vào các hàm cần thiết
|
| 25 |
+
# Tạo một bản sao của demo.py mà không phụ thuộc vào ffmpeg thư viện Python
|
| 26 |
+
with open('first_order_model/demo.py', 'r') as f:
|
| 27 |
+
demo_code = f.read()
|
| 28 |
+
|
| 29 |
+
# Thay thế dòng import ffmpeg
|
| 30 |
+
demo_code = demo_code.replace('import ffmpeg', '# import ffmpeg')
|
| 31 |
+
|
| 32 |
+
# Viết lại demo.py đã sửa
|
| 33 |
+
with open('first_order_model/demo_fixed.py', 'w') as f:
|
| 34 |
+
f.write(demo_code)
|
| 35 |
+
|
| 36 |
+
# Tạo file helper với hàm load_checkpoints
|
| 37 |
+
with open('load_helper.py', 'w') as f:
|
| 38 |
+
f.write("""
|
| 39 |
+
import yaml
|
| 40 |
+
import torch
|
| 41 |
+
from first_order_model.modules.generator import OcclusionAwareGenerator
|
| 42 |
+
from first_order_model.modules.keypoint_detector import KPDetector
|
| 43 |
+
|
| 44 |
+
def load_checkpoints(config_path, checkpoint_path, device='cpu'):
|
| 45 |
+
with open(config_path) as f:
|
| 46 |
+
config = yaml.full_load(f)
|
| 47 |
+
|
| 48 |
+
generator = OcclusionAwareGenerator(**config['model_params']['generator_params'],
|
| 49 |
+
**config['model_params']['common_params'])
|
| 50 |
+
generator.to(device)
|
| 51 |
+
|
| 52 |
+
kp_detector = KPDetector(**config['model_params']['kp_detector_params'],
|
| 53 |
+
**config['model_params']['common_params'])
|
| 54 |
+
kp_detector.to(device)
|
| 55 |
+
|
| 56 |
+
checkpoint = torch.load(checkpoint_path, map_location=device)
|
| 57 |
+
generator.load_state_dict(checkpoint['generator'])
|
| 58 |
+
kp_detector.load_state_dict(checkpoint['kp_detector'])
|
| 59 |
+
|
| 60 |
+
generator.eval()
|
| 61 |
+
kp_detector.eval()
|
| 62 |
+
|
| 63 |
+
return generator, kp_detector
|
| 64 |
+
|
| 65 |
+
def normalize_kp(kp_source, kp_driving, kp_driving_initial,
|
| 66 |
+
use_relative_movement=True, use_relative_jacobian=True, adapt_movement_scale=True):
|
| 67 |
+
from first_order_model.sync_batchnorm.batchnorm import SynchronizedBatchNorm2d
|
| 68 |
+
|
| 69 |
+
kp_new = {k: v for k, v in kp_driving.items()}
|
| 70 |
+
|
| 71 |
+
if use_relative_movement:
|
| 72 |
+
kp_value_diff = (kp_driving['value'] - kp_driving_initial['value'])
|
| 73 |
+
kp_value_diff_abs = torch.abs(kp_value_diff)
|
| 74 |
+
|
| 75 |
+
if adapt_movement_scale:
|
| 76 |
+
distance = torch.max(kp_value_diff_abs, dim=2, keepdim=True)[0]
|
| 77 |
+
distance = torch.max(distance, dim=1, keepdim=True)[0]
|
| 78 |
+
|
| 79 |
+
kp_source_diff = torch.abs(kp_source['value'])
|
| 80 |
+
kp_source_max = torch.max(kp_source_diff, dim=2, keepdim=True)[0]
|
| 81 |
+
kp_source_max = torch.max(kp_source_max, dim=1, keepdim=True)[0]
|
| 82 |
+
|
| 83 |
+
movement_scale = kp_source_max / (distance + 1e-6)
|
| 84 |
+
|
| 85 |
+
kp_new['value'] = kp_source['value'] + movement_scale * kp_value_diff
|
| 86 |
+
else:
|
| 87 |
+
kp_new['value'] = kp_source['value'] + kp_value_diff
|
| 88 |
+
|
| 89 |
+
if use_relative_jacobian:
|
| 90 |
+
jacobian_diff = torch.matmul(kp_driving['jacobian'], torch.inverse(kp_driving_initial['jacobian']))
|
| 91 |
+
kp_new['jacobian'] = torch.matmul(jacobian_diff, kp_source['jacobian'])
|
| 92 |
+
|
| 93 |
+
return kp_new
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
# Bây giờ import các module cần thiết
|
| 97 |
import gradio as gr
|
| 98 |
import numpy as np
|
|
|
|
| 100 |
import imageio
|
| 101 |
from skimage.transform import resize
|
| 102 |
from skimage import img_as_ubyte
|
| 103 |
+
from PIL import Image
|
| 104 |
|
| 105 |
+
# Import hàm load_checkpoints từ file helper
|
| 106 |
+
from load_helper import load_checkpoints, normalize_kp
|
|
|
|
| 107 |
|
| 108 |
# Tải mô hình pre-trained
|
| 109 |
def download_model():
|
| 110 |
model_path = 'checkpoints/vox-cpk.pth.tar'
|
| 111 |
+
if not os.path.exists('checkpoints'):
|
| 112 |
os.makedirs('checkpoints', exist_ok=True)
|
| 113 |
+
|
| 114 |
+
if not os.path.exists(model_path):
|
| 115 |
+
os.system('wget -P checkpoints https://drive.google.com/uc?export=download&id=1PyQJmkdCsAkOYwUyaj_l-l0as-iLDgeH -O checkpoints/vox-cpk.pth.tar')
|
|
|
|
| 116 |
|
| 117 |
config_path = 'first_order_model/config/vox-256.yaml'
|
| 118 |
+
if not os.path.exists('first_order_model/config'):
|
| 119 |
os.makedirs('first_order_model/config', exist_ok=True)
|
| 120 |
+
|
| 121 |
+
if not os.path.exists(config_path):
|
| 122 |
+
os.system('wget -P first_order_model/config https://drive.google.com/uc?export=download&id=1pZUMNRjkBiuBEM68oj9nskuWgJR-5QMn -O first_order_model/config/vox-256.yaml')
|
|
|
|
| 123 |
|
| 124 |
return config_path, model_path
|
| 125 |
|
|
|
|
| 128 |
config_path, checkpoint_path = download_model()
|
| 129 |
|
| 130 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 131 |
+
print(f"Using device: {device}")
|
| 132 |
|
| 133 |
# Tải mô hình và cấu hình
|
| 134 |
generator, kp_detector = load_checkpoints(config_path, checkpoint_path, device=device)
|
|
|
|
| 165 |
kp_driving = kp_detector(driving_frame)
|
| 166 |
|
| 167 |
# Chuẩn hóa keypoints
|
| 168 |
+
kp_norm = normalize_kp(
|
| 169 |
+
kp_source=kp_source,
|
| 170 |
+
kp_driving=kp_driving,
|
| 171 |
+
kp_driving_initial=kp_driving_initial,
|
| 172 |
+
use_relative_movement=relative,
|
| 173 |
+
use_relative_jacobian=relative,
|
| 174 |
+
adapt_movement_scale=adapt_movement_scale
|
| 175 |
+
)
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
# Tạo frame
|
| 178 |
out = generator(source, kp_source=kp_source, kp_driving=kp_norm)
|
|
|
|
| 180 |
|
| 181 |
# Lưu video kết quả
|
| 182 |
output_path = 'result.mp4'
|
| 183 |
+
os.system(f"rm -f {output_path}") # Xóa video nếu tồn tại
|
| 184 |
+
|
| 185 |
+
# Lưu frames thành video sử dụng imageio
|
| 186 |
+
frames = [img_as_ubyte(frame) for frame in predictions]
|
| 187 |
+
imageio.mimsave(output_path, frames, fps=fps)
|
| 188 |
|
| 189 |
return output_path
|
| 190 |
|
requirements.txt
CHANGED
|
@@ -8,4 +8,5 @@ scikit-image==0.19.3
|
|
| 8 |
scikit-learn
|
| 9 |
matplotlib
|
| 10 |
PyYAML==5.3.1
|
| 11 |
-
face-alignment==1.3.5
|
|
|
|
|
|
| 8 |
scikit-learn
|
| 9 |
matplotlib
|
| 10 |
PyYAML==5.3.1
|
| 11 |
+
face-alignment==1.3.5
|
| 12 |
+
ffmpeg-python
|