Upload extract_kps_sequence_and_audio.py
Browse files
scripts/extract_kps_sequence_and_audio.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import torch
|
| 6 |
+
from insightface.app import FaceAnalysis
|
| 7 |
+
from imageio_ffmpeg import get_ffmpeg_exe
|
| 8 |
+
|
| 9 |
+
parser = argparse.ArgumentParser()
|
| 10 |
+
parser.add_argument('--video_path', type=str, default='')
|
| 11 |
+
parser.add_argument('--kps_sequence_save_path', type=str, default='')
|
| 12 |
+
parser.add_argument('--audio_save_path', type=str, default='')
|
| 13 |
+
parser.add_argument('--device', type=str, default='cuda')
|
| 14 |
+
parser.add_argument('--gpu_id', type=int, default=0)
|
| 15 |
+
parser.add_argument('--insightface_model_path', type=str, default='./model_ckpts/insightface_models/')
|
| 16 |
+
parser.add_argument('--height', type=int, default=512)
|
| 17 |
+
parser.add_argument('--width', type=int, default=512)
|
| 18 |
+
args = parser.parse_args()
|
| 19 |
+
|
| 20 |
+
app = FaceAnalysis(
|
| 21 |
+
providers=['CUDAExecutionProvider' if args.device == 'cuda' else 'CPUExecutionProvider'],
|
| 22 |
+
provider_options=[{'device_id': args.gpu_id}] if args.device == 'cuda' else [],
|
| 23 |
+
root=args.insightface_model_path,
|
| 24 |
+
)
|
| 25 |
+
app.prepare(ctx_id=0, det_size=(args.height, args.width))
|
| 26 |
+
|
| 27 |
+
os.system(f'{get_ffmpeg_exe()} -i "{args.video_path}" -y -vn "{args.audio_save_path}"')
|
| 28 |
+
|
| 29 |
+
kps_sequence = []
|
| 30 |
+
video_capture = cv2.VideoCapture(args.video_path)
|
| 31 |
+
frame_idx = 0
|
| 32 |
+
while video_capture.isOpened():
|
| 33 |
+
ret, frame = video_capture.read()
|
| 34 |
+
if not ret:
|
| 35 |
+
break
|
| 36 |
+
frame = cv2.resize(frame, (args.width, args.height))
|
| 37 |
+
faces = app.get(frame)
|
| 38 |
+
assert len(faces) == 1, f'There are {len(faces)} faces in the {frame_idx}-th frame. Only one face is supported.'
|
| 39 |
+
|
| 40 |
+
kps = faces[0].kps[:3]
|
| 41 |
+
kps_sequence.append(kps)
|
| 42 |
+
frame_idx += 1
|
| 43 |
+
torch.save(kps_sequence, args.kps_sequence_save_path)
|