-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_fer2013_to_images_and_landmarks.py
166 lines (154 loc) · 6.52 KB
/
convert_fer2013_to_images_and_landmarks.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import numpy as np
import pandas as pd
import os
import argparse
import errno
import scipy.misc
import dlib
import cv2
from skimage.feature import hog
# initialization
image_height = 48
image_width = 48
window_size = 24
window_step = 6
ONE_HOT_ENCODING = True
SAVE_IMAGES = False
GET_LANDMARKS = False
GET_HOG_FEATURES = False
GET_HOG_IMAGES = False
GET_HOG_WINDOWS_FEATURES = False
SELECTED_LABELS = []
IMAGES_PER_LABEL = 500
OUTPUT_FOLDER_NAME = "fer2013_features"
# parse arguments and initialize variables:
parser = argparse.ArgumentParser()
parser.add_argument("-j", "--jpg", default="no", help="save images as .jpg files")
parser.add_argument("-l", "--landmarks", default="yes", help="extract Dlib Face landmarks")
parser.add_argument("-ho", "--hog", default="yes", help="extract HOG features")
parser.add_argument("-hw", "--hog_windows", default="yes", help="extract HOG features from a sliding window")
parser.add_argument("-hi", "--hog_images", default="no", help="extract HOG images")
parser.add_argument("-o", "--onehot", default="yes", help="one hot encoding")
parser.add_argument("-e", "--expressions", default="0,1,2,3,4,5,6", help="choose the faciale expression you want to use: 0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral")
args = parser.parse_args()
if args.jpg == "yes":
SAVE_IMAGES = True
if args.landmarks == "yes":
GET_LANDMARKS = True
if args.hog == "yes":
GET_HOG_FEATURES = True
if args.hog_windows == "yes":
GET_HOG_WINDOWS_FEATURES = True
if args.hog_images == "yes":
GET_HOG_IMAGES = True
if args.onehot == "yes":
ONE_HOT_ENCODING = True
if args.expressions != "":
expressions = args.expressions.split(",")
for i in range(0,len(expressions)):
label = int(expressions[i])
if (label >=0 and label<=6 ):
SELECTED_LABELS.append(label)
if SELECTED_LABELS == []:
SELECTED_LABELS = [0,1,2,3,4,5,6]
print( str(len(SELECTED_LABELS)) + " expressions")
# loading Dlib predictor and preparing arrays:
print( "preparing")
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
original_labels = [0, 1, 2, 3, 4, 5, 6]
new_labels = list(set(original_labels) & set(SELECTED_LABELS))
nb_images_per_label = list(np.zeros(len(new_labels), 'uint8'))
try:
os.makedirs(OUTPUT_FOLDER_NAME)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(OUTPUT_FOLDER_NAME):
pass
else:
raise
def get_landmarks(image, rects):
# this function have been copied from http://bit.ly/2cj7Fpq
if len(rects) > 1:
raise BaseException("TooManyFaces")
if len(rects) == 0:
raise BaseException("NoFaces")
return np.matrix([[p.x, p.y] for p in predictor(image, rects[0]).parts()])
def get_new_label(label, one_hot_encoding=False):
if one_hot_encoding:
new_label = new_labels.index(label)
label = list(np.zeros(len(new_labels), 'uint8'))
label[new_label] = 1
return label
else:
return new_labels.index(label)
def sliding_hog_windows(image):
hog_windows = []
for y in range(0, image_height, window_step):
for x in range(0, image_width, window_step):
window = image[y:y+window_size, x:x+window_size]
hog_windows.extend(hog(window, orientations=8, pixels_per_cell=(8, 8),
cells_per_block=(1, 1), visualise=False))
return hog_windows
print( "importing csv file")
data = pd.read_csv('fer2013.csv')
for category in data['Usage'].unique():
print( "converting set: " + category + "...")
# create folder
if not os.path.exists(category):
try:
os.makedirs(OUTPUT_FOLDER_NAME + '/' + category)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(OUTPUT_FOLDER_NAME):
pass
else:
raise
# get samples and labels of the actual category
category_data = data[data['Usage'] == category]
samples = category_data['pixels'].values
labels = category_data['emotion'].values
# get images and extract features
images = []
labels_list = []
landmarks = []
hog_features = []
hog_images = []
for i in range(len(samples)):
try:
if labels[i] in SELECTED_LABELS and nb_images_per_label[get_new_label(labels[i])] < IMAGES_PER_LABEL:
image = np.fromstring(samples[i], dtype=int, sep=" ").reshape((image_height, image_width))
images.append(image)
if SAVE_IMAGES:
scipy.misc.imsave(category + '/' + str(i) + '.jpg', image)
if GET_HOG_WINDOWS_FEATURES:
features = sliding_hog_windows(image)
f, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualise=True)
hog_features.append(features)
if GET_HOG_IMAGES:
hog_images.append(hog_image)
elif GET_HOG_FEATURES:
features, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16),
cells_per_block=(1, 1), visualise=True)
hog_features.append(features)
if GET_HOG_IMAGES:
hog_images.append(hog_image)
if GET_LANDMARKS:
scipy.misc.imsave('temp.jpg', image)
image2 = cv2.imread('temp.jpg')
face_rects = [dlib.rectangle(left=1, top=1, right=47, bottom=47)]
face_landmarks = get_landmarks(image2, face_rects)
landmarks.append(face_landmarks)
labels_list.append(get_new_label(labels[i], one_hot_encoding=ONE_HOT_ENCODING))
nb_images_per_label[get_new_label(labels[i])] += 1
except Exception as e:
print( "error in image: " + str(i) + " - " + str(e))
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/images.npy', images)
if ONE_HOT_ENCODING:
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/labels.npy', labels_list)
else:
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/labels.npy', labels_list)
if GET_LANDMARKS:
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/landmarks.npy', landmarks)
if GET_HOG_FEATURES or GET_HOG_WINDOWS_FEATURES:
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/hog_features.npy', hog_features)
if GET_HOG_IMAGES:
np.save(OUTPUT_FOLDER_NAME + '/' + category + '/hog_images.npy', hog_images)