File size: 1,522 Bytes
619cfae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
import numpy as np
import cv2
from tensorflow.keras.models import load_model
import os
# Ensure the 'upload' directory exists
upload_folder = 'uploads'
if not os.path.exists(upload_folder):
    os.makedirs(upload_folder)
    
# Load the pre-trained model
model = load_model("emotion_detector.keras")

def get_result(img_path):
    img = cv2.imread(img_path)
    img_resize = cv2.resize(img, (224, 224))
    img_resize = np.array(img_resize, dtype=np.float32)
    img_resize /= 255.0
    img_input = img_resize.reshape(1, 224, 224, 3)
    prediction = model.predict(img_input)
    emotion_dict = {0: 'angry 😡',
                    1: 'disgust 🤢',
                    2: 'fear 😱',
                    3: 'happy 😀',
                    4: 'neutral 😐',
                    5: 'sad 😢',
                    6: 'surprise 😲'}
    
    max_index = np.argmax(np.array(prediction[0]))
    pred=int(np.round(prediction[0][max_index]))
    emotion = emotion_dict[max_index]

    return f"He/she is feeling {emotion}"
    
        
st.title("Let\'s detect the Emotion 😀 😢 😡 😱 🤢 😲 😐 ")

uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_image is not None:

    image = Image.open(uploaded_image)
    
    image_path = os.path.join(upload_folder, uploaded_image.name)
    image.save(image_path)
    output = get_result(image_path)
    
    st.write(output)
    st.image(image, use_container_width=True)