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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -66
app.py CHANGED
@@ -2,86 +2,77 @@ import gradio as gr
2
  import cv2
3
  import random
4
  import os
5
- from services.video_service import get_random_video_frame
6
  from services.detection_service import detect_objects
7
  from services.thermal_service import detect_thermal_anomalies
8
- from services.shadow_detection import detect_shadow_coverage
9
- from services.salesforce_dispatcher import send_to_salesforce
10
 
11
- # Initialize counters
12
- frame_counter = 0
13
- intrusion_counter = 0
14
- thermal_counter = 0
15
- shadow_issue_counter = 0
16
 
17
- # Random frame generator
18
- frame_gen = get_random_video_frame()
 
 
19
 
20
- # Metrics state updater
21
  def monitor_feed():
22
- global frame_counter, intrusion_counter, thermal_counter, shadow_issue_counter
23
-
24
- try:
25
- frame, current_video = next(frame_gen)
26
-
27
- temp_path = "temp.jpg"
28
- cv2.imwrite(temp_path, frame)
29
-
30
- detections = detect_objects(temp_path)
31
- thermal_boxes = detect_thermal_anomalies(temp_path)
32
- shadow_flag = detect_shadow_coverage(temp_path)
33
-
34
- frame_counter += 1
35
- intrusion_detected = any(d['label'] == 'person' for d in detections)
36
-
37
- if intrusion_detected:
38
- intrusion_counter += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- if thermal_boxes:
41
- thermal_counter += 1
42
-
43
- if shadow_flag:
44
- shadow_issue_counter += 1
45
-
46
- # Commented Salesforce Push (can enable later)
47
- # alert_payload = {
48
- # "detections": detections,
49
- # "thermal": bool(thermal_boxes),
50
- # "shadow_issue": shadow_flag,
51
- # }
52
- # send_to_salesforce(alert_payload)
53
-
54
- return frame, frame_counter, intrusion_counter, thermal_counter, shadow_issue_counter, current_video
55
-
56
- except StopIteration:
57
- return None, frame_counter, intrusion_counter, thermal_counter, shadow_issue_counter, "End of Video"
58
-
59
-
60
- # Gradio Blocks UI
61
  def build_interface():
62
- with gr.Blocks(title="Solar Surveillance Feed Simulation with Live Metrics") as app:
 
63
  with gr.Row():
64
  with gr.Column(scale=3):
65
- video_display = gr.Image(label="Surveillance Feed", show_label=True)
66
-
67
  with gr.Column(scale=1):
68
- gr.Markdown("## 📊 Live Metrics")
69
- frame_count_display = gr.Number(label="Frames Processed", interactive=False)
70
- intrusion_count_display = gr.Number(label="Intrusions Detected", interactive=False)
71
- thermal_count_display = gr.Number(label="Thermal Hotspots", interactive=False)
72
- shadow_count_display = gr.Number(label="Shadow Issues", interactive=False)
73
- current_video_display = gr.Textbox(label="Current Video", interactive=False)
74
 
75
- app.load(
76
- monitor_feed,
77
- inputs=[],
78
- outputs=[video_display, frame_count_display, intrusion_count_display, thermal_count_display, shadow_count_display, current_video_display],
79
- every=1.0 # seconds
80
- )
81
- return app
82
 
 
83
 
84
- # Launch the app
85
  if __name__ == "__main__":
86
  demo = build_interface()
 
87
  demo.launch()
 
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()