CoolFace
Apppublic

incolor/facial_expression_classifier

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
1likes
app.py84 linesDownload Raw Back to root
1# Facial expression classifier2import os3from fastai.vision.all import *4import gradio as gr5 6# Emotion7learn_emotion = load_learner('emotions_vgg19.pkl')8learn_emotion_labels = learn_emotion.dls.vocab9 10# Sentiment11learn_sentiment = load_learner('sentiment_vgg19.pkl')12learn_sentiment_labels = learn_sentiment.dls.vocab13 14# Predict15def predict(img):16    img = PILImage.create(img)17    18    pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)19    20    pred_sentiment, pred_sentiment_idx, probs_sentiment = learn_sentiment.predict(img)21    22    #emotions = {f'emotion_{learn_emotion_labels[i]}': float(probs_emotion[i]) for i in range(len(learn_emotion_labels))}23    #sentiments = {f'sentiment_{learn_sentiment_labels[i]}': float(probs_sentiment[i]) for i in range(len(learn_sentiment_labels))}24    25    emotions = {learn_emotion_labels[i]: float(probs_emotion[i]) for i in range(len(learn_emotion_labels))}26    sentiments = {learn_sentiment_labels[i]: float(probs_sentiment[i]) for i in range(len(learn_sentiment_labels))}27        28    return [emotions, sentiments] #{**emotions, **sentiments}29 30# Gradio31title = "Facial Emotion and Sentiment Detector"32 33description = gr.Markdown(34                """Ever wondered what a person might be feeling looking at their picture? 35                 Well, now you can! Try this fun app. Just upload a facial image in JPG or36                 PNG format. Voila! you can now see what they might have felt when the picture37                 was taken.38                 39                 **Tip**: Be sure to only include face to get best results. Check some sample images40                 below for inspiration!""").value41 42article = gr.Markdown(43             """**DISCLAIMER:** This model does not reveal the actual emotional state of a person. Use and 44             interpret results at your own risk! It was built as a demo for AI course. Samples images45             were downloaded from VG & AftenPosten news webpages. Copyrights belong to respective46             brands. All rights reserved.47             48             **PREMISE:** The idea is to determine an overall sentiment of a news site on a daily basis49             based on the pictures. We are restricting pictures to only include close-up facial50             images.51             52             **DATA:** FER2013 dataset consists of 48x48 pixel grayscale images of faces. There are 28,709 53             images in the training set and 3,589 images in the test set. However, for this demo all 54             pictures were combined into a single dataset and 80:20 split was used for training. Images55             are assigned one of the 7 emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral.56             In addition to these 7 classes, images were re-classified into 3 sentiment categories based57             on emotions:58             59             Positive (Happy, Surprise)60             61             Negative (Angry, Disgust, Fear, Sad)62             63             Neutral (Neutral)64             65             FER2013 (preliminary version) dataset can be downloaded at:66             https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data67             68             **MODEL:** VGG19 was used as the base model and trained on FER2013 dataset. Model was trained69             using PyTorch and FastAI. Two models were trained, one for detecting emotion and the other70             for detecting sentiment. Although, this could have been done with just one model, here two71             models were trained for the demo.""").value72 73enable_queue=True74 75examples = ['happy1.jpg', 'happy2.jpg', 'angry1.png', 'angry2.jpg', 'neutral1.jpg', 'neutral2.jpg']76 77gr.Interface(fn = predict, 78             inputs = gr.Image(shape=(48, 48), image_mode='L'), 79             outputs = [gr.Label(label='Emotion'), gr.Label(label='Sentiment')], #gr.Label(),80             title = title,81             examples = examples,82             description = description,83             article=article,84             allow_flagging='never').launch(enable_queue=enable_queue)