faraday commited on
Commit
97551e8
1 Parent(s): 86716b3

move extract kps_sequence from video as util

Browse files
app.py CHANGED
@@ -3,6 +3,7 @@ import shutil
3
  import subprocess
4
 
5
  from inference import InferenceEngine
 
6
 
7
  output_dir = "output"
8
  temp_audio_path = "temp.mp3"
@@ -73,13 +74,13 @@ def run_demo(
73
  progress((25,100), desc="Extract keypoints and audio...")
74
  audio_path = video.replace(".mp4", ".mp3")
75
 
76
- subprocess.run([
77
- "python",
78
- "scripts/extract_kps_sequence_and_audio.py",
79
- "--video_path", video,
80
- "--kps_sequence_save_path", kps_sequence_save_path,
81
- "--audio_save_path", audio_path
82
- ], check=True)
83
  progress((50,100), desc="Keypoints and audio extracted successfully.")
84
  #return "Keypoints and audio extracted successfully."
85
  rem_progress = (75,100)
 
3
  import subprocess
4
 
5
  from inference import InferenceEngine
6
+ from sequence_utils import extract_kps_sequence_from_video
7
 
8
  output_dir = "output"
9
  temp_audio_path = "temp.mp3"
 
74
  progress((25,100), desc="Extract keypoints and audio...")
75
  audio_path = video.replace(".mp4", ".mp3")
76
 
77
+ extract_kps_sequence_from_video(
78
+ INFERENCE_ENGINE.app,
79
+ video,
80
+ audio_path,
81
+ kps_sequence_save_path
82
+ )
83
+
84
  progress((50,100), desc="Keypoints and audio extracted successfully.")
85
  #return "Keypoints and audio extracted successfully."
86
  rem_progress = (75,100)
scripts/extract_kps_sequence_and_audio.py DELETED
@@ -1,47 +0,0 @@
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
- def main(args):
10
- app = FaceAnalysis(
11
- providers=['CUDAExecutionProvider'],
12
- provider_options=[{'device_id': args.gpu_id}],
13
- root=args.insightface_model_path,
14
- )
15
- app.prepare(ctx_id=0, det_size=(args.height, args.width))
16
-
17
- os.system(f'{get_ffmpeg_exe()} -i "{args.video_path}" -y -vn "{args.audio_save_path}"')
18
-
19
- kps_sequence = []
20
- video_capture = cv2.VideoCapture(args.video_path)
21
- frame_idx = 0
22
- while video_capture.isOpened():
23
- ret, frame = video_capture.read()
24
- if not ret:
25
- break
26
- faces = app.get(frame)
27
- assert len(faces) == 1, f'There are {len(faces)} faces in the {frame_idx}-th frame. Only one face is supported.'
28
-
29
- kps = faces[0].kps[:3]
30
- kps_sequence.append(kps)
31
- frame_idx += 1
32
- torch.save(kps_sequence, args.kps_sequence_save_path)
33
-
34
-
35
- if __name__ == '__main__':
36
- parser = argparse.ArgumentParser()
37
- parser.add_argument('--video_path', type=str, default='')
38
- parser.add_argument('--kps_sequence_save_path', type=str, default='')
39
- parser.add_argument('--audio_save_path', type=str, default='')
40
- parser.add_argument('--device', type=str, default='cuda')
41
- parser.add_argument('--gpu_id', type=int, default=0)
42
- parser.add_argument('--insightface_model_path', type=str, default='./model_ckpts/insightface_models/')
43
- parser.add_argument('--height', type=int, default=512)
44
- parser.add_argument('--width', type=int, default=512)
45
- args = parser.parse_args()
46
-
47
- main(args)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
sequence_utils.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ from imageio_ffmpeg import get_ffmpeg_exe
5
+
6
+ def extract_kps_sequence_from_video(
7
+ face_analysis_app,
8
+ video_path, audio_save_path,
9
+ kps_sequence_save_path):
10
+ os.system(f'{get_ffmpeg_exe()} -i "{video_path}" -y -vn "{audio_save_path}"')
11
+
12
+ kps_sequence = []
13
+ video_capture = cv2.VideoCapture(video_path)
14
+ frame_idx = 0
15
+ while video_capture.isOpened():
16
+ ret, frame = video_capture.read()
17
+ if not ret:
18
+ break
19
+ faces = face_analysis_app.get(frame)
20
+ assert len(faces) == 1, f'There are {len(faces)} faces in the {frame_idx}-th frame. Only one face is supported.'
21
+
22
+ kps = faces[0].kps[:3]
23
+ kps_sequence.append(kps)
24
+ frame_idx += 1
25
+ torch.save(kps_sequence, kps_sequence_save_path)
26
+