LoginSignup
203
187

More than 3 years have passed since last update.

pip で OpenCV のインストール

Last updated at Posted at 2017-11-18

OpenCV をインストールしてから Python に関連付けるのは手間.

また, venv 等を使用して仮想環境を管理している場合,pip だけでパッケージを管理したくなる.そういうときは opencv-pythonopencv-contrib-python という非公式ビルド済みパッケージが便利.

install 方法

main (core) モジュールのみで良い場合:

$ pip install opencv-python

contrib (extra) モジュールも必要な場合:

$ pip install opencv-contrib-python

main/extra モジュールの分類についてはこちら.OpenCV の使いたい機能に応じて選択する.

商用利用しないのであれば,main モジュールが包含される opencv-contrib-python の方にしておけば良いと思います.もしくは core だけではできないことが出てきたら入れ直すか.

install, import に失敗する場合の対処法

pip 失敗

pip のバージョンが古いと失敗する場合がある.アップグレードする.

$ pip install --upgrade pip

opencv-python · PyPI
Q: Pip fails with Could not find a version that satisfies the requirement ...?
A: Most likely the issue is related to too old pip and can be fixed by running pip install --upgrade pip.

Python インタープリタの指定ミス

システムの Python と処理用の Python がある場合,誤ってシステムの Python の方にモジュールをインストールしてしまっている可能性がある.

$ python -V

で OpenCV をインストールしたい対象の Python インタープリタが実行されることを確認した後,

$ python -m pip install opencv-python

とする.

例えば Mac で homebrew を使って python をインストールした場合,python or python2 コマンドでは 2 系が,python3 コマンドでは 3 系が実行される.3 系の方にインストールしたい場合は,

$ python3 -m pip install opencv-python

とする.

Windows 特有の失敗

Q: Import fails on Windows: ImportError: DLL load failed: The specified module could not be found.?
A: If the import fails on Windows, make sure you have Visual C++ redistributable 2015 installed. If you are using older Windows version than Windows 10 and latest system updates are not installed, Universal C Runtime might be also required.
If the above does not help, check if you are using Anaconda. Old Anaconda versions have a bug which causes the error, see this issue for a manual fix.

opencv-python · PyPI

その他の問題

それぞれのページ後半の Q&A を参照すると解決できるかもしれません.

代替のインストール方法(Windows)

ここ(Unofficial Windows Binaries for Python Extension Packages)から自身の環境に合ったものををダウンロードする.

例: opencv_python‑3.4.6+contrib‑cp37‑cp37m‑win_amd64.whl

  • opencv_python-3.4.6: モジュール自体のバージョン
  • contrib: contrib モジュールを含む
  • cp37: Python 自体のバージョン (Python 3.7.X)
  • win32/win64/win_amd64
    • win32: Intel CPU 32bit Windows
    • win64: Intel CPU 64bit Windows
    • win_amd64: AMD CPU 64bit Windows

以下のコマンドを実行して OpenCV をインストールする.

$ cd ~/Downloads
$ pip install *.whl

or

$ cd ~/Downloads
$ pip install opencv_python‑3.4.6+contrib‑cp37‑cp37m‑win_amd64.whl

※ダウンロードした .whl ファイル名を入力する.

参考

203
187
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
203
187