Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,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 |
# Custom CSS for styling to match the screenshot
|
14 |
st.markdown(
|
@@ -19,7 +21,6 @@ st.markdown(
|
|
19 |
font-size: 24px;
|
20 |
font-weight: bold;
|
21 |
color: #333;
|
22 |
-
F;
|
23 |
}
|
24 |
.status {
|
25 |
text-align: center;
|
@@ -50,6 +51,11 @@ F;
|
|
50 |
color: #333;
|
51 |
margin-bottom: 5px;
|
52 |
}
|
|
|
|
|
|
|
|
|
|
|
53 |
</style>
|
54 |
""",
|
55 |
unsafe_allow_html=True
|
@@ -64,6 +70,13 @@ if 'frame_numbers' not in st.session_state:
|
|
64 |
st.session_state.frame_numbers = []
|
65 |
if 'total_detected' not in st.session_state:
|
66 |
st.session_state.total_detected = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
def main():
|
69 |
# Header
|
@@ -148,15 +161,28 @@ def main():
|
|
148 |
num_anomalies = len(faults)
|
149 |
|
150 |
# Draw bounding boxes and labels
|
|
|
151 |
for fault in faults:
|
152 |
x, y = int(fault['location'][0]), int(fault['location'][1])
|
153 |
-
cv2.rectangle(
|
154 |
-
cv2.putText(
|
155 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
# Update video feed with timestamp
|
158 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
159 |
-
video_placeholder.image(
|
160 |
|
161 |
# Update logs and metrics
|
162 |
log_entry = f"{timestamp} - Frame {frame_count} - Anomalies: {num_anomalies}"
|
@@ -165,20 +191,23 @@ def main():
|
|
165 |
st.session_state.anomaly_counts.append(num_anomalies)
|
166 |
st.session_state.frame_numbers.append(frame_count)
|
167 |
|
168 |
-
# Keep only the last 100 frames for trends
|
169 |
if len(st.session_state.frame_numbers) > 100:
|
170 |
st.session_state.frame_numbers.pop(0)
|
171 |
st.session_state.anomaly_counts.pop(0)
|
|
|
|
|
172 |
|
173 |
# Update live logs
|
174 |
with logs_placeholder.container():
|
175 |
for log in st.session_state.logs[::-1]:
|
176 |
st.markdown(f'<div class="log-entry">{log}</div>', unsafe_allow_html=True)
|
177 |
|
178 |
-
# Update last 5 captured events
|
179 |
with events_placeholder.container():
|
180 |
-
for
|
181 |
-
st.markdown(f'<div class="log-entry">{log}</div>', unsafe_allow_html=True)
|
|
|
182 |
|
183 |
# Update live metrics
|
184 |
metrics_placeholder.markdown(
|
|
|
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 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
|
15 |
# Custom CSS for styling to match the screenshot
|
16 |
st.markdown(
|
|
|
21 |
font-size: 24px;
|
22 |
font-weight: bold;
|
23 |
color: #333;
|
|
|
24 |
}
|
25 |
.status {
|
26 |
text-align: center;
|
|
|
51 |
color: #333;
|
52 |
margin-bottom: 5px;
|
53 |
}
|
54 |
+
.snapshot-img {
|
55 |
+
max-width: 100%;
|
56 |
+
height: auto;
|
57 |
+
margin-bottom: 10px;
|
58 |
+
}
|
59 |
</style>
|
60 |
""",
|
61 |
unsafe_allow_html=True
|
|
|
70 |
st.session_state.frame_numbers = []
|
71 |
if 'total_detected' not in st.session_state:
|
72 |
st.session_state.total_detected = 0
|
73 |
+
if 'snapshots' not in st.session_state:
|
74 |
+
st.session_state.snapshots = []
|
75 |
+
|
76 |
+
# Create snapshots directory if it doesn't exist
|
77 |
+
SNAPSHOT_FOLDER = "./snapshots"
|
78 |
+
if not os.path.exists(SNAPSHOT_FOLDER):
|
79 |
+
os.makedirs(SNAPSHOT_FOLDER)
|
80 |
|
81 |
def main():
|
82 |
# Header
|
|
|
161 |
num_anomalies = len(faults)
|
162 |
|
163 |
# Draw bounding boxes and labels
|
164 |
+
annotated_frame = frame_rgb.copy()
|
165 |
for fault in faults:
|
166 |
x, y = int(fault['location'][0]), int(fault['location'][1])
|
167 |
+
cv2.rectangle(annotated_frame, (x-30, y-30), (x+30, y+30), (255, 0, 0), 2)
|
168 |
+
cv2.putText(annotated_frame, f"{fault['type']}", (x, y-40),
|
169 |
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0), 2)
|
170 |
|
171 |
+
# Save snapshot if faults are detected
|
172 |
+
if faults:
|
173 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
174 |
+
fault_types = "_".join([fault['type'].replace(" ", "_") for fault in faults])
|
175 |
+
snapshot_filename = f"snapshot_{timestamp}_frame_{frame_count}_{fault_types}.png"
|
176 |
+
snapshot_path = os.path.join(SNAPSHOT_FOLDER, snapshot_filename)
|
177 |
+
cv2.imwrite(snapshot_path, cv2.cvtColor(annotated_frame, cv2.COLOR_RGB2BGR))
|
178 |
+
st.session_state.snapshots.append({
|
179 |
+
"path": snapshot_path,
|
180 |
+
"log": f"{timestamp} - Frame {frame_count} - Anomalies: {num_anomalies} ({fault_types})"
|
181 |
+
})
|
182 |
+
|
183 |
# Update video feed with timestamp
|
184 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
185 |
+
video_placeholder.image(annotated_frame, channels="RGB", caption=f"{timestamp}")
|
186 |
|
187 |
# Update logs and metrics
|
188 |
log_entry = f"{timestamp} - Frame {frame_count} - Anomalies: {num_anomalies}"
|
|
|
191 |
st.session_state.anomaly_counts.append(num_anomalies)
|
192 |
st.session_state.frame_numbers.append(frame_count)
|
193 |
|
194 |
+
# Keep only the last 100 frames for trends and last 5 snapshots
|
195 |
if len(st.session_state.frame_numbers) > 100:
|
196 |
st.session_state.frame_numbers.pop(0)
|
197 |
st.session_state.anomaly_counts.pop(0)
|
198 |
+
if len(st.session_state.snapshots) > 5:
|
199 |
+
st.session_state.snapshots.pop(0)
|
200 |
|
201 |
# Update live logs
|
202 |
with logs_placeholder.container():
|
203 |
for log in st.session_state.logs[::-1]:
|
204 |
st.markdown(f'<div class="log-entry">{log}</div>', unsafe_allow_html=True)
|
205 |
|
206 |
+
# Update last 5 captured events with snapshots
|
207 |
with events_placeholder.container():
|
208 |
+
for snapshot in st.session_state.snapshots[::-1]:
|
209 |
+
st.markdown(f'<div class="log-entry">{snapshot["log"]}</div>', unsafe_allow_html=True)
|
210 |
+
st.image(snapshot["path"], caption="Fault Snapshot", use_column_width=True)
|
211 |
|
212 |
# Update live metrics
|
213 |
metrics_placeholder.markdown(
|