Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
|
|
|
| 3 |
from services.video_service import get_random_video_frame
|
| 4 |
-
from services.thermal_service import detect_thermal_anomalies
|
| 5 |
from services.overlay_service import overlay_boxes
|
|
|
|
| 6 |
from services.metrics_service import update_metrics
|
| 7 |
-
from PIL import Image
|
| 8 |
-
import numpy as np
|
| 9 |
|
| 10 |
-
#
|
|
|
|
| 11 |
paused = False
|
| 12 |
-
frame_rate = 1 # seconds
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
| 15 |
def monitor_feed():
|
| 16 |
global paused
|
| 17 |
if paused:
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
frame = overlay_boxes(frame, detected_boxes)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
# Helper functions for controls
|
| 36 |
def toggle_pause():
|
| 37 |
global paused
|
| 38 |
paused = not paused
|
| 39 |
-
return "鈻讹笍 Resume" if paused else "鈴革笍 Pause"
|
| 40 |
|
| 41 |
-
def
|
| 42 |
global frame_rate
|
| 43 |
-
frame_rate =
|
| 44 |
-
return frame_rate
|
| 45 |
-
|
| 46 |
-
def loop_monitor():
|
| 47 |
-
while True:
|
| 48 |
-
yield from monitor_feed()
|
| 49 |
|
| 50 |
def build_interface():
|
| 51 |
-
global paused
|
| 52 |
-
|
| 53 |
with gr.Blocks() as app:
|
| 54 |
-
gr.Markdown("#
|
| 55 |
-
|
| 56 |
with gr.Row():
|
| 57 |
-
video_output = gr.Image(label="
|
| 58 |
-
metrics_output = gr.JSON(label="
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
fps_slider.change(set_fps, inputs=fps_slider, outputs=fps_slider)
|
| 67 |
|
| 68 |
-
app.load(loop_monitor, outputs=[video_output, metrics_output])
|
| 69 |
|
| 70 |
return app
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo = build_interface()
|
| 74 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
from services.video_service import get_random_video_frame
|
|
|
|
| 5 |
from services.overlay_service import overlay_boxes
|
| 6 |
+
from services.thermal_service import detect_thermal_anomalies
|
| 7 |
from services.metrics_service import update_metrics
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Control Variables
|
| 10 |
+
frame_rate = 1
|
| 11 |
paused = False
|
|
|
|
| 12 |
|
| 13 |
+
# Temp image path (optional, depends if thermal needs)
|
| 14 |
+
TEMP_IMAGE_PATH = "temp.jpg"
|
| 15 |
+
|
| 16 |
def monitor_feed():
|
| 17 |
global paused
|
| 18 |
if paused:
|
| 19 |
+
frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
| 20 |
+
detected_boxes = []
|
| 21 |
+
else:
|
| 22 |
+
frame = get_random_video_frame()
|
| 23 |
+
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
| 24 |
+
detected_boxes = detect_thermal_anomalies(TEMP_IMAGE_PATH)
|
| 25 |
+
frame = overlay_boxes(frame, detected_boxes)
|
| 26 |
+
|
| 27 |
+
metrics = update_metrics(detected_boxes)
|
| 28 |
+
return frame[:, :, ::-1], metrics
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
def loop_monitor():
|
| 31 |
+
while True:
|
| 32 |
+
yield monitor_feed()
|
| 33 |
|
|
|
|
| 34 |
def toggle_pause():
|
| 35 |
global paused
|
| 36 |
paused = not paused
|
|
|
|
| 37 |
|
| 38 |
+
def update_frame_rate(value):
|
| 39 |
global frame_rate
|
| 40 |
+
frame_rate = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def build_interface():
|
|
|
|
|
|
|
| 43 |
with gr.Blocks() as app:
|
| 44 |
+
gr.Markdown("# \ud83d\ude80 Surveillance Monitor Dashboard")
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
+
video_output = gr.Image(label="Live Video Feed")
|
| 47 |
+
metrics_output = gr.JSON(label="Metrics")
|
| 48 |
|
| 49 |
with gr.Row():
|
| 50 |
+
pause_button = gr.Button("\u23f8\ufe0f Pause / Resume")
|
| 51 |
+
frame_rate_slider = gr.Slider(minimum=0.1, maximum=5.0, value=1.0, label="Frame Rate (sec)")
|
| 52 |
|
| 53 |
+
pause_button.click(toggle_pause)
|
| 54 |
+
frame_rate_slider.change(update_frame_rate, inputs=[frame_rate_slider])
|
|
|
|
| 55 |
|
| 56 |
+
app.load(fn=loop_monitor, outputs=[video_output, metrics_output], every=1)
|
| 57 |
|
| 58 |
return app
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo = build_interface()
|
| 62 |
+
demo.launch()
|