Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +23 -60
src/streamlit_app.py
CHANGED
|
@@ -1,69 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def load_model():
|
| 10 |
-
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # YOLOv5 small model (you can use your custom model here)
|
| 11 |
-
return model
|
| 12 |
|
| 13 |
-
|
| 14 |
-
def extract_frames(video_path):
|
| 15 |
-
cap = cv2.VideoCapture(video_path)
|
| 16 |
-
frames = []
|
| 17 |
-
while True:
|
| 18 |
-
ret, frame = cap.read()
|
| 19 |
-
if not ret:
|
| 20 |
-
break
|
| 21 |
-
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert to RGB
|
| 22 |
-
frames.append(rgb_frame)
|
| 23 |
-
cap.release()
|
| 24 |
-
return frames
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
results.append(model(img)) # Use YOLOv5 to detect objects
|
| 32 |
-
return results
|
| 33 |
|
| 34 |
-
|
| 35 |
-
def display_results(results):
|
| 36 |
-
for idx, result in enumerate(results):
|
| 37 |
-
st.subheader(f"Frame {idx+1}")
|
| 38 |
-
st.image(result.render()[0]) # Render and display frame with bounding boxes
|
| 39 |
-
st.write("Detected Faults:")
|
| 40 |
-
st.write(result.pandas().xywh) # Display predictions as a table
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
st.title("Solar Fault Detection - Thermal Image Analysis")
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
with open(video_path, "wb") as f:
|
| 52 |
-
f.write(uploaded_video.read())
|
| 53 |
-
st.video(uploaded_video) # Display video preview
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# Extract frames from the video
|
| 59 |
-
frames = extract_frames(video_path)
|
| 60 |
-
|
| 61 |
-
# Process frames with the YOLOv5 model
|
| 62 |
-
results = process_frames_with_model(frames, model)
|
| 63 |
-
|
| 64 |
-
# Display results on the Streamlit app
|
| 65 |
-
display_results(results)
|
| 66 |
-
|
| 67 |
-
# Run the app
|
| 68 |
-
if __name__ == "__main__":
|
| 69 |
-
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from video_utils import extract_frames, annotate_video
|
| 3 |
+
from model import predict_fault
|
| 4 |
+
import os
|
|
|
|
| 5 |
|
| 6 |
+
st.title("๐ Solar Panel Fault Detection from Video")
|
| 7 |
+
st.write("Upload a drone video of solar panels to detect faults: **cracked**, **dusted**, **shaded**, **overheated**.")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
uploaded_file = st.file_uploader("Upload a solar panel video", type=["mp4", "avi"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
if uploaded_file is not None:
|
| 12 |
+
# Save uploaded video temporarily
|
| 13 |
+
video_path = os.path.join("temp_video.mp4")
|
| 14 |
+
with open(video_path, "wb") as f:
|
| 15 |
+
f.write(uploaded_file.read())
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
st.video(video_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
st.write("๐ Processing video...")
|
| 20 |
+
frames = extract_frames(video_path, interval=30) # every 30th frame (~1/sec)
|
|
|
|
| 21 |
|
| 22 |
+
predictions = {}
|
| 23 |
+
for frame_idx, image in frames:
|
| 24 |
+
label = predict_fault(image)
|
| 25 |
+
predictions[frame_idx] = label
|
| 26 |
+
st.image(image, caption=f"Frame {frame_idx}: {label}", use_column_width=True)
|
| 27 |
|
| 28 |
+
st.write("๐๏ธ Generating annotated video...")
|
| 29 |
+
output_video_path = annotate_video(video_path, predictions)
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
st.video(output_video_path)
|
| 32 |
+
st.success("โ
Detection complete.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|