Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code errors and replaced deprecated imports. #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions convert_fer2013_to_images_and_landmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import os
import argparse
import errno
import scipy.misc
# import scipy.misc
from skimage import img_as_ubyte

import imageio
import dlib
import cv2

Expand All @@ -26,13 +29,18 @@

# 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("-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("-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")
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
Expand All @@ -47,17 +55,17 @@
if args.onehot == "yes":
ONE_HOT_ENCODING = True
if args.expressions != "":
expressions = args.expressions.split(",")
for i in range(0,len(expressions)):
expressions = args.expressions.split(",")
for i in range(0, len(expressions)):
label = int(expressions[i])
if (label >=0 and label<=6 ):
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")
SELECTED_LABELS = [0, 1, 2, 3, 4, 5, 6]
print(str(len(SELECTED_LABELS)) + " expressions")

# loading Dlib predictor and preparing arrays:
print( "preparing")
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))
Expand All @@ -70,6 +78,7 @@
else:
raise


def get_landmarks(image, rects):
# this function have been copied from http://bit.ly/2cj7Fpq
if len(rects) > 1:
Expand All @@ -78,6 +87,7 @@ def get_landmarks(image, rects):
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)
Expand All @@ -87,20 +97,22 @@ def get_new_label(label, one_hot_encoding=False):
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))
cells_per_block=(1, 1), visualize=False))
return hog_windows

print( "importing csv file")

print("importing csv file")
data = pd.read_csv('fer2013.csv')

for category in data['Usage'].unique():
print( "converting set: " + category + "...")
print("converting set: " + category + "...")
# create folder
if not os.path.exists(category):
try:
Expand All @@ -110,12 +122,12 @@ def sliding_hog_windows(image):
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 = []
Expand All @@ -125,25 +137,30 @@ def sliding_hog_windows(image):
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))
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)
# scipy.misc.imsave(category + '/' + str(i) + '.jpg', image)
#img_as_ubyte
imageio.imwrite(category + '/' + str(i) + '.jpg', img_as_ubyte(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)
cells_per_block=(1, 1), visualize=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)
cells_per_block=(1, 1), visualize=True)
hog_features.append(features)
if GET_HOG_IMAGES:
hog_images.append(hog_image)
if GET_LANDMARKS:
scipy.misc.imsave('temp.jpg', image)
# scipy.misc.imsave('temp.jpg', image)
#img_as_ubyte(img)
imageio.imwrite('temp.jpg', img_as_ubyte(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)
Expand Down