Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
|
|
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
transforms.ToTensor(),
|
| 15 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 16 |
-
std=[0.229, 0.224, 0.225])
|
| 17 |
-
])
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
input_tensor = transform(image).unsqueeze(0)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import requests
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
import numpy as np
|
| 7 |
+
from io import BytesIO
|
| 8 |
|
| 9 |
+
# Initialize the object detection model
|
| 10 |
+
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 11 |
+
thermal_model = YOLO("thermal_model.pt")
|
| 12 |
|
| 13 |
+
def detect_intrusion(image):
|
| 14 |
+
detections = object_detector(image)
|
| 15 |
+
return [d for d in detections if d['score'] > 0.7]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def detect_thermal_anomalies(image):
|
| 18 |
+
results = thermal_model(image)
|
| 19 |
+
flagged = []
|
| 20 |
+
for r in results:
|
| 21 |
+
if hasattr(r, 'temperature') and r.temperature > 75:
|
| 22 |
+
flagged.append(r)
|
| 23 |
+
return flagged
|
| 24 |
|
| 25 |
+
def detect_shading(image):
|
| 26 |
+
# Basic approach to detect shadows or dust
|
| 27 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 28 |
+
_, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)
|
| 29 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 30 |
+
return len(contours) > 5 # heuristic for detecting large shadow regions
|
| 31 |
|
| 32 |
+
def process_frame(frame):
|
| 33 |
+
# Convert the frame into the format expected by the AI models
|
| 34 |
+
detections = detect_intrusion(frame)
|
| 35 |
+
thermal_anomalies = detect_thermal_anomalies(frame)
|
| 36 |
+
shading = detect_shading(frame)
|
| 37 |
|
| 38 |
+
return detections, thermal_anomalies, shading
|
|
|
|
| 39 |
|
| 40 |
+
def create_alert(detections, thermal_anomalies, shading):
|
| 41 |
+
alert_message = "Solar Panel Fault Detected!"
|
| 42 |
+
if detections:
|
| 43 |
+
alert_message += " Intrusion detected!"
|
| 44 |
+
if thermal_anomalies:
|
| 45 |
+
alert_message += " Overheating detected!"
|
| 46 |
+
if shading:
|
| 47 |
+
alert_message += " Shading or dust detected!"
|
| 48 |
|
| 49 |
+
# Optionally send to Salesforce or another CRM system
|
| 50 |
+
payload = {
|
| 51 |
+
"Alert_Type__c": "Fault Detected",
|
| 52 |
+
"Message__c": alert_message,
|
| 53 |
+
"Confidence_Score__c": 85 # Example value, replace with actual confidence
|
| 54 |
+
}
|
| 55 |
+
requests.post("YOUR_SALESFORCE_API_ENDPOINT", json=payload)
|
| 56 |
+
|
| 57 |
+
return alert_message
|
| 58 |
+
|
| 59 |
+
# Streamlit interface
|
| 60 |
+
st.title("Solar Panel Fault Detection")
|
| 61 |
+
uploaded_file = st.file_uploader("Upload a video", type=["mp4"])
|
| 62 |
+
|
| 63 |
+
if uploaded_file:
|
| 64 |
+
video_bytes = uploaded_file.read()
|
| 65 |
+
video = cv2.VideoCapture(BytesIO(video_bytes))
|
| 66 |
+
|
| 67 |
+
while video.isOpened():
|
| 68 |
+
ret, frame = video.read()
|
| 69 |
+
if not ret:
|
| 70 |
+
break
|
| 71 |
+
|
| 72 |
+
detections, thermal_anomalies, shading = process_frame(frame)
|
| 73 |
+
alert_message = create_alert(detections, thermal_anomalies, shading)
|
| 74 |
+
|
| 75 |
+
st.image(frame, caption="Current Frame", channels="BGR")
|
| 76 |
+
st.write(alert_message)
|
| 77 |
+
|
| 78 |
+
# Display alerts or other relevant info
|