Spaces:
Runtime error
Runtime error
Commit
·
1e6032a
1
Parent(s):
0565c21
Create classifier.py
Browse files- classifier.py +56 -0
classifier.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import library
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import pickle
|
7 |
+
import json
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import tensorflow as tf
|
10 |
+
from tensorflow import keras
|
11 |
+
from tensorflow.keras.models import load_model
|
12 |
+
from tensorflow.keras.preprocessing import image
|
13 |
+
from tensorflow.keras.applications.efficientnet import preprocess_input
|
14 |
+
|
15 |
+
# Load trained model
|
16 |
+
model = load_model('emotion_detection.h5', compile=False)
|
17 |
+
# Define class labels
|
18 |
+
class_labels = ['Contempt', 'angry', 'disgust','fear','happy','neutral','sad','surprised']
|
19 |
+
|
20 |
+
def predict_and_display(uploaded_file, model, class_labels):
|
21 |
+
img = Image.open(uploaded_file)
|
22 |
+
img = img.resize((224, 224))
|
23 |
+
img_array = np.array(img)
|
24 |
+
img_array = np.expand_dims(img_array, axis=0)
|
25 |
+
img_array = preprocess_input(img_array)
|
26 |
+
|
27 |
+
prediction = model.predict(img_array)
|
28 |
+
predicted_class_index = np.argmax(prediction)
|
29 |
+
predicted_class_label = class_labels[predicted_class_index]
|
30 |
+
|
31 |
+
st.image(img, use_column_width=True)
|
32 |
+
st.write(f"Detected Emoption of the Facial Expression is: {predicted_class_label}")
|
33 |
+
|
34 |
+
def run():
|
35 |
+
st.write('##### Facial Emotions/Expressions Detection')
|
36 |
+
# Making Form
|
37 |
+
# Create a Streamlit form
|
38 |
+
with st.form(key='Facial Emotions/Expressions Detection'):
|
39 |
+
# Add a file uploader to the form
|
40 |
+
uploaded_files = st.file_uploader("Upload a file of one of these format .JPEG/.JPG/.PNG file", accept_multiple_files=True)
|
41 |
+
|
42 |
+
# Check if any file is uploaded
|
43 |
+
if uploaded_files:
|
44 |
+
for uploaded_file in uploaded_files:
|
45 |
+
st.write("filename:", uploaded_file.name)
|
46 |
+
# Close the form
|
47 |
+
submitted = st.form_submit_button('Predict')
|
48 |
+
|
49 |
+
if submitted:
|
50 |
+
for uploaded_file in uploaded_files:
|
51 |
+
# Use the predict_and_display function with the uploaded image data
|
52 |
+
predict_and_display(uploaded_file, model, class_labels)
|
53 |
+
|
54 |
+
|
55 |
+
if __name__ == '__main__':
|
56 |
+
run()
|