SuriRaja commited on
Commit
a812c0f
·
1 Parent(s): 8b83c1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -53
app.py CHANGED
@@ -1,94 +1,65 @@
1
- import gradio as gr
2
  import cv2
3
  import time
4
  import os
5
- from datetime import datetime
6
- from services.video_service import get_next_video_frame
7
  from services.thermal_service import detect_thermal_anomalies
8
  from services.overlay_service import overlay_boxes
9
  from services.metrics_service import update_metrics
10
 
11
- # Globals
12
  paused = False
13
- frame_rate = 1
14
- frame_count = 0
 
 
15
 
16
- # Constants
17
- TEMP_IMAGE_PATH = "temp.jpg"
18
 
19
- # Core monitor function
20
  def monitor_feed():
21
  global paused
22
- global frame_count
23
 
24
- frame = None
25
 
26
  if paused:
27
- if os.path.exists(TEMP_IMAGE_PATH):
28
- frame = cv2.imread(TEMP_IMAGE_PATH)
 
 
 
29
 
30
- if frame is None:
31
- frame = get_next_video_frame()
32
 
33
- if not paused:
34
  detected_boxes = detect_thermal_anomalies(frame)
35
  frame = overlay_boxes(frame, detected_boxes)
36
- cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
37
  metrics = update_metrics(detected_boxes)
38
- else:
39
- metrics = update_metrics([])
40
 
41
- frame = cv2.resize(frame, (640, 480)) # Resize frame to fixed window size
42
 
43
- # Add frame count and timestamp on frame
44
- frame_count += 1
45
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
46
- cv2.putText(frame, f"Frame: {frame_count}", (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
47
- cv2.putText(frame, f"{timestamp}", (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
48
 
49
- return frame[:, :, ::-1], metrics # BGR to RGB
50
 
51
- # Gradio UI
52
- with gr.Blocks(theme=gr.themes.Soft()) as app:
53
- gr.Markdown("# 🌐 Thermal Anomaly Monitoring Dashboard", elem_id="main-title")
54
 
55
- status_text = gr.Markdown("**Status:** 🟢 Running", elem_id="status-banner")
 
 
56
 
57
  with gr.Row():
58
  with gr.Column(scale=3):
59
- video_output = gr.Image(label="Live Video Feed", elem_id="video-feed", width=640, height=480)
60
  with gr.Column(scale=1):
61
- metrics_output = gr.Label(label="Live Metrics", elem_id="metrics")
62
 
63
  with gr.Row():
64
  pause_btn = gr.Button("⏸️ Pause")
65
  resume_btn = gr.Button("▶️ Resume")
66
- frame_slider = gr.Slider(0.2, 5, value=1, label="Frame Interval (seconds)")
67
 
68
  def toggle_pause():
69
  global paused
70
- paused = True
71
- return "**Status:** ⏸️ Paused"
72
-
73
- def toggle_resume():
74
- global paused
75
- paused = False
76
- return "**Status:** 🟢 Running"
77
-
78
- def set_frame_rate(val):
79
- global frame_rate
80
- frame_rate = val
81
-
82
- pause_btn.click(toggle_pause, outputs=status_text)
83
- resume_btn.click(toggle_resume, outputs=status_text)
84
- frame_slider.change(set_frame_rate, inputs=[frame_slider])
85
 
86
  def streaming_loop():
87
  while True:
88
- yield monitor_feed()
 
89
  time.sleep(frame_rate)
90
 
91
- app.load(streaming_loop, outputs=[video_output, metrics_output])
92
-
93
- if __name__ == "__main__":
94
- app.launch()
 
 
1
  import cv2
2
  import time
3
  import os
4
+ from services.video_service import get_random_video_frame
 
5
  from services.thermal_service import detect_thermal_anomalies
6
  from services.overlay_service import overlay_boxes
7
  from services.metrics_service import update_metrics
8
 
9
+
10
  paused = False
11
+ frame_rate = 1.0 # seconds
12
+
13
+
14
+
15
 
 
 
16
 
 
17
  def monitor_feed():
18
  global paused
 
19
 
20
+
21
 
22
  if paused:
23
+ frame = get_random_video_frame() # Still fetch to keep system active
24
+ else:
25
+ frame = get_random_video_frame()
26
+
27
+
28
 
 
 
29
 
 
30
  detected_boxes = detect_thermal_anomalies(frame)
31
  frame = overlay_boxes(frame, detected_boxes)
32
+
33
  metrics = update_metrics(detected_boxes)
34
+ return frame[:, :, ::-1], metrics # BGR → RGB
 
35
 
 
36
 
 
 
 
 
 
37
 
 
38
 
39
+ return frame[:, :, ::-1], {"Detections": 0}
 
 
40
 
41
+
42
+ with gr.Blocks() as app:
43
+ gr.Markdown("# 🌡️ Solar Surveillance Thermal Monitoring")
44
 
45
  with gr.Row():
46
  with gr.Column(scale=3):
47
+ video_output = gr.Image(label="Live Video Feed", elem_id="video-feed")
48
  with gr.Column(scale=1):
49
+ metrics_output = gr.Label(label="Detection Metrics")
50
 
51
  with gr.Row():
52
  pause_btn = gr.Button("⏸️ Pause")
53
  resume_btn = gr.Button("▶️ Resume")
54
+ frame_slider = gr.Slider(0.5, 5.0, step=0.1, value=1.0, label="Frame Interval (seconds)")
55
 
56
  def toggle_pause():
57
  global paused
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def streaming_loop():
60
  while True:
61
+ frame, metrics = monitor_feed()
62
+ yield frame, metrics
63
  time.sleep(frame_rate)
64
 
65
+ app.load(streaming_loop, outputs=[video_output, metrics_output])