faultdetection3 / app.py
Sanjayraju30's picture
Update app.py
417ab56 verified
raw
history blame
2.59 kB
import streamlit as st
import cv2
import requests
from transformers import pipeline
from ultralytics import YOLO
import numpy as np
from io import BytesIO
# Initialize the object detection model
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
thermal_model = YOLO("thermal_model.pt")
def detect_intrusion(image):
detections = object_detector(image)
return [d for d in detections if d['score'] > 0.7]
def detect_thermal_anomalies(image):
results = thermal_model(image)
flagged = []
for r in results:
if hasattr(r, 'temperature') and r.temperature > 75:
flagged.append(r)
return flagged
def detect_shading(image):
# Basic approach to detect shadows or dust
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
return len(contours) > 5 # heuristic for detecting large shadow regions
def process_frame(frame):
# Convert the frame into the format expected by the AI models
detections = detect_intrusion(frame)
thermal_anomalies = detect_thermal_anomalies(frame)
shading = detect_shading(frame)
return detections, thermal_anomalies, shading
def create_alert(detections, thermal_anomalies, shading):
alert_message = "Solar Panel Fault Detected!"
if detections:
alert_message += " Intrusion detected!"
if thermal_anomalies:
alert_message += " Overheating detected!"
if shading:
alert_message += " Shading or dust detected!"
# Optionally send to Salesforce or another CRM system
payload = {
"Alert_Type__c": "Fault Detected",
"Message__c": alert_message,
"Confidence_Score__c": 85 # Example value, replace with actual confidence
}
requests.post("YOUR_SALESFORCE_API_ENDPOINT", json=payload)
return alert_message
# Streamlit interface
st.title("Solar Panel Fault Detection")
uploaded_file = st.file_uploader("Upload a video", type=["mp4"])
if uploaded_file:
video_bytes = uploaded_file.read()
video = cv2.VideoCapture(BytesIO(video_bytes))
while video.isOpened():
ret, frame = video.read()
if not ret:
break
detections, thermal_anomalies, shading = process_frame(frame)
alert_message = create_alert(detections, thermal_anomalies, shading)
st.image(frame, caption="Current Frame", channels="BGR")
st.write(alert_message)
# Display alerts or other relevant info