-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess_dataset.py
65 lines (61 loc) · 2.65 KB
/
preprocess_dataset.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
import sys
from imutils import paths,face_utils
import cv2
import os
import numpy as np
from tensorflow.keras.preprocessing.image import load_img,img_to_array
from src.face_landmark_predictor import FaceLandmarkPredictor
def getDatasetImages(dataset_path):
imagePaths = list(paths.list_images(dataset_path))
data = []
labels = []
filenames = []
for imagePath in imagePaths:
image = cv2.imread(imagePath)
label = imagePath.split(os.path.sep)[-2]
filename = imagePath.split(os.path.sep)[-1]
data.append(image)
labels.append(label)
filenames.append(filename)
return data,labels,filenames
def getBoudingboxFromLandmarksList(landmaks_list):
bouding_box = [landmaks_list[0],landmaks_list[-1]]
for landmark in landmaks_list:
if bouding_box[0][0] < bouding_box[0][0]:
bouding_box[0][0] = landmark[0]
if landmark[1] < bouding_box[0][1]:
bouding_box[0][1] = landmark[1]
if landmark[0] > bouding_box[1][0]:
bouding_box[1][0] = landmark[0]
if landmark[1] > bouding_box[1][1]:
bouding_box[1][1] = landmark[1]
return bouding_box
if __name__ == "__main__":
if len(sys.argv)!=2:
raise IOError("usage: python3 preprocess_dataset.py dataset_path")
img_list,labels,files = getDatasetImages(sys.argv[1])
face_landmark_predictor = FaceLandmarkPredictor()
for img,label,file in zip(img_list,labels,files):
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
landmark_list = face_landmark_predictor.predictLandmarks(img_gray,[0,0,100,100])
isInRange = lambda idx, idx_range : True if idx>=idx_range[0] and idx<=idx_range[1] else False
range_idxs = []
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["left_eye"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["right_eye"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["right_eyebrow"])
range_idxs.append(face_utils.FACIAL_LANDMARKS_68_IDXS["left_eyebrow"])
good_landmarks_list = []
for i in range(48):
for range_idx in range_idxs:
if isInRange(i, range_idx) or i == 30:
good_landmarks_list.append(list(landmark_list[i]))
bb = getBoudingboxFromLandmarksList(good_landmarks_list)
pt1 = (bb[0][0],bb[0][1])
pt2 = (bb[1][0],bb[1][1])
try:
eye_img = img[pt1[1]:pt2[1],pt1[0]:pt2[0]]
eye_img = cv2.resize(eye_img,64,64),interpolation=cv2.INTER_AREA)
except:
continue
new_file_path =f'./dataset/eyes/{label}/{file}'
cv2.imwrite(new_file_path,eye_img)