This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load the weights for the optimal model | |
lstm_model.load_weights('biLSTM.h5') | |
# Converts a class vector (integers) to binary class matrix | |
# for use with categorical_crossentropy | |
# IMPORTANT for CATEGORICAL_CROSSENTROPY! | |
y_test = np_utils.to_categorical(y_test, 2) | |
# generator for validating the LSTM model | |
test_gen = generate_batch(X_test, y_test, batch_size, expected_frames) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
import glob | |
# Set image dir of your choice | |
imageDir = r'dataset\nervous_frames\_0\\' | |
# Read in files and sort | |
filenames = os.listdir(imageDir) | |
filenames.sort(key= lambda x: int(x[:-4])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create the bidirectional LSTM model | |
# create a LSTM model | |
lstm_model = Sequential() | |
lstm_model.add(Bidirectional(LSTM(units=256, return_sequences=False), | |
input_shape=(n_frames, n_tokens))) | |
lstm_model.add(Dense(512, activation='relu')) | |
lstm_model.add(Dropout(0.5)) | |
# two categories: happy/amused smiles and nervous/awkward smiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# combine these features | |
features = np.concatenate((features_happy, features_awkward), axis=0) | |
# generate corresponding labels | |
labels = np.concatenate((np.ones(len(features_happy)), np.zeros(len(features_awkward))), axis=0) | |
# save features and labels to file | |
np.savez_compressed('dataset', f=features, l=labels) | |
# split the dataset into training and testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_features(iDirName, cnn_model): | |
# get face detector from dlib and initialize the face aligner | |
detector = dlib.get_frontal_face_detector() | |
# open the video file | |
iFramePaths = [os.path.join(iDirName, iFramePath) for iFramePath in os.listdir(iDirName)] | |
# an empty list to hold the results | |
features = [] | |
# process each frame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_features_dir(ddir, cnn_model): | |
# check the existence of the directory | |
if not os.path.exists(ddir): | |
raise ValueError('The directory "{}" does not exist!'.format(ddir)) | |
# create an empty list to hold the result | |
features = [] | |
# get the list of image dirs in this | |
iDirNames = [os.path.join(ddir, idir) for idir in os.listdir(ddir)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# construct the path for the directory of happy smiles | |
dir_happy = os.path.join('dataset', 'happy_frames\\') | |
# construct the path for the directory of awkward smiles | |
dir_awkward = os.path.join('dataset', 'nervous_frames\\') | |
# create the pre-trained VGG-Face model | |
cnn_model = VGGFace(model='resnet50', include_top=False, input_shape=(224, 224, 3), weights=None) | |
# load the weights from the pre-trained model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
from matplotlib import pyplot as plt | |
import numpy as np | |
def read_csvs(dirPath): | |
# Reads in the OpenFace output csv files | |
# read input directory | |
files = os.listdir(dirPath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FeatureExtraction.exe -fdir "C:\dataset\happy_frames\_1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffmpeg -loglevel warning -i cropped.mp4 %d.jpg |
NewerOlder