├── FaceExpressionRecSurvey.pdf ├── README.md ├── cam_face_landmarks.py ├── classifier_test.py ├── codes ├── Co-training_.py ├── EM_curve.py ├── RFClassifier_plot.py ├── ROC_SVM_all-labels.py ├── SVMClassifier.py ├── barinmatplotlibKNN.py ├── classifier_MLP_plot.py ├── classifier_cluster_label.py ├── classifier_maximization_approach.py ├── cnn.py ├── emotion.csv ├── read_data.py ├── read_database.py └── supervised.py ├── emos.csv └── read_database.py /FaceExpressionRecSurvey.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ayushi-jain97/Emotion-detection/b4faa3149ef823f5a9c83c8269e5a215fadb1f5b/FaceExpressionRecSurvey.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Emotion-detection 2 | Identify the emotion (neutral, anger, contempt, disgust, fear, happy, sadness, surprise) in a given static image. This is an example of a Supervised Machine Learning problem. 3 | 4 | # Prerequisites 5 | * CK+ AU Coded Database is available for free. It can be downloaded from here. http://www.consortium.ri.cmu.edu/ckagree/ 6 | * Install the following python libraries: 7 | * pandas 8 | * sklearn 9 | * numpy 10 | 11 | # Getting Started 12 | Run read_database.py 13 | ```python 14 | python read_database.py 15 | ``` 16 | This will create a file emos.csv that can be opened and edited in Excel. This file has all features (AU Codes) and labels (Emotions). 17 | 18 | Next run classifier_test.py 19 | ```python 20 | python classifier_test.py 21 | ``` 22 | See the ouput and compare accuracy, recall, precision, F-measure of different classifiers. 23 | Additionally, Confusion Matrix can be printed 24 | ```python 25 | from pandas_ml import ConfusionMatrix as cm 26 | print cm(y_true,y_predicted) 27 | ``` 28 | 29 | 30 | -------------------------------------------------------------------------------- /cam_face_landmarks.py: -------------------------------------------------------------------------------- 1 | import dlib 2 | import cv2 3 | 4 | predictor_path = "shape_predictor_68_face_landmarks.dat" 5 | feed = cv2.VideoCapture(0) 6 | print feed.read()[1].shape 7 | detector = dlib.get_frontal_face_detector() 8 | predictor = dlib.shape_predictor(predictor_path) 9 | win = dlib.image_window() 10 | 11 | while True: 12 | img = feed.read()[1] 13 | win.clear_overlay() 14 | win.set_image(img) 15 | 16 | dets = detector(img, 1) 17 | print("Number of faces detected: {}".format(len(dets))) 18 | for k, d in enumerate(dets): 19 | print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom())) 20 | shape = predictor(img, d) 21 | print("Part 0: {}, Part 1: {} ...".format(shape.part(0),shape.part(1))) 22 | win.add_overlay(shape) 23 | print("") 24 | win.add_overlay(dets) 25 | dlib.hit_enter_to_continue() -------------------------------------------------------------------------------- /classifier_test.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | from sklearn.cross_validation import train_test_split 3 | from sklearn.metrics import accuracy_score, precision_recall_fscore_support 4 | import numpy as np 5 | df = pd.read_csv("emotion.csv",header=0) 6 | 7 | df = df.drop(["Person Id", "Person SubID"],axis=1) 8 | df_train = df[df["Emotion"]!=-1] 9 | df_test = df[df["Emotion"]==-1] 10 | 11 | y = np.array(df_train["Emotion"]) 12 | X = np.array(df_train.drop(["Emotion"],axis=1)) 13 | 14 | X_train, X_cv, y_train, y_cv = train_test_split(X,y,test_size=0.2,random_state=42) 15 | 16 | print X_train.shape, X_cv.shape 17 | 18 | # SVM, CNN, ANN, KNN, Random Forest, Naive Bayes 19 | from sklearn.svm import SVC 20 | clf_svm = SVC(kernel="rbf", C=10000) 21 | from sklearn.neighbors import KNeighborsClassifier 22 | clf_knn = KNeighborsClassifier(n_neighbors=3) 23 | from sklearn.ensemble import RandomForestClassifier 24 | clf_rf = RandomForestClassifier(n_estimators=10, min_samples_split=50) 25 | from sklearn.naive_bayes import GaussianNB 26 | clf_nb = GaussianNB() 27 | from sklearn.neural_network import MLPClassifier 28 | clf_nn = MLPClassifier(solver='lbgfs', alpha=1e-5, hidden_layer_sizes=(5, 4), random_state=1) 29 | from sklearn.ensemble import AdaBoostClassifier 30 | clf_ada = AdaBoostClassifier(n_estimators=50) 31 | 32 | print "Training SVM classifier..." 33 | clf_svm.fit(X_train, y_train) 34 | pred = clf_svm.predict(X_cv) 35 | print accuracy_score(pred, y_cv) 36 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 37 | print "Training K Nearest Neighbours classifier..." 38 | clf_knn.fit(X_train, y_train) 39 | pred = clf_knn.predict(X_cv) 40 | print accuracy_score(pred, y_cv) 41 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 42 | print "Training Random Forest classifier..." 43 | clf_rf.fit(X_train, y_train) 44 | pred = clf_rf.predict(X_cv) 45 | print accuracy_score(pred, y_cv) 46 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 47 | print "Training Gaussian Naive Bayes classifier..." 48 | clf_nb.fit(X_train, y_train) 49 | pred = clf_nb.predict(X_cv) 50 | print accuracy_score(pred, y_cv) 51 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 52 | print "Training Multilayer Perceptron classifier..." 53 | clf_nn.fit(X_train, y_train) 54 | pred = clf_nn.predict(X_cv) 55 | print accuracy_score(pred, y_cv) 56 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 57 | print "Training Adaboost classifier..." 58 | clf_ada.fit(X_train, y_train) 59 | pred = clf_ada.predict(X_cv) 60 | print accuracy_score(pred, y_cv) 61 | print precision_recall_fscore_support(pred, y_cv, average='weighted', labels=list(range(8))) 62 | -------------------------------------------------------------------------------- /codes/Co-training_.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Co-Training Semi-Supervised Learning Approach on CK+ dataset 3 | 4 | # Libraries Used 5 | pandas, sklearn, numpy 6 | 7 | # Variables 8 | clf_svm : SVM classifier 9 | df : pandas dataframe 10 | df_labelled : Dataframe of labelled data 11 | df_unlabelled : Dataframe of unlabelled data 12 | X : inputs 13 | y : labels 14 | pred : predictions 15 | k : Parameter for co-training 16 | ''' 17 | 18 | # Libraries used 19 | import pandas as pd # To read database 20 | from sklearn.cross_validation import train_test_split # To split database 21 | from sklearn.metrics import accuracy_score, precision_recall_fscore_support # Result of model on database 22 | import numpy as np # Mathematical analysis 23 | from sklearn.svm import SVC # To apply SVM 24 | from sklearn.grid_search import GridSearchCV # For Hyperparameter tuning 25 | 26 | param_grid = { 27 | 'C': [1e-2, 1e-1, 1e0, 1e2, 1e1, 1e3, 5e3, 1e4, 5e4,1e5,450000], 28 | 'kernel': ['linear', 'rbf'] 29 | } 30 | 31 | clf_svm1 = GridSearchCV(SVC(), param_grid) # Classifier 1 32 | clf_svm2 = GridSearchCV(SVC(), param_grid) # Classifier 2 33 | 34 | # Read database 35 | df = pd.read_csv("emotion.csv",header=0) 36 | df = df.drop(["Person Id", "Person SubID"],axis=1) 37 | df_labelled = df[df["Emotion"]!=-1] 38 | df_unlabelled = df[df["Emotion"]==-1] 39 | 40 | # Seperate labelled and unlabelled data 41 | y_labelled = df_labelled["Emotion"] 42 | X_labelled = df_labelled.drop(["Emotion"],axis=1) 43 | 44 | y_unlabelled = df_unlabelled["Emotion"] 45 | X_unlabelled = df_unlabelled.drop(["Emotion"],axis=1) 46 | 47 | #COTRAINING 48 | k=4 49 | X_train1, X_train2, y_train1, y_train2 = train_test_split(np.array(X_labelled),np.array(y_labelled),test_size=0.5,random_state=42) 50 | length1=X_unlabelled.shape[0] 51 | X_u=np.array(X_unlabelled) 52 | X_unlabelled1=np.array(X_u[0:(length1/2)]) 53 | X_unlabelled2=np.array(X_u[(length1/2):]) 54 | np.random.shuffle(X_unlabelled1) 55 | np.random.shuffle(X_unlabelled2) 56 | 57 | low,high=0,k 58 | while(low