Error importing cv2

I’m trying to deploy an app based on this repository:

I get the following error:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Traceback:

File "/usr/local/lib/python3.7/site-packages/streamlit/script_runner.py", line 324, in _run_script
    exec(code, module.__dict__)File "/app/birdspyview/birdspyview.py", line 5, in <module>
    from helpers import calculate_homography, apply_homography_to_image, line_intersect, get_si_from_coordsFile "/app/birdspyview/helpers.py", line 1, in <module>
    import cv2File "/home/appuser/.local/lib/python3.7/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *

I never got this error while deploying locally. Can anyone help?

Hi @rjtavares! Do you know if opencv-python relies on an OS-level package to be installed?

A quick web search indicates you may need to install some additional packages. Could it be that you have these additional packages installed locally?

In Streamlit sharing platform, we support specifying a list of Debian packages to install via apt-get in a packages.txt file. The file is expected to be at the top level alongside requirements.txt. Could you please give it a shot?

Of course, ffmpeg was missing… Seems to be running now. Thanks!

1 Like

Hey @rjtavares I saw your repo. ffmpeg is a requirement for my project as well but streamlit share is not installing it for some reason. My requirements.txt and packages.txt are in a folder where app.py is. requirements.txt install correctly but in the error log I don’t see packages.txt being used at all. Any idea why this is happening?

That is the problem, both files shall be in the root folder of the github project.

Thanks for the response, I’ll give it a try. The thing is I have a Github repo for all my streamlit apps and then subfolders for each app.

This misfeature should have been resolved. Meaning, you should now be able to have the requirements.txt file in the same subdirectory to the app, and if not present, Streamlit sharing will walk up the directory tree to the top-level until it finds a requirements.txt file.

Best,
Randy

Thanks @Franky1 that solved the error but now I am getting another error. I have a file uploader in my app and I am using two files to perform some operations. However, when I am uploading some local file on streamlit share I am getting “No such file or directory”.

This is the link to my app: streamlit_samples/app.py at master · dipam7/streamlit_samples · GitHub

And this is how I upload files in the app

Screenshot of the error:

Hey @randyzwitch requirements.txt was working fine. It was the packages.txt that was not getting detected / installed

Ehm, that debug information is very limited…
I guess:

  • the ffmpeg call was not succesful or
  • the video file is stored in a different folder
    • the video file is in the root folder and your app expect it in the subfolder (or vice versa)

I’m testing with the same files that I am using locally and all of them are failing on streamlit share so this is unlikely. The error also says no such file or directory.

Locally, it works regardless of whether the file is in the root folder or a subfolder.

Is there any way to ssh into the VM and see what’s happening? How do I debug?

@Franky1

I was trying to run os.system(‘ls’) to see the contents. I did not see the videos anywhere. Does file uploader even work with streamlit share? Where do my uploaded files go?

Yes, I assure you that file_uploader works on Streamlit sharing.

Files don’t “go anywhere” per se, they stay in RAM in a BytesIO buffer. To use the uploaded file, you can pass this buffer (in your code, back and over) to whatever function needs to read the bytes. However, note that not all packages accept buffers. If your function happens to be one of them, then you need to save the buffer to an actual file.

Best,
Randy

Thanks @randyzwitch I tried what you suggested but I don’t know why it’s not working for me.

This is what I am trying

back = st.file_uploader("Choose background video", type=["mp4", "mov"])
over = st.file_uploader("Choose overlay video", type=["mp4", "mov"])

if back is not None and over is not None:
    tfile = tempfile.NamedTemporaryFile(delete=False)
    tfile.write(back.read())
    tfile.write(over.read())

The reason I need to write these as videos is because I am using an ffmpeg command like

cmd = f'ffmpeg -i {bk} -i {ov} -map 0:0 -map 1:1 ...'
op = subprocess.check_output(cmd, shell=True)

but it still can’t find the video.

I tried

os.system('ls')
os.system('ls video_overlay')

since my app.py is in the folder video_overlay but I can’t see the videos being written anywhere.

The videos are < 10 secs so I don’t think size is an issue.
I also tried using time.sleep() after the tfile.write() to pause for it to finish writing the video.

Any suggestions

I haven’t used tempfile.NamedTemporaryFile yet, but after looking at the python docs, i don’t think it work in that way.

Hi @dipam7,

The file is written to the /tmp/ directory. Try printing out tfile.name. Here’s a minimal example demonstrating how to write the uploaded video file to disk and play the video using st.video:

import streamlit as st
import tempfile

back = st.file_uploader("Choose background video", type=["mp4", "mov"])

if back:
    tfile = tempfile.NamedTemporaryFile(delete=False)
    tfile.write(back.read())
    st.video(tfile.name)

You can adapt the above :point_up: snippet to your use case :grinning_face_with_smiling_eyes:

Here’s what the app looks like:

Happy Streamlit-ing! :balloon:
Snehan

1 Like

@dipam7
I forked your repo and made a version that also works on streamlit sharing.

However i removed the download option. Because the download function is not a good idea with potentially large files. At least not the way it was done here. The browser may freeze and over 50MB it won’t work at all. You still can right-click on the video and download it that way.

2 Likes

@dipam7
I fiddled a bit with javascript and improved the download method.
A bit of a dirty hack, but it works, also on streamlit sharing.
See in my repo.

https://share.streamlit.io/franky1/streamlit_samples/video_overlay/app.py

1 Like

@Franky1

Thank you so much for your help. I used your app.py and it works well. The output is generated but the download button didn’t work.

2021-05-22 01:52:42.701 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.7/site-packages/streamlit/script_runner.py", line 337, in _run_script
    exec(code, module.__dict__)
  File "/app/streamlit_samples/video_overlay/app.py", line 84, in <module>
    download_button('videodownload.html')
  File "/app/streamlit_samples/video_overlay/app.py", line 19, in download_button
    calc_file = codecs.open(html_file, 'r')
  File "/usr/local/lib/python3.7/codecs.py", line 904, in open
    file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: 'videodownload.html'

Anyway I just wrote an instruction to right click and download the output. Thanks for all the help. Appreciate it.