Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python web application with OpenCV in Heroku

I am building a web application that uses OpenCV in its back-end. I have built the application on Ubuntu (and I tried it on Windows, too) and it works fine. Currently, I am trying to configure OpenCV to work on Heroku. As OpenCV is not possible to be loaded using pip, I read about using heroku buildpacks which provide customization for the server environment.

The following is my attempt to test two of OpenCV buildpacks:

  1. I build simple web server with Flask that tries to import OpenCV:

    #hello.py
    import os 
    from flask import Flask
    
    app = Flask(__name__)  
    @app.route("/")
    def hello():
        text = ''
        try:
            import cv2
            text = 'success' 
        except:
            text = 'fail'
            pass
        return text + ' to load openCV'
    
    if __name__ == "__main__":
        port = int(os.environ.get("PORT", 5000))
        app.run(host='0.0.0.0', port=port)
    

    The above code should return either success or fail in loading OpenCV.

  2. Then I configured Heroku to use (heroku multi buildpack) by running the following command:

    heroku buildpacks:set https://github.com/ddollar/heroku-buildpack-multi

  3. In the .buildpacks file (that is required by multi buildpack) I put the https://github.com/heroku/heroku-buildpack-python and https://github.com/slobdell/heroku-buildpack-python-opencv-scipy buildpacks.

    The first one is for compiling a python application and for installing other modules (e.g., Flask) through pip. The second buildpack is the one that is supposed to load OpenCV.

After all, the whole application did not work!

I got (Application Error) page in Heroku as following screenshot: enter image description here

I tried to use other buildpack (https://github.com/diogojc/heroku-buildpack-python-opencv-scipy) but I got the same result.

My questions are:
What is wrong in the steps I did?
How should I call (or use) OpenCV within my application in heroku?
Should I use import statement or some other commands?

like image 348
azri.dev Avatar asked Jul 07 '15 14:07

azri.dev


People also ask

Does heroku support Opencv?

It is required since opencv has some additional dependencies that are not installed on heroku by default and needs to be installed manually. These dependencies can be installed by using linux commands sudo apt get install or sudo apt install.

Can we use Opencv with Web application?

Because OpenCV. js is able to run directly inside browser, the OpenCV. js tutorial web pages are intuitive and interactive. For example, using WebRTC API and evaluating JavaScript code would allow developers to change the parameters of CV functions and do live CV coding on web pages to see the results in real time.


2 Answers

I could install by doing as follows:

  1. cd /path/to/your/dir && git init

  2. heroku create MYAPP (start from scratch)

  3. heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git --app MYAPP

  4. create .buildpacks as follows:

    https://github.com/heroku/heroku-buildpack-python
    https://github.com/diogojc/heroku-buildpack-python-opencv-scipy#cedar14
    
  5. git add . && git commit -m 'MESSAGE' && git push heroku master

like image 138
kyon Avatar answered Oct 19 '22 05:10

kyon


For anyone seeing this today and having the same issue, switch opencv-python in your requirements.txt to opencv-python-headless. This sidesteps the problem with the problematic library file.

like image 27
Aristides Avatar answered Oct 19 '22 04:10

Aristides