Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,44 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
-
import
|
4 |
-
import os
|
5 |
from services.detection_service import detect_objects
|
6 |
from services.thermal_service import detect_thermal_anomalies
|
7 |
-
from services.
|
8 |
|
9 |
-
# Paths
|
10 |
TEMP_IMAGE_PATH = "temp.jpg"
|
11 |
|
12 |
-
# Initialize Metrics
|
13 |
-
total_frames = 0
|
14 |
-
total_detections = 0
|
15 |
-
total_anomalies = 0
|
16 |
-
|
17 |
def monitor_feed():
|
18 |
-
global total_frames, total_detections, total_anomalies
|
19 |
-
|
20 |
frame = get_random_video_frame()
|
21 |
-
|
22 |
if frame is None:
|
23 |
-
return None,
|
24 |
|
25 |
-
# Save frame temporarily
|
26 |
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
detection_boxes = []
|
31 |
-
if detections:
|
32 |
-
for det in detections:
|
33 |
-
box = det['box']
|
34 |
-
label = det['label']
|
35 |
-
detection_boxes.append(label)
|
36 |
-
x, y, w, h = box
|
37 |
-
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
38 |
-
cv2.putText(frame, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
anomaly_labels = []
|
43 |
-
if anomalies:
|
44 |
-
for anomaly in anomalies:
|
45 |
-
label = anomaly['label']
|
46 |
-
anomaly_labels.append(label)
|
47 |
|
48 |
-
|
49 |
-
total_frames += 1
|
50 |
-
total_detections += len(detection_boxes)
|
51 |
-
total_anomalies += len(anomaly_labels)
|
52 |
-
|
53 |
-
# Overlay live metrics
|
54 |
-
metrics_text = f"Frames: {total_frames} | Detections: {total_detections} | Anomalies: {total_anomalies}"
|
55 |
-
cv2.putText(frame, metrics_text, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
|
56 |
-
|
57 |
-
return frame, {"Detections": detection_boxes}, {"Thermal Anomalies": anomaly_labels}
|
58 |
|
59 |
def build_interface():
|
60 |
-
with gr.Blocks() as demo:
|
61 |
-
gr.Markdown("# Solar Surveillance
|
62 |
with gr.Row():
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
return demo
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
demo = build_interface()
|
77 |
-
demo.
|
78 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
+
from services.video_service import get_random_video_frame
|
|
|
4 |
from services.detection_service import detect_objects
|
5 |
from services.thermal_service import detect_thermal_anomalies
|
6 |
+
from services.overlay_service import overlay_boxes
|
7 |
|
|
|
8 |
TEMP_IMAGE_PATH = "temp.jpg"
|
9 |
|
|
|
|
|
|
|
|
|
|
|
10 |
def monitor_feed():
|
|
|
|
|
11 |
frame = get_random_video_frame()
|
|
|
12 |
if frame is None:
|
13 |
+
return None, 0, 0 # 🛡️ If invalid frame, return defaults to keep app alive
|
14 |
|
|
|
15 |
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
16 |
|
17 |
+
boxes = detect_objects(TEMP_IMAGE_PATH)
|
18 |
+
thermal_boxes = detect_thermal_anomalies(TEMP_IMAGE_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
all_boxes = boxes + thermal_boxes
|
21 |
+
image_with_boxes = overlay_boxes(TEMP_IMAGE_PATH, all_boxes)
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
return image_with_boxes, len(all_boxes), len(thermal_boxes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def build_interface():
|
26 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
27 |
+
gr.Markdown("# 🚀 Solar Surveillance POC Demo")
|
28 |
with gr.Row():
|
29 |
+
image_display = gr.Image(label="🔴 Live Surveillance Feed", interactive=False)
|
30 |
+
with gr.Column():
|
31 |
+
total_detections = gr.Number(label="Total Detections", value=0, interactive=False)
|
32 |
+
thermal_alerts = gr.Number(label="Thermal Alerts", value=0, interactive=False)
|
33 |
+
|
34 |
+
demo.load(
|
35 |
+
fn=monitor_feed,
|
36 |
+
inputs=[],
|
37 |
+
outputs=[image_display, total_detections, thermal_alerts],
|
38 |
+
every=1 # auto-refresh every 1 second
|
39 |
+
)
|
40 |
return demo
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
demo = build_interface()
|
44 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|