Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +109 -32
src/streamlit_app.py
CHANGED
@@ -1,40 +1,117 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
""
|
7 |
-
|
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 |
-
|
14 |
-
"""
|
15 |
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import pandas as pd
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
from services.video_service import get_video_frame
|
7 |
+
from services.detection_service import detect_objects
|
8 |
+
from services.fault_service import detect_pole_faults
|
9 |
+
from services.salesforce_dispatcher import send_to_salesforce
|
10 |
|
11 |
+
st.title("Pole Fault Detection")
|
12 |
+
st.write("Upload a video (.mp4) or image (.jpg, .png) to detect pole faults.")
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# File uploader
|
15 |
+
uploaded_file = st.file_uploader("Choose a file", type=["mp4", "jpg", "png"])
|
16 |
|
17 |
+
if uploaded_file is not None:
|
18 |
+
# Save uploaded file to temporary location
|
19 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{uploaded_file.name.split('.')[-1]}") as tmp_file:
|
20 |
+
tmp_file.write(uploaded_file.read())
|
21 |
+
file_path = tmp_file.name
|
22 |
|
23 |
+
if uploaded_file.type.startswith("video"):
|
24 |
+
# Process video
|
25 |
+
st.subheader("Processed Video")
|
26 |
+
video_placeholder = st.empty()
|
27 |
+
fault_table = st.empty()
|
28 |
+
frame_gen = get_video_frame(file_path)
|
29 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
30 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
31 |
+
cap = cv2.VideoCapture(file_path)
|
32 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
33 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
34 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
35 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
36 |
+
faults_list = []
|
37 |
|
38 |
+
for frame in frame_gen:
|
39 |
+
# Save frame temporarily for detection
|
40 |
+
cv2.imwrite("temp.jpg", frame)
|
41 |
+
detections = detect_objects("temp.jpg")
|
42 |
+
faults = detect_pole_faults("temp.jpg")
|
43 |
+
alert_payload = {
|
44 |
+
"detections": detections,
|
45 |
+
"faults": bool(faults),
|
46 |
+
"fault_details": faults
|
47 |
+
}
|
48 |
+
# Send to Salesforce
|
49 |
+
send_to_salesforce(alert_payload)
|
50 |
+
faults_list.extend(faults)
|
51 |
+
# Annotate frame (basic text overlay for faults)
|
52 |
+
for fault in faults:
|
53 |
+
cv2.putText(
|
54 |
+
frame,
|
55 |
+
f"{fault['fault_type']} ({fault['confidence']:.2f})",
|
56 |
+
(50, 50),
|
57 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
58 |
+
1,
|
59 |
+
(0, 0, 255),
|
60 |
+
2
|
61 |
+
)
|
62 |
+
out.write(frame)
|
63 |
+
# Display frame in Streamlit
|
64 |
+
video_placeholder.image(frame, channels="BGR")
|
65 |
|
66 |
+
out.release()
|
67 |
+
cap.release()
|
68 |
+
# Display final video
|
69 |
+
st.video(output_path)
|
70 |
+
# Display faults table
|
71 |
+
if faults_list:
|
72 |
+
df = pd.DataFrame(faults_list)
|
73 |
+
fault_table.subheader("Detected Faults")
|
74 |
+
fault_table.dataframe(df)
|
75 |
+
else:
|
76 |
+
fault_table.write("No faults detected.")
|
77 |
+
# Clean up
|
78 |
+
os.remove(file_path)
|
79 |
+
os.remove(output_path)
|
80 |
+
os.remove("temp.jpg")
|
81 |
|
82 |
+
else:
|
83 |
+
# Process image
|
84 |
+
st.subheader("Processed Image")
|
85 |
+
image = cv2.imread(file_path)
|
86 |
+
cv2.imwrite("temp.jpg", image)
|
87 |
+
detections = detect_objects("temp.jpg")
|
88 |
+
faults = detect_pole_faults("temp.jpg")
|
89 |
+
alert_payload = {
|
90 |
+
"detections": detections,
|
91 |
+
"faults": bool(faults),
|
92 |
+
"fault_details": faults
|
93 |
+
}
|
94 |
+
# Send to Salesforce
|
95 |
+
send_to_salesforce(alert_payload)
|
96 |
+
# Annotate image (basic text overlay for faults)
|
97 |
+
for fault in faults:
|
98 |
+
cv2.putText(
|
99 |
+
image,
|
100 |
+
f"{fault['fault_type']} ({fault['confidence']:.2f})",
|
101 |
+
(50, 50),
|
102 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
103 |
+
1,
|
104 |
+
(0, 0, 255),
|
105 |
+
2
|
106 |
+
)
|
107 |
+
st.image(image, channels="BGR", caption="Processed Image")
|
108 |
+
# Display faults table
|
109 |
+
if faults:
|
110 |
+
st.subheader("Detected Faults")
|
111 |
+
df = pd.DataFrame(faults)
|
112 |
+
st.dataframe(df)
|
113 |
+
else:
|
114 |
+
st.write("No faults detected.")
|
115 |
+
# Clean up
|
116 |
+
os.remove(file_path)
|
117 |
+
os.remove("temp.jpg")
|