File size: 13,284 Bytes
516b9a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import time
import json
import base64
import tempfile
import shutil
import subprocess
from io import BytesIO
from typing import Dict, Any, Optional

import torch
import numpy as np
import cv2
import glob
import pickle
from tqdm import tqdm
import copy
from argparse import Namespace
import requests
import gdown
from huggingface_hub import snapshot_download

from musetalk.utils.utils import get_file_type, get_video_fps, datagen
from musetalk.utils.preprocessing import (
    get_landmark_and_bbox,
    read_imgs,
    coord_placeholder,
)
from musetalk.utils.blending import get_image
from musetalk.utils.utils import load_all_model


class EndpointHandler:
    def __init__(self, path=""):
        """
        Initialize the handler with model loading and checkpoint downloading.
        """
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.project_dir = path or os.path.abspath(os.path.dirname(__file__))
        self.checkpoints_dir = os.path.join(self.project_dir, "checkpoints")

        # Download and load models
        self._download_model()
        self._load_models()

        # Create temp directory for processing
        self.temp_dir = tempfile.mkdtemp()

    def _download_model(self):
        """Download model checkpoints if they don't exist."""
        if not os.path.exists(self.checkpoints_dir):
            os.makedirs(self.checkpoints_dir)
            print("Checkpoint Not Downloaded, start downloading...")
            tic = time.time()

            # Main model
            snapshot_download(
                repo_id="TMElyralab/MuseTalk",
                local_dir=self.checkpoints_dir,
                max_workers=8,
                local_dir_use_symlinks=True,
            )

            # VAE weights
            snapshot_download(
                repo_id="stabilityai/sd-vae-ft-mse",
                local_dir=self.checkpoints_dir,
                max_workers=8,
                local_dir_use_symlinks=True,
            )

            # DWPose
            snapshot_download(
                repo_id="yzd-v/DWPose",
                local_dir=self.checkpoints_dir,
                max_workers=8,
                local_dir_use_symlinks=True,
            )

            # Whisper model
            self._download_whisper()

            # Face parser
            self._download_face_parser()

            # ResNet
            self._download_resnet()

            toc = time.time()
            print(f"Download cost {toc-tic} seconds")
        else:
            print("Already downloaded the model.")

    def _download_whisper(self):
        """Download Whisper model."""
        url = "https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt"
        response = requests.get(url)
        if response.status_code == 200:
            file_path = f"{self.checkpoints_dir}/whisper/tiny.pt"
            os.makedirs(f"{self.checkpoints_dir}/whisper/", exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(response.content)
        else:
            print(f"Whisper download failed, status code: {response.status_code}")

    def _download_face_parser(self):
        """Download face parser model."""
        url = "https://drive.google.com/uc?id=154JgKpzCPW82qINcVieuPH3fZ2e0P812"
        os.makedirs(f"{self.checkpoints_dir}/face-parse-bisent/", exist_ok=True)
        output_path = f"{self.checkpoints_dir}/face-parse-bisent/79999_iter.pth"
        gdown.download(url, output_path, quiet=False)

    def _download_resnet(self):
        """Download ResNet model."""
        url = "https://download.pytorch.org/models/resnet18-5c106cde.pth"
        response = requests.get(url)
        if response.status_code == 200:
            file_path = (
                f"{self.checkpoints_dir}/face-parse-bisent/resnet18-5c106cde.pth"
            )
            with open(file_path, "wb") as f:
                f.write(response.content)
        else:
            print(f"ResNet download failed, status code: {response.status_code}")

    def _load_models(self):
        """Load all required models."""
        print("Loading models...")
        self.audio_processor, self.vae, self.unet, self.pe = load_all_model()
        self.timesteps = torch.tensor([0], device=self.device)
        print("Models loaded successfully!")

    def _save_file_from_base64(self, base64_data: str, filename: str) -> str:
        """Save base64 encoded file to temporary location."""
        file_data = base64.b64decode(base64_data)
        file_path = os.path.join(self.temp_dir, filename)
        with open(file_path, "wb") as f:
            f.write(file_data)
        return file_path

    def _preprocess_video(self, video_path: str) -> str:
        """Preprocess video to ensure 25fps."""
        dir_path, file_name = os.path.split(video_path)
        if file_name.startswith("processed_"):
            return video_path

        output_file_name = "processed_" + file_name
        output_video = os.path.join(dir_path, output_file_name)

        # Convert to 25fps
        command = f"ffmpeg -i {video_path} -r 25 {output_video} -y"
        subprocess.run(command, shell=True, check=True)
        return output_video

    @torch.no_grad()
    def _generate_video(
        self, audio_path: str, video_path: str, bbox_shift: int = -1
    ) -> str:
        """Generate lip-synced video."""
        # Setup arguments
        args_dict = {
            "result_dir": self.temp_dir,
            "fps": 25,
            "batch_size": 8,
            "output_vid_name": "",
            "use_saved_coord": False,
        }
        args = Namespace(**args_dict)

        # Generate output paths
        input_basename = os.path.basename(video_path).split(".")[0]
        audio_basename = os.path.basename(audio_path).split(".")[0]
        output_basename = f"{input_basename}_{audio_basename}"
        result_img_save_path = os.path.join(args.result_dir, output_basename)
        crop_coord_save_path = os.path.join(
            result_img_save_path, input_basename + ".pkl"
        )
        os.makedirs(result_img_save_path, exist_ok=True)

        output_vid_name = os.path.join(args.result_dir, output_basename + ".mp4")

        # Extract frames from video
        if get_file_type(video_path) == "video":
            save_dir_full = os.path.join(args.result_dir, input_basename)
            os.makedirs(save_dir_full, exist_ok=True)
            cmd = f"ffmpeg -v fatal -i {video_path} -start_number 0 {save_dir_full}/%08d.png"
            os.system(cmd)
            input_img_list = sorted(
                glob.glob(os.path.join(save_dir_full, "*.[jpJP][pnPN]*[gG]"))
            )
            fps = get_video_fps(video_path)
        else:
            input_img_list = glob.glob(os.path.join(video_path, "*.[jpJP][pnPN]*[gG]"))
            input_img_list = sorted(
                input_img_list,
                key=lambda x: int(os.path.splitext(os.path.basename(x))[0]),
            )
            fps = args.fps

        # Extract audio features
        whisper_feature = self.audio_processor.audio2feat(audio_path)
        whisper_chunks = self.audio_processor.feature2chunks(
            feature_array=whisper_feature, fps=fps
        )

        # Preprocess input images
        if os.path.exists(crop_coord_save_path) and args.use_saved_coord:
            print("Using extracted coordinates")
            with open(crop_coord_save_path, "rb") as f:
                coord_list = pickle.load(f)
            frame_list = read_imgs(input_img_list)
        else:
            print("Extracting landmarks...")
            coord_list, frame_list = get_landmark_and_bbox(input_img_list, bbox_shift)
            with open(crop_coord_save_path, "wb") as f:
                pickle.dump(coord_list, f)

        # Prepare input latents
        input_latent_list = []
        for bbox, frame in zip(coord_list, frame_list):
            if bbox == coord_placeholder:
                continue
            x1, y1, x2, y2 = bbox
            crop_frame = frame[y1:y2, x1:x2]
            crop_frame = cv2.resize(
                crop_frame, (256, 256), interpolation=cv2.INTER_LANCZOS4
            )
            latents = self.vae.get_latents_for_unet(crop_frame)
            input_latent_list.append(latents)

        # Create cyclic lists for smoothing
        frame_list_cycle = frame_list + frame_list[::-1]
        coord_list_cycle = coord_list + coord_list[::-1]
        input_latent_list_cycle = input_latent_list + input_latent_list[::-1]

        # Inference batch by batch
        print("Starting inference...")
        video_num = len(whisper_chunks)
        batch_size = args.batch_size
        gen = datagen(whisper_chunks, input_latent_list_cycle, batch_size)
        res_frame_list = []

        for i, (whisper_batch, latent_batch) in enumerate(
            tqdm(gen, total=int(np.ceil(float(video_num) / batch_size)))
        ):
            tensor_list = [torch.FloatTensor(arr) for arr in whisper_batch]
            audio_feature_batch = torch.stack(tensor_list).to(self.unet.device)
            audio_feature_batch = self.pe(audio_feature_batch)

            pred_latents = self.unet.model(
                latent_batch, self.timesteps, encoder_hidden_states=audio_feature_batch
            ).sample
            recon = self.vae.decode_latents(pred_latents)
            for res_frame in recon:
                res_frame_list.append(res_frame)

        # Pad to full image
        print("Padding talking image to original video...")
        for i, res_frame in enumerate(tqdm(res_frame_list)):
            bbox = coord_list_cycle[i % (len(coord_list_cycle))]
            ori_frame = copy.deepcopy(frame_list_cycle[i % (len(frame_list_cycle))])
            x1, y1, x2, y2 = bbox
            try:
                res_frame = cv2.resize(res_frame.astype(np.uint8), (x2 - x1, y2 - y1))
            except:
                continue

            combine_frame = get_image(ori_frame, res_frame, bbox)
            cv2.imwrite(f"{result_img_save_path}/{str(i).zfill(8)}.png", combine_frame)

        # Create video from images
        cmd_img2video = f"ffmpeg -y -v fatal -r {fps} -f image2 -i {result_img_save_path}/%08d.png -vcodec libx264 -vf format=rgb24,scale=out_color_matrix=bt709,format=yuv420p -crf 18 {self.temp_dir}/temp.mp4"
        os.system(cmd_img2video)

        # Combine with audio
        cmd_combine_audio = f"ffmpeg -y -v fatal -i {audio_path} -i {self.temp_dir}/temp.mp4 {output_vid_name}"
        os.system(cmd_combine_audio)

        # Cleanup
        os.remove(f"{self.temp_dir}/temp.mp4")
        shutil.rmtree(result_img_save_path)

        print(f"Result saved to {output_vid_name}")
        return output_vid_name

    def _encode_video_to_base64(self, video_path: str) -> str:
        """Encode video file to base64."""
        with open(video_path, "rb") as video_file:
            video_bytes = video_file.read()
            return base64.b64encode(video_bytes).decode("utf-8")

    def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
        """
        Handle inference request.

        Expected input format:
        {
            "inputs": {
                "audio": "base64_encoded_audio_file",
                "video": "base64_encoded_video_file",
                "bbox_shift": -1  # optional, defaults to -1
            }
        }

        Returns:
        {
            "video": "base64_encoded_output_video",
            "message": "success"
        }
        """
        try:
            # Extract inputs
            inputs = data.get("inputs", {})
            audio_b64 = inputs.get("audio")
            video_b64 = inputs.get("video")
            bbox_shift = inputs.get("bbox_shift", -1)

            if not audio_b64 or not video_b64:
                return {"error": "Both audio and video inputs are required"}

            # Save input files
            audio_path = self._save_file_from_base64(audio_b64, "input_audio.wav")
            video_path = self._save_file_from_base64(video_b64, "input_video.mp4")

            # Preprocess video
            processed_video_path = self._preprocess_video(video_path)

            # Generate output video
            output_video_path = self._generate_video(
                audio_path, processed_video_path, bbox_shift
            )

            # Encode result to base64
            result_b64 = self._encode_video_to_base64(output_video_path)

            return {"video": result_b64, "message": "success"}

        except Exception as e:
            return {"error": f"Processing failed: {str(e)}"}

        finally:
            # Cleanup temporary files
            if hasattr(self, "temp_dir") and os.path.exists(self.temp_dir):
                try:
                    shutil.rmtree(self.temp_dir)
                    self.temp_dir = (
                        tempfile.mkdtemp()
                    )  # Create new temp dir for next request
                except:
                    pass

    def __del__(self):
        """Cleanup on destruction."""
        if hasattr(self, "temp_dir") and os.path.exists(self.temp_dir):
            try:
                shutil.rmtree(self.temp_dir)
            except:
                pass