Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,102 @@
|
|
1 |
import os
|
2 |
-
import cv2
|
3 |
import random
|
4 |
-
import
|
5 |
-
import gradio as gr
|
6 |
import numpy as np
|
7 |
-
|
8 |
from services.overlay_service import overlay_boxes
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
# Global control flags
|
14 |
-
is_paused = False
|
15 |
-
frame_rate = 1 # Default frame rate (frames per second)
|
16 |
-
|
17 |
-
# Live Metrics
|
18 |
-
metrics = {
|
19 |
-
"total_frames": 0,
|
20 |
-
"detected_objects": 0,
|
21 |
-
}
|
22 |
-
|
23 |
-
def monitor_feed():
|
24 |
-
global is_paused, frame_rate, metrics
|
25 |
-
|
26 |
-
while True:
|
27 |
-
if not is_paused:
|
28 |
-
frame, anomaly_label = get_random_video_frame()
|
29 |
-
|
30 |
-
if frame is None:
|
31 |
-
time.sleep(1)
|
32 |
-
continue
|
33 |
-
|
34 |
-
# Overlay anomaly text
|
35 |
-
frame = overlay_boxes(frame, anomaly_label)
|
36 |
-
|
37 |
-
# Save temporary image
|
38 |
-
cv2.imwrite(TEMP_IMAGE_PATH, frame)
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
|
|
|
|
44 |
|
45 |
-
|
|
|
46 |
|
47 |
-
def toggle_pause():
|
48 |
-
global is_paused
|
49 |
-
is_paused = not is_paused
|
50 |
-
return "Paused" if is_paused else "Running"
|
51 |
|
52 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
global frame_rate
|
54 |
frame_rate = new_rate
|
55 |
-
|
56 |
|
57 |
def build_interface():
|
58 |
-
with gr.Blocks() as
|
59 |
-
gr.Markdown("# Solar Surveillance
|
60 |
-
|
61 |
with gr.Row():
|
62 |
-
video_output = gr.Image(label="Live Feed")
|
63 |
-
metrics_output = gr.
|
64 |
|
65 |
with gr.Row():
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
|
70 |
-
frame_rate_slider.change(set_frame_rate, inputs=frame_rate_slider, outputs=[])
|
71 |
|
72 |
-
demo.load(monitor_feed, outputs=[video_output, metrics_output], every=None)
|
73 |
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
demo = build_interface()
|
78 |
-
demo.launch(share=True) # Important for Hugging Face Spaces!
|
|
|
1 |
import os
|
|
|
2 |
import random
|
3 |
+
import cv2
|
|
|
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 |
+
# Global control variables
|
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 |
+
# Metrics
|
17 |
+
total_detections = 0
|
18 |
+
anomaly_counter = {}
|
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 |
+
paused = True
|
63 |
+
|
64 |
+
def resume_monitor():
|
65 |
+
global paused
|
66 |
+
paused = False
|
67 |
+
|
68 |
+
def update_frame_rate(new_rate):
|
69 |
global frame_rate
|
70 |
frame_rate = new_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 |
+
pause_btn = gr.Button("Pause")
|
84 |
+
resume_btn = gr.Button("Resume")
|
85 |
+
frame_rate_slider = gr.Slider(0.1, 5.0, value=1.0, label="Frame Rate (seconds)")
|
86 |
+
|
87 |
+
pause_btn.click(fn=pause_monitor)
|
88 |
+
resume_btn.click(fn=resume_monitor)
|
89 |
+
frame_rate_slider.change(fn=update_frame_rate, inputs=frame_rate_slider)
|
90 |
+
|
91 |
+
def loop_monitor():
|
92 |
+
while True:
|
93 |
+
yield monitor_feed()
|
94 |
+
|
95 |
+
app.live(loop_monitor, outputs=[video_output, metrics_output], interval=lambda: frame_rate)
|
96 |
|
97 |
+
return app
|
|
|
98 |
|
|
|
99 |
|
100 |
+
demo = build_interface()
|
101 |
|
102 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|