Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,142 @@
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
3 |
-
from services.video_service import process_video
|
4 |
-
from services.detection_service import detect_faults_solar, detect_faults_windmill
|
5 |
-
from services.thermal_service import detect_hotspots
|
6 |
-
from services.shadow_detection import detect_shadows
|
7 |
-
from PIL import Image
|
8 |
import os
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
11 |
from models.solar_model import load_solar_model
|
12 |
from models.windmill_model import load_windmill_model
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def main():
|
18 |
-
st.title("
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
if video_file:
|
24 |
video_path = os.path.join(VIDEO_FOLDER, video_file)
|
25 |
-
st.write(f"Processing video: {video_file}")
|
26 |
-
|
27 |
-
# Open the video using OpenCV
|
28 |
cap = cv2.VideoCapture(video_path)
|
29 |
-
|
30 |
if not cap.isOpened():
|
31 |
-
st.error("Error
|
32 |
return
|
33 |
|
34 |
-
|
35 |
-
stframe = st.empty()
|
36 |
-
|
37 |
-
# Choose fault detection type
|
38 |
-
choice = st.selectbox("Choose Fault Detection", ["Solar Panel", "Windmill"])
|
39 |
-
model = None
|
40 |
-
if choice == "Solar Panel":
|
41 |
-
model = load_solar_model()
|
42 |
-
else:
|
43 |
-
model = load_windmill_model()
|
44 |
-
|
45 |
while cap.isOpened():
|
46 |
ret, frame = cap.read()
|
47 |
if not ret:
|
48 |
break
|
49 |
|
50 |
-
|
51 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
52 |
|
53 |
-
#
|
54 |
-
faults = detect_faults_solar(model, frame_rgb) if
|
|
|
55 |
|
56 |
-
# Draw bounding boxes and labels
|
57 |
for fault in faults:
|
58 |
x, y = int(fault['location'][0]), int(fault['location'][1])
|
59 |
-
cv2.
|
60 |
-
cv2.
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
cap.release()
|
|
|
66 |
|
67 |
if __name__ == "__main__":
|
68 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
+
import numpy as np
|
5 |
+
from datetime import datetime
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
from services.detection_service import detect_faults_solar, detect_faults_windmill
|
8 |
+
from services.anomaly_service import track_anomalies, predict_anomaly
|
9 |
from models.solar_model import load_solar_model
|
10 |
from models.windmill_model import load_windmill_model
|
11 |
+
from config.settings import VIDEO_FOLDER
|
12 |
+
|
13 |
+
# Initialize session state for logs, metrics, and trends
|
14 |
+
if 'logs' not in st.session_state:
|
15 |
+
st.session_state.logs = []
|
16 |
+
if 'anomaly_counts' not in st.session_state:
|
17 |
+
st.session_state.anomaly_counts = []
|
18 |
+
if 'frame_numbers' not in st.session_state:
|
19 |
+
st.session_state.frame_numbers = []
|
20 |
+
if 'total_detected' not in st.session_state:
|
21 |
+
st.session_state.total_detected = 0
|
22 |
|
23 |
def main():
|
24 |
+
st.title("Thermal Anomaly Monitoring Dashboard")
|
25 |
+
st.markdown("**Status:** 🟢 Running")
|
26 |
+
|
27 |
+
# Sidebar for video selection and detection type
|
28 |
+
st.sidebar.header("Settings")
|
29 |
+
video_files = [f for f in os.listdir(VIDEO_FOLDER) if f.endswith('.mp4')]
|
30 |
+
if not video_files:
|
31 |
+
st.error("No videos found in the 'data' folder. Please add .mp4 files.")
|
32 |
+
return
|
33 |
+
video_file = st.sidebar.selectbox("Select Video", video_files)
|
34 |
+
detection_type = st.sidebar.selectbox("Detection Type", ["Solar Panel", "Windmill"])
|
35 |
+
|
36 |
+
# Load the appropriate model
|
37 |
+
model = load_solar_model() if detection_type == "Solar Panel" else load_windmill_model()
|
38 |
+
|
39 |
+
# Layout: Two columns for video feed and metrics
|
40 |
+
col1, col2 = st.columns([3, 1])
|
41 |
+
|
42 |
+
with col1:
|
43 |
+
st.subheader("Live Video Feed")
|
44 |
+
video_placeholder = st.empty()
|
45 |
+
|
46 |
+
with col2:
|
47 |
+
st.subheader("Live Metrics")
|
48 |
+
metrics_placeholder = st.empty()
|
49 |
+
|
50 |
+
# Layout: Two columns for logs and trends
|
51 |
+
col3, col4 = st.columns([1, 2])
|
52 |
+
|
53 |
+
with col3:
|
54 |
+
st.subheader("Live Logs")
|
55 |
+
logs_placeholder = st.empty()
|
56 |
+
st.subheader("Last 5 Captured Events")
|
57 |
+
events_placeholder = st.empty()
|
58 |
+
|
59 |
+
with col4:
|
60 |
+
st.subheader("Detection Trends")
|
61 |
+
trends_placeholder = st.empty()
|
62 |
+
|
63 |
+
# Process video
|
64 |
if video_file:
|
65 |
video_path = os.path.join(VIDEO_FOLDER, video_file)
|
|
|
|
|
|
|
66 |
cap = cv2.VideoCapture(video_path)
|
|
|
67 |
if not cap.isOpened():
|
68 |
+
st.error("Error: Could not open video file.")
|
69 |
return
|
70 |
|
71 |
+
frame_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
while cap.isOpened():
|
73 |
ret, frame = cap.read()
|
74 |
if not ret:
|
75 |
break
|
76 |
|
77 |
+
frame_count += 1
|
78 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
79 |
|
80 |
+
# Detect faults
|
81 |
+
faults = detect_faults_solar(model, frame_rgb) if detection_type == "Solar Panel" else detect_faults_windmill(model, frame_rgb)
|
82 |
+
num_anomalies = len(faults)
|
83 |
|
84 |
+
# Draw bounding boxes and labels
|
85 |
for fault in faults:
|
86 |
x, y = int(fault['location'][0]), int(fault['location'][1])
|
87 |
+
cv2.rectangle(frame_rgb, (x-30, y-30), (x+30, y+30), (255, 0, 0), 2)
|
88 |
+
cv2.putText(frame_rgb, f"{fault['type']}", (x, y-40),
|
89 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
90 |
+
|
91 |
+
# Update video feed with timestamp
|
92 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
93 |
+
video_placeholder.image(frame_rgb, channels="RGB", caption=f"{timestamp}")
|
94 |
+
|
95 |
+
# Update logs and metrics
|
96 |
+
log_entry = f"{timestamp} - Frame {frame_count} - Anomalies: {num_anomalies}"
|
97 |
+
st.session_state.logs.append(log_entry)
|
98 |
+
st.session_state.total_detected += num_anomalies
|
99 |
+
st.session_state.anomaly_counts.append(num_anomalies)
|
100 |
+
st.session_state.frame_numbers.append(frame_count)
|
101 |
+
|
102 |
+
# Keep only the last 100 frames for trends to avoid memory issues
|
103 |
+
if len(st.session_state.frame_numbers) > 100:
|
104 |
+
st.session_state.frame_numbers.pop(0)
|
105 |
+
st.session_state.anomaly_counts.pop(0)
|
106 |
+
|
107 |
+
# Update live logs (show all logs, scrollable)
|
108 |
+
with logs_placeholder.container():
|
109 |
+
for log in st.session_state.logs[::-1]: # Reverse to show newest first
|
110 |
+
st.write(log)
|
111 |
+
|
112 |
+
# Update last 5 captured events
|
113 |
+
with events_placeholder.container():
|
114 |
+
for log in st.session_state.logs[-5:][::-1]: # Last 5, newest first
|
115 |
+
st.write(log)
|
116 |
+
|
117 |
+
# Update live metrics
|
118 |
+
metrics_placeholder.write(f"""
|
119 |
+
**anomalies:** {num_anomalies}
|
120 |
+
**total_detected:** {st.session_state.total_detected}
|
121 |
+
""")
|
122 |
+
|
123 |
+
# Predictive anomaly detection
|
124 |
+
prediction = predict_anomaly(st.session_state.anomaly_counts)
|
125 |
+
if prediction:
|
126 |
+
metrics_placeholder.warning(f"**Prediction:** Potential issue detected - anomaly spike detected!")
|
127 |
+
|
128 |
+
# Update trends graph
|
129 |
+
fig, ax = plt.subplots()
|
130 |
+
ax.plot(st.session_state.frame_numbers, st.session_state.anomaly_counts, marker='o')
|
131 |
+
ax.set_title("Anomalies Over Time")
|
132 |
+
ax.set_xlabel("Frame")
|
133 |
+
ax.set_ylabel("Count")
|
134 |
+
ax.grid(True)
|
135 |
+
trends_placeholder.pyplot(fig)
|
136 |
+
plt.close(fig)
|
137 |
|
138 |
cap.release()
|
139 |
+
st.success("Video processing completed.")
|
140 |
|
141 |
if __name__ == "__main__":
|
142 |
+
main()
|