Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ 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
|
@@ -82,6 +83,30 @@ if 'paused' not in st.session_state:
|
|
82 |
SNAPSHOT_FOLDER = "./snapshots"
|
83 |
os.makedirs(SNAPSHOT_FOLDER, exist_ok=True)
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# Core monitor function
|
86 |
def monitor_feed(video_path, detection_type, model):
|
87 |
if st.session_state.paused and st.session_state.last_frame is not None:
|
@@ -98,6 +123,8 @@ def monitor_feed(video_path, detection_type, model):
|
|
98 |
return None, None, None, None, None
|
99 |
|
100 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
|
|
|
|
101 |
faults = detect_faults_solar(model, frame_rgb) if detection_type == "Solar Panel" else detect_faults_windmill(model, frame_rgb)
|
102 |
num_anomalies = len(faults)
|
103 |
|
|
|
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
|
|
|
83 |
SNAPSHOT_FOLDER = "./snapshots"
|
84 |
os.makedirs(SNAPSHOT_FOLDER, exist_ok=True)
|
85 |
|
86 |
+
# Function to resize and pad image to 640x640 while preserving aspect ratio
|
87 |
+
def preprocess_image(image, target_size=(640, 640)):
|
88 |
+
h, w = image.shape[:2]
|
89 |
+
target_h, target_w = target_size
|
90 |
+
|
91 |
+
# Calculate scaling factor to maintain aspect ratio
|
92 |
+
scale = min(target_w / w, target_h / h)
|
93 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
94 |
+
|
95 |
+
# Resize image
|
96 |
+
resized_image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
|
97 |
+
|
98 |
+
# Create a blank 640x640 image
|
99 |
+
padded_image = np.zeros((target_h, target_w, 3), dtype=np.uint8)
|
100 |
+
|
101 |
+
# Calculate padding offsets
|
102 |
+
top = (target_h - new_h) // 2
|
103 |
+
left = (target_w - new_w) // 2
|
104 |
+
|
105 |
+
# Place resized image in the center
|
106 |
+
padded_image[top:top + new_h, left:left + new_w] = resized_image
|
107 |
+
|
108 |
+
return padded_image
|
109 |
+
|
110 |
# Core monitor function
|
111 |
def monitor_feed(video_path, detection_type, model):
|
112 |
if st.session_state.paused and st.session_state.last_frame is not None:
|
|
|
123 |
return None, None, None, None, None
|
124 |
|
125 |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
126 |
+
# Preprocess frame to 640x640
|
127 |
+
frame_rgb = preprocess_image(frame_rgb, target_size=(640, 640))
|
128 |
faults = detect_faults_solar(model, frame_rgb) if detection_type == "Solar Panel" else detect_faults_windmill(model, frame_rgb)
|
129 |
num_anomalies = len(faults)
|
130 |
|