Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
from model import EmotionPredictor
|
| 5 |
|
|
|
|
| 6 |
# Initialize the predictor
|
| 7 |
predictor = EmotionPredictor()
|
| 8 |
|
|
@@ -14,6 +15,7 @@ face_cascade = cv2.CascadeClassifier(
|
|
| 14 |
if face_cascade.empty():
|
| 15 |
raise RuntimeError("Failed to load Haar Cascade")
|
| 16 |
|
|
|
|
| 17 |
def predict_emotion(image):
|
| 18 |
"""
|
| 19 |
Predict emotion from an image.
|
|
@@ -80,6 +82,7 @@ def predict_emotion(image):
|
|
| 80 |
|
| 81 |
return output_frame_rgb, f"Detected emotion(s): {emotion_text}"
|
| 82 |
|
|
|
|
| 83 |
# Create Gradio interface
|
| 84 |
with gr.Blocks(title="Smilo😃 - Real-Time Emotion Detection") as demo:
|
| 85 |
gr.HTML("""
|
|
@@ -93,9 +96,7 @@ with gr.Blocks(title="Smilo😃 - Real-Time Emotion Detection") as demo:
|
|
| 93 |
<h1 style="color: white; margin: 0; margin-bottom: 10px; font-weight: 900; font-size: 3.5em; display: flex; align-items: center; justify-content: center; gap: 10px;">
|
| 94 |
Smilo <span style="font-size: 0.9em;">😃</span>
|
| 95 |
</h1>
|
| 96 |
-
<p style="color: #f0f0f0; font-size: 1.2em; margin: 0; font-weight: 400; letter-spacing: 0.5px;">Real-Time Emotion Detection powered by PyTorch
|
| 97 |
-
<img src="https://skillicons.dev/icons?i=pytorch,opencv"/>
|
| 98 |
-
</p>
|
| 99 |
</div>
|
| 100 |
""")
|
| 101 |
|
|
@@ -112,12 +113,20 @@ with gr.Blocks(title="Smilo😃 - Real-Time Emotion Detection") as demo:
|
|
| 112 |
image_output = gr.Image(label="Annotated Image")
|
| 113 |
emotion_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 114 |
|
| 115 |
-
# Connect the function
|
| 116 |
submit_btn.click(
|
| 117 |
fn=predict_emotion,
|
| 118 |
inputs=[image_input],
|
| 119 |
outputs=[image_output, emotion_output]
|
| 120 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
| 123 |
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from model import EmotionPredictor
|
| 5 |
|
| 6 |
+
|
| 7 |
# Initialize the predictor
|
| 8 |
predictor = EmotionPredictor()
|
| 9 |
|
|
|
|
| 15 |
if face_cascade.empty():
|
| 16 |
raise RuntimeError("Failed to load Haar Cascade")
|
| 17 |
|
| 18 |
+
|
| 19 |
def predict_emotion(image):
|
| 20 |
"""
|
| 21 |
Predict emotion from an image.
|
|
|
|
| 82 |
|
| 83 |
return output_frame_rgb, f"Detected emotion(s): {emotion_text}"
|
| 84 |
|
| 85 |
+
|
| 86 |
# Create Gradio interface
|
| 87 |
with gr.Blocks(title="Smilo😃 - Real-Time Emotion Detection") as demo:
|
| 88 |
gr.HTML("""
|
|
|
|
| 96 |
<h1 style="color: white; margin: 0; margin-bottom: 10px; font-weight: 900; font-size: 3.5em; display: flex; align-items: center; justify-content: center; gap: 10px;">
|
| 97 |
Smilo <span style="font-size: 0.9em;">😃</span>
|
| 98 |
</h1>
|
| 99 |
+
<p style="color: #f0f0f0; font-size: 1.2em; margin: 0; font-weight: 400; letter-spacing: 0.5px;">Real-Time Emotion Detection powered by PyTorch</p>
|
|
|
|
|
|
|
| 100 |
</div>
|
| 101 |
""")
|
| 102 |
|
|
|
|
| 113 |
image_output = gr.Image(label="Annotated Image")
|
| 114 |
emotion_output = gr.Textbox(label="Prediction Result", interactive=False)
|
| 115 |
|
| 116 |
+
# Connect the function to the button
|
| 117 |
submit_btn.click(
|
| 118 |
fn=predict_emotion,
|
| 119 |
inputs=[image_input],
|
| 120 |
outputs=[image_output, emotion_output]
|
| 121 |
)
|
| 122 |
+
|
| 123 |
+
# Also run prediction when image is uploaded
|
| 124 |
+
image_input.change(
|
| 125 |
+
fn=predict_emotion,
|
| 126 |
+
inputs=[image_input],
|
| 127 |
+
outputs=[image_output, emotion_output]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
demo.launch()
|