-
Notifications
You must be signed in to change notification settings - Fork 14
/
data_builder.py
146 lines (110 loc) · 5.17 KB
/
data_builder.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
import os
import cv2
import dlib
import numpy as np
from zipfile import ZipFile
from sklearn.preprocessing import OneHotEncoder
import utils
face_detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor("face_detectors/shape_predictor_68_face_landmarks.dat")
class DataBuilder():
def __init__(self, path, classes, img_to_exclude=[]):
self.path = path
self.classes = classes
self.img_to_exclude = img_to_exclude
def class_image_count(self):
total_images = 0
for dir_ in os.listdir(self.path):
if dir_ in self.classes:
count = 0
for f in os.listdir(self.path + dir_ + "/"):
if not dir_ + "/" + f in self.img_to_exclude:
count += 1
print(f"class {dir_} has {count} images")
total_images += count
print(f"total images are {total_images}")
def build_from_directory(self):
raise NotImplementedError("build_from_directory is not implemented")
def build_from_zip(self, path_from, path_to):
with ZipFile(self.path_from, 'r') as zip_:
print('Extracting all the files...')
zip_.extractall(path_to)
print('Done!')
self.zip_extractor(path_to)
self.path = path_to
self.build_from_directory()
class ImageToArray(DataBuilder):
def build_from_directory(self):
img_arr = []
img_label = []
label_to_text = {}
label = 0
for dir_ in os.listdir(self.path):
if dir_ in self.classes:
for f in os.listdir(self.path + dir_ + "/"):
if not dir_ + "/" + f in self.img_to_exclude:
img_arr.append(np.expand_dims(cv2.imread(self.path + dir_ + "/" + f, 0), axis=2))
img_label.append(label)
print(f"loaded {dir_} images to numpy arrays...")
label_to_text[label] = dir_
label += 1
img_arr = np.array(img_arr)
img_label = np.array(img_label)
img_label = OneHotEncoder(sparse=False).fit_transform(img_label.reshape(-1,1))
return img_arr, img_label, label_to_text
class ImageToROI(DataBuilder):
def build_from_directory(self):
print("Extracting Eyes and mouth ROI, this may take some time")
roi1_arr = []
roi2_arr = []
self.img_to_exclude = []
for dir_ in os.listdir(self.path):
if dir_ in self.classes:
print(f"processing {dir_} images...")
for f in os.listdir(self.path + dir_ + "/"):
gray_img = cv2.imread(self.path + dir_ + "/" + f, 0)
gray_img = cv2.resize(gray_img, (96,96))
faces = face_detector(gray_img)
if faces:
for face in faces:
try:
landmarks = shape_predictor(gray_img, face)
roi1, roi2 = utils.extract_roi1_roi2(gray_img, landmarks)
roi1_arr.append(roi1), roi2_arr.append(roi2)
except Exception:
self.img_to_exclude.append(dir_ + "/" + f)
break
else:
self.img_to_exclude.append(dir_ + "/" + f)
print(f"\ntotal images to exclude: {len(self.img_to_exclude)}")
return np.array(roi1_arr), np.array(roi2_arr), self.img_to_exclude
class ImageToHOGFeatures(DataBuilder):
def build_from_directory(self):
print("Extracting HOG Features...")
hog_features = []
for dir_ in os.listdir(self.path):
if dir_ in self.classes:
for f in os.listdir(self.path + dir_ + "/"):
if not dir_ + "/" + f in self.img_to_exclude:
gray_img = cv2.imread(self.path + dir_ + "/" + f, 0)
hogfeat = utils.extract_hog_features(gray_img)
hog_features.append(hogfeat)
print(f"processed {dir_} images...")
return np.array(hog_features)
class ImageToKeyLandmarksDistances(DataBuilder):
def build_from_directory(self):
print("Extracting KeyLandmarks Distances...")
kl_distances = []
for dir_ in os.listdir(self.path):
if dir_ in self.classes:
print(f"processing {dir_} images...")
for f in os.listdir(self.path + dir_ + "/"):
if not dir_ + "/" + f in self.img_to_exclude:
gray_img = cv2.imread(self.path + dir_ + "/" + f, 0)
gray_img = cv2.resize(gray_img, (96,96))
faces = face_detector(gray_img)
landmarks_coord = utils.get_landmarks(gray_img, gray_img, faces[0])
key_landmarks_coords = utils.get_keylandmarks_coords(landmarks_coord)
all_kl_dists = utils.get_keylandmarks_distances(key_landmarks_coords)
kl_distances.append(all_kl_dists)
return np.array(kl_distances)