Sirivennela commited on
Commit
0885772
·
verified ·
1 Parent(s): 8f9d69f

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +67 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,69 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
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()