Datasets:

Modalities:
Text
Formats:
text
Libraries:
Datasets
License:
Yasser Dahou commited on
Commit
3591539
·
verified ·
1 Parent(s): 4e8bcee

Delete crop_videos.py

Browse files
Files changed (1) hide show
  1. crop_videos.py +0 -105
crop_videos.py DELETED
@@ -1,105 +0,0 @@
1
- import json
2
- import numpy as np
3
- import os
4
- from tqdm import tqdm
5
- import subprocess
6
- from glob import glob
7
- import argparse
8
- import time
9
- from utils import crop_video, crop_face, write_video, crop_and_save_audio
10
- from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
11
- import sys
12
-
13
-
14
- '''
15
- Crop the untrimmed videos into multiple clips using corresponding start and end times, bounding boxes and face landmarks.
16
-
17
- Usage:
18
- python crop_videos.py --video_dir /path/to/25-fps-videos --save_path /path/to/save/the/clips --json /path/to/json/file
19
-
20
- To save videos using ffmpeg, add "--use_ffmpeg True". This takes additional time but saves disk space.
21
-
22
- To additionally save audio as separate wav files, add "--save_audio True"
23
-
24
- To merge audio with video and save as a single mp4, add "--merge_audio True"
25
- '''
26
-
27
-
28
- def write_clip(metadata, vid_p, args):
29
- '''
30
- param metadata: dict containing start, end, bounding boxes, landmarks
31
- param vid_p: path to original untrimmed video at 25fps
32
- param args: main args
33
- '''
34
- for k, clip in enumerate(metadata):
35
- # get the clip frames and corresponding landmarks
36
- video, landmarks = crop_video(vid_p, clip)
37
- # get the cropped sequence around the mouth using the landmarks
38
- crop_seq = crop_face(video, landmarks)
39
- save_video_path = os.path.join(args.save_path, 'videos', vid_p.split('/')[-1][:-4], f'{str(k).zfill(5)}.mp4')
40
- save_audio_path = save_video_path.replace('.mp4','.wav')
41
- # get the audio part of the clip
42
- if args.save_audio or args.merge_audio:
43
- crop_and_save_audio(vid_p, save_audio_path, clip['start'], clip['end'])
44
- # write clip to disk
45
- write_video(save_video_path, crop_seq, save_audio_path, merge_audio=args.merge_audio, use_ffmpeg=args.use_ffmpeg)
46
- return
47
-
48
-
49
- def main(args):
50
- savepath = args.save_path
51
- json_path = args.json
52
- vid_dir = args.video_dir
53
-
54
- video_list = glob(os.path.join(vid_dir, '25_fps_videos*', '*.mp4'))
55
- print(f'Loading json file {json_path}')
56
- data = json.load(open(json_path,'r'))
57
- print(f'Total number of videos {len(video_list)}. Json length {len(data)}')
58
- video_ids = list(data.keys())
59
- count_clips = 0
60
-
61
- futures = []
62
- writer_str = 'Ffmpeg' if args.use_ffmpeg else 'cv2.VideoWriter'
63
- print(f'Using {writer_str} to save the cropped clips.')
64
-
65
- with tqdm(total=len(video_ids), file=sys.stdout) as progress:
66
- with ProcessPoolExecutor() as executor:
67
- for z in video_ids:
68
- idx = [k for k, i in enumerate(video_list) if z in i]
69
- metadata = data[z]
70
- vid_p = video_list[idx[0]]
71
- os.makedirs(os.path.join(savepath, 'videos', vid_p.split('/')[-1][:-4]), exist_ok=True)
72
- future = executor.submit(write_clip, metadata, vid_p, args)
73
- futures.append(future)
74
-
75
- for _ in as_completed(futures):
76
- progress.update()
77
-
78
- print(f'Cropping videos completed.')
79
- print(f'Getting the labels.')
80
- labels = {}
81
- for z in tqdm(video_ids):
82
- metadata = data[z]
83
- for k, clip in enumerate(metadata):
84
- labk = clip['label']
85
- fi = os.path.join('videos', vid_p.split('/')[-1][:-4], f'{str(k).zfill(5)}.mp4')
86
- labels[fi] = labk
87
- label_file = f'{args.save_path}/labels.json'
88
- with open(label_file, 'w', encoding='utf-8') as f:
89
- json.dump(labels, f)
90
-
91
-
92
-
93
- if __name__ == "__main__":
94
- parser = argparse.ArgumentParser(description='Vhisper crop videos')
95
- parser.add_argument('--save_path', type=str, default='', help='Path for saving.')
96
- parser.add_argument('--json', type=str, default='', help='Json path')
97
- parser.add_argument('--video_dir', type=str, default='', help='Path to directory where original videos are stored.')
98
- parser.add_argument('--save_audio', type=bool, default=False, help='Whether to save audio info.')
99
- parser.add_argument('--merge_audio', type=bool, default=False, help='Whether to merge audio with the video when saving.')
100
- parser.add_argument('--use_ffmpeg', type=bool, default=False, help='Whether to use ffmpeg instead of cv2 for saving the video.')
101
-
102
- args = parser.parse_args()
103
- tic = time.time()
104
- main(args)
105
- print(f'Elpased total time for processing: {time.time()-tic} seconds')