-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathalign_face.py
More file actions
54 lines (46 loc) · 1.5 KB
/
Copy pathalign_face.py
File metadata and controls
54 lines (46 loc) · 1.5 KB
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
import argparse
import os
from glob import glob
from utils.utils import VideoAligner
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('--files', type=str)
parser.add_argument('--out_dir', type=str)
parser.add_argument('--filetype', type=str, choices=['image', 'video'], default='image')
parser.add_argument('--gpu', type=int, default=0)
args = parser.parse_args()
# Load target image
path = args.files
if os.path.isdir(path):
if args.filetype == 'image':
files = glob(path + '*.png')
files += glob(path + '*.jpg')
elif args.filetype == 'video':
files = glob(path + '*.mp4')
files += glob(path + '*.avi')
else:
raise NotImplementedError
else:
files = [path]
# Select filetype
ext = files[0].split('.')[-1]
print(ext)
if ext.lower() in ['avi', 'mp4', 'flv', 'mpg']:
filetype = 'video'
elif ext.lower() in ['jpg', 'jpeg', 'png']:
filetype = 'image'
else:
print("Unknown file type")
raise NotImplementedError
aligner = VideoAligner(device=f'cuda:{args.gpu}')
if not os.path.exists(args.out_dir):
os.makedirs(args.outdir, exist_ok=True)
for file in files:
if filetype == 'image':
save_path = args.out_dir + file.split('/')[-1].split('.')[0] + '.png'
print(f"Saving to {save_path}")
aligner.align_single_image(file, save_path)
else:
save_dir = os.path.join(args.out_dir, file.split('/')[-1].split('.')[0])
print(f"Saving to {save_dir}")
aligner.align_video(file, save_dir)