SuriRaja commited on
Commit
f10f0a7
·
1 Parent(s): 23ab25d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -56
app.py CHANGED
@@ -1,78 +1,44 @@
1
  import gradio as gr
2
  import cv2
3
- import random
4
- import os
5
  from services.detection_service import detect_objects
6
  from services.thermal_service import detect_thermal_anomalies
7
- from services.video_service import get_random_video_frame
8
 
9
- # Paths
10
  TEMP_IMAGE_PATH = "temp.jpg"
11
 
12
- # Initialize Metrics
13
- total_frames = 0
14
- total_detections = 0
15
- total_anomalies = 0
16
-
17
  def monitor_feed():
18
- global total_frames, total_detections, total_anomalies
19
-
20
  frame = get_random_video_frame()
21
-
22
  if frame is None:
23
- return None, None, None
24
 
25
- # Save frame temporarily
26
  cv2.imwrite(TEMP_IMAGE_PATH, frame)
27
 
28
- # Object detection
29
- detections = detect_objects(TEMP_IMAGE_PATH)
30
- detection_boxes = []
31
- if detections:
32
- for det in detections:
33
- box = det['box']
34
- label = det['label']
35
- detection_boxes.append(label)
36
- x, y, w, h = box
37
- cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
38
- cv2.putText(frame, label, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
39
 
40
- # Thermal detection
41
- anomalies = detect_thermal_anomalies(TEMP_IMAGE_PATH)
42
- anomaly_labels = []
43
- if anomalies:
44
- for anomaly in anomalies:
45
- label = anomaly['label']
46
- anomaly_labels.append(label)
47
 
48
- # Update metrics
49
- total_frames += 1
50
- total_detections += len(detection_boxes)
51
- total_anomalies += len(anomaly_labels)
52
-
53
- # Overlay live metrics
54
- metrics_text = f"Frames: {total_frames} | Detections: {total_detections} | Anomalies: {total_anomalies}"
55
- cv2.putText(frame, metrics_text, (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 0), 2)
56
-
57
- return frame, {"Detections": detection_boxes}, {"Thermal Anomalies": anomaly_labels}
58
 
59
  def build_interface():
60
- with gr.Blocks() as demo:
61
- gr.Markdown("# Solar Surveillance System \U0001F310\U0001F50D")
62
  with gr.Row():
63
- with gr.Column(scale=3):
64
- image_output = gr.Image(label="Live Video Feed")
65
- with gr.Column(scale=1):
66
- detection_json = gr.JSON(label="Detections")
67
- thermal_json = gr.JSON(label="Thermal Anomalies")
68
- gr.Markdown("**Live Metrics Auto Updating** \U0001F4CA")
69
-
70
- demo_btn = gr.Button("Start Monitoring")
71
- demo_btn.click(fn=monitor_feed, outputs=[image_output, detection_json, thermal_json])
72
-
 
73
  return demo
74
 
75
  if __name__ == "__main__":
76
  demo = build_interface()
77
- demo.queue()
78
- demo.launch()
 
1
  import gradio as gr
2
  import cv2
3
+ from services.video_service import get_random_video_frame
 
4
  from services.detection_service import detect_objects
5
  from services.thermal_service import detect_thermal_anomalies
6
+ from services.overlay_service import overlay_boxes
7
 
 
8
  TEMP_IMAGE_PATH = "temp.jpg"
9
 
 
 
 
 
 
10
  def monitor_feed():
 
 
11
  frame = get_random_video_frame()
 
12
  if frame is None:
13
+ return None, 0, 0 # 🛡️ If invalid frame, return defaults to keep app alive
14
 
 
15
  cv2.imwrite(TEMP_IMAGE_PATH, frame)
16
 
17
+ boxes = detect_objects(TEMP_IMAGE_PATH)
18
+ thermal_boxes = detect_thermal_anomalies(TEMP_IMAGE_PATH)
 
 
 
 
 
 
 
 
 
19
 
20
+ all_boxes = boxes + thermal_boxes
21
+ image_with_boxes = overlay_boxes(TEMP_IMAGE_PATH, all_boxes)
 
 
 
 
 
22
 
23
+ return image_with_boxes, len(all_boxes), len(thermal_boxes)
 
 
 
 
 
 
 
 
 
24
 
25
  def build_interface():
26
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
27
+ gr.Markdown("# 🚀 Solar Surveillance POC Demo")
28
  with gr.Row():
29
+ image_display = gr.Image(label="🔴 Live Surveillance Feed", interactive=False)
30
+ with gr.Column():
31
+ total_detections = gr.Number(label="Total Detections", value=0, interactive=False)
32
+ thermal_alerts = gr.Number(label="Thermal Alerts", value=0, interactive=False)
33
+
34
+ demo.load(
35
+ fn=monitor_feed,
36
+ inputs=[],
37
+ outputs=[image_display, total_detections, thermal_alerts],
38
+ every=1 # auto-refresh every 1 second
39
+ )
40
  return demo
41
 
42
  if __name__ == "__main__":
43
  demo = build_interface()
44
+ demo.launch(server_name="0.0.0.0", server_port=7860)