surveillance / app.py
SuriRaja's picture
Update app.py
9da1fb6
raw
history blame
2.26 kB
# app.py
import gradio as gr
import cv2
import os
from services.video_service import get_video_frame
from services.detection_service import detect_objects
from services.thermal_service import detect_thermal_anomalies
from services.shadow_detection import detect_shadow_coverage
from services.salesforce_dispatcher import send_to_salesforce
# Global frame generator
frame_gen = get_video_frame("data/drone_day.mp4")
def monitor_feed():
try:
frame = next(frame_gen)
temp_path = "temp.jpg"
cv2.imwrite(temp_path, frame)
detections = detect_objects(temp_path)
thermal_flags = detect_thermal_anomalies(temp_path)
shadow_flag = detect_shadow_coverage(temp_path)
# Prepare alert payload
alert_payload = {
"detections": detections,
"thermal": bool(thermal_flags),
"shadow_issue": shadow_flag,
}
send_to_salesforce(alert_payload)
# Create human-readable alert
detected_classes = [d['label'] for d in detections]
confidence_scores = [round(d['score'] * 100, 2) for d in detections]
confidence_max = max(confidence_scores) if confidence_scores else 0
alert_message = f"""
🚨 Detection Summary 🚨
Detected Objects: {', '.join(detected_classes) if detected_classes else 'None'}
Thermal Issue: {'Yes' if alert_payload['thermal'] else 'No'}
Shadow Issue: {'Yes' if alert_payload['shadow_issue'] else 'No'}
Highest Confidence: {confidence_max}%
"""
# Return the frame and the alert
return frame, alert_message.strip()
except StopIteration:
return None, "End of Video Feed."
# 🎯 Create better Gradio Interface
with gr.Blocks(title="Solar Surveillance Feed Simulation") as app:
gr.Markdown("# β˜€οΈ Solar Site Live Surveillance")
gr.Markdown("### Live Drone Feed with AI Intrusion + Thermal + Shadow Monitoring")
video_output = gr.Image(label="Drone Camera View", shape=(512, 512))
alert_output = gr.Textbox(label="AI Generated Alerts", lines=5)
live_button = gr.Button("Start Live Surveillance πŸš€")
live_button.click(fn=monitor_feed, outputs=[video_output, alert_output])
app.queue().launch()