Import cv2 error

I have a local streamlit app on image classification running. I tried to deploy on streamlit cloud but received this error on importing cv2.

What can I do to fix it ?

ImportError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)File "app.py", line 6, in <module>
    import cv2

Did you create a requirements.txt file with

opencv-python

in it?

Yes I did.

Can you try it with opencv-python-headless as suggested here Unable to import cv2 - #2 by Kanak?

I tried with headless and the issue persists

Hi @yeongnam :wave:

Could you please share the link to your GitHub repo? That way we can check if there’s an issue in your requirements file that’s preventing the Python dependencies from getting installed on Cloud.

Plse see my repo at github:

I get below error message:

FileNotFoundError: This app has encountered an error. The original error message is redacted to prevent data leaks. Full error details have been recorded in the logs (if you’re on Streamlit Cloud, click on ‘Manage app’ in the lower right of your app).

Traceback:

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)File "app.py", line 21, in <module>
    img_to_bytes("Supermarket.png")File "app.py", line 15, in img_to_bytes
    img_bytes = Path(img_path).read_bytes()File "/usr/local/lib/python3.9/pathlib.py", line 1259, in read_bytes
    with self.open(mode='rb') as f:File "/usr/local/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,File "/usr/local/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
[01:08:15] 🐍 Python dependencies were installed from /app/transform-tomorrow-s-supermarket-today/requirements.txt using pip.
Check if streamlit is installed
Streamlit is already installed
[01:08:17] 📦 Processed dependencies!



2022-08-10 01:08:31.203701: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-08-10 01:08:31.203750: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2022-08-10 01:08:33.066 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "app.py", line 21, in <module>
    img_to_bytes("Supermarket.png")
  File "app.py", line 15, in img_to_bytes
    img_bytes = Path(img_path).read_bytes()
  File "/usr/local/lib/python3.9/pathlib.py", line 1259, in read_bytes
    with self.open(mode='rb') as f:
  File "/usr/local/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/usr/local/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'Supermarket.png'
2022-08-10 01:10:46.843 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "app.py", line 21, in <module>
    img_to_bytes("Supermarket.png")
  File "app.py", line 15, in img_to_bytes
    img_bytes = Path(img_path).read_bytes()
  File "/usr/local/lib/python3.9/pathlib.py", line 1259, in read_bytes
    with self.open(mode='rb') as f:
  File "/usr/local/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/usr/local/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'Supermarket.png'
2022-08-10 01:12:12.133 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "app.py", line 21, in <module>
    img_to_bytes("Supermarket.png")
  File "app.py", line 15, in img_to_bytes
    img_bytes = Path(img_path).read_bytes()
  File "/usr/local/lib/python3.9/pathlib.py", line 1259, in read_bytes
    with self.open(mode='rb') as f:
  File "/usr/local/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/usr/local/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'Supermarket.png'
2022-08-10 01:15:34.999 Uncaught app exception
Traceback (most recent call last):
  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py", line 557, in _run_script
    exec(code, module.__dict__)
  File "app.py", line 21, in <module>
    img_to_bytes("Supermarket.png")
  File "app.py", line 15, in img_to_bytes
    img_bytes = Path(img_path).read_bytes()
  File "/usr/local/lib/python3.9/pathlib.py", line 1259, in read_bytes
    with self.open(mode='rb') as f:
  File "/usr/local/lib/python3.9/pathlib.py", line 1252, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/usr/local/lib/python3.9/pathlib.py", line 1120, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'Supermarket.png'

Hi @yeongnam :wave:

I’ve marked the issue as solved because adding opencv-python-headless to your requirements.txt fixed the Import cv2 error.

The file in your repo is named Supermarket.PNG. You either need to capitalize the file extension on line 21 or rename the file.

thank you