Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,48 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
# Convert
|
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 |
-
return frame
|
51 |
-
|
52 |
-
# Define the Gradio interface
|
53 |
-
def process_video(video):
|
54 |
-
# Convert the video frame to a format Gradio can handle
|
55 |
-
frame = cv2.cvtColor(video, cv2.COLOR_BGR2RGB)
|
56 |
-
result_frame = detect_emotion(frame)
|
57 |
-
return cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
|
58 |
-
|
59 |
-
# Create Gradio Interface
|
60 |
-
gr.Interface(fn=process_video, inputs=gr.Video(sources=["webcam"]), outputs="video", live=True).launch()
|
61 |
-
|
62 |
-
|
63 |
-
# import numpy as np
|
64 |
-
# from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
65 |
-
# import gradio as gr
|
66 |
-
# import gradio as gr
|
67 |
-
# import tensorflow as tf
|
68 |
-
# import numpy as np
|
69 |
-
# from PIL import Image
|
70 |
-
# import cv2
|
71 |
-
# from tensorflow.keras.preprocessing import image
|
72 |
-
|
73 |
-
# model = tf.keras.models.load_model('Final_Resnet50_Best_model.keras')
|
74 |
-
|
75 |
-
# # Emotion labels dictionary
|
76 |
-
# emotion_labels = {'angry': 0, 'disgust': 1, 'fear': 2, 'happy': 3, 'neutral': 4, 'sad': 5, 'surprise': 6}
|
77 |
-
# index_to_emotion = {v: k for k, v in emotion_labels.items()}
|
78 |
-
|
79 |
-
# def prepare_image(img_pil):
|
80 |
-
# """Preprocess the PIL image to fit your model's input requirements."""
|
81 |
-
# # Convert the PIL image to a numpy array with the target size
|
82 |
-
# img = img_pil.resize((224, 224))
|
83 |
-
# img_array = img_to_array(img)
|
84 |
-
# img_array = np.expand_dims(img_array, axis=0) # Convert single image to a batch.
|
85 |
-
# img_array /= 255.0 # Rescale pixel values to [0,1], as done during training
|
86 |
-
# return img_array
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
# # Define the Gradio interface
|
91 |
-
# def predict_emotion(image):
|
92 |
-
# # Preprocess the image
|
93 |
-
# processed_image = prepare_image(image)
|
94 |
-
# # Make prediction using the model
|
95 |
-
# prediction = model.predict(processed_image)
|
96 |
-
# # Get the emotion label with the highest probability
|
97 |
-
# predicted_class = np.argmax(prediction, axis=1)
|
98 |
-
# predicted_emotion = index_to_emotion.get(predicted_class[0], "Unknown Emotion")
|
99 |
-
# return predicted_emotion
|
100 |
-
|
101 |
-
# interface = gr.Interface(
|
102 |
-
# fn=predict_emotion, # Your prediction function
|
103 |
-
# inputs=gr.Image(type="pil"), # Input for uploading an image, directly compatible with PIL images
|
104 |
-
# outputs="text", # Output as text displaying the predicted emotion
|
105 |
-
# title="Emotion Detection",
|
106 |
-
# description="Upload an image and see the predicted emotion."
|
107 |
-
# )
|
108 |
-
|
109 |
-
# # Launch the Gradio interface
|
110 |
-
# interface.launch()
|
|
|
1 |
+
import numpy as np
|
2 |
+
from tensorflow.keras.preprocessing.image import img_to_array, load_img
|
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
|
14 |
+
emotion_labels = {'angry': 0, 'disgust': 1, 'fear': 2, 'happy': 3, 'neutral': 4, 'sad': 5, 'surprise': 6}
|
15 |
+
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 |
+
# Preprocess the image
|
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=gr.Image(type="pil"), # Input for uploading an image, directly compatible with PIL images
|
42 |
+
outputs="text", # Output as text displaying the predicted emotion
|
43 |
+
title="Emotion Detection",
|
44 |
+
description="Upload an image and see the predicted emotion."
|
45 |
+
)
|
46 |
+
|
47 |
+
# Launch the Gradio interface
|
48 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|