|
import os |
|
import shutil |
|
import ffmpeg |
|
from datetime import datetime |
|
from pathlib import Path |
|
import numpy as np |
|
import cv2 |
|
import torch |
|
import spaces |
|
|
|
|
|
|
|
from PIL import Image |
|
from torchvision import transforms |
|
|
|
|
|
|
|
|
|
|
|
|
|
from src.utils.util import get_fps, read_frames, save_videos_grid |
|
|
|
|
|
|
|
from src.utils.pose_util import project_points_with_trans, matrix_to_euler_and_translation, euler_and_translation_to_matrix |
|
from src.audio2vid import smooth_pose_seq |
|
from src.utils.crop_face_single import crop_face |
|
from src.create_modules import lmk_extractor, vis, pipe |
|
|
|
@spaces.GPU |
|
def video2video(ref_img, source_video, size=512, steps=25, length=150, seed=42): |
|
cfg = 3.5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generator = torch.manual_seed(seed) |
|
|
|
width, height = size, size |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
date_str = datetime.now().strftime("%Y%m%d") |
|
time_str = datetime.now().strftime("%H%M") |
|
save_dir_name = f"{time_str}--seed_{seed}-{size}x{size}" |
|
|
|
save_dir = Path(f"output/{date_str}/{save_dir_name}") |
|
save_dir.mkdir(exist_ok=True, parents=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ref_image_np = cv2.cvtColor(ref_img, cv2.COLOR_RGB2BGR) |
|
ref_image_np = crop_face(ref_image_np, lmk_extractor) |
|
if ref_image_np is None: |
|
return None, Image.fromarray(ref_img) |
|
|
|
ref_image_np = cv2.resize(ref_image_np, (size, size)) |
|
ref_image_pil = Image.fromarray(cv2.cvtColor(ref_image_np, cv2.COLOR_BGR2RGB)) |
|
|
|
face_result = lmk_extractor(ref_image_np) |
|
if face_result is None: |
|
return None, ref_image_pil |
|
|
|
lmks = face_result['lmks'].astype(np.float32) |
|
ref_pose = vis.draw_landmarks((ref_image_np.shape[1], ref_image_np.shape[0]), lmks, normed=True) |
|
|
|
source_images = read_frames(source_video) |
|
src_fps = get_fps(source_video) |
|
pose_transform = transforms.Compose( |
|
[transforms.Resize((height, width)), transforms.ToTensor()] |
|
) |
|
|
|
step = 1 |
|
if src_fps == 60: |
|
src_fps = 30 |
|
step = 2 |
|
|
|
pose_trans_list = [] |
|
verts_list = [] |
|
bs_list = [] |
|
src_tensor_list = [] |
|
args_L = len(source_images) if length==0 or length*step > len(source_images) else length*step |
|
args_L = min(args_L, 300*step) |
|
for src_image_pil in source_images[: args_L: step]: |
|
src_tensor_list.append(pose_transform(src_image_pil)) |
|
src_img_np = cv2.cvtColor(np.array(src_image_pil), cv2.COLOR_RGB2BGR) |
|
frame_height, frame_width, _ = src_img_np.shape |
|
src_img_result = lmk_extractor(src_img_np) |
|
if src_img_result is None: |
|
break |
|
pose_trans_list.append(src_img_result['trans_mat']) |
|
verts_list.append(src_img_result['lmks3d']) |
|
bs_list.append(src_img_result['bs']) |
|
|
|
|
|
|
|
trans_mat_arr = np.array(pose_trans_list) |
|
verts_arr = np.array(verts_list) |
|
bs_arr = np.array(bs_list) |
|
min_bs_idx = np.argmin(bs_arr.sum(1)) |
|
|
|
|
|
pose_arr = np.zeros([trans_mat_arr.shape[0], 6]) |
|
|
|
for i in range(pose_arr.shape[0]): |
|
euler_angles, translation_vector = matrix_to_euler_and_translation(trans_mat_arr[i]) |
|
pose_arr[i, :3] = euler_angles |
|
pose_arr[i, 3:6] = translation_vector |
|
|
|
init_tran_vec = face_result['trans_mat'][:3, 3] |
|
pose_arr[:, 3:6] = pose_arr[:, 3:6] - pose_arr[0, 3:6] + init_tran_vec |
|
|
|
pose_arr_smooth = smooth_pose_seq(pose_arr, window_size=3) |
|
pose_mat_smooth = [euler_and_translation_to_matrix(pose_arr_smooth[i][:3], pose_arr_smooth[i][3:6]) for i in range(pose_arr_smooth.shape[0])] |
|
pose_mat_smooth = np.array(pose_mat_smooth) |
|
|
|
|
|
verts_arr = verts_arr - verts_arr[min_bs_idx] + face_result['lmks3d'] |
|
|
|
projected_vertices = project_points_with_trans(verts_arr, pose_mat_smooth, [frame_height, frame_width]) |
|
|
|
pose_list = [] |
|
for i, verts in enumerate(projected_vertices): |
|
lmk_img = vis.draw_landmarks((frame_width, frame_height), verts, normed=False) |
|
pose_image_np = cv2.resize(lmk_img, (width, height)) |
|
pose_list.append(pose_image_np) |
|
|
|
pose_list = np.array(pose_list) |
|
|
|
video_length = len(pose_list) |
|
|
|
video = pipe( |
|
ref_image_pil, |
|
pose_list, |
|
ref_pose, |
|
width, |
|
height, |
|
video_length, |
|
steps, |
|
cfg, |
|
generator=generator, |
|
).videos |
|
|
|
save_path = f"{save_dir}/{size}x{size}_{time_str}_noaudio.mp4" |
|
save_videos_grid( |
|
video, |
|
save_path, |
|
n_rows=1, |
|
fps=src_fps, |
|
) |
|
|
|
audio_output = f'{save_dir}/audio_from_video.aac' |
|
|
|
try: |
|
ffmpeg.input(source_video).output(audio_output, acodec='copy').run() |
|
|
|
stream = ffmpeg.input(save_path) |
|
audio = ffmpeg.input(audio_output) |
|
ffmpeg.output(stream.video, audio.audio, save_path.replace('_noaudio.mp4', '.mp4'), vcodec='copy', acodec='aac', shortest=None).run() |
|
|
|
os.remove(save_path) |
|
os.remove(audio_output) |
|
except: |
|
shutil.move( |
|
save_path, |
|
save_path.replace('_noaudio.mp4', '.mp4') |
|
) |
|
|
|
return save_path.replace('_noaudio.mp4', '.mp4'), ref_image_pil |
|
|