File size: 3,787 Bytes
0a2e9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from infer_utils import load_poses_whole_video, resizecrop
import os
import cv2
import json
import argparse
import numpy as np
import glob
from PIL import Image
from functools import partial

import decord
from unit_test.io_utils import save_video
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm

# Output directory
cache_base_dir="../input_cache"

# ref_path, video_path, interval, align, mode, h, w, face_change, head_change, without_face
# interval: Frame interval (sample every N frames)
# align: Whether to perform retargeting (True/False)
# mode: Alignment mode
#       - "ref": Align to reference image
#       - "pose": Use original driving video
# h: Output height
# w: Output width
# face_change: Whether to fade facial landmarks (True: fade, False: normal color)
# head_change: Whether to fade head landmarks (True: fade, False: normal color)
# without_face: Whether to skip drawing facial landmarks (True: skip, False: draw)
# 
task_list = [
    ("../examples/img.png","../examples/vid.mp4",1,True,"ref","","",True,False,False),   
    ("../examples/joker2_resize.png","../examples/douyinvid5_v2.mp4",1,False,"pose","","",True,True,False),    
    ("../examples/musk.jpg","../examples/vid2.mp4",1,True,"ref","","",True,False,False),       
    ("../examples/maodie.png","../examples/vid2.mp4",1,True,"ref","","",True,True,False),       
]

def process_one(reference_path, video_path, frame_interval, do_align, alignmode, h=None, w=None, face_change=True, head_change=True, without_face=False):

    if not h or not w:
        ref_img_tmp = Image.open(reference_path).convert("RGB")
        w, h = ref_img_tmp.size
    else:
        h = int(h)
        w = int(w)


    max_short = 768
    if min(h, w) > max_short:
        if h < w:
            scale = max_short / h
            h, w = max_short, int(w * scale)
        else:
            scale = max_short / w
            w, h = max_short, int(h * scale)
    new_h = (h // 16) * 16
    new_w = (w // 16) * 16
    transform = partial(resizecrop, th=new_h, tw=new_w)
    anchor_idx = 0

    pose_tensor, image_input, pose_input, mask_input = load_poses_whole_video(
        video_path=video_path,
        reference=reference_path,
        frame_interval=frame_interval,
        do_align=do_align,
        transform=transform,
        alignmode=alignmode,
        anchor_idx=anchor_idx,
        face_change=face_change,
        head_change=head_change,
        without_face=without_face,
    )


    if os.path.isdir(video_path):
        fps = 30
        print(f"⚠ {video_path} isdir, use default fps={fps}")
    else:
        vr = decord.VideoReader(video_path)
        fps = vr.get_avg_fps()

    output_fps = fps / frame_interval

 
    video_tensor = pose_tensor / 255 * 2 - 1


    vidname = os.path.splitext(os.path.basename(video_path))[0]
    refname = os.path.splitext(os.path.basename(reference_path))[0]
    cache_dir = os.path.join(cache_base_dir, f"ref_{refname}_driven_{vidname}_align_{alignmode}_h_{new_h}_w_{new_w}_fps_{output_fps}")
    os.makedirs(cache_dir, exist_ok=True)


    save_video(os.path.join(cache_dir, "pose.mp4"), video_tensor, fps=output_fps)
    image_input.save(os.path.join(cache_dir, "image_input.png"))
    pose_input.save(os.path.join(cache_dir, "pose_input.png"))
    mask_input.save(os.path.join(cache_dir, "mask_input.png"))

if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=1) as executor:
        futures = []
        for ref_path, video_path , interval, align, mode, h, w, face_change, head_change, without_face in task_list:
            futures.append(executor.submit(process_one, ref_path, video_path, interval, align, mode, h, w, face_change, head_change, without_face))
        
        for f in tqdm(futures):
            f.result()