-
Notifications
You must be signed in to change notification settings - Fork 1
/
face_detection_operation.py
68 lines (54 loc) · 2.13 KB
/
face_detection_operation.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
67
68
from os.path import join, exists
from os import mkdir, listdir
import glob
from mtcnn import MTCNN
from PIL import Image
import numpy as np
import cv2
def save_cropped_face(images_root_folder,
required_size=(224, 224),
cropped_folder='dataset'):
if not exists(images_root_folder):
return Exception("Input Images folder is not exist.")
file_types = ["*.png", "*.PNG", "*.JPEG", "*.jpeg", "*.jpg", "*.JPG"]
people = listdir(images_root_folder)
for file_type in file_types:
for person in people:
for i, image_file in enumerate(glob.glob( \
join(images_root_folder, person, file_type) \
) \
):
print(f"processing {image_file}")
img = cv2.imread(image_file)
detector = MTCNN()
results = detector.detect_faces(img)
if not results:
continue
x, y, width, height = results[0]['box']
face = img[y:y + height, x:x + width]
try:
image = Image.fromarray(face)
except ValueError:
continue
image = image.resize(required_size)
face_array = np.asarray(image)
if not exists(cropped_folder):
mkdir(cropped_folder)
if not exists(join(cropped_folder, person)):
mkdir(join(cropped_folder, person))
output_file_name = f"{person}_{i}{image_file[-4:]}"
cv2.imwrite(
join(cropped_folder, person, output_file_name),
face_array)
def get_detected_face(filename, required_size=(224, 224)):
img = cv2.imread(filename)
detector = MTCNN()
results = detector.detect_faces(img)
x, y, width, height = results[0]['box']
face = img[y:y + height, x:x + width]
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = np.asarray(image)
return face_array, face
if __name__ == "__main__":
save_cropped_face("people")