Spaces:
Sleeping
Sleeping
Commit
·
998f789
1
Parent(s):
89b0c64
latest changes
Browse files
app.py
CHANGED
|
@@ -80,6 +80,11 @@ transform = transforms.Compose([
|
|
| 80 |
|
| 81 |
# --- 4. Gradio Live Inference Function (Generator) ---
|
| 82 |
# This function will receive individual frames from the webcam
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
def predict_live_frames(input_frame):
|
| 84 |
global frame_buffer, current_prediction_label, current_probabilities # Use global to maintain state across calls
|
| 85 |
|
|
@@ -112,8 +117,6 @@ def predict_live_frames(input_frame):
|
|
| 112 |
|
| 113 |
# --- Sliding Window ---
|
| 114 |
# Keep the last few frames to allow continuous predictions
|
| 115 |
-
# For example, if NUM_FRAMES is 8, and we want a new prediction every 2 frames,
|
| 116 |
-
# we slide the window by 2:
|
| 117 |
slide_window_by = 1 # Predict every frame (most "real-time" feel but highest compute)
|
| 118 |
# Or: NUM_FRAMES // 2 (e.g., predict every 4 frames for NUM_FRAMES=8)
|
| 119 |
# Or: NUM_FRAMES (non-overlapping windows, less frequent updates)
|
|
@@ -151,16 +154,11 @@ def predict_live_frames(input_frame):
|
|
| 151 |
yield cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
|
| 152 |
|
| 153 |
|
| 154 |
-
# --- Initialize global state for the generator function ---
|
| 155 |
-
frame_buffer = [] # Buffer for collecting frames for model input
|
| 156 |
-
current_prediction_label = "Initializing..."
|
| 157 |
-
current_probabilities = {label: 0.0 for label in CLASS_LABELS} # Initial probabilities
|
| 158 |
-
|
| 159 |
# --- 5. Gradio Interface Setup ---
|
| 160 |
iface = gr.Interface(
|
| 161 |
fn=predict_live_frames,
|
| 162 |
-
# Use gr.
|
| 163 |
-
inputs=gr.
|
| 164 |
# Outputs are updated continuously by the generator
|
| 165 |
outputs=gr.Image(type="numpy", label="Live Prediction Output"), # Using Image as output for continuous frames
|
| 166 |
title="Real-time Violence Detection with SmallVideoClassifier (Webcam)",
|
|
|
|
| 80 |
|
| 81 |
# --- 4. Gradio Live Inference Function (Generator) ---
|
| 82 |
# This function will receive individual frames from the webcam
|
| 83 |
+
# Initialize global state for the generator function (before the predict function)
|
| 84 |
+
frame_buffer = [] # Buffer for collecting frames for model input
|
| 85 |
+
current_prediction_label = "Initializing..."
|
| 86 |
+
current_probabilities = {label: 0.0 for label in CLASS_LABELS} # Initial probabilities
|
| 87 |
+
|
| 88 |
def predict_live_frames(input_frame):
|
| 89 |
global frame_buffer, current_prediction_label, current_probabilities # Use global to maintain state across calls
|
| 90 |
|
|
|
|
| 117 |
|
| 118 |
# --- Sliding Window ---
|
| 119 |
# Keep the last few frames to allow continuous predictions
|
|
|
|
|
|
|
| 120 |
slide_window_by = 1 # Predict every frame (most "real-time" feel but highest compute)
|
| 121 |
# Or: NUM_FRAMES // 2 (e.g., predict every 4 frames for NUM_FRAMES=8)
|
| 122 |
# Or: NUM_FRAMES (non-overlapping windows, less frequent updates)
|
|
|
|
| 154 |
yield cv2.cvtColor(display_frame, cv2.COLOR_BGR2RGB)
|
| 155 |
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
# --- 5. Gradio Interface Setup ---
|
| 158 |
iface = gr.Interface(
|
| 159 |
fn=predict_live_frames,
|
| 160 |
+
# CORRECTED: Use gr.Video with sources=["webcam"] for webcam input
|
| 161 |
+
inputs=gr.Video(sources=["webcam"], streaming=True, label="Live Webcam Feed for Violence Detection"),
|
| 162 |
# Outputs are updated continuously by the generator
|
| 163 |
outputs=gr.Image(type="numpy", label="Live Prediction Output"), # Using Image as output for continuous frames
|
| 164 |
title="Real-time Violence Detection with SmallVideoClassifier (Webcam)",
|