DSatishchandra commited on
Commit
59c6524
·
verified ·
1 Parent(s): 51ef337

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -8
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import numpy as np
5
  from datetime import datetime
6
  import matplotlib.pyplot as plt
7
- import time # Added import for time module
8
  from services.detection_service import detect_faults_solar, detect_faults_windmill
9
  from services.anomaly_service import track_anomalies, predict_anomaly
10
  from models.solar_model import load_solar_model
@@ -68,7 +68,7 @@ st.markdown(
68
  # Initialize session state
69
  if 'paused' not in st.session_state:
70
  st.session_state.paused = False
71
- st.session_state.frame_rate = 1.0
72
  st.session_state.frame_count = 0
73
  st.session_state.logs = []
74
  st.session_state.anomaly_counts = []
@@ -264,7 +264,7 @@ def main():
264
  with col6:
265
  resume_btn = st.button("▶️ Resume")
266
  with col7:
267
- frame_rate = st.slider("Frame Interval (seconds)", 0.0005, 0.5, st.session_state.frame_rate)
268
 
269
  # Handle button clicks and slider
270
  if pause_btn:
@@ -279,10 +279,46 @@ def main():
279
  video_path = os.path.join(VIDEO_FOLDER, video_file)
280
  while True:
281
  if st.session_state.paused:
282
- frame, metrics, logs, chart, snapshots = monitor_feed(video_path, detection_type, model)
283
- if frame is None:
284
- st.success("Video processing completed.")
285
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  else:
287
  frame, metrics, logs, chart, snapshots = monitor_feed(video_path, detection_type, model)
288
  if frame is None:
@@ -291,7 +327,6 @@ def main():
291
 
292
  # Update UI
293
  video_placeholder.image(frame, channels="RGB", width=640)
294
- # Fix for f-string backslash issue
295
  metrics_formatted = metrics.replace("\n", "<br>")
296
  metrics_placeholder.markdown(
297
  f'<div class="metrics-text">{metrics_formatted}</div>',
 
4
  import numpy as np
5
  from datetime import datetime
6
  import matplotlib.pyplot as plt
7
+ import time
8
  from services.detection_service import detect_faults_solar, detect_faults_windmill
9
  from services.anomaly_service import track_anomalies, predict_anomaly
10
  from models.solar_model import load_solar_model
 
68
  # Initialize session state
69
  if 'paused' not in st.session_state:
70
  st.session_state.paused = False
71
+ st.session_state.frame_rate = 0.2 # Adjusted default to reduce flicker
72
  st.session_state.frame_count = 0
73
  st.session_state.logs = []
74
  st.session_state.anomaly_counts = []
 
264
  with col6:
265
  resume_btn = st.button("▶️ Resume")
266
  with col7:
267
+ frame_rate = st.slider("Frame Interval (seconds)", 0.1, 1.0, st.session_state.frame_rate) # Adjusted range
268
 
269
  # Handle button clicks and slider
270
  if pause_btn:
 
279
  video_path = os.path.join(VIDEO_FOLDER, video_file)
280
  while True:
281
  if st.session_state.paused:
282
+ # Skip processing if paused and last frame exists
283
+ if st.session_state.last_frame is not None:
284
+ frame, metrics, logs, chart, snapshots = monitor_feed(video_path, detection_type, model)
285
+ if frame is None:
286
+ st.success("Video processing completed.")
287
+ break
288
+ # Update UI only if necessary
289
+ video_placeholder.image(frame, channels="RGB", width=640)
290
+ metrics_formatted = metrics.replace("\n", "<br>")
291
+ metrics_placeholder.markdown(
292
+ f'<div class="metrics-text">{metrics_formatted}</div>',
293
+ unsafe_allow_html=True
294
+ )
295
+ logs_formatted = logs.replace("\n", "</div><div class='log-entry'>")
296
+ logs_placeholder.markdown(
297
+ f'<div class="log-entry">{logs_formatted}</div>',
298
+ unsafe_allow_html=True
299
+ )
300
+ trends_placeholder.image(chart)
301
+ with gallery_placeholder.container():
302
+ cols = st.columns(5)
303
+ for i, col in enumerate(cols):
304
+ with col:
305
+ if i < len(snapshots):
306
+ st.image(snapshots[i], width=100)
307
+ st.markdown(f'<div class="log-entry">{st.session_state.snapshots[i]["log"]}</div>', unsafe_allow_html=True)
308
+ else:
309
+ st.empty()
310
+ prediction = predict_anomaly(st.session_state.anomaly_counts)
311
+ if prediction:
312
+ prediction_placeholder.warning("**Prediction:** Potential issue detected - anomaly spike detected!")
313
+ else:
314
+ prediction_placeholder.empty()
315
+ time.sleep(st.session_state.frame_rate)
316
+ continue # Avoid rerun when paused
317
+ else:
318
+ frame, metrics, logs, chart, snapshots = monitor_feed(video_path, detection_type, model)
319
+ if frame is None:
320
+ st.success("Video processing completed.")
321
+ break
322
  else:
323
  frame, metrics, logs, chart, snapshots = monitor_feed(video_path, detection_type, model)
324
  if frame is None:
 
327
 
328
  # Update UI
329
  video_placeholder.image(frame, channels="RGB", width=640)
 
330
  metrics_formatted = metrics.replace("\n", "<br>")
331
  metrics_placeholder.markdown(
332
  f'<div class="metrics-text">{metrics_formatted}</div>',