-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_generator.py
149 lines (122 loc) · 5.46 KB
/
image_generator.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from PIL import Image, ImageFile
from os.path import join, isfile, dirname, splitext
from os import listdir
import face_recognition
import numpy as np
import os
DIR_PATH = os.path.abspath(dirname(__file__))
def create_mask_images(s_dir, t_dir, m_dir):
s_dir = join(DIR_PATH, s_dir)
t_dir = join(DIR_PATH, t_dir)
m_dir = join(DIR_PATH, m_dir)
existing_files = [join(t_dir, f) for f in listdir(t_dir)]
for existing_file in existing_files:
os.remove(existing_file)
images = [join(s_dir, f) for f in listdir(s_dir) if isfile(
join(s_dir, f)) and splitext(join(s_dir, f))[1] == '.jpg']
masks = [join(m_dir, f)
for f in listdir(m_dir) if isfile(join(m_dir, f))]
for image in images:
mask_path = masks.pop(0)
FaceMasker(image, mask_path, t_dir).mask()
masks.append(mask_path)
class FaceMasker:
KEY_FACIAL_FEATURES = ('nose_bridge', 'chin')
def __init__(self, face_path, mask_path, target_dir, model='hog'):
self.face_path = face_path
self.mask_path = mask_path
self.target_dir = target_dir
self.model = model
self._face_img: ImageFile = None
self._mask_img: ImageFile = None
def mask(self):
face_image_np = face_recognition.load_image_file(self.face_path)
face_locations = face_recognition.face_locations(
face_image_np, model=self.model)
face_landmarks = face_recognition.face_landmarks(
face_image_np, face_locations)
self._face_img = Image.fromarray(face_image_np)
self._mask_img = Image.open(self.mask_path)
found_face = False
for face_landmark in face_landmarks:
# check whether facial features meet requirement
skip = False
for facial_feature in self.KEY_FACIAL_FEATURES:
if facial_feature not in face_landmark:
skip = True
break
if skip:
continue
# mask face
found_face = True
self._mask_face(face_landmark)
if found_face:
# save
self._save()
else:
print('Found no face.')
def _mask_face(self, face_landmark: dict):
nose_bridge = face_landmark['nose_bridge']
nose_point = nose_bridge[len(nose_bridge) * 1 // 4]
nose_v = np.array(nose_point)
chin = face_landmark['chin']
chin_len = len(chin)
chin_bottom_point = chin[chin_len // 2]
chin_bottom_v = np.array(chin_bottom_point)
chin_left_point = chin[chin_len // 8]
chin_right_point = chin[chin_len * 7 // 8]
# split mask and resize
width = self._mask_img.width
height = self._mask_img.height
width_ratio = 1.2
new_height = int(np.linalg.norm(nose_v - chin_bottom_v))
# left
mask_left_img = self._mask_img.crop((0, 0, width // 2, height))
mask_left_width = self.get_distance_from_point_to_line(
chin_left_point, nose_point, chin_bottom_point)
mask_left_width = int(mask_left_width * width_ratio)
mask_left_img = mask_left_img.resize((mask_left_width, new_height))
# right
mask_right_img = self._mask_img.crop((width // 2, 0, width, height))
mask_right_width = self.get_distance_from_point_to_line(
chin_right_point, nose_point, chin_bottom_point)
mask_right_width = int(mask_right_width * width_ratio)
mask_right_img = mask_right_img.resize((mask_right_width, new_height))
# merge mask
size = (mask_left_img.width + mask_right_img.width, new_height)
mask_img = Image.new('RGBA', size)
mask_img.paste(mask_left_img, (0, 0), mask_left_img)
mask_img.paste(
mask_right_img, (mask_left_img.width, 0), mask_right_img)
# rotate mask
angle = np.arctan2(
chin_bottom_point[1] - nose_point[1], chin_bottom_point[0] - nose_point[0])
rotated_mask_img = mask_img.rotate(angle, expand=True)
# calculate mask location
center_x = (nose_point[0] + chin_bottom_point[0]) // 2
center_y = (nose_point[1] + chin_bottom_point[1]) // 2
offset = mask_img.width // 2 - mask_left_img.width
radian = angle * np.pi / 180
box_x = center_x + int(offset * np.cos(radian)) - \
rotated_mask_img.width // 2
box_y = center_y + int(offset * np.sin(radian)) - \
rotated_mask_img.height // 2
# add mask
self._face_img.paste(mask_img, (box_x, box_y), mask_img)
def _save(self):
file_name = self.face_path.split('/')[-1]
new_face_path = join(self.target_dir, file_name)
self._face_img.save(new_face_path)
print(f'Save to {new_face_path}')
@staticmethod
def get_distance_from_point_to_line(point, line_point1, line_point2):
distance = np.abs((line_point2[1] - line_point1[1]) * point[0] +
(line_point1[0] - line_point2[0]) * point[1] +
(line_point2[0] - line_point1[0]) * line_point1[1] +
(line_point1[1] - line_point2[1]) * line_point1[0]) / \
np.sqrt((line_point2[1] - line_point1[1]) * (line_point2[1] - line_point1[1]) +
(line_point1[0] - line_point2[0]) * (line_point1[0] - line_point2[0]))
return int(distance)
if __name__ == '__main__':
create_mask_images('images/without_mask',
'images/with_mask', 'images/masks')