SuriRaja commited on
Commit
e9d1ccd
·
1 Parent(s): 75151af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -26
app.py CHANGED
@@ -6,6 +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, reset_video_index
10
  from services.thermal_service import detect_thermal_anomalies
11
  from services.overlay_service import overlay_boxes
@@ -17,6 +18,7 @@ frame_rate = 1
17
  frame_count = 0
18
  log_entries = []
19
  anomaly_counts = []
 
20
  last_frame = None
21
  last_metrics = {}
22
  last_timestamp = ""
@@ -54,25 +56,28 @@ def monitor_feed():
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:
66
- log_entries.pop(0)
67
- # if len(anomaly_counts) > 100:
68
- # anomaly_counts.pop(0)
69
-
70
- metrics_str = "\n".join([f"{k}: {v}" for k, v in last_metrics.items()])
71
 
72
- return frame[:, :, ::-1], metrics_str, "\n".join(log_entries[-10:]), generate_chart(), last_detected_images
73
 
74
- # Chart generator
75
- def generate_chart():
76
  fig, ax = plt.subplots(figsize=(4, 2))
77
  ax.plot(anomaly_counts[-50:], marker='o')
78
  ax.set_title("Anomalies Over Time")
@@ -84,31 +89,45 @@ def generate_chart():
84
  plt.close(fig)
85
  return chart_path
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  # Gradio UI
88
  with gr.Blocks(theme=gr.themes.Soft()) as app:
89
- gr.Markdown("# 🌐 Thermal Anomaly Monitoring Dashboard", elem_id="main-title")
90
 
91
- status_text = gr.Markdown("**Status:** 🟢 Running", elem_id="status-banner")
92
 
93
  with gr.Row():
94
  with gr.Column(scale=3):
95
- video_output = gr.Image(label="Live Video Feed", elem_id="video-feed", width=640, height=480)
96
  with gr.Column(scale=1):
97
- metrics_output = gr.Textbox(label="Live Metrics", lines=5)
98
 
99
  with gr.Row():
100
- with gr.Column():
101
- logs_output = gr.Textbox(label="Live Logs", lines=10)
102
- with gr.Column():
103
- chart_output = gr.Image(label="Detection Trends")
104
 
105
  with gr.Row():
106
- captured_images = gr.Gallery(label="Last 5 Captured Events", columns=1, height="auto")
107
 
108
  with gr.Row():
109
  pause_btn = gr.Button("⏸️ Pause")
110
  resume_btn = gr.Button("▶️ Resume")
111
- frame_slider = gr.Slider(0.0005, 0.5, value=1, label="Frame Interval (seconds)")
112
 
113
  def toggle_pause():
114
  global paused
@@ -130,11 +149,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
130
 
131
  def streaming_loop():
132
  while True:
133
- frame, metrics, logs, chart, captured = monitor_feed()
134
- yield frame, metrics, logs, chart, captured
135
  time.sleep(frame_rate)
136
 
137
- app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output, captured_images])
138
 
139
  if __name__ == "__main__":
140
  app.launch(share=True)
 
6
  import matplotlib.pyplot as plt
7
  import numpy as np
8
  from datetime import datetime
9
+ from collections import Counter
10
  from services.video_service import get_next_video_frame, reset_video_index
11
  from services.thermal_service import detect_thermal_anomalies
12
  from services.overlay_service import overlay_boxes
 
18
  frame_count = 0
19
  log_entries = []
20
  anomaly_counts = []
21
+ anomaly_types_all = []
22
  last_frame = None
23
  last_metrics = {}
24
  last_timestamp = ""
 
56
  last_frame = frame.copy()
57
  last_metrics = metrics.copy()
58
 
59
+ # Update persistent logs and stats
60
+ anomaly_detected = len(last_metrics.get('anomalies', []))
61
+ anomaly_types_all.extend([a['label'] for a in 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:
66
+ log_entries.pop(0)
67
+ if len(anomaly_counts) > 500:
68
+ anomaly_counts.pop(0)
69
+ if len(anomaly_types_all) > 500:
70
+ anomaly_types_all.pop(0)
71
+
72
  frame = cv2.resize(last_frame, (640, 480))
73
  cv2.putText(frame, f"Frame: {frame_count}", (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
74
  cv2.putText(frame, f"{last_timestamp}", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
75
 
76
+ return frame[:, :, ::-1], last_metrics, "\n".join(log_entries[-10:]), generate_line_chart(), generate_pie_chart(), last_detected_images
 
 
 
 
 
 
 
 
 
77
 
78
+ # Line chart
79
 
80
+ def generate_line_chart():
 
81
  fig, ax = plt.subplots(figsize=(4, 2))
82
  ax.plot(anomaly_counts[-50:], marker='o')
83
  ax.set_title("Anomalies Over Time")
 
89
  plt.close(fig)
90
  return chart_path
91
 
92
+ # Pie chart for anomaly types
93
+ def generate_pie_chart():
94
+ if not anomaly_types_all:
95
+ return None
96
+ fig, ax = plt.subplots(figsize=(4, 2))
97
+ count = Counter(anomaly_types_all[-200:])
98
+ labels, sizes = zip(*count.items())
99
+ ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
100
+ ax.axis('equal')
101
+ fig.tight_layout()
102
+ pie_path = "pie_temp.png"
103
+ fig.savefig(pie_path)
104
+ plt.close(fig)
105
+ return pie_path
106
+
107
  # Gradio UI
108
  with gr.Blocks(theme=gr.themes.Soft()) as app:
109
+ gr.Markdown("# 🛡️ Command Room Dashboard: Thermal Anomaly Monitoring")
110
 
111
+ status_text = gr.Markdown("**Status:** 🟢 Running")
112
 
113
  with gr.Row():
114
  with gr.Column(scale=3):
115
+ video_output = gr.Image(label="Live Video Feed", width=640, height=480)
116
  with gr.Column(scale=1):
117
+ metrics_output = gr.Textbox(label="Live Metrics", lines=4)
118
 
119
  with gr.Row():
120
+ logs_output = gr.Textbox(label="Live Logs", lines=8)
121
+ chart_output = gr.Image(label="Detection Trend")
122
+ pie_output = gr.Image(label="Anomaly Types")
 
123
 
124
  with gr.Row():
125
+ captured_images = gr.Gallery(label="Captured Events (Last 5)")
126
 
127
  with gr.Row():
128
  pause_btn = gr.Button("⏸️ Pause")
129
  resume_btn = gr.Button("▶️ Resume")
130
+ frame_slider = gr.Slider(0.2, 5, value=1, label="Frame Interval (seconds)")
131
 
132
  def toggle_pause():
133
  global paused
 
149
 
150
  def streaming_loop():
151
  while True:
152
+ frame, metrics, logs, chart, pie, captured = monitor_feed()
153
+ yield frame, str(metrics), logs, chart, pie, captured
154
  time.sleep(frame_rate)
155
 
156
+ app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output, pie_output, captured_images])
157
 
158
  if __name__ == "__main__":
159
  app.launch(share=True)