Skip to content

Instantly share code, notes, and snippets.

@Justin-Heer
Justin-Heer / lstm-test-model.py
Created August 16, 2020 18:31
testing the model accuracy on the test set
# 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)
@Justin-Heer
Justin-Heer / output_training_ex.py
Created August 16, 2020 18:26
outputs a video of a training example
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]))
@Justin-Heer
Justin-Heer / train-bilstm.py
Last active August 16, 2020 17:30
create and train the bi-lstm
# 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
@Justin-Heer
Justin-Heer / train-test-val-split.py
Created August 16, 2020 16:29
joins features together and splits the data to prep it for the Bi-LSTM
# 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
@Justin-Heer
Justin-Heer / extract_features.py
Created August 16, 2020 16:20
extract_features
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
@Justin-Heer
Justin-Heer / extract_features_dir.py
Created August 16, 2020 16:19
extract_features_dir
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)]
@Justin-Heer
Justin-Heer / Train-CNN.py
Last active August 17, 2020 06:03
Shows how to prepare the data for the CNN and train it
# 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
@Justin-Heer
Justin-Heer / dataset-exploration.py
Last active August 16, 2020 15:47
basic dataset exploration
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)
@Justin-Heer
Justin-Heer / openface-feature-extraction.bat
Last active August 16, 2020 15:27
extracts features using openface
FeatureExtraction.exe -fdir "C:\dataset\happy_frames\_1"
@Justin-Heer
Justin-Heer / frames.bat
Created August 16, 2020 15:09
extracts frames from the cropped video
ffmpeg -loglevel warning -i cropped.mp4 %d.jpg