Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,17 @@
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
4 |
-
from PIL import Image
|
5 |
-
import numpy as np
|
6 |
import cv2
|
|
|
|
|
|
|
7 |
|
8 |
# Set page configuration
|
9 |
st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
|
10 |
|
11 |
# Title and description
|
12 |
st.title("Solar Panel Fault Detection PoC")
|
13 |
-
st.write("Upload a thermal
|
14 |
|
15 |
# Load model and processor
|
16 |
@st.cache_resource
|
@@ -21,92 +22,153 @@ def load_model():
|
|
21 |
|
22 |
processor, model = load_model()
|
23 |
|
24 |
-
# Function to process
|
25 |
-
def detect_faults(
|
26 |
-
# Convert
|
27 |
-
|
28 |
-
|
29 |
-
# Convert to RGB if necessary
|
30 |
-
if img_np.shape[-1] == 4:
|
31 |
-
img_np = img_np[:, :, :3]
|
32 |
|
33 |
-
# Prepare
|
34 |
-
inputs = processor(images=
|
35 |
|
36 |
# Run inference
|
37 |
with torch.no_grad():
|
38 |
outputs = model(**inputs)
|
39 |
|
40 |
# Post-process outputs
|
41 |
-
target_sizes = torch.tensor([
|
42 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
43 |
|
44 |
# Initialize fault detection
|
45 |
faults = {"Thermal Fault": False, "Dust Fault": False, "Power Generation Fault": False}
|
46 |
-
|
47 |
|
48 |
-
# Analyze
|
49 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
50 |
box = [int(i) for i in box.tolist()]
|
51 |
# Simulate fault detection based on bounding box and pixel intensity
|
52 |
-
roi =
|
53 |
mean_intensity = np.mean(roi)
|
54 |
|
55 |
# Thermal Fault: High intensity (hotspot)
|
56 |
-
if mean_intensity > 200: # Adjust threshold based on thermal
|
57 |
faults["Thermal Fault"] = True
|
58 |
-
cv2.rectangle(
|
59 |
-
cv2.putText(
|
60 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
61 |
|
62 |
# Dust Fault: Low intensity or irregular patterns
|
63 |
elif mean_intensity < 100: # Adjust threshold
|
64 |
faults["Dust Fault"] = True
|
65 |
-
cv2.rectangle(
|
66 |
-
cv2.putText(
|
67 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
68 |
|
69 |
# Power Generation Fault: Any detected anomaly may indicate reduced efficiency
|
70 |
if faults["Thermal Fault"] or faults["Dust Fault"]:
|
71 |
faults["Power Generation Fault"] = True
|
72 |
|
73 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# File uploader
|
76 |
-
uploaded_file = st.file_uploader("Upload a thermal
|
77 |
|
78 |
if uploaded_file is not None:
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
#
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
st.
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
if
|
104 |
-
st.
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
# Footer
|
111 |
st.markdown("---")
|
112 |
-
st.write("Built with Streamlit
|
|
|
1 |
import streamlit as st
|
2 |
import torch
|
3 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
|
|
|
|
4 |
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import tempfile
|
7 |
+
import os
|
8 |
|
9 |
# Set page configuration
|
10 |
st.set_page_config(page_title="Solar Panel Fault Detection", layout="wide")
|
11 |
|
12 |
# Title and description
|
13 |
st.title("Solar Panel Fault Detection PoC")
|
14 |
+
st.write("Upload a thermal video (MP4) of a solar panel to detect thermal, dust, and power generation faults.")
|
15 |
|
16 |
# Load model and processor
|
17 |
@st.cache_resource
|
|
|
22 |
|
23 |
processor, model = load_model()
|
24 |
|
25 |
+
# Function to process frame and detect faults
|
26 |
+
def detect_faults(frame):
|
27 |
+
# Convert frame to RGB if necessary
|
28 |
+
if frame.shape[-1] == 4:
|
29 |
+
frame = frame[:, :, :3]
|
|
|
|
|
|
|
30 |
|
31 |
+
# Prepare frame for model
|
32 |
+
inputs = processor(images=frame, return_tensors="pt")
|
33 |
|
34 |
# Run inference
|
35 |
with torch.no_grad():
|
36 |
outputs = model(**inputs)
|
37 |
|
38 |
# Post-process outputs
|
39 |
+
target_sizes = torch.tensor([frame.shape[:2]])
|
40 |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
41 |
|
42 |
# Initialize fault detection
|
43 |
faults = {"Thermal Fault": False, "Dust Fault": False, "Power Generation Fault": False}
|
44 |
+
annotated_frame = frame.copy()
|
45 |
|
46 |
+
# Analyze frame for faults
|
47 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
48 |
box = [int(i) for i in box.tolist()]
|
49 |
# Simulate fault detection based on bounding box and pixel intensity
|
50 |
+
roi = frame[box[1]:box[3], box[0]:box[2]]
|
51 |
mean_intensity = np.mean(roi)
|
52 |
|
53 |
# Thermal Fault: High intensity (hotspot)
|
54 |
+
if mean_intensity > 200: # Adjust threshold based on thermal video scale
|
55 |
faults["Thermal Fault"] = True
|
56 |
+
cv2.rectangle(annotated_frame, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2)
|
57 |
+
cv2.putText(annotated_frame, "Thermal Fault", (box[0], box[1]-10),
|
58 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
|
59 |
|
60 |
# Dust Fault: Low intensity or irregular patterns
|
61 |
elif mean_intensity < 100: # Adjust threshold
|
62 |
faults["Dust Fault"] = True
|
63 |
+
cv2.rectangle(annotated_frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
|
64 |
+
cv2.putText(annotated_frame, "Dust Fault", (box[0], box[1]-10),
|
65 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
66 |
|
67 |
# Power Generation Fault: Any detected anomaly may indicate reduced efficiency
|
68 |
if faults["Thermal Fault"] or faults["Dust Fault"]:
|
69 |
faults["Power Generation Fault"] = True
|
70 |
|
71 |
+
return annotated_frame, faults
|
72 |
+
|
73 |
+
# Function to process video and generate annotated output
|
74 |
+
def process_video(video_path):
|
75 |
+
# Open video
|
76 |
+
cap = cv2.VideoCapture(video_path)
|
77 |
+
if not cap.isOpened():
|
78 |
+
st.error("Error: Could not open video file.")
|
79 |
+
return None, None
|
80 |
+
|
81 |
+
# Get video properties
|
82 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
83 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
84 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
85 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
86 |
+
|
87 |
+
# Create temporary file for output video
|
88 |
+
output_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
89 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
90 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
91 |
+
|
92 |
+
# Initialize fault summary
|
93 |
+
video_faults = {"Thermal Fault": False, "Dust Fault": False, "Power Generation Fault": False}
|
94 |
+
|
95 |
+
# Process each frame
|
96 |
+
frame_count = 0
|
97 |
+
with st.spinner("Analyzing video..."):
|
98 |
+
progress = st.progress(0)
|
99 |
+
while cap.isOpened():
|
100 |
+
ret, frame = cap.read()
|
101 |
+
if not ret:
|
102 |
+
break
|
103 |
+
|
104 |
+
# Convert BGR to RGB
|
105 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
106 |
+
|
107 |
+
# Detect faults in frame
|
108 |
+
annotated_frame, faults = detect_faults(frame_rgb)
|
109 |
+
|
110 |
+
# Update video faults
|
111 |
+
for fault in video_faults:
|
112 |
+
video_faults[fault] |= faults[fault]
|
113 |
+
|
114 |
+
# Convert back to BGR for writing
|
115 |
+
annotated_frame_bgr = cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR)
|
116 |
+
out.write(annotated_frame_bgr)
|
117 |
+
|
118 |
+
# Update progress
|
119 |
+
frame_count += 1
|
120 |
+
progress.progress(frame_count / total_frames)
|
121 |
+
|
122 |
+
cap.release()
|
123 |
+
out.release()
|
124 |
+
|
125 |
+
return output_path, video_faults
|
126 |
|
127 |
# File uploader
|
128 |
+
uploaded_file = st.file_uploader("Upload a thermal video", type=["mp4"])
|
129 |
|
130 |
if uploaded_file is not None:
|
131 |
+
# Save uploaded video to temporary file
|
132 |
+
tfile = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
133 |
+
tfile.write(uploaded_file.read())
|
134 |
+
tfile.close()
|
135 |
+
|
136 |
+
# Display uploaded video
|
137 |
+
st.video(tfile.name, format="video/mp4")
|
138 |
+
|
139 |
+
# Process video
|
140 |
+
output_path, video_faults = process_video(tfile.name)
|
141 |
+
|
142 |
+
if output_path:
|
143 |
+
# Display results
|
144 |
+
st.subheader("Fault Detection Results")
|
145 |
+
st.video(output_path, format="video/mp4")
|
146 |
+
|
147 |
+
# Show fault summary
|
148 |
+
st.write("**Detected Faults in Video:**")
|
149 |
+
for fault, detected in video_faults.items():
|
150 |
+
status = "Detected" if detected else "Not Detected"
|
151 |
+
color = "red" if detected else "green"
|
152 |
+
st.markdown(f"- **{fault}**: <span style='color:{color}'>{status}</span>", unsafe_allow_html=True)
|
153 |
+
|
154 |
+
# Provide recommendations
|
155 |
+
if any(video_faults.values()):
|
156 |
+
st.subheader("Recommendations")
|
157 |
+
if video_faults["Thermal Fault"]:
|
158 |
+
st.write("- **Thermal Fault**: Inspect for damaged components or overheating issues.")
|
159 |
+
if video_faults["Dust Fault"]:
|
160 |
+
st.write("- **Dust Fault**: Schedule cleaning to remove dust accumulation.")
|
161 |
+
if video_faults["Power Generation Fault"]:
|
162 |
+
st.write("- **Power Generation Fault**: Investigate efficiency issues due to detected faults.")
|
163 |
+
else:
|
164 |
+
st.write("No faults detected. The solar panel appears to be functioning normally.")
|
165 |
+
|
166 |
+
# Clean up temporary files
|
167 |
+
os.unlink(output_path)
|
168 |
+
|
169 |
+
# Clean up uploaded file
|
170 |
+
os.unlink(tfile.name)
|
171 |
|
172 |
# Footer
|
173 |
st.markdown("---")
|
174 |
+
st.write("Built with Streamlit, Hugging Face Transformers, and OpenCV for Solar Panel Fault Detection PoC")
|