forked from fbplab/MEBeauty-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
face_crop_align.py
66 lines (42 loc) · 2.09 KB
/
face_crop_align.py
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
import os
import cv2
from deepface import DeepFace
import argparse
import numpy as np
import shutil
from PIL import Image
def ignore_files(dir, files):
return [f for f in files if os.path.isfile(os.path.join(dir, f))]
def crop(images_path, results_path, method):
if os.path.isdir(images_path) == False:
print('Can not find the folder ', images_path)
return
if os.path.isdir(results_path) == False:
shutil.copytree(images_path, results_path, ignore=ignore_files)
print('The folder', results_path, 'has been created')
else:
print('The folder ', results_path, ' already exist, use another folder name to save the result images')
return
for path, subdirs, files in os.walk(images_path):
for name in files:
fullname = os.path.join(path, name)
#face detection and alignment
try:
detected_face = DeepFace.detectFace(img_path = fullname, detector_backend = method)
cv2.imwrite(fullname.replace(images_path, results_path), cv2.cvtColor(detected_face*255, cv2.COLOR_RGB2BGR))
print(fullname.replace(images_path, results_path))
except:
print('The file ', fullname.replace('images', 'results'), ' has no face')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--images_path', type=str, help='image folder name',
default = 'images' )
parser.add_argument('--results_path', type=str, help='where to save the crop pics',
default = 'crop_align_images' )
parser.add_argument('--method', type=str, help='the crop method can be opencv, dlib, mtcnn, ssd, retinanet',
default = 'opencv' )
args = parser.parse_args()
images_path = args.images_path
results_path = args.results_path
method = args.method
crop(images_path, results_path, method)