0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

python opencvを用いたコードのexeファイル化(Pyinstaller)

Last updated at Posted at 2021-05-22

概要

近年普及しているpythonはコーディングが容易であり画像処理、機械学習をはじめバックエンドの処理まで幅広い機能の構築が可能である。そんなpythonを用いてopencvを使用したコードをexe化する際に躓いたので覚書

問題

pyinstallerを用いてopencvの使用されているコードをexe化するとcv2内で使用しているnumpyのimportが完了できず動作しない

解決方法

opencvをpipでDL後同時にインストールされているnumpyを一度削除。その後numpyのバージョンを1.15.0に指定してDL

おまけ

pyinstallerにて--noconsoleを指定するとエラーが発生することがある。
原因はexe化後のpathをコードが上手く認識できない様子。。。。。
そこでpathを指定する際に以下の関数を通すことで回避できる。

def resource_path(relative_path):
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?