LoginSignup
25
17

More than 5 years have passed since last update.

HomeBrewでinstallしたOpenCVをpyenvから参照する

Last updated at Posted at 2016-05-07

内容

Python で OpenCV を import しようとした時にいろいろと調べていて、
Homebrew から install した OpenCV のライブラリを pyenvから参照できる方法を調査したので、メモしておく。

前提

python3 で OpenCV のライブラリを使いたかったので、 pyenv の local のバージョンは anaconda3-2.x を使いました。
今回、自分の環境は anaconda3-2.5.0 を設定しています。

pyenv
$ pyenv local
anaconda3-2.5.0

作業手順

  1. Homebrew で OpenCV を python3 で使えるようにインストール
    インストールするときのオプションを調べます。

    $brew info opencv3
    

    実際にインストールを行います。

    $ brew install opencv3 --with-python3                                                                                                          2.1.1
    ==> Installing opencv3 from homebrew/science
    ==> Installing dependencies for homebrew/science/opencv3: eigen, ilmbase, openexr, homebrew/python/numpy
    ==> Installing homebrew/science/opencv3 dependency: eigen
    ==> Downloading https://homebrew.bintray.com/bottles/eigen-3.2.8.el_capitan.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Pouring eigen-3.2.8.el_capitan.bottle.tar.gz
    🍺  /usr/local/Cellar/eigen/3.2.8: 368 files, 3.5M
    ==> Installing homebrew/science/opencv3 dependency: ilmbase
    ==> Downloading https://homebrew.bintray.com/bottles/ilmbase-2.2.0.el_capitan.bottle.1.tar.gz
    ######################################################################## 100.0%
    ==> Pouring ilmbase-2.2.0.el_capitan.bottle.1.tar.gz
    🍺  /usr/local/Cellar/ilmbase/2.2.0: 347 files, 5.5M
    ==> Installing homebrew/science/opencv3 dependency: openexr
    ==> Downloading https://homebrew.bintray.com/bottles/openexr-2.2.0.el_capitan.bottle.1.tar.gz
    ######################################################################## 100.0%
    ==> Pouring openexr-2.2.0.el_capitan.bottle.1.tar.gz
    🍺  /usr/local/Cellar/openexr/2.2.0: 129 files, 11.4M
    ==> Installing homebrew/science/opencv3 dependency: homebrew/python/numpy
    ==> Using Homebrew-provided fortran compiler.
    This may be changed by setting the FC environment variable.
    ==> Downloading https://pypi.python.org/packages/source/n/numpy/numpy-1.10.4.tar.gz
    ######################################################################## 100.0%
    ==> Downloading https://pypi.python.org/packages/source/n/nose/nose-1.3.7.tar.gz
    ######################################################################## 100.0%
    ==> python -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/numpy/1.10.4/libexec/nose --single-version-externally-managed --r
    ==> python setup.py build --fcompiler=gnu95 install --prefix=/usr/local/Cellar/numpy/1.10.4
    ==> Downloading https://pypi.python.org/packages/source/n/nose/nose-1.3.7.tar.gz
    Already downloaded: /Library/Caches/Homebrew/numpy--nose-1.3.7.tar.gz
    ==> python3 -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/numpy/1.10.4/libexec/nose --single-version-externally-managed --
    ==> python3 setup.py build --fcompiler=gnu95 install --prefix=/usr/local/Cellar/numpy/1.10.4
    ==> Caveats
    Python modules have been installed and Homebrew's site-packages is not
    in your Python sys.path, so you will not be able to import the modules
    this formula installed. If you plan to develop with these modules,
    please run:
    mkdir -p /Users/atsushi/.local/lib/python3.5/site-packages
    echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/atsushi/.local/lib/python3.5/site-packages/homebrew.pth
    ==> Summary
    🍺  /usr/local/Cellar/numpy/1.10.4: 1,393 files, 26.5M, built in 4 minutes 10 seconds
    ==> Installing homebrew/science/opencv3
    ==> Using Homebrew-provided fortran compiler.
    This may be changed by setting the FC environment variable.
    ==> Downloading https://github.com/Itseez/opencv/archive/3.1.0.tar.gz
    ==> Downloading from https://codeload.github.com/Itseez/opencv/tar.gz/3.1.0
    ######################################################################## 100.0%
    ==> Patching
    patching file modules/videoio/src/cap_qtkit.mm
    ==> Downloading https://downloads.sourceforge.net/project/opencvlibrary/3rdparty/ippicv/ippicv_macosx_20141027.tgz
    ==> Downloading from http://liquidtelecom.dl.sourceforge.net/project/opencvlibrary/3rdparty/ippicv/ippicv_macosx_20141027.tgz
    ######################################################################## 100.0%
    ==> cmake .. -DCMAKE_C_FLAGS_RELEASE=-DNDEBUG -DCMAKE_CXX_FLAGS_RELEASE=-DNDEBUG -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/opencv3/3.1.0_2 -DCMAKE_BUI
    ==> make
    ==> make install
    ==> Caveats
    This formula is keg-only, which means it was not symlinked into /usr/local.
    opencv3 and opencv install many of the same files.
    Generally there are no consequences of this for you. If you build your
    own software and it requires this formula, you'll need to add to your
    build variables:
    LDFLAGS:  -L/usr/local/opt/opencv3/lib
    CPPFLAGS: -I/usr/local/opt/opencv3/include
    If you need Python to find bindings for this keg-only formula, run:
    echo /usr/local/opt/opencv3/lib/python2.7/site-packages >> /usr/local/lib/python2.7/site-packages/opencv3.pth
    mkdir -p /Users/atsushi/.local/lib/python3.5/site-packages
    echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/atsushi/.local/lib/python3.5/site-packages/homebrew.pth
    ==> Summary
    🍺  /usr/local/Cellar/opencv3/3.1.0_2: 251 files, 121.8M, built in 41 minutes 40 seconds
    
  2. シンボリックリンクを貼る

    ln -s /path/to/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so /path/to/.pyenv/versions/anaconda3-2.5.0/lib/python3.5/site-packages/
    

参照できているか確認する

Homebrew で install した OpenCV のバージョンが出力できれば完了のはずです。以下の手順でバージョンを調べられます。

Python 3.5.1 |Anaconda 2.5.0 (x86_64)| (default, Dec  7 2015, 11:24:55)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'

感想

シンプルなので、 pip install でインストールできないライブラリなどの場合は楽に使えるようになるかと思います。
OpenCV 以外にどのようなライブラリがあるかはよくわかりませんが…笑

備考

とりあえず python で OpenCV のライブラリを import して バージョン出力するところまでやりました。
API を参照するなどはまだ確認できていないので、確認ができましたら改めて追記します。

25
17
6

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
25
17