SuriRaja commited on
Commit
d95bdc7
·
1 Parent(s): 9267999

Update app.py

Browse files

Fixed the Variable Frame issue

Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -2,40 +2,57 @@ import gradio as gr
2
  import cv2
3
  import time
4
  import os
5
- from services.video_service import get_random_video_frame
6
  from services.thermal_service import detect_thermal_anomalies
7
  from services.overlay_service import overlay_boxes
8
  from services.metrics_service import update_metrics
9
 
 
10
  paused = False
11
- frame_rate = 1.0 # seconds
12
 
 
 
 
 
13
  def monitor_feed():
14
  global paused
 
 
 
15
  if paused:
16
- frame = get_random_video_frame() # Still fetch to keep system active
17
- else:
18
- frame = get_random_video_frame()
 
 
 
 
19
  detected_boxes = detect_thermal_anomalies(frame)
20
  frame = overlay_boxes(frame, detected_boxes)
 
21
  metrics = update_metrics(detected_boxes)
22
- return frame[:, :, ::-1], metrics # BGR → RGB
 
 
 
23
 
24
- return frame[:, :, ::-1], {"Detections": 0}
25
 
 
26
  with gr.Blocks() as app:
27
- gr.Markdown("# 🌡️ Solar Surveillance Thermal Monitoring")
28
 
29
  with gr.Row():
30
  with gr.Column(scale=3):
31
- video_output = gr.Image(label="Live Video Feed", elem_id="video-feed")
32
  with gr.Column(scale=1):
33
- metrics_output = gr.Label(label="Detection Metrics")
34
 
35
  with gr.Row():
36
  pause_btn = gr.Button("⏸️ Pause")
37
  resume_btn = gr.Button("▶️ Resume")
38
- frame_slider = gr.Slider(0.5, 5.0, step=0.1, value=1.0, label="Frame Interval (seconds)")
39
 
40
  def toggle_pause():
41
  global paused
@@ -55,8 +72,7 @@ with gr.Blocks() as app:
55
 
56
  def streaming_loop():
57
  while True:
58
- frame, metrics = monitor_feed()
59
- yield frame, metrics
60
  time.sleep(frame_rate)
61
 
62
  app.load(streaming_loop, outputs=[video_output, metrics_output])
 
2
  import cv2
3
  import time
4
  import os
5
+ from services.video_service import get_next_video_frame
6
  from services.thermal_service import detect_thermal_anomalies
7
  from services.overlay_service import overlay_boxes
8
  from services.metrics_service import update_metrics
9
 
10
+ # Globals
11
  paused = False
12
+ frame_rate = 1
13
 
14
+ # Constants
15
+ TEMP_IMAGE_PATH = "temp.jpg"
16
+
17
+ # Core monitor function
18
  def monitor_feed():
19
  global paused
20
+
21
+ frame = None
22
+
23
  if paused:
24
+ if os.path.exists(TEMP_IMAGE_PATH):
25
+ frame = cv2.imread(TEMP_IMAGE_PATH)
26
+
27
+ if frame is None:
28
+ frame = get_next_video_frame()
29
+
30
+ if not paused:
31
  detected_boxes = detect_thermal_anomalies(frame)
32
  frame = overlay_boxes(frame, detected_boxes)
33
+ cv2.imwrite(TEMP_IMAGE_PATH, frame)
34
  metrics = update_metrics(detected_boxes)
35
+ else:
36
+ metrics = update_metrics([])
37
+
38
+ frame = cv2.resize(frame, (640, 480)) # Resize frame to fixed window size
39
 
40
+ return frame[:, :, ::-1], metrics # BGR to RGB
41
 
42
+ # Gradio UI
43
  with gr.Blocks() as app:
44
+ gr.Markdown("# 🌐 Thermal Anomaly Monitoring Dashboard")
45
 
46
  with gr.Row():
47
  with gr.Column(scale=3):
48
+ video_output = gr.Image(label="Live Video Feed", elem_id="video-feed", width=640, height=480)
49
  with gr.Column(scale=1):
50
+ metrics_output = gr.Label(label="Live Metrics", elem_id="metrics")
51
 
52
  with gr.Row():
53
  pause_btn = gr.Button("⏸️ Pause")
54
  resume_btn = gr.Button("▶️ Resume")
55
+ frame_slider = gr.Slider(0.2, 5, value=1, label="Frame Interval (seconds)")
56
 
57
  def toggle_pause():
58
  global paused
 
72
 
73
  def streaming_loop():
74
  while True:
75
+ yield monitor_feed()
 
76
  time.sleep(frame_rate)
77
 
78
  app.load(streaming_loop, outputs=[video_output, metrics_output])