Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,102 +1,65 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
-
import
|
4 |
-
import numpy as np
|
5 |
-
import gradio as gr
|
6 |
-
from services.overlay_service import overlay_boxes
|
7 |
from services.video_service import get_random_video_frame
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
paused = False
|
11 |
-
frame_rate = 1.0 # seconds between frames
|
12 |
-
|
13 |
-
# Temporary image path for current frame
|
14 |
TEMP_IMAGE_PATH = "temp_frame.jpg"
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
# Define list of possible anomaly labels for simulation
|
21 |
-
possible_labels = ["Panel Crack", "Dust Accumulation", "Human Intrusion", "Overheating"]
|
22 |
-
|
23 |
|
|
|
24 |
def monitor_feed():
|
25 |
-
global total_detections, anomaly_counter
|
26 |
-
if paused:
|
27 |
-
frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
28 |
-
cv2.putText(frame, "PAUSED", (200, 250), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 5)
|
29 |
-
else:
|
30 |
-
frame = get_random_video_frame()
|
31 |
-
if frame is None:
|
32 |
-
frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
33 |
-
else:
|
34 |
-
# Simulate random detections
|
35 |
-
detections = []
|
36 |
-
for _ in range(random.randint(1, 5)):
|
37 |
-
label = random.choice(possible_labels)
|
38 |
-
x1, y1 = random.randint(0, frame.shape[1]//2), random.randint(0, frame.shape[0]//2)
|
39 |
-
x2, y2 = x1 + random.randint(50, 150), y1 + random.randint(50, 150)
|
40 |
-
detections.append({"box": [x1, y1, x2, y2], "label": label})
|
41 |
-
|
42 |
-
frame = overlay_boxes(frame, detections)
|
43 |
-
|
44 |
-
# Update metrics
|
45 |
-
total_detections += len(detections)
|
46 |
-
for det in detections:
|
47 |
-
anomaly_counter[det["label"]] = anomaly_counter.get(det["label"], 0) + 1
|
48 |
-
|
49 |
-
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
50 |
-
return TEMP_IMAGE_PATH, generate_metrics_text()
|
51 |
-
|
52 |
-
|
53 |
-
def generate_metrics_text():
|
54 |
-
metrics_text = f"Total Detections: {total_detections}\n"
|
55 |
-
for label, count in anomaly_counter.items():
|
56 |
-
metrics_text += f"{label}: {count}\n"
|
57 |
-
return metrics_text
|
58 |
-
|
59 |
-
|
60 |
-
def pause_monitor():
|
61 |
global paused
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
global paused
|
66 |
-
paused =
|
67 |
|
68 |
-
def
|
69 |
global frame_rate
|
70 |
-
frame_rate =
|
71 |
|
|
|
|
|
|
|
|
|
72 |
|
|
|
73 |
def build_interface():
|
74 |
with gr.Blocks() as app:
|
75 |
-
gr.Markdown("# Solar Surveillance - Live Monitor \ud83d\ude80")
|
76 |
-
|
77 |
-
with gr.Row():
|
78 |
-
video_output = gr.Image(label="Live Feed", show_label=True)
|
79 |
-
metrics_output = gr.Textbox(label="Live Metrics", interactive=False)
|
80 |
-
|
81 |
with gr.Row():
|
82 |
with gr.Column():
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
yield monitor_feed()
|
94 |
|
95 |
-
app.
|
96 |
|
97 |
return app
|
98 |
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
demo.launch(
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
import os
|
4 |
import random
|
5 |
+
import time
|
|
|
|
|
|
|
6 |
from services.video_service import get_random_video_frame
|
7 |
+
from services.metrics_service import update_metrics
|
8 |
+
from services.overlay_service import overlay_boxes
|
9 |
|
10 |
+
# Constants
|
|
|
|
|
|
|
|
|
11 |
TEMP_IMAGE_PATH = "temp_frame.jpg"
|
12 |
+
VIDEO_FOLDER = "data/sample_videos"
|
13 |
|
14 |
+
# State Variables
|
15 |
+
paused = False
|
16 |
+
frame_rate = 1 # Default 1 FPS
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# Helper Functions
|
19 |
def monitor_feed():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
global paused
|
21 |
+
frame = get_random_video_frame()
|
22 |
+
if frame is None:
|
23 |
+
return None, update_metrics()
|
24 |
+
if not paused:
|
25 |
+
frame = overlay_boxes(frame)
|
26 |
+
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
27 |
+
return TEMP_IMAGE_PATH, update_metrics()
|
28 |
+
|
29 |
+
def toggle_pause():
|
30 |
global paused
|
31 |
+
paused = not paused
|
32 |
|
33 |
+
def set_frame_rate(value):
|
34 |
global frame_rate
|
35 |
+
frame_rate = int(value)
|
36 |
|
37 |
+
def loop_monitor():
|
38 |
+
while True:
|
39 |
+
time.sleep(1 / frame_rate if frame_rate > 0 else 1)
|
40 |
+
yield monitor_feed()
|
41 |
|
42 |
+
# Gradio App
|
43 |
def build_interface():
|
44 |
with gr.Blocks() as app:
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Row():
|
46 |
with gr.Column():
|
47 |
+
video_output = gr.Image(label="Live Drone Feed")
|
48 |
+
with gr.Column():
|
49 |
+
metrics_output = gr.Label(label="Live Metrics")
|
50 |
|
51 |
+
with gr.Row():
|
52 |
+
pause_button = gr.Button("Pause / Resume")
|
53 |
+
frame_rate_slider = gr.Slider(1, 10, value=1, step=1, label="Frame Rate (Frames per second)")
|
54 |
|
55 |
+
pause_button.click(toggle_pause)
|
56 |
+
frame_rate_slider.change(set_frame_rate, inputs=[frame_rate_slider])
|
|
|
57 |
|
58 |
+
app.load(fn=loop_monitor, outputs=[video_output, metrics_output])
|
59 |
|
60 |
return app
|
61 |
|
62 |
+
# Launch
|
63 |
+
if __name__ == "__main__":
|
64 |
+
demo = build_interface()
|
65 |
+
demo.queue().launch()
|