Sirivennela commited on
Commit
91a0ae1
ยท
verified ยท
1 Parent(s): 69fa285

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +23 -60
src/streamlit_app.py CHANGED
@@ -1,69 +1,32 @@
1
  import streamlit as st
2
- import cv2
3
- import torch
4
- import numpy as np
5
- from PIL import Image
6
 
7
- # Load pre-trained YOLOv5 model (change to your model if custom)
8
- @st.cache_resource
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
- # Extract frames from the video
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
- # Process frames with YOLOv5 model
27
- def process_frames_with_model(frames, model):
28
- results = []
29
- for frame in frames:
30
- img = Image.fromarray(frame)
31
- results.append(model(img)) # Use YOLOv5 to detect objects
32
- return results
33
 
34
- # Display results
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
- # Main function for Streamlit interface
43
- def main():
44
- st.title("Solar Fault Detection - Thermal Image Analysis")
45
 
46
- st.sidebar.header("Upload Video")
47
- uploaded_video = st.sidebar.file_uploader("Choose a video file", type=["mp4", "mov"])
 
 
 
48
 
49
- if uploaded_video is not None:
50
- video_path = "/mnt/data/uploaded_video.mp4"
51
- with open(video_path, "wb") as f:
52
- f.write(uploaded_video.read())
53
- st.video(uploaded_video) # Display video preview
54
 
55
- # Load YOLOv5 model (or your custom model)
56
- model = load_model()
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.")