Sanjayraju30 commited on
Commit
c18be6f
·
verified ·
1 Parent(s): d790b78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -46
app.py CHANGED
@@ -1,53 +1,32 @@
1
- import gradio as gr
2
- from transformers import DetrImageProcessor, DetrForObjectDetection
3
- import torch
4
  import cv2
5
- import numpy as np
6
- from PIL import Image
 
 
 
7
 
8
- # Load Hugging Face object detection model
9
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
10
- model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
11
 
12
- def detect_intrusion(video_path):
13
- cap = cv2.VideoCapture(video_path)
14
- alerts = []
15
- count = 0
16
- while cap.isOpened() and count < 20:
17
- ret, frame = cap.read()
18
- if not ret:
19
- break
20
- image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
21
- inputs = processor(images=image, return_tensors="pt")
22
- outputs = model(**inputs)
23
- target_sizes = torch.tensor([image.size[::-1]])
24
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
25
- for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
26
- label_name = model.config.id2label[label.item()]
27
- if label_name == "person":
28
- alerts.append(f"Frame {count}: 🔴 Person Detected!")
29
- break
30
- count += 1
31
- cap.release()
32
- return "\n".join(alerts) if alerts else "✅ No intrusion detected."
33
 
34
- def detect_overheat(temp, humidity, solar_output):
35
- if temp > 75:
36
- return "🔥 Overheat Fault!"
37
- elif humidity < 20 and solar_output < 300:
38
- return "🌫️ Dust/Shade Fault!"
39
- else:
40
- return "✅ All Good"
 
41
 
42
- video_tab = gr.Interface(fn=detect_intrusion,
43
- inputs=gr.Video(label="Upload Video"),
44
- outputs=gr.Textbox(label="Intrusion Detection Alerts"))
45
 
46
- sensor_tab = gr.Interface(fn=detect_overheat,
47
- inputs=[gr.Number(label="Temperature (°C)"),
48
- gr.Number(label="Humidity (%)"),
49
- gr.Number(label="Solar Output (W)")],
50
- outputs=gr.Textbox(label="Sensor Fault Detection"))
51
-
52
- gr.TabbedInterface([video_tab, sensor_tab], ["Intrusion (Video)", "Sensor (Input)"]).launch()
53
 
 
1
+ import gradio as gr
 
 
2
  import cv2
3
+ from services.video_service import get_video_frame
4
+ from services.detection_service import detect_objects
5
+ from services.thermal_service import detect_thermal_anomalies
6
+ from services.shadow_detection import detect_shadow_coverage
7
+ from services.salesforce_dispatcher import send_to_salesforce
8
 
9
+ frame_gen = get_video_frame(&quot;data/drone_day.mp4&quot;)
 
 
10
 
11
+ def monitor_feed():
12
+ try:
13
+ frame = next(frame_gen)
14
+ cv2.imwrite(&quot;temp.jpg&quot;, frame)
15
+ detections = detect_objects(&quot;temp.jpg&quot;)
16
+ thermal = detect_thermal_anomalies(&quot;temp.jpg&quot;)
17
+ shadow_flag = detect_shadow_coverage(&quot;temp.jpg&quot;)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ alert_payload = {
20
+ &quot;detections&quot;: detections,
21
+ &quot;thermal&quot;: bool(thermal),
22
+ &quot;shadow_issue&quot;: shadow_flag,
23
+ }
24
+ send_to_salesforce(alert_payload)
25
+ return frame
26
+ except StopIteration:
27
 
28
+ return None
 
 
29
 
30
+ iface = gr.Interface(fn=monitor_feed, inputs=[], outputs=&quot;image&quot;, live=True, title=&quot;Solar
31
+ Surveillance Feed Simulation&quot;)
 
 
 
 
 
32