Reading uploaded video from st.file_uploader as numpy array or torch tensor

I am building a video classification app in streamlit and trying to do…

  1. Get query video via st.file_uploader()
  2. Convert it to numpy array or torch tensor
  3. Preview it with st.video
  4. 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.

Hi,

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')