Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 numpy as np
|
| 9 |
+
|
| 10 |
+
# Load models for solar and windmill detection
|
| 11 |
+
from models.solar_model import load_solar_model
|
| 12 |
+
from models.windmill_model import load_windmill_model
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
st.title("Solar Panel and Windmill Fault Detection")
|
| 16 |
+
|
| 17 |
+
# Upload video file
|
| 18 |
+
uploaded_file = st.file_uploader("Upload a Video (MP4 or AVI)", type=["mp4", "avi"])
|
| 19 |
+
|
| 20 |
+
if uploaded_file is not None:
|
| 21 |
+
video_path = "/tmp/" + uploaded_file.name
|
| 22 |
+
with open(video_path, "wb") as f:
|
| 23 |
+
f.write(uploaded_file.read())
|
| 24 |
+
|
| 25 |
+
# Open the video using OpenCV
|
| 26 |
+
cap = cv2.VideoCapture(video_path)
|
| 27 |
+
|
| 28 |
+
# Check if video was opened successfully
|
| 29 |
+
if not cap.isOpened():
|
| 30 |
+
st.error("Error opening video stream or file")
|
| 31 |
+
return
|
| 32 |
+
|
| 33 |
+
# Process frames in real-time
|
| 34 |
+
stframe = st.empty()
|
| 35 |
+
|
| 36 |
+
# Choose fault detection type
|
| 37 |
+
choice = st.selectbox("Choose Fault Detection", ["Solar Panel", "Windmill"])
|
| 38 |
+
model = None
|
| 39 |
+
|
| 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 |
+
# Convert the frame to RGB (Streamlit uses RGB)
|
| 51 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 52 |
+
|
| 53 |
+
# Preprocess the frame and detect faults
|
| 54 |
+
faults = detect_faults_solar(model, frame_rgb) if choice == "Solar Panel" else detect_faults_windmill(model, frame_rgb)
|
| 55 |
+
|
| 56 |
+
# Show faults on the frame
|
| 57 |
+
for fault in faults:
|
| 58 |
+
# For example, draw bounding boxes for faults on the frame
|
| 59 |
+
x, y = int(fault['location'][0]), int(fault['location'][1])
|
| 60 |
+
cv2.putText(frame, f"Fault: {fault['type']}", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
|
| 61 |
+
cv2.rectangle(frame, (x-20, y-20), (x+80, y+40), (0, 0, 255), 2)
|
| 62 |
+
|
| 63 |
+
# Update the live feed with the processed frame
|
| 64 |
+
stframe.image(frame_rgb, channels="RGB", use_column_width=True)
|
| 65 |
+
|
| 66 |
+
cap.release()
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
main()
|