I am building a video classification app in streamlit and trying to do…
Get query video via st.file_uploader()
Convert it to numpy array or torch tensor
Preview it with st.video
Run my model.
My app is working as expected but my current implementation requires the app to copy the uploaded file into working directory and passing the path of copied one to st.video to preview it.
uploaded_video = st.file_uploader("Choose video", type=["mp4", "avi"])
if uploaded_video is not None: # run only when user uploads video
vid = uploaded_video.name
with open(vid, mode="wb") as f:
f.write(uploaded_video.read())
query = (glob("*.mp4") + glob("./*.avi"))[0]
video_tensor = torchvision.io.read_video(query)
st.video(query)
I am feeling it pretty wasteful and looking for the way to directory convert uploadedFile object into numpy array. Is there anyway to do so?
If there is such way, I think it’s useful for others dealing with video recognition task so wishing to get some help. Thanks in advance.
I’d view the solution a different way and give the user a selection of video to choose from:
import os
from os import environ as osenv
import streamlit as st
import torchvision
from glob2 import glob
is_local = st.checkbox('Toggle local/remote (local is default)', value=True)
# Generate videos list
if is_local:
# IF THE APP IS BEING RUN LOCALLY
# You could get the user to provide their base folder - here I'm pointing
# to my default Windows videos folder
base_dir = st.text_input('Set your videos base path', osenv.get('HOMEPATH'))
path = os.path.join(base_dir, "Videos")
files = glob(path + r"\*.mp4") + glob(path + r"\*.avi")
else:
# IF THE APP IS HOSTED ON A SERVER
# Generate the videos selection on the server - here I've
# manually constructed a list of Streamlit videos
files = [
'https://www.youtube.com/watch?v=Jte0Reue7z8',
'https://www.youtube.com/watch?v=sCvdt79asrE',
'https://www.youtube.com/watch?v=MTaL_1UCb2g'
]
video = st.selectbox('Make selection', files)
if video:
video_tensor = torchvision.io.read_video(video)
st.video(video)
else:
st.write('No video selected')
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.