How to use OpenCV with Heroku
Asked Answered
S

9

12

When I attempt to deploy my application to Heroku I receive the following error:

File "/app/project/app/_ _init__.py", line 22, in <module>
File "/app/project/app/views.py", line 6, in <module>
import cv2
from .cv2 import *
File "/app/.heroku/python/lib/python3.6/site-packages/cv2/_ _init__.py", line 4, in <module>
2018-03-24T20:40:55.986945+00:00 app[web.1]: ImportError: libSM.so.6: cannot open shared object file: No such file or directory```

OpenCV is unable to find the libsm directory, however this application runs correctly locally. I have tried using a specific buildpack however those did not seem to find my site-packages folder.

How do I use openCV (python) on Heroku?

Stuckup answered 24/3, 2018 at 20:46 Comment(0)
O
39

You can install these missing libraries taking advantage of the heroku-buildpack-apt.

At the time of this writing, I have succesfully done it for this repo, with the following steps:

  1. Add heroku-buildpack-apt to your buildpacks on Heroku platform
  2. Create a file named Aptfile and add the following libs:
libsm6
libxrender1
libfontconfig1
libice6

(one per line). Example here.

Edit: in newer versions of OpenCV, you need only to list python-opencv on the Aptfile, as seen in the docs.

Opuntia answered 23/6, 2018 at 20:53 Comment(3)
It worked for me, many thanks! But I would like to know what I've actually done... I don't have any experience with these build packs.Epanaphora
@mimic, buildpacks can be seen as a "set of scripts, and depending on the programming language, the scripts will retrieve dependencies, output generated assets or compiled code, and more". In my app, I wanted to use Python3, so I added the Python buildpack on Heroku. As some needed libraries were still missing from the system, the heroku-buildpack-apt let's you manage packages in a way similar to how APT works.Opuntia
It worked for me too. Thanks a lot! This should be marked as the solution.Toxicant
O
11

Use opencv-python-headless as it is out of libSM6 dependency. check this out.
Add the following line your requirements.txt and remove the old open-cv entry:

opencv-python-headless==4.2.0.32
Oblige answered 11/8, 2020 at 17:27 Comment(1)
perfect! I was trying many things, at last this worked for me, Thank you:)Checkmate
F
6

New Aptfile and requirements.txt attributes works for me:

in Aptfile

libsm6
libxrender1
libfontconfig1
libice6

in requirements.txt

opencv-python-headless==4.2.0.32

Remember to have the Buildpack included in settings.

https://github.com/heroku/heroku-buildpack-apt
Flaxman answered 13/2, 2021 at 19:3 Comment(0)
O
1

You have to install some dependencies, as Heroku will not automatically do it for you.

  1. Add an Aptfile in your project directory and add the below file
  • libsm6

  • libxrender1

  • libfontconfig1

  • libice6

    NOTE: Aptfile should not have any .txt or any other extension. Just like the Procfile

  1. Push the edited code to Github

  2. In heroku dashboard,
    goto your-app --> settings --> buildpacks --> add buildpacks --> https://github.com/heroku/heroku-buildpack-apt.git
    copy and paste this link --> add buildpack

  3. Deploy your app

enter image description here

Oilla answered 7/1, 2021 at 5:34 Comment(0)
B
0

For windows users, be sure to use unix style line endings in the Aptfile when following @Lelo suggestion above

Blida answered 12/1, 2019 at 16:47 Comment(1)
@Bawantha please find an explanation here: cs.toronto.edu/~krueger/csc209h/tut/….Opuntia
J
0

Referring to Lelo's answer regarding the installation of libraries, OpenCV has changed their required libraries (4.4.0 at the time of writing).

Hence, to get the latest ones, you just need python-opencv in the Aptfile instead of the other libraries.

This was referred to Install OpenCV-Python in Ubuntu.

Jackstraws answered 18/8, 2020 at 6:33 Comment(1)
For me, python3-opencv (note the added 3) worked, and without it Heroku reported that the package was not found.Surrejoinder
P
0

In my case, I was having error like ImportError: libSM.so.6 , in this case one needs to add libsm1 (all in lower case) to aptfile and it worked.

Plumbism answered 14/10, 2021 at 11:25 Comment(0)
C
0

The error you're encountering is related to a missing shared library libGL.so.1 that is required by the OpenCV library (cv2) used in your code. This issue is commonly seen when running applications in headless environments like Heroku, which doesn't have the necessary graphical libraries installed.

To resolve this issue, you can try the following steps:

  1. Update your Aptfile to include the libgl1-mesa-dev package, which provides the libGL.so.1 library:

    tesseract-ocr
    tesseract-ocr-eng
    libsm6
    libxrender1
    libfontconfig1
    libice6
    libglu1
    libgl1-mesa-dev
    
  2. Commit and push the changes to your repository.

  3. Trigger a new deployment on Heroku. The updated Aptfile will prompt Heroku to install the additional dependencies specified.

By adding libgl1-mesa-dev, you should be able to resolve the missing libGL.so.1 error and successfully execute your code on Heroku.

Centistere answered 19/6, 2023 at 14:58 Comment(0)
E
-1

Use this instead .it worked for me.

pip install opencv-contrib-python
Elwaine answered 14/10, 2021 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.