Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,130 +1,84 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
-
import
|
4 |
import random
|
5 |
-
import time
|
6 |
-
|
7 |
from services.video_service import get_random_video_frame
|
8 |
from services.overlay_service import overlay_boxes
|
9 |
from services.detection_service import detect_objects
|
10 |
from services.thermal_service import detect_thermal_anomalies
|
11 |
|
|
|
12 |
TEMP_IMAGE_PATH = "temp.jpg"
|
13 |
|
14 |
-
|
15 |
-
|
|
|
16 |
|
|
|
|
|
|
|
17 |
if frame is None:
|
18 |
-
|
19 |
-
return None, "No frame available", 0, 0
|
20 |
|
21 |
-
# Save current frame
|
22 |
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
23 |
|
24 |
-
# Object detection
|
25 |
detections = detect_objects(TEMP_IMAGE_PATH)
|
26 |
-
|
27 |
-
# Thermal detection
|
28 |
thermal_detections = detect_thermal_anomalies(TEMP_IMAGE_PATH)
|
29 |
|
30 |
# Merge all detections
|
31 |
-
|
32 |
-
|
33 |
-
# Overlay
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def build_interface():
|
46 |
-
with gr.Blocks() as
|
47 |
with gr.Row():
|
48 |
-
|
49 |
-
|
50 |
-
anomaly_info = gr.Textbox(label="Anomaly Summary", interactive=False)
|
51 |
-
anomaly_count = gr.Number(label="Anomaly Count", interactive=False)
|
52 |
-
total_objects = gr.Number(label="Total Detections", interactive=False)
|
53 |
-
|
54 |
-
def loop_monitor():
|
55 |
-
while True:
|
56 |
-
time.sleep(1) # every 1 second
|
57 |
-
yield monitor_feed()
|
58 |
-
|
59 |
-
demo.load(loop_monitor, outputs=[image_display, anomaly_info, anomaly_count, total_objects])
|
60 |
-
|
61 |
-
return demo
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo = build_interface()
|
65 |
-
demo.queue()
|
66 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
67 |
-
import gradio as gr
|
68 |
-
import cv2
|
69 |
-
import os
|
70 |
-
import random
|
71 |
-
import time
|
72 |
-
|
73 |
-
from services.video_service import get_random_video_frame
|
74 |
-
from services.overlay_service import overlay_boxes
|
75 |
-
from services.detection_service import detect_objects
|
76 |
-
from services.thermal_service import detect_thermal_anomalies
|
77 |
-
|
78 |
-
TEMP_IMAGE_PATH = "temp.jpg"
|
79 |
-
|
80 |
-
def monitor_feed():
|
81 |
-
frame = get_random_video_frame()
|
82 |
-
if frame is None:
|
83 |
-
return None, "No frame available", 0, 0
|
84 |
-
|
85 |
-
# Save current frame
|
86 |
-
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
87 |
-
|
88 |
-
# Object detection
|
89 |
-
detections = detect_objects(TEMP_IMAGE_PATH)
|
90 |
-
|
91 |
-
# Thermal detection
|
92 |
-
thermal_detections = detect_thermal_anomalies(TEMP_IMAGE_PATH)
|
93 |
-
|
94 |
-
# Merge all detections
|
95 |
-
all_detections = detections + thermal_detections
|
96 |
-
|
97 |
-
# Overlay bounding boxes
|
98 |
-
result_image = overlay_boxes(TEMP_IMAGE_PATH, all_detections)
|
99 |
-
|
100 |
-
if result_image is not None:
|
101 |
-
cv2.imwrite(TEMP_IMAGE_PATH, result_image)
|
102 |
-
|
103 |
-
# For metric panel
|
104 |
-
anomaly_count = len([d for d in all_detections if d[4] != "normal"])
|
105 |
-
total_count = len(all_detections)
|
106 |
-
|
107 |
-
return TEMP_IMAGE_PATH, f"Detected {anomaly_count} anomalies", anomaly_count, total_count
|
108 |
-
|
109 |
-
def build_interface():
|
110 |
-
with gr.Blocks() as demo:
|
111 |
with gr.Row():
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
time.sleep(1) # every 1 second
|
121 |
-
yield monitor_feed()
|
122 |
|
123 |
-
|
|
|
124 |
|
125 |
-
return
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
+
import numpy as np
|
4 |
import random
|
|
|
|
|
5 |
from services.video_service import get_random_video_frame
|
6 |
from services.overlay_service import overlay_boxes
|
7 |
from services.detection_service import detect_objects
|
8 |
from services.thermal_service import detect_thermal_anomalies
|
9 |
|
10 |
+
# Constants
|
11 |
TEMP_IMAGE_PATH = "temp.jpg"
|
12 |
|
13 |
+
# Global Variables
|
14 |
+
paused = False
|
15 |
+
frame_rate = 1 # default frame rate in seconds
|
16 |
|
17 |
+
# Helper functions
|
18 |
+
def monitor_feed():
|
19 |
+
frame, anomaly_type = get_random_video_frame()
|
20 |
if frame is None:
|
21 |
+
return None, "Error loading video frame"
|
|
|
22 |
|
|
|
23 |
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
24 |
|
|
|
25 |
detections = detect_objects(TEMP_IMAGE_PATH)
|
|
|
|
|
26 |
thermal_detections = detect_thermal_anomalies(TEMP_IMAGE_PATH)
|
27 |
|
28 |
# Merge all detections
|
29 |
+
combined_detections = detections + thermal_detections
|
30 |
+
|
31 |
+
# Overlay the detections
|
32 |
+
annotated_frame = overlay_boxes(frame.copy(), combined_detections)
|
33 |
+
|
34 |
+
# Update status text
|
35 |
+
status_text = f"Active Anomaly: {anomaly_type} | Objects Detected: {len(detections)} | Thermal Alerts: {len(thermal_detections)}"
|
36 |
+
|
37 |
+
return annotated_frame, status_text
|
38 |
+
|
39 |
+
def loop_monitor():
|
40 |
+
global paused
|
41 |
+
while True:
|
42 |
+
if not paused:
|
43 |
+
yield monitor_feed()
|
44 |
+
else:
|
45 |
+
yield None, "Paused"
|
46 |
+
import time
|
47 |
+
time.sleep(frame_rate)
|
48 |
+
|
49 |
+
# Pause/Resume control
|
50 |
+
def toggle_pause():
|
51 |
+
global paused
|
52 |
+
paused = not paused
|
53 |
+
return "Resume" if paused else "Pause"
|
54 |
+
|
55 |
+
# Frame Rate control
|
56 |
+
def update_frame_rate(new_rate):
|
57 |
+
global frame_rate
|
58 |
+
frame_rate = new_rate
|
59 |
+
return f"Frame Rate set to {new_rate} sec"
|
60 |
+
|
61 |
+
# Build Gradio Interface
|
62 |
def build_interface():
|
63 |
+
with gr.Blocks() as app:
|
64 |
with gr.Row():
|
65 |
+
video_output = gr.Image(label="Surveillance Feed", interactive=False)
|
66 |
+
metrics_output = gr.Textbox(label="Live Metrics", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
with gr.Row():
|
68 |
+
pause_button = gr.Button("Pause")
|
69 |
+
frame_rate_slider = gr.Slider(minimum=0.5, maximum=5, value=1, step=0.5, label="Frame Update Interval (seconds)")
|
70 |
+
|
71 |
+
# Live update feed
|
72 |
+
app.load(loop_monitor, outputs=[video_output, metrics_output], every=frame_rate)
|
73 |
|
74 |
+
# Pause/Resume button logic
|
75 |
+
pause_button.click(toggle_pause, outputs=pause_button)
|
|
|
|
|
76 |
|
77 |
+
# Frame Rate Slider logic
|
78 |
+
frame_rate_slider.change(update_frame_rate, inputs=frame_rate_slider, outputs=None)
|
79 |
|
80 |
+
return app
|
81 |
|
82 |
+
# Launch the app
|
83 |
+
demo = build_interface()
|
84 |
+
demo.launch()
|
|