I have 2D spatio-temporal data that I would like to display as a video. The data is stored as a 3D numpy array and I am trying to display it with st.video whose documentation says it accepts numpy arrays.
However, all I see is a black screen that does not play.
Below is sample code. What is the best way to display this data with Streamlit?
import numpy as np
import streamlit as st
height, width, n_frames = 20, 20, 100
data = np.linspace(0, 255, height*width*n_frames).reshape(height,width,n_frames)
st.video(data)
import numpy as np
import streamlit as st
import cv2
import subprocess
from tempfile import NamedTemporaryFile
height, width, n_frames = 360, 200, 30
frames_per_second = 12
# Generate some data
data = np.linspace(0, 256, height * width * n_frames, dtype=np.uint8).reshape(
n_frames, width, height
)
data[:, 50:100, 50:100] = 255 # A white square
"### `st.video(np.ndarray)`"
st.video(data) ## This will fail
temp_files_kwargs = dict(suffix=".mp4", dir=".")
with (
NamedTemporaryFile(**temp_files_kwargs) as temporal_video,
NamedTemporaryFile(**temp_files_kwargs) as converted_video,
):
out = cv2.VideoWriter(
temporal_video.name,
cv2.VideoWriter_fourcc(*"mp4v"),
frames_per_second,
(height, width),
False,
)
for frame in data:
out.write(frame)
out.release()
## Call ffmpeg to convert to a web-playable codec.
subprocess.call(
args=f"ffmpeg -y -i {temporal_video.name} -c:v libx264 {converted_video.name}".split(),
stdout=subprocess.DEVNULL,
)
## This will show a video
"""### `st.video("encoded_data.mp4")`"""
st.video(converted_video.name)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.