Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ import random
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
import numpy as np
|
8 |
from datetime import datetime
|
9 |
-
from services.video_service import get_next_video_frame
|
10 |
from services.thermal_service import detect_thermal_anomalies
|
11 |
from services.overlay_service import overlay_boxes
|
12 |
from services.metrics_service import update_metrics
|
@@ -17,43 +17,49 @@ frame_rate = 1
|
|
17 |
frame_count = 0
|
18 |
log_entries = []
|
19 |
anomaly_counts = []
|
|
|
|
|
|
|
|
|
20 |
|
21 |
# Constants
|
22 |
TEMP_IMAGE_PATH = "temp.jpg"
|
|
|
|
|
23 |
|
24 |
# Core monitor function
|
25 |
def monitor_feed():
|
26 |
-
global paused
|
27 |
-
global frame_count
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
frame = cv2.imread(TEMP_IMAGE_PATH)
|
34 |
-
|
35 |
-
if frame is None:
|
36 |
frame = get_next_video_frame()
|
37 |
-
|
38 |
-
if not paused:
|
39 |
detected_boxes = detect_thermal_anomalies(frame)
|
40 |
frame = overlay_boxes(frame, detected_boxes)
|
41 |
cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
42 |
metrics = update_metrics(detected_boxes)
|
43 |
-
else:
|
44 |
-
metrics = update_metrics([])
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
51 |
cv2.putText(frame, f"Frame: {frame_count}", (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
52 |
-
cv2.putText(frame, f"{
|
53 |
|
54 |
-
|
55 |
-
|
56 |
-
log_entries.append(f"{timestamp} - Frame {frame_count} - Anomalies Detected: {anomaly_detected}")
|
57 |
anomaly_counts.append(anomaly_detected)
|
58 |
|
59 |
if len(log_entries) > 100:
|
@@ -61,11 +67,7 @@ def monitor_feed():
|
|
61 |
if len(anomaly_counts) > 100:
|
62 |
anomaly_counts.pop(0)
|
63 |
|
64 |
-
|
65 |
-
label_output = {"Anomalies": anomaly_detected}
|
66 |
-
|
67 |
-
return frame[:, :, ::-1], label_output, "\n".join(log_entries[-10:]), generate_chart()
|
68 |
-
|
69 |
|
70 |
# Chart generator
|
71 |
def generate_chart():
|
@@ -98,6 +100,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
98 |
with gr.Column():
|
99 |
chart_output = gr.Image(label="Detection Trends")
|
100 |
|
|
|
|
|
|
|
101 |
with gr.Row():
|
102 |
pause_btn = gr.Button("鈴革笍 Pause")
|
103 |
resume_btn = gr.Button("鈻讹笍 Resume")
|
@@ -123,11 +128,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
123 |
|
124 |
def streaming_loop():
|
125 |
while True:
|
126 |
-
frame, metrics, logs, chart = monitor_feed()
|
127 |
-
yield frame, metrics, logs, chart
|
128 |
time.sleep(frame_rate)
|
129 |
|
130 |
-
app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output])
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
app.launch(share=True)
|
|
|
6 |
import matplotlib.pyplot as plt
|
7 |
import numpy as np
|
8 |
from datetime import datetime
|
9 |
+
from services.video_service import get_next_video_frame, reset_video_index
|
10 |
from services.thermal_service import detect_thermal_anomalies
|
11 |
from services.overlay_service import overlay_boxes
|
12 |
from services.metrics_service import update_metrics
|
|
|
17 |
frame_count = 0
|
18 |
log_entries = []
|
19 |
anomaly_counts = []
|
20 |
+
last_frame = None
|
21 |
+
last_metrics = {}
|
22 |
+
last_timestamp = ""
|
23 |
+
last_detected_images = []
|
24 |
|
25 |
# Constants
|
26 |
TEMP_IMAGE_PATH = "temp.jpg"
|
27 |
+
CAPTURED_FRAMES_DIR = "captured_frames"
|
28 |
+
os.makedirs(CAPTURED_FRAMES_DIR, exist_ok=True)
|
29 |
|
30 |
# Core monitor function
|
31 |
def monitor_feed():
|
32 |
+
global paused, frame_count, last_frame, last_metrics, last_timestamp
|
|
|
33 |
|
34 |
+
if paused and last_frame is not None:
|
35 |
+
frame = last_frame.copy()
|
36 |
+
metrics = last_metrics.copy()
|
37 |
+
else:
|
|
|
|
|
|
|
38 |
frame = get_next_video_frame()
|
|
|
|
|
39 |
detected_boxes = detect_thermal_anomalies(frame)
|
40 |
frame = overlay_boxes(frame, detected_boxes)
|
41 |
cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
42 |
metrics = update_metrics(detected_boxes)
|
|
|
|
|
43 |
|
44 |
+
frame_count += 1
|
45 |
+
last_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
46 |
+
|
47 |
+
if detected_boxes:
|
48 |
+
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"frame_{frame_count}.jpg")
|
49 |
+
cv2.imwrite(captured_frame_path, frame)
|
50 |
+
last_detected_images.append(captured_frame_path)
|
51 |
+
if len(last_detected_images) > 5:
|
52 |
+
last_detected_images.pop(0)
|
53 |
|
54 |
+
last_frame = frame.copy()
|
55 |
+
last_metrics = metrics.copy()
|
56 |
+
|
57 |
+
frame = cv2.resize(last_frame, (640, 480))
|
58 |
cv2.putText(frame, f"Frame: {frame_count}", (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
59 |
+
cv2.putText(frame, f"{last_timestamp}", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
60 |
|
61 |
+
anomaly_detected = len(last_metrics.get('anomalies', []))
|
62 |
+
log_entries.append(f"{last_timestamp} - Frame {frame_count} - Anomalies: {anomaly_detected}")
|
|
|
63 |
anomaly_counts.append(anomaly_detected)
|
64 |
|
65 |
if len(log_entries) > 100:
|
|
|
67 |
if len(anomaly_counts) > 100:
|
68 |
anomaly_counts.pop(0)
|
69 |
|
70 |
+
return frame[:, :, ::-1], metrics, "\n".join(log_entries[-10:]), generate_chart(), last_detected_images
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# Chart generator
|
73 |
def generate_chart():
|
|
|
100 |
with gr.Column():
|
101 |
chart_output = gr.Image(label="Detection Trends")
|
102 |
|
103 |
+
with gr.Row():
|
104 |
+
captured_images = gr.Gallery(label="Last 5 Captured Events").style(grid=[1], height="auto")
|
105 |
+
|
106 |
with gr.Row():
|
107 |
pause_btn = gr.Button("鈴革笍 Pause")
|
108 |
resume_btn = gr.Button("鈻讹笍 Resume")
|
|
|
128 |
|
129 |
def streaming_loop():
|
130 |
while True:
|
131 |
+
frame, metrics, logs, chart, captured = monitor_feed()
|
132 |
+
yield frame, metrics, logs, chart, captured
|
133 |
time.sleep(frame_rate)
|
134 |
|
135 |
+
app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output, captured_images])
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
app.launch(share=True)
|