Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from services.video_service import VideoService
|
| 5 |
+
from services.detection_service import DetectionService
|
| 6 |
+
from services.thermal_service import ThermalService
|
| 7 |
+
from services.shadow_detection import ShadowDetection
|
| 8 |
+
from services.salesforce_dispatcher import SalesforceDispatcher
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Initialize services
|
| 12 |
+
video_service = VideoService()
|
| 13 |
+
detection_service = DetectionService(model_name="facebook/detr-resnet-50")
|
| 14 |
+
thermal_service = ThermalService()
|
| 15 |
+
shadow_detection = ShadowDetection()
|
| 16 |
+
salesforce_dispatcher = SalesforceDispatcher()
|
| 17 |
+
|
| 18 |
+
# Paths to video files
|
| 19 |
+
VIDEO_PATHS = {
|
| 20 |
+
"Day Feed": "data/drone_day.mp4",
|
| 21 |
+
"Night Feed": "data/night_intrusion.mp4",
|
| 22 |
+
"Thermal Feed": "data/thermal_hotspot.mp4",
|
| 23 |
+
"Shadow/Dust Feed": "data/shadow_dust_issue.mp4",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
def process_video(video_type, confidence_threshold=0.9):
|
| 27 |
+
"""Process the selected video feed and return results."""
|
| 28 |
+
video_path = VIDEO_PATHS.get(video_type)
|
| 29 |
+
if not video_path or not os.path.exists(video_path):
|
| 30 |
+
return "Video file not found.", None, None
|
| 31 |
+
|
| 32 |
+
# Load and process video
|
| 33 |
+
frames = video_service.load_video(video_path)
|
| 34 |
+
results = []
|
| 35 |
+
output_frames = []
|
| 36 |
+
|
| 37 |
+
for frame in frames:
|
| 38 |
+
# Convert frame to PIL Image
|
| 39 |
+
frame_pil = video_service.frame_to_pil(frame)
|
| 40 |
+
|
| 41 |
+
if video_type == "Thermal Feed":
|
| 42 |
+
# Detect overheating (hot spots)
|
| 43 |
+
detections = thermal_service.detect_hotspots(frame_pil, detection_service, confidence_threshold)
|
| 44 |
+
alert_type = "Overheating"
|
| 45 |
+
elif video_type == "Shadow/Dust Feed":
|
| 46 |
+
# Detect dusty or shaded panels
|
| 47 |
+
detections = shadow_detection.detect_shadow_dust(frame_pil, detection_service, confidence_threshold)
|
| 48 |
+
alert_type = "Shadow/Dust"
|
| 49 |
+
else:
|
| 50 |
+
# General object detection for day/night feeds
|
| 51 |
+
detections = detection_service.detect_objects(frame_pil, confidence_threshold)
|
| 52 |
+
alert_type = "General"
|
| 53 |
+
|
| 54 |
+
# Draw detections on frame
|
| 55 |
+
annotated_frame = video_service.draw_detections(frame, detections)
|
| 56 |
+
|
| 57 |
+
# Generate Salesforce case and notifications
|
| 58 |
+
if detections:
|
| 59 |
+
case_id = salesforce_dispatcher.create_case(
|
| 60 |
+
subject=f"{alert_type} Detected in {video_type}",
|
| 61 |
+
description=str(detections)
|
| 62 |
+
)
|
| 63 |
+
salesforce_dispatcher.send_email(
|
| 64 |
+
to="admin@solarplant.com",
|
| 65 |
+
subject=f"Alert: {alert_type} in {video_type}",
|
| 66 |
+
body=f"Case ID: {case_id}\nDetails: {detections}"
|
| 67 |
+
)
|
| 68 |
+
salesforce_dispatcher.send_whatsapp(
|
| 69 |
+
to="+1234567890",
|
| 70 |
+
message=f"Alert: {alert_type} detected in {video_type}. Case ID: {case_id}"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
results.append(detections)
|
| 74 |
+
output_frames.append(annotated_frame)
|
| 75 |
+
|
| 76 |
+
# Save output video
|
| 77 |
+
output_path = "output_annotated.mp4"
|
| 78 |
+
video_service.save_video(output_frames, output_path)
|
| 79 |
+
|
| 80 |
+
return str(results), output_path, None
|
| 81 |
+
|
| 82 |
+
# Gradio Interface
|
| 83 |
+
with gr.Blocks() as demo:
|
| 84 |
+
gr.Markdown("# Solar Panel Monitoring System")
|
| 85 |
+
video_type = gr.Dropdown(
|
| 86 |
+
choices=["Day Feed", "Night Feed", "Thermal Feed", "Shadow/Dust Feed"],
|
| 87 |
+
label="Select Drone Feed"
|
| 88 |
+
)
|
| 89 |
+
confidence_threshold = gr.Slider(0.5, 1.0, value=0.9, label="Confidence Threshold")
|
| 90 |
+
process_button = gr.Button("Process Video")
|
| 91 |
+
output_text = gr.Textbox(label="Detection Results")
|
| 92 |
+
output_video = gr.Video(label="Annotated Video")
|
| 93 |
+
error_message = gr.Textbox(label="Error Message")
|
| 94 |
+
|
| 95 |
+
process_button.click(
|
| 96 |
+
fn=process_video,
|
| 97 |
+
inputs=[video_type, confidence_threshold],
|
| 98 |
+
outputs=[output_text, output_video, error_message]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
demo.launch()
|