Fix: replace show_download_button with gr.DownloadButton for Gradio 5.x
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
import threading
|
|
|
|
|
|
|
| 5 |
from keras.models import load_model
|
| 6 |
|
| 7 |
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -31,10 +33,10 @@ face_cascade = cv2.CascadeClassifier(
|
|
| 31 |
)
|
| 32 |
|
| 33 |
# ββ Shared state for background inference βββββββββββββββββββββββββ
|
| 34 |
-
# Stores the latest annotation result so the webcam never waits for inference
|
| 35 |
_lock = threading.Lock()
|
| 36 |
-
_latest_result = {"faces": [], "emotions": []}
|
| 37 |
-
_inference_busy = False
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def _run_inference(gray_frame):
|
|
@@ -145,22 +147,36 @@ def process_frame(frame):
|
|
| 145 |
with _lock:
|
| 146 |
results = dict(_latest_result)
|
| 147 |
|
| 148 |
-
annotated_bgr
|
| 149 |
-
annotated_rgb
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
-
return annotated_rgb
|
| 155 |
|
| 156 |
|
| 157 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
with gr.Blocks(title="Real-Time Emotion Detection") as demo:
|
| 159 |
|
| 160 |
gr.Markdown("""
|
| 161 |
# π Real-Time Emotion Detection
|
| 162 |
Click **βΆ Start** on the webcam to begin live detection.
|
| 163 |
-
|
| 164 |
Emotions detected: π Angry | π Happy | π Neutral | π’ Sad | π² Surprised
|
| 165 |
""")
|
| 166 |
|
|
@@ -173,20 +189,28 @@ with gr.Blocks(title="Real-Time Emotion Detection") as demo:
|
|
| 173 |
)
|
| 174 |
# Right β annotated output (updated every frame via bg inference)
|
| 175 |
output_img = gr.Image(
|
| 176 |
-
label="π― Detected Emotion"
|
| 177 |
-
show_download_button=True # β built-in download button
|
| 178 |
)
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
webcam.stream(
|
| 183 |
fn=process_frame,
|
| 184 |
inputs=webcam,
|
| 185 |
-
outputs=
|
| 186 |
stream_every=0.04,
|
| 187 |
time_limit=600
|
| 188 |
)
|
| 189 |
|
|
|
|
|
|
|
|
|
|
| 190 |
gr.Markdown("""
|
| 191 |
---
|
| 192 |
**Model:** Custom CNN β Conv2D(45) β Conv2D(64) β Conv2D(128) β Dense(256) β Softmax(5)
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
import threading
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
from keras.models import load_model
|
| 8 |
|
| 9 |
# ββ Load model ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
# ββ Shared state for background inference βββββββββββββββββββββββββ
|
|
|
|
| 36 |
_lock = threading.Lock()
|
| 37 |
+
_latest_result = {"faces": [], "emotions": []}
|
| 38 |
+
_inference_busy = False
|
| 39 |
+
_latest_snapshot = {"frame": None} # holds latest annotated frame for download
|
| 40 |
|
| 41 |
|
| 42 |
def _run_inference(gray_frame):
|
|
|
|
| 147 |
with _lock:
|
| 148 |
results = dict(_latest_result)
|
| 149 |
|
| 150 |
+
annotated_bgr = _annotate_frame(frame_bgr.copy(), results)
|
| 151 |
+
annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
|
| 152 |
|
| 153 |
+
# Store latest annotated frame so download button can access it
|
| 154 |
+
with _lock:
|
| 155 |
+
_latest_snapshot["frame"] = annotated_rgb.copy()
|
| 156 |
|
| 157 |
+
return annotated_rgb
|
| 158 |
|
| 159 |
|
| 160 |
# ββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 161 |
+
def save_snapshot():
|
| 162 |
+
"""Saves the latest annotated frame to a temp file and returns the path."""
|
| 163 |
+
with _lock:
|
| 164 |
+
frame = _latest_snapshot["frame"]
|
| 165 |
+
if frame is None:
|
| 166 |
+
return None
|
| 167 |
+
# Save as PNG to a named temp file
|
| 168 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 169 |
+
tmp.close()
|
| 170 |
+
cv2.imwrite(tmp.name, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
|
| 171 |
+
return tmp.name
|
| 172 |
+
|
| 173 |
+
|
| 174 |
with gr.Blocks(title="Real-Time Emotion Detection") as demo:
|
| 175 |
|
| 176 |
gr.Markdown("""
|
| 177 |
# π Real-Time Emotion Detection
|
| 178 |
Click **βΆ Start** on the webcam to begin live detection.
|
| 179 |
+
Click **π₯ Download Snapshot** to save the current detection as an image.
|
| 180 |
Emotions detected: π Angry | π Happy | π Neutral | π’ Sad | π² Surprised
|
| 181 |
""")
|
| 182 |
|
|
|
|
| 189 |
)
|
| 190 |
# Right β annotated output (updated every frame via bg inference)
|
| 191 |
output_img = gr.Image(
|
| 192 |
+
label="π― Detected Emotion"
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
+
# Download button β saves latest annotated frame as PNG
|
| 196 |
+
with gr.Row():
|
| 197 |
+
download_btn = gr.DownloadButton(
|
| 198 |
+
label="π₯ Download Snapshot",
|
| 199 |
+
variant="primary"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
# stream_every=0.04 β ~25 fps | inference in background β no freeze
|
| 203 |
webcam.stream(
|
| 204 |
fn=process_frame,
|
| 205 |
inputs=webcam,
|
| 206 |
+
outputs=output_img,
|
| 207 |
stream_every=0.04,
|
| 208 |
time_limit=600
|
| 209 |
)
|
| 210 |
|
| 211 |
+
# On click β save frame to temp file β trigger browser download
|
| 212 |
+
download_btn.click(fn=save_snapshot, inputs=None, outputs=download_btn)
|
| 213 |
+
|
| 214 |
gr.Markdown("""
|
| 215 |
---
|
| 216 |
**Model:** Custom CNN β Conv2D(45) β Conv2D(64) β Conv2D(128) β Dense(256) β Softmax(5)
|