amrezo drsaikirant88 commited on
Commit
02c66ed
·
0 Parent(s):

Duplicate from schibsted/facial_expression_classifier

Browse files

Co-authored-by: Saikiran Tharimena <drsaikirant88@users.noreply.huggingface.co>

Files changed (12) hide show
  1. .gitattributes +31 -0
  2. README.md +14 -0
  3. angry1.png +0 -0
  4. angry2.jpg +0 -0
  5. app.py +84 -0
  6. emotions_vgg19.pkl +3 -0
  7. happy1.jpg +0 -0
  8. happy2.jpg +0 -0
  9. neutral1.jpg +0 -0
  10. neutral2.jpg +0 -0
  11. requirements.txt +1 -0
  12. sentiment_vgg19.pkl +3 -0
.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Facial Expression Classifier
3
+ emoji: 🏢
4
+ colorFrom: purple
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 3.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: schibsted/facial_expression_classifier
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
angry1.png ADDED
angry2.jpg ADDED
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Facial expression classifier
2
+ import os
3
+ from fastai.vision.all import *
4
+ import gradio as gr
5
+
6
+ # Emotion
7
+ learn_emotion = load_learner('emotions_vgg19.pkl')
8
+ learn_emotion_labels = learn_emotion.dls.vocab
9
+
10
+ # Sentiment
11
+ learn_sentiment = load_learner('sentiment_vgg19.pkl')
12
+ learn_sentiment_labels = learn_sentiment.dls.vocab
13
+
14
+ # Predict
15
+ def 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
+ # Gradio
31
+ title = "Facial Emotion and Sentiment Detector"
32
+
33
+ description = 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 or
36
+ PNG format. Voila! you can now see what they might have felt when the picture
37
+ was taken.
38
+
39
+ **Tip**: Be sure to only include face to get best results. Check some sample images
40
+ below for inspiration!""").value
41
+
42
+ article = 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 images
45
+ were downloaded from VG & AftenPosten news webpages. Copyrights belong to respective
46
+ brands. All rights reserved.
47
+
48
+ **PREMISE:** The idea is to determine an overall sentiment of a news site on a daily basis
49
+ based on the pictures. We are restricting pictures to only include close-up facial
50
+ 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. Images
55
+ 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 based
57
+ 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/data
67
+
68
+ **MODEL:** VGG19 was used as the base model and trained on FER2013 dataset. Model was trained
69
+ using PyTorch and FastAI. Two models were trained, one for detecting emotion and the other
70
+ for detecting sentiment. Although, this could have been done with just one model, here two
71
+ models were trained for the demo.""").value
72
+
73
+ enable_queue=True
74
+
75
+ examples = ['happy1.jpg', 'happy2.jpg', 'angry1.png', 'angry2.jpg', 'neutral1.jpg', 'neutral2.jpg']
76
+
77
+ gr.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)
emotions_vgg19.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:335570ccf5133bc1fc0d4cc9bb7835b72fd457a64c0cdefbbc863f1c24cc4454
3
+ size 82965753
happy1.jpg ADDED
happy2.jpg ADDED
neutral1.jpg ADDED
neutral2.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fastai
sentiment_vgg19.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e6fc0f18d5a082cd835b6b8cbd32041170be0ac20b7ca555df1209e2e4d181c3
3
+ size 82957497