analytics-jiten's picture
Update classifier.py
d558337
# 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((256, 256))
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.markdown(f"<h3 style='font-weight: bold;'>Recognized Emotion of the Facial Expression is:</h3><h1 style='color:blue; font-weight: bold;'> {predicted_class_label}</h1>",unsafe_allow_html=True)
st.image(img, use_column_width=True)
def run():
st.write('##### Facial Emotions/Expressions Recognition')
# Making Form
# Create a Streamlit form
with st.form(key='Facial Emotions/Expressions Recognition'):
# 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('Recognize')
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()