Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,10 @@
|
|
1 |
import numpy as np
|
2 |
-
from tensorflow.keras.preprocessing.image import img_to_array
|
3 |
-
import gradio as gr
|
4 |
import gradio as gr
|
5 |
import tensorflow as tf
|
6 |
-
import numpy as np
|
7 |
from PIL import Image
|
8 |
-
import cv2
|
9 |
-
from tensorflow.keras.preprocessing import image
|
10 |
|
|
|
11 |
model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras')
|
12 |
|
13 |
# Emotion labels dictionary
|
@@ -16,33 +13,82 @@ index_to_emotion = {v: k for k, v in emotion_labels.items()}
|
|
16 |
|
17 |
def prepare_image(img_pil):
|
18 |
"""Preprocess the PIL image to fit your model's input requirements."""
|
19 |
-
# Convert the PIL image to a numpy array with the target size
|
20 |
img = img_pil.resize((224, 224))
|
21 |
img_array = img_to_array(img)
|
22 |
img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
|
23 |
img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
|
24 |
return img_array
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
# Define the Gradio interface
|
29 |
def predict_emotion(image):
|
30 |
-
|
31 |
processed_image = prepare_image(image)
|
32 |
-
# Make prediction using the model
|
33 |
prediction = model.predict(processed_image)
|
34 |
-
# Get the emotion label with the highest probability
|
35 |
predicted_class = np.argmax(prediction, axis=1)
|
36 |
predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
|
37 |
return predicted_emotion
|
38 |
|
|
|
39 |
interface = gr.Interface(
|
40 |
fn=predict_emotion, # Your prediction function
|
41 |
-
inputs=
|
|
|
|
|
|
|
42 |
outputs="text", # Output as text displaying the predicted emotion
|
43 |
title="Emotion Detection",
|
44 |
-
description="Upload an image
|
45 |
)
|
46 |
|
47 |
# Launch the Gradio interface
|
48 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import numpy as np
|
2 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
|
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
|
|
5 |
from PIL import Image
|
|
|
|
|
6 |
|
7 |
+
# Load your pre-trained model
|
8 |
model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras')
|
9 |
|
10 |
# Emotion labels dictionary
|
|
|
13 |
|
14 |
def prepare_image(img_pil):
|
15 |
"""Preprocess the PIL image to fit your model's input requirements."""
|
|
|
16 |
img = img_pil.resize((224, 224))
|
17 |
img_array = img_to_array(img)
|
18 |
img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
|
19 |
img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
|
20 |
return img_array
|
21 |
|
|
|
|
|
|
|
22 |
def predict_emotion(image):
|
23 |
+
"""Predict the emotion from the given image."""
|
24 |
processed_image = prepare_image(image)
|
|
|
25 |
prediction = model.predict(processed_image)
|
|
|
26 |
predicted_class = np.argmax(prediction, axis=1)
|
27 |
predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
|
28 |
return predicted_emotion
|
29 |
|
30 |
+
# Define the Gradio interface
|
31 |
interface = gr.Interface(
|
32 |
fn=predict_emotion, # Your prediction function
|
33 |
+
inputs=[
|
34 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
35 |
+
gr.inputs.Image(type="pil", source="webcam", label="Capture Image")
|
36 |
+
], # Input options: upload or capture from webcam
|
37 |
outputs="text", # Output as text displaying the predicted emotion
|
38 |
title="Emotion Detection",
|
39 |
+
description="Upload an image or capture one from your webcam to see the predicted emotion."
|
40 |
)
|
41 |
|
42 |
# Launch the Gradio interface
|
43 |
+
interface.launch()
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
# import numpy as np
|
48 |
+
# from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
49 |
+
# import gradio as gr
|
50 |
+
# import gradio as gr
|
51 |
+
# import tensorflow as tf
|
52 |
+
# import numpy as np
|
53 |
+
# from PIL import Image
|
54 |
+
# import cv2
|
55 |
+
# from tensorflow.keras.preprocessing import image
|
56 |
+
|
57 |
+
# model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras')
|
58 |
+
|
59 |
+
# # Emotion labels dictionary
|
60 |
+
# emotion_labels = {'angry': 0, 'disgust': 1, 'fear': 2, 'happy': 3, 'neutral': 4, 'sad': 5, 'surprise': 6}
|
61 |
+
# index_to_emotion = {v: k for k, v in emotion_labels.items()}
|
62 |
+
|
63 |
+
# def prepare_image(img_pil):
|
64 |
+
# """Preprocess the PIL image to fit your model's input requirements."""
|
65 |
+
# # Convert the PIL image to a numpy array with the target size
|
66 |
+
# img = img_pil.resize((224, 224))
|
67 |
+
# img_array = img_to_array(img)
|
68 |
+
# img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
|
69 |
+
# img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
|
70 |
+
# return img_array
|
71 |
+
|
72 |
+
|
73 |
+
|
74 |
+
# # Define the Gradio interface
|
75 |
+
# def predict_emotion(image):
|
76 |
+
# # Preprocess the image
|
77 |
+
# processed_image = prepare_image(image)
|
78 |
+
# # Make prediction using the model
|
79 |
+
# prediction = model.predict(processed_image)
|
80 |
+
# # Get the emotion label with the highest probability
|
81 |
+
# predicted_class = np.argmax(prediction, axis=1)
|
82 |
+
# predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
|
83 |
+
# return predicted_emotion
|
84 |
+
|
85 |
+
# interface = gr.Interface(
|
86 |
+
# fn=predict_emotion, # Your prediction function
|
87 |
+
# inputs=gr.Image(type="pil"), # Input for uploading an image, directly compatible with PIL images
|
88 |
+
# outputs="text", # Output as text displaying the predicted emotion
|
89 |
+
# title="Emotion Detection",
|
90 |
+
# description="Upload an image and see the predicted emotion."
|
91 |
+
# )
|
92 |
+
|
93 |
+
# # Launch the Gradio interface
|
94 |
+
# interface.launch()
|