Sanjayraju30 commited on
Commit
417ab56
·
verified ·
1 Parent(s): a0c38ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -27
app.py CHANGED
@@ -1,37 +1,78 @@
1
  import streamlit as st
2
- import torch
3
- import torchvision.transforms as transforms
4
- from PIL import Image
 
5
  import numpy as np
 
6
 
7
- # Load your pre-trained model
8
- model = torch.load('model/your_model_file.pt')
9
- model.eval()
10
 
11
- # Define image transformations
12
- transform = transforms.Compose([
13
- transforms.Resize((224, 224)),
14
- transforms.ToTensor(),
15
- transforms.Normalize(mean=[0.485, 0.456, 0.406],
16
- std=[0.229, 0.224, 0.225])
17
- ])
18
 
19
- st.title("VIEP: Utility Pole Fault Detection")
 
 
 
 
 
 
20
 
21
- uploaded_file = st.file_uploader("Upload an image of a utility pole", type=["jpg", "jpeg", "png"])
 
 
 
 
 
22
 
23
- if uploaded_file is not None:
24
- image = Image.open(uploaded_file).convert('RGB')
25
- st.image(image, caption='Uploaded Image', use_column_width=True)
 
 
26
 
27
- # Preprocess the image
28
- input_tensor = transform(image).unsqueeze(0)
29
 
30
- # Perform inference
31
- with torch.no_grad():
32
- output = model(input_tensor)
33
- _, predicted = torch.max(output, 1)
 
 
 
 
34
 
35
- # Map the prediction to class names
36
- classes = ['No Fault', 'Fault Detected']
37
- st.write(f"Prediction: {classes[predicted.item()]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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