File size: 2,758 Bytes
a9ad9c4
 
 
 
 
 
edb5c24
a9ad9c4
 
edb5c24
a9ad9c4
 
edb5c24
 
a9ad9c4
edb5c24
 
d5a5542
 
 
 
 
 
 
 
 
 
edb5c24
e0510c6
edb5c24
e0510c6
 
a9ad9c4
edb5c24
e0510c6
 
 
 
a9ad9c4
edb5c24
e0510c6
edb5c24
a9ad9c4
edb5c24
e0510c6
a9ad9c4
edb5c24
a9ad9c4
 
 
 
 
 
 
 
 
494a291
 
 
 
 
 
 
a9ad9c4
494a291
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import streamlit as st
from PIL import Image
from transformers import pipeline
import pandas as pd
import matplotlib.pyplot as plt

# Configuration to suppress warnings in Streamlit
st.set_option('deprecation.showPyplotGlobalUse', False)

# Initialize the image classification pipeline with a specific model
pipe = pipeline("image-classification", model="trpakov/vit-face-expression")

# Setting the title of the Streamlit application
st.title("Emotion Recognition App by Prateek Mohan")

# Interface for uploading an image
uploaded_image = st.file_uploader("Please upload an image", type=["jpg", "png"], accept_multiple_files=False)
color_map = {
        'Neutral': '#B38B6D',  
        'Happy': '#FFFF00',    
        'Sad': '#0000FF',     
        'Angry': '#FF0000',    
        'Disgust': '#008000',  
        'Surprise': '#FFA500', 
        'Fear': '#000000'      
       
    }
# Process the image immediately after upload
if uploaded_image:
    # Load and display the image
    image = Image.open(uploaded_image)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Emotion prediction using the pre-trained model
    result = pipe(image)
    predicted_class = result[0]["label"]
    predicted_emotion = predicted_class.split("_")[-1].capitalize()
    emotion_score = result[0]["score"]

    # Displaying the predicted emotion and its confidence score
    st.write(f"Predicted Emotion: {predicted_emotion}")
    st.write(f"Confidence Score: {emotion_score:.4f}")

    # Data preparation for visualization
    emotion_counts = pd.Series([predicted_emotion])

    # Color mapping for different emotions
    color_map = {
        'Neutral': '#B38B6D',
        'Happy': '#FFFF00',
        'Sad': '#0000FF',
        'Angry': '#FF0000',
        'Disgust': '#008000',
        'Surprise': '#FFA500',
        'Fear': '#000000'
    }
    # pie_colors = [color_map.get(emotion, '#999999') for emotion in emotion_counts.index]  
    # # Pie chart visualization
    # fig_pie, ax_pie = plt.subplots(figsize=(4, 4))
    # ax_pie.pie(emotion_counts, labels=emotion_counts.index, autopct='%1.1f%%', startangle=140, colors=pie_colors)
    # ax_pie.axis('equal')
    # ax_pie.set_title("Emotion Distribution")
    # st.pyplot(fig_pie)

    # # Bar chart visualization
    # fig_bar, ax_bar = plt.subplots(figsize=(4, 4))
    # emotion_counts.plot(kind='bar', color=pie_colors, ax=ax_bar)
    # ax_bar.set_xlabel('Emotion')
    # ax_bar.set_ylabel('Count')
    # ax_bar.set_title("Emotion Count")
    # ax_bar.yaxis.set_major_locator(plt.MaxNLocator(integer=True))
    # for i in ax_bar.patches:
    #     ax_bar.text(i.get_x() + i.get_width() / 2, i.get_height() + 0.1, str(int(i.get_height())), ha='center', va='bottom')
    # st.pyplot(fig_bar)