-
Notifications
You must be signed in to change notification settings - Fork 4
/
preprocess.py
87 lines (81 loc) · 2.54 KB
/
preprocess.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
'''
1. Load gambar dr full path
2. Masukin ke face detect
3. Kalau ga ada muka, resize gambar asli
Kalau ada, masukin ke alignment
Ukuran resize langsung sama align diatur biar sama
'''
import numpy as np
import cv2
import dlib
import os
import pandas as pd
from tqdm import tqdm
'''
Clean the data for abnormal age (negative), no face
detected (face_score is NaN), more than one face
(both face_score and second_face_score not NaN)
Input : pandas DataFrame
Output : pandas DataFrame
'''
def cleanData(db_frame):
cleaned = db_frame.loc[(db_frame['age'] >= 0) &\
(db_frame['age'] <= 100) &\
(~db_frame['face_score'].isnull()) &\
(db_frame['second_face_score'].isnull()) &\
(~db_frame['gender'].isnull()) ,\
['db_name', 'full_path', 'age', 'gender']
]
return cleaned
'''
Load all image from dataset
Input : pandas DataFrame
Output : pandas DataFrame
'''
def loadImage(db_frame, test=False):
images = None
print('[PREP] Read all image...')
if test == True:
images = [cv2.imread(os.path.join('{}_crop'.format(db), img_path[0])) \
for (db, img_path) in tqdm(zip(db_frame['db_name'].tolist()[:10], \
db_frame['full_path'].tolist()[:10]), total=len(db_frame['db_name'].tolist()[:10]))]
else :
images = [cv2.imread(os.path.join('{}_crop'.format(db), img_path[0])) \
for (db, img_path) in tqdm(zip(db_frame['db_name'].tolist(), \
db_frame['full_path'].tolist()), total=len(db_frame['db_name'].tolist()))]
db_frame['image'] = images
return db_frame
'''
Get resized image
Return square-resized image
'''
def resizeImg(image, size=140):
BLACK = [0,0,0]
h = image.shape[0]
w = image.shape[1]
if w<h:
border = h-w
image= cv2.copyMakeBorder(image,0,0,border,0,cv2.BORDER_CONSTANT,value=BLACK)
else:
border = w-h
image= cv2.copyMakeBorder(image,border,0,0,0,cv2.BORDER_CONSTANT,value=BLACK)
resized = cv2.resize(image, (size,size), interpolation = cv2.INTER_CUBIC)
return resized
'''
Get aligned face if possible, else return whole resized image
Input : single image (numpy array)
Output : single image (numpy array)
'''
def getAlignedFace(image, padding=0.4, size=140):
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
rects = detector(image, 1)
result = None
# if detect exactly 1 face, get aligned face
if len(rects) == 1:
shape = predictor(image, rects[0])
result = dlib.get_face_chip(image, shape, padding=padding, size=size)
# use resized full image
else :
result = resizeImg(image, size)
return result