Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
import os
|
| 7 |
+
# Ensure the 'upload' directory exists
|
| 8 |
+
upload_folder = 'uploads'
|
| 9 |
+
if not os.path.exists(upload_folder):
|
| 10 |
+
os.makedirs(upload_folder)
|
| 11 |
+
|
| 12 |
+
# Load the pre-trained model
|
| 13 |
+
model = load_model("emotion_detector.keras")
|
| 14 |
+
|
| 15 |
+
def get_result(img_path):
|
| 16 |
+
img = cv2.imread(img_path)
|
| 17 |
+
img_resize = cv2.resize(img, (224, 224))
|
| 18 |
+
img_resize = np.array(img_resize, dtype=np.float32)
|
| 19 |
+
img_resize /= 255.0
|
| 20 |
+
img_input = img_resize.reshape(1, 224, 224, 3)
|
| 21 |
+
prediction = model.predict(img_input)
|
| 22 |
+
emotion_dict = {0: 'angry 😡',
|
| 23 |
+
1: 'disgust 🤢',
|
| 24 |
+
2: 'fear 😱',
|
| 25 |
+
3: 'happy 😀',
|
| 26 |
+
4: 'neutral 😐',
|
| 27 |
+
5: 'sad 😢',
|
| 28 |
+
6: 'surprise 😲'}
|
| 29 |
+
|
| 30 |
+
max_index = np.argmax(np.array(prediction[0]))
|
| 31 |
+
pred=int(np.round(prediction[0][max_index]))
|
| 32 |
+
emotion = emotion_dict[max_index]
|
| 33 |
+
|
| 34 |
+
return f"He/she is feeling {emotion}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
st.title("Let\'s detect the Emotion 😀 😢 😡 😱 🤢 😲 😐 ")
|
| 38 |
+
|
| 39 |
+
uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 40 |
+
|
| 41 |
+
if uploaded_image is not None:
|
| 42 |
+
|
| 43 |
+
image = Image.open(uploaded_image)
|
| 44 |
+
|
| 45 |
+
image_path = os.path.join(upload_folder, uploaded_image.name)
|
| 46 |
+
image.save(image_path)
|
| 47 |
+
output = get_result(image_path)
|
| 48 |
+
|
| 49 |
+
st.write(output)
|
| 50 |
+
st.image(image, use_container_width=True)
|