Spaces:
Sleeping
Sleeping
Commit
·
209fb19
1
Parent(s):
8b9234c
Update inference.py
Browse files- inference.py +91 -85
inference.py
CHANGED
@@ -41,6 +41,68 @@ def load_historical(fpath):
|
|
41 |
|
42 |
st.set_page_config(layout="wide")
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Define the main function to run the Streamlit app
|
45 |
def run_app():
|
46 |
# Set Streamlit options
|
@@ -57,92 +119,36 @@ def run_app():
|
|
57 |
# Load historical herring
|
58 |
df_historical_herring = load_historical(fpath="herring_count_all.csv")
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
##
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
)
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
st.video(video_bytes)
|
88 |
-
st.subheader("Upload your own video...")
|
89 |
-
|
90 |
-
# Initialize accepted file types for upload
|
91 |
-
img_types = ["jpg", "png", "jpeg"]
|
92 |
-
video_types = ["mp4", "avi"]
|
93 |
-
|
94 |
-
# Allow user to upload an image or video file
|
95 |
-
uploaded_file = st.file_uploader("Select an image or video file...", type=img_types + video_types)
|
96 |
-
|
97 |
-
# Display the uploaded file
|
98 |
-
if uploaded_file is not None:
|
99 |
-
if str(uploaded_file.type).split("/")[-1] in img_types:
|
100 |
-
# Display uploaded image
|
101 |
-
image = Image.open(uploaded_file)
|
102 |
-
st.image(image, caption="Uploaded image", use_column_width=True)
|
103 |
-
|
104 |
-
# TBD: Inference code to run and display for single image
|
105 |
-
|
106 |
-
elif str(uploaded_file.type).split("/")[-1] in video_types:
|
107 |
-
# Display uploaded video
|
108 |
-
st.video(uploaded_file)
|
109 |
-
|
110 |
-
# Convert streamlit video object to OpenCV format to run inferences
|
111 |
-
tfile = tempfile.NamedTemporaryFile(delete=False)
|
112 |
-
tfile.write(uploaded_file.read())
|
113 |
-
vf = cv.VideoCapture(tfile.name)
|
114 |
-
|
115 |
-
# Run inference on the uploaded video
|
116 |
-
with st.spinner("Running inference..."):
|
117 |
-
frames, counts, timestamps = inference.main(vf)
|
118 |
-
logging.info("INFO: Completed running inference on frames")
|
119 |
-
st.balloons()
|
120 |
-
|
121 |
-
# Convert OpenCV Numpy frames in-memory to IO Bytes for streamlit
|
122 |
-
streamlit_video_file = frames_to_video(frames=frames, fps=11)
|
123 |
-
|
124 |
-
# Show processed video and provide download button
|
125 |
-
st.video(streamlit_video_file)
|
126 |
-
st.download_button(
|
127 |
-
label="Download processed video",
|
128 |
-
data=streamlit_video_file,
|
129 |
-
mime="mp4",
|
130 |
-
file_name="processed_video.mp4",
|
131 |
-
)
|
132 |
-
|
133 |
-
# Create dataframe for fish counts and timestamps
|
134 |
-
df_counts_time = pd.DataFrame(
|
135 |
-
data={"fish_count": counts, "timestamps": timestamps[1:]}
|
136 |
-
)
|
137 |
-
|
138 |
-
# Display fish count vs. timestamp chart
|
139 |
-
st.altair_chart(
|
140 |
-
plot_count_date(dataframe=df_counts_time),
|
141 |
-
use_container_width=True,
|
142 |
-
)
|
143 |
-
|
144 |
-
else:
|
145 |
-
st.write("No file uploaded")
|
146 |
|
147 |
# Run the app if the script is executed directly
|
148 |
if __name__ == "__main__":
|
|
|
41 |
|
42 |
st.set_page_config(layout="wide")
|
43 |
|
44 |
+
|
45 |
+
def process_uploaded_file():
|
46 |
+
st.subheader("Upload your own video...")
|
47 |
+
|
48 |
+
# Initialize accepted file types for upload
|
49 |
+
img_types = ["jpg", "png", "jpeg"]
|
50 |
+
video_types = ["mp4", "avi"]
|
51 |
+
|
52 |
+
# Allow user to upload an image or video file
|
53 |
+
uploaded_file = st.file_uploader("Select an image or video file...", type=img_types + video_types)
|
54 |
+
|
55 |
+
# Display the uploaded file
|
56 |
+
if uploaded_file is not None:
|
57 |
+
if str(uploaded_file.type).split("/")[-1] in img_types:
|
58 |
+
# Display uploaded image
|
59 |
+
image = Image.open(uploaded_file)
|
60 |
+
st.image(image, caption="Uploaded image", use_column_width=True)
|
61 |
+
|
62 |
+
# TBD: Inference code to run and display for single image
|
63 |
+
|
64 |
+
elif str(uploaded_file.type).split("/")[-1] in video_types:
|
65 |
+
# Display uploaded video
|
66 |
+
st.video(uploaded_file)
|
67 |
+
|
68 |
+
# Convert streamlit video object to OpenCV format to run inferences
|
69 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
70 |
+
tfile.write(uploaded_file.read())
|
71 |
+
vf = cv.VideoCapture(tfile.name)
|
72 |
+
|
73 |
+
# Run inference on the uploaded video
|
74 |
+
with st.spinner("Running inference..."):
|
75 |
+
frames, counts, timestamps = inference.main(vf)
|
76 |
+
logging.info("INFO: Completed running inference on frames")
|
77 |
+
st.balloons()
|
78 |
+
|
79 |
+
# Convert OpenCV Numpy frames in-memory to IO Bytes for streamlit
|
80 |
+
streamlit_video_file = frames_to_video(frames=frames, fps=11)
|
81 |
+
|
82 |
+
# Show processed video and provide download button
|
83 |
+
st.video(streamlit_video_file)
|
84 |
+
st.download_button(
|
85 |
+
label="Download processed video",
|
86 |
+
data=streamlit_video_file,
|
87 |
+
mime="mp4",
|
88 |
+
file_name="processed_video.mp4",
|
89 |
+
)
|
90 |
+
|
91 |
+
# Create dataframe for fish counts and timestamps
|
92 |
+
df_counts_time = pd.DataFrame(
|
93 |
+
data={"fish_count": counts, "timestamps": timestamps[1:]}
|
94 |
+
)
|
95 |
+
|
96 |
+
# Display fish count vs. timestamp chart
|
97 |
+
st.altair_chart(
|
98 |
+
plot_count_date(dataframe=df_counts_time),
|
99 |
+
use_container_width=True,
|
100 |
+
)
|
101 |
+
|
102 |
+
else:
|
103 |
+
st.write("No file uploaded")
|
104 |
+
|
105 |
+
|
106 |
# Define the main function to run the Streamlit app
|
107 |
def run_app():
|
108 |
# Set Streamlit options
|
|
|
119 |
# Load historical herring
|
120 |
df_historical_herring = load_historical(fpath="herring_count_all.csv")
|
121 |
|
122 |
+
main_tab, upload_tab = st.tabs(["Analysis", "Upload video for analysis"])
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
with main_tab:
|
127 |
+
# Create two columns for layout
|
128 |
+
col1, col2 = st.columns(2)
|
129 |
+
## Col1 #########################################
|
130 |
+
with col1:
|
131 |
+
## Initial visualizations
|
132 |
+
# Plot historical data
|
133 |
+
st.altair_chart(
|
134 |
+
plot_historical_data(df_historical_herring),
|
135 |
+
use_container_width=True,
|
136 |
+
)
|
137 |
+
|
138 |
+
# Display map of fishery locations
|
139 |
+
st.subheader("Map of Fishery Locations")
|
140 |
+
st.map(
|
141 |
+
pd.DataFrame(
|
142 |
+
np.random.randn(5, 2) / [50, 50] + [42.41, -71.38],
|
143 |
+
columns=["lat", "lon"],
|
144 |
+
),use_container_width=True
|
145 |
)
|
146 |
+
with col2:
|
147 |
+
# Display example processed video
|
148 |
+
st.subheader("Example of processed video")
|
149 |
+
st.video(video_bytes)
|
150 |
+
with upload_tab:
|
151 |
+
process_uploaded_file()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
# Run the app if the script is executed directly
|
154 |
if __name__ == "__main__":
|