LoginSignup
9
8

More than 5 years have passed since last update.

Python(Flask)で作った画像認識アプリのHerokuデプロイはOpenCVインストールにheroku.ymlを使おう

Last updated at Posted at 2018-04-01

症状

画像認識アプリをFlaskでつくった。
一見Herokuデプロイはうまくいったかに見える。
しかし以下の画面。

https://gyazo.com/98adba5a2f96966c8e9685b604d4e236

ログを確認。

app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/cv2/__init__.py", line 4, in <module>
app[web.1]:     from .cv2 import *
app[web.1]: ImportError: libSM.so.6: cannot open shared object file: No such file or directory
app[web.1]: [4] [INFO] Shutting down: Master
app[web.1]: [4] [INFO] Reason: Worker failed to boot.
heroku[web.1]: Process exited with status 3
heroku[web.1]: State changed from up to crashed
heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=xxxx.herokuapp.com request_id=xxxxx-xxxx-xxxx-xxxx-xxxxxxxx fwd="xx.xxx.xx.xx" dyno= connect= service= status=503 bytes= protocol=https

なんやこれ。

エラーの原因

なんかimport cv2っぽいんでコメントアウトしたらエラー解決。
※まあ、もちろんcv2がないのでpythonが動かんのだが。。
ということで、つまりimport cv2が原因。
Heroku環境でopenCVをインストールできていなかったということだろう。

でどうやってOpenCVをHerokuにインストールするのか

ちゃんとrequirements.txtにopencv-pythonはあるのに。。

requirements.txt
Flask==0.12
gunicorn==19.7.1
Jinja2==2.9.4
numpy==1.14.1
opencv-python==3.4.0.12
tensorflow==1.5.0

いろいろ調べているとDockerもしくはheroku.ymlをつかってHerokuにOpenCVをインストールせねばらならんらしい。

Docker知らんし、heroku.ymlでやろう!

以下のファイルを作成。

heroku.yml
build:
  languages:
    - python
  packages:
    - libopencv-dev
run:
  web: gunicorn web:app --log-file=-

runtime.txtProcfilerequirement.txtはそのままで大丈夫大丈夫。。

で、

$ git add -A
$ git commit -m "heroku.yml やったったで"
$ heroku stack:set container
$ git push heroku master

するとHerokuにズコズコopenCVが入ってきます!
いっちょ上がりです!

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8