nirnit-13 commited on
Commit
88abe6a
Β·
1 Parent(s): 8438cdf

Fix: replace show_download_button with gr.DownloadButton for Gradio 5.x

Browse files
Files changed (1) hide show
  1. app.py +38 -14
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": []} # filled by bg thread
37
- _inference_busy = False # prevent overlapping calls
 
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 = _annotate_frame(frame_bgr.copy(), results)
149
- annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB)
150
 
151
- # Snapshot = same annotated frame, returned as a downloadable image
152
- snapshot = annotated_rgb.copy()
 
153
 
154
- return annotated_rgb, snapshot
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
- Use the **πŸ“₯ Download** button below the output to save the current detection as an image.
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
- # stream_every=0.04 β†’ ~25 fps capture rate
181
- # inference runs in background so capture is never blocked
 
 
 
 
 
 
182
  webcam.stream(
183
  fn=process_frame,
184
  inputs=webcam,
185
- outputs=[output_img, output_img],
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)