diff --git a/convert_fer2013_to_images_and_landmarks.py b/convert_fer2013_to_images_and_landmarks.py index a481f15..f3660c4 100755 --- a/convert_fer2013_to_images_and_landmarks.py +++ b/convert_fer2013_to_images_and_landmarks.py @@ -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 @@ -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 @@ -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)) @@ -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: @@ -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) @@ -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: @@ -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 = [] @@ -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)