File size: 1,988 Bytes
1e6032a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import library
import streamlit as st
import pandas as pd
import numpy as np
from PIL import Image
import pickle
import json
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.efficientnet import preprocess_input

# Load trained model
model = load_model('emotion_detection.h5', compile=False)
# Define class labels
class_labels = ['Contempt', 'angry', 'disgust','fear','happy','neutral','sad','surprised']
  
def predict_and_display(uploaded_file, model, class_labels):
    img = Image.open(uploaded_file)
    img = img.resize((224, 224))
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)

    prediction = model.predict(img_array)
    predicted_class_index = np.argmax(prediction)
    predicted_class_label = class_labels[predicted_class_index]

    st.image(img, use_column_width=True)
    st.write(f"Detected Emoption of the Facial Expression is: {predicted_class_label}")

def run():
    st.write('##### Facial Emotions/Expressions Detection')
    # Making Form
    # Create a Streamlit form
    with st.form(key='Facial Emotions/Expressions Detection'):
        # Add a file uploader to the form
        uploaded_files = st.file_uploader("Upload a file of one of these format .JPEG/.JPG/.PNG file", accept_multiple_files=True)

        # Check if any file is uploaded
        if uploaded_files:
            for uploaded_file in uploaded_files:
                st.write("filename:", uploaded_file.name)
        # Close the form
        submitted = st.form_submit_button('Predict')
    
    if submitted:
        for uploaded_file in uploaded_files:
            # Use the predict_and_display function with the uploaded image data
            predict_and_display(uploaded_file, model, class_labels)

        
if __name__ == '__main__':
    run()