【 MacBook El Capitan 】Pyenv に入れた Python3.5.2 から Python3用 の OpenCV3 を 使うための環境構築メモ

  • 6
    Like
  • 1
    Comment
More than 1 year has passed since last update.

色々とつまづいたので、自分が後でどうやったか(別のPCマシンでも)再現できるように、備忘録を兼ねて記事を立てました。

【 要約 】


  • Pyenv で入れた各バージョンのPython から OpenCV3 を使えるようにするためには、
    Pyenvディレクトリ内の各バージョン毎のPythonディレクトリの中にあるsite-packagesディレクトリ から、OpenCV3のsite-packagesディレクトリ直下にある「cv2.cpython-35m-darwin.so」ファイル に シンボリック・リンク を 張る必要があるようです。

  • シンボリンク・リンクの張り元、張り先 両ディレクトリ の フルパスを間違えてリンクをはってもPython から import cv3 を実行できなくなるので、両ディレクトリの場所(フルパス)を丁寧に確認する必要があります。


( 記事は少し長いので、紆余曲折を経て 作業実行したやり方 を 先に示します )

以下のサイトのシンボリックリンクを貼るコードを参考に。

bash
$ cd ~/.pyenv/versions/anaconda3-2.5.0/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

( 以下でうまくいきました )

1. シンボリックで結ぶ以下の2つのディレクトリとファイルの場所(パス)を確認


  • Pyenvで入れたPython 3.5.2 の site-packagesディレクトリ
  • brew install でインストールした OpenCV3 の site-packagesディレクトリ内にある soファイル

  • Pyenvで入れたPython3.5.2のディレクトリのパスは、最終行の以下であることを確認した。

/Users/hirofumiyashima/.pyenv/versions/3.5.2/lib/python3.5/site-packages

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ cd .pyenv/versions/3.5.2/lib/python3.5/site-packages/
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ pwd
/Users/hirofumiyashima/.pyenv/versions/3.5.2/lib/python3.5/site-packages
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$

( このディレクトリの中身をのぞいてみる )

HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ ls
Cython                  easy_install.py             pandas                  pytz-2016.6.1.dist-info
Cython-0.24.1.dist-info         filelock-2.0.6-py3.5.egg-info       pandas-0.18.1.dist-info         pyximport
README                  filelock.py             pip                 scikit_learn-0.17.1.dist-info
__pycache__             google                  pip-8.1.2.dist-info         scipy
chainer                 matplotlib              pkg_resources               scipy-0.17.1.dist-info
chainer-1.11.0-py3.5.egg-info       matplotlib-1.5.1-py3.5-nspkg.pth    protobuf-2.6.1-py3.5-nspkg.pth      setuptools
cupy                    matplotlib-1.5.1.dist-info      protobuf-2.6.1-py3.5.egg-info       setuptools-24.3.0.dist-info
cv2.cpython-35m-darwin.so       mpl_toolkits                pylab.py                six-1.10.0.dist-info
cycler-0.10.0.dist-info         nose                    pyparsing-2.1.5.dist-info       six.py
cycler.py               nose-1.3.7.dist-info            pyparsing.py                sklearn
cython.py               numpy                   python_dateutil-2.5.3.dist-info
dateutil                numpy-1.11.1.dist-info          pytz
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$
  • brew install でインストールした OpenCV3 の site-packagesディレクトリ内にある soファイル の場所(パス)は、以下で確認できた以下の通り。

/usr/local/Cellar/opencv3/3.1.0_3/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so

HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ cd /usr/local/Cellar/opencv3/3.1.0_3
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ pwd
/usr/local/Cellar/opencv3/3.1.0_3
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls
INSTALL_RECEIPT.json    LICENSE         README.md       bin         include         lib         share
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls lib/
libopencv_calib3d.3.1.0.dylib       libopencv_highgui.3.1.dylib     libopencv_objdetect.dylib       libopencv_video.3.1.0.dylib
libopencv_calib3d.3.1.dylib     libopencv_highgui.dylib         libopencv_photo.3.1.0.dylib     libopencv_video.3.1.dylib
libopencv_calib3d.dylib         libopencv_imgcodecs.3.1.0.dylib     libopencv_photo.3.1.dylib       libopencv_video.dylib
libopencv_core.3.1.0.dylib      libopencv_imgcodecs.3.1.dylib       libopencv_photo.dylib           libopencv_videoio.3.1.0.dylib
libopencv_core.3.1.dylib        libopencv_imgcodecs.dylib       libopencv_shape.3.1.0.dylib     libopencv_videoio.3.1.dylib
libopencv_core.dylib            libopencv_imgproc.3.1.0.dylib       libopencv_shape.3.1.dylib       libopencv_videoio.dylib
libopencv_features2d.3.1.0.dylib    libopencv_imgproc.3.1.dylib     libopencv_shape.dylib           libopencv_videostab.3.1.0.dylib
libopencv_features2d.3.1.dylib      libopencv_imgproc.dylib         libopencv_stitching.3.1.0.dylib     libopencv_videostab.3.1.dylib
libopencv_features2d.dylib      libopencv_ml.3.1.0.dylib        libopencv_stitching.3.1.dylib       libopencv_videostab.dylib
libopencv_flann.3.1.0.dylib     libopencv_ml.3.1.dylib          libopencv_stitching.dylib       pkgconfig
libopencv_flann.3.1.dylib       libopencv_ml.dylib          libopencv_superres.3.1.0.dylib      python2.7
libopencv_flann.dylib           libopencv_objdetect.3.1.0.dylib     libopencv_superres.3.1.dylib        python3.5
libopencv_highgui.3.1.0.dylib       libopencv_objdetect.3.1.dylib       libopencv_superres.dylib
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ 
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls lib/python3.5/site-packages/
cv2.cpython-35m-darwin.so

( 参考 )拡張子 so のファイルとは?

.soファイルとは、UNIX系OSの共有ライブラリのファイル形式。拡張子が「.so」であることからこのように呼ばれる。

実行可能形式のプログラムが格納されているが、単体で起動することはできず、他のプログラムにリンクしてその機能を呼び出すようになっている。

この形式のファイルはプログラムの実行時にリンクされる動的リンク(ダイナミックリンク)ライブラリで、ビルド時にリンクされる静的リンク(スタティックリンク)ライブラリの場合は「.a」(archiveの略)という拡張子になる。

2. 両者 を シンボリックリンク で 結ぶ

HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ cd
HirofumiYashima-no-MacBook:~ hirofumiyashima$ pwd
/Users/hirofumiyashima
HirofumiYashima-no-MacBook:~ hirofumiyashima$
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ln -s /usr/local/Cellar/opencv3/3.1.0_3/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so /Users/hirofumiyashima/.pyenv/versions/3.5.2/lib/python3.5/site-packages/cv2.so
HirofumiYashima-no-MacBook:~ hirofumiyashima$

3. Python3.5.2 を 起動して、cv3モジュールがimportできるかチェック

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ python
Python 3.5.2 (default, Jul 23 2016, 14:25:12) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import cv2
>>> 
>>> quit()
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

( 以下、上記に至るまでの(パス誤りを含めた)模索過程を備忘録まで )

作業するPythonが3系であることを確認して、3系用 の OpenCV3 を brew install

  1. シンボリックリンクを貼る
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/
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$  
HirofumiYashima-no-MacBook:~ hirofumiyashima$ pwd
/Users/hirofumiyashima
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ pyenv version
3.5.2 (set by /Users/hirofumiyashima/.pyenv/version)
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew install opencv3
Error: No available formula with the name "opencv3" 
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
This formula was found in a tap:
homebrew/science/opencv3
To install it, run:
  brew install homebrew/science/opencv3
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

以下のようにアドバイスが表示された。

This formula was found in a tap:
homebrew/science/opencv3

To install it, run:
brew install homebrew/science/opencv3

ここでは、brew install homebrew/science/opencv3 を実行せずに、今後のために、tap science しておく。

+* daisukeokaossさん Qiita記事(2015/07/17)「MacにOpenCVをインストールする方法」

HomeBrewをアップデートすること

brew update
brew install -v cmake

Tapもする必要がある

brew tap homebrew/science

OpenCVをHomebrewにてインストールする

brew install opencv

その前に、brew を updateして、-vオプショん で make しておく。

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew update
Already up-to-date.
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew install -v cmake
==> Downloading https://homebrew.bintray.com/bottles/cmake-3.6.0_1.el_capitan.bottle.tar.gz
/usr/bin/curl --fail --remote-time --location --user-agent Homebrew/0.9.9 (Macintosh; Intel Mac OS X 10.11.4) curl/7.43.0 https://homebrew.bintray.com/bottles/cmake-3.6.0_1.el_capitan.bottle.tar.gz -C 0 -o /Users/hirofumiyashima/Library/Caches/Homebrew/cmake-3.6.0_1.el_capitan.bottle.tar.gz.incomplete
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
100 10.0M  100 10.0M    0     0   951k      0  0:00:10  0:00:10 --:--:-- 1332k
==> Verifying cmake-3.6.0_1.el_capitan.bottle.tar.gz checksum
==> Pouring cmake-3.6.0_1.el_capitan.bottle.tar.gz
tar xvzf /Users/hirofumiyashima/Library/Caches/Homebrew/cmake-3.6.0_1.el_capitan.bottle.tar.gz
x cmake/3.6.0_1/
x cmake/3.6.0_1/bin/
x cmake/3.6.0_1/Copyright.txt
x cmake/3.6.0_1/INSTALL_RECEIPT.json
x cmake/3.6.0_1/README.rst
x cmake/3.6.0_1/share/
x cmake/3.6.0_1/share/aclocal/
x cmake/3.6.0_1/share/cmake/
x cmake/3.6.0_1/share/doc/
x cmake/3.6.0_1/share/emacs/
x cmake/3.6.0_1/share/man/
x cmake/3.6.0_1/share/man/man1/
x cmake/3.6.0_1/share/man/man7/
x cmake/3.6.0_1/share/man/man7/cmake-buildsystem.7

( 中略 )

mkdir -p /usr/local/share/man/man7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-buildsystem.7 cmake-buildsystem.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-commands.7 cmake-commands.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-compile-features.7 cmake-compile-features.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-developer.7 cmake-developer.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-generator-expressions.7 cmake-generator-expressions.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-generators.7 cmake-generators.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-language.7 cmake-language.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-modules.7 cmake-modules.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-packages.7 cmake-packages.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-policies.7 cmake-policies.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-properties.7 cmake-properties.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-qt.7 cmake-qt.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-toolchains.7 cmake-toolchains.7
ln -s ../../../Cellar/cmake/3.6.0_1/share/man/man7/cmake-variables.7 cmake-variables.7
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/cmake
==> Summary
🍺  /usr/local/Cellar/cmake/3.6.0_1: 2,028 files, 27.8M
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

( tap を実行 )

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew tap homebrew/science
==> Tapping homebrew/science
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-science'...
remote: Counting objects: 606, done.
remote: Compressing objects: 100% (604/604), done.
remote: Total 606 (delta 1), reused 65 (delta 0), pack-reused 0
Receiving objects: 100% (606/606), 521.65 KiB | 623.00 KiB/s, done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.
Tapped 593 formulae (625 files, 1.6M)
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew info opencv3 | grep python3
--with-python3
    Build with python3 support
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

With-python3 オプションをつけて、Python3用のOpenCV3 を brew install

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew install opencv3 --with-python3 
==> Installing opencv3 from homebrew/science
==> Tapping homebrew/python
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-python'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (22/22), done.
remote: Total 22 (delta 1), reused 7 (delta 0), pack-reused 0
Unpacking objects: 100% (22/22), done.
Checking connectivity... done.
Tapped 18 formulae (63 files, 90.5K)
==> Installing dependencies for homebrew/science/opencv3: gmp, mpfr, libmpc, isl, gcc, eigen, jpeg, libpng, libtiff, ilmbase, openexr, home
==> Installing homebrew/science/opencv3 dependency: gmp
==> Downloading https://homebrew.bintray.com/bottles/gmp-6.1.1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gmp-6.1.1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/gmp/6.1.1: 17 files, 3.2M
==> Installing homebrew/science/opencv3 dependency: mpfr
==> Downloading https://homebrew.bintray.com/bottles/mpfr-3.1.4.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mpfr-3.1.4.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/mpfr/3.1.4: 25 files, 3.6M
==> Installing homebrew/science/opencv3 dependency: libmpc
==> Downloading https://homebrew.bintray.com/bottles/libmpc-1.0.3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libmpc-1.0.3.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libmpc/1.0.3: 11 files, 350.1K


( 中略 )

==> Applying cdb9c60dcb65e04e7c0bd6bef9b86841191c785a.diff
patching file samples/gpu/CMakeLists.txt
==> Downloading https://raw.githubusercontent.com/Itseez/opencv_3rdparty/81a676001ca8075ada498583e4166079e5744668/ippicv/ippicv_macosx_20151201.tg
######################################################################## 100.0%
==> cmake .. -DCMAKE_C_FLAGS_RELEASE=-DNDEBUG -DCMAKE_CXX_FLAGS_RELEASE=-DNDEBUG -DCMAKE_INSTALL_PREFIX=/usr/local/Cellar/opencv3/3.1.0_3 -DCMAKE_
==> 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/hirofumiyashima/.local/lib/python3.5/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/hirofumiyashima/.local/lib/python3.5/site-packages/homebrew.pth
==> Summary
🍺  /usr/local/Cellar/opencv3/3.1.0_3: 282 files, 120.9M, built in 10 minutes 24 seconds
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

インストールできたので、Pyenv の Python 3.5.2環境 の site-packagesディレクトリ から、インストールしたOpenCV3 の site-packagesディレクトリ に シンボリックリンク を 貼る

(シンボリック先) OpenCV3 の site-packagesディレクトリ のパスは、以下と表示されていた。

==> Summary
🍺  /usr/local/Cellar/opencv3/3.1.0_3: 282 files, 120.9M,

だから、/usr/local/Cellar/opencv3/3.1.0_3 配下のディレクトリ。

(シンボリック元)

Pyenv の Python 3.5.2環境 の site-packagesディレクトリ

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls .pyenv/versions/3.5.2/lib/python3.5/site-packages/

Cython                  matplotlib              pylab.py
Cython-0.24.1.dist-info         matplotlib-1.5.1-py3.5-nspkg.pth    pyparsing-2.1.5.dist-info
README                  matplotlib-1.5.1.dist-info      pyparsing.py
__pycache__             mpl_toolkits                python_dateutil-2.5.3.dist-info
chainer                 nose                    pytz
chainer-1.11.0-py3.5.egg-info       nose-1.3.7.dist-info            pytz-2016.6.1.dist-info
cupy                    numpy                   pyximport
cycler-0.10.0.dist-info         numpy-1.11.1.dist-info          scikit_learn-0.17.1.dist-info
cycler.py               pandas                  scipy
cython.py               pandas-0.18.1.dist-info         scipy-0.17.1.dist-info
dateutil                pip                 setuptools
easy_install.py             pip-8.1.2.dist-info         setuptools-24.3.0.dist-info
filelock-2.0.6-py3.5.egg-info       pkg_resources               six-1.10.0.dist-info
filelock.py             protobuf-2.6.1-py3.5-nspkg.pth      six.py
google                  protobuf-2.6.1-py3.5.egg-info       sklearn
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

ちなみに、Pyenvには、他に以下のPythonバージョンも入れている。

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls .pyenv/versions/
2.7     3.5.0       3.5.1       3.5.2       anaconda-4.0.0  py351tensorflow
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

Python 3.5.1 の site-packagesディレクトリのパスは以下の通り

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls .pyenv/versions/3.5.1/lib/python3.5/site-packages/
README              easy_install.py         pkg_resources
__pycache__         pip             setuptools
_markerlib          pip-7.1.2.dist-info     setuptools-18.2.dist-info
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

参考ウェブサイトにならい、シンボリックリンクをはる

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ln -s /path/to/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so /path/to/.pyenv/versions/3.5.2/lib/python3.5/site-packages/
ln: /path/to/.pyenv/versions/3.5.2/lib/python3.5/site-packages/: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

(以下でリンクを張れた)

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ln -s /usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so .pyenv/versions/3.5.2/lib/python3.5/site-packages/
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

Python 3.5.2 を起動するも、import cv2 失敗

Python3.5.2(pyenv)
HirofumiYashima-no-MacBook:~ hirofumiyashima$ python
Python 3.5.2 (default, Jul 23 2016, 14:25:12) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>> 
>>> quit()
HirofumiYashima-no-MacBook:~ hirofumiyashima$

以下、いずれもうまくいかない。

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls /usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/
ls: /usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls ~/usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/
ls: /Users/hirofumiyashima/usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls ../usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/
ls: ../usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls ./usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/
ls: ./usr/local/Cellar/opencv3/3.1.0_2/lib/python3.5/site-packages/: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls
Desktop     Downloads   Movies      Pictures    chainer     log     tensorflow
Documents   Library     Music       Public      data        result
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls ..
Shared      hirofumiyashima
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls ...
ls: ...: No such file or directory
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ls
Desktop     Downloads   Movies      Pictures    chainer     log     tensorflow
Documents   Library     Music       Public      data        result
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

以下のサイトを頼りに、以下のコードを行ったものの、うまく行かず

Hatena Blog それマグで! (2016-01-06)「brew で opencv3 を入れる」

link する

brew link opencv3 --force

opencv3 はkeg only だったのでリンクする

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ brew link opencv3 --force
Linking /usr/local/Cellar/opencv3/3.1.0_3... 57 symlinks created
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ python
Python 3.5.2 (default, Jul 23 2016, 14:25:12) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'cv2'
>>> 
>>> quit()
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

karunruさん Qiita記事(2016/03/08)「pyenvでのpython3.5にopenCV3.1をインストールした」

上記のサイトでは、wget でOpenCVの圧縮ファイルを取得したものを、解凍して、コンパイルしてインストールしている

参考にしたのは、シンボリックリンクを張っている最後の2行です。

$ cd
$ wget https://github.com/Itseez/opencv/archive/3.1.0.zip
$ wget https://github.com/Itseez/opencv_contrib/archive/3.1.0.zip
$ unzip 3.1.0.zip
$ unzip 3.1.0.zip.1
$ cd opencv-3.1.0
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_opencv_java=OFF \
-D WITH_IPP=OFF -D WITH_1394=OFF \
-D WITH_FFMPEG=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_opencv_python2=ON \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_video=OFF \
-D BUILD_opencv_videoio=OFF \
-D BUILD_opencv_videostab=OFF \
-D PYTHON_EXECUTABLE=$(which python) \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/opencv_contrib-3.1.0/modules ..
$ make -j4
$ sudo make install
$ cd ~/.pyenv/versions/anaconda3-2.5.0/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

( 最後の2行 )

$ cd ~/.pyenv/versions/anaconda3-2.5.0/lib/python3.5/site-packages/

$ ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

( リンク元の Pyenv Python3.5.2 site-packagesディレクトリの場所 )

MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 
HirofumiYashima-no-MacBook:~ hirofumiyashima$ cd .pyenv/versions/3.5.2/lib/python3.5/site-packages/
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ pwd
/Users/hirofumiyashima/.pyenv/versions/3.5.2/lib/python3.5/site-packages
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ 
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ ls
Cython                  easy_install.py             pandas                  pytz-2016.6.1.dist-info
Cython-0.24.1.dist-info         filelock-2.0.6-py3.5.egg-info       pandas-0.18.1.dist-info         pyximport
README                  filelock.py             pip                 scikit_learn-0.17.1.dist-info
__pycache__             google                  pip-8.1.2.dist-info         scipy
chainer                 matplotlib              pkg_resources               scipy-0.17.1.dist-info
chainer-1.11.0-py3.5.egg-info       matplotlib-1.5.1-py3.5-nspkg.pth    protobuf-2.6.1-py3.5-nspkg.pth      setuptools
cupy                    matplotlib-1.5.1.dist-info      protobuf-2.6.1-py3.5.egg-info       setuptools-24.3.0.dist-info
cv2.cpython-35m-darwin.so       mpl_toolkits                pylab.py                six-1.10.0.dist-info
cycler-0.10.0.dist-info         nose                    pyparsing-2.1.5.dist-info       six.py
cycler.py               nose-1.3.7.dist-info            pyparsing.py                sklearn
cython.py               numpy                   python_dateutil-2.5.3.dist-info
dateutil                numpy-1.11.1.dist-info          pytz
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ 

( リンク先の OpenCV3 の site-packagesディレクトリ の 場所 )

MacBook Terminal
HirofumiYashima-no-MacBook:site-packages hirofumiyashima$ cd /usr/local/Cellar/opencv3/3.1.0_3
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ pwd
/usr/local/Cellar/opencv3/3.1.0_3
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls
INSTALL_RECEIPT.json    LICENSE         README.md       bin         include         lib         share
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls lib/
libopencv_calib3d.3.1.0.dylib       libopencv_highgui.3.1.dylib     libopencv_objdetect.dylib       libopencv_video.3.1.0.dylib
libopencv_calib3d.3.1.dylib     libopencv_highgui.dylib         libopencv_photo.3.1.0.dylib     libopencv_video.3.1.dylib
libopencv_calib3d.dylib         libopencv_imgcodecs.3.1.0.dylib     libopencv_photo.3.1.dylib       libopencv_video.dylib
libopencv_core.3.1.0.dylib      libopencv_imgcodecs.3.1.dylib       libopencv_photo.dylib           libopencv_videoio.3.1.0.dylib
libopencv_core.3.1.dylib        libopencv_imgcodecs.dylib       libopencv_shape.3.1.0.dylib     libopencv_videoio.3.1.dylib
libopencv_core.dylib            libopencv_imgproc.3.1.0.dylib       libopencv_shape.3.1.dylib       libopencv_videoio.dylib
libopencv_features2d.3.1.0.dylib    libopencv_imgproc.3.1.dylib     libopencv_shape.dylib           libopencv_videostab.3.1.0.dylib
libopencv_features2d.3.1.dylib      libopencv_imgproc.dylib         libopencv_stitching.3.1.0.dylib     libopencv_videostab.3.1.dylib
libopencv_features2d.dylib      libopencv_ml.3.1.0.dylib        libopencv_stitching.3.1.dylib       libopencv_videostab.dylib
libopencv_flann.3.1.0.dylib     libopencv_ml.3.1.dylib          libopencv_stitching.dylib       pkgconfig
libopencv_flann.3.1.dylib       libopencv_ml.dylib          libopencv_superres.3.1.0.dylib      python2.7
libopencv_flann.dylib           libopencv_objdetect.3.1.0.dylib     libopencv_superres.3.1.dylib        python3.5
libopencv_highgui.3.1.0.dylib       libopencv_objdetect.3.1.dylib       libopencv_superres.dylib
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ 
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls lib/python3.5/
site-packages
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ ls lib/python3.5/site-packages/
cv2.cpython-35m-darwin.so
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ 

シンポリックリンク を 張る

リンク元(Pyenv側)にcdしてからシンボリックを貼るのではなく、リンク元のpath と リンク先のpath の両方をフルパス で ln -s コマンドに渡す )

MacBook Terminal
HirofumiYashima-no-MacBook:3.1.0_3 hirofumiyashima$ cd
HirofumiYashima-no-MacBook:~ hirofumiyashima$ pwd
/Users/hirofumiyashima
HirofumiYashima-no-MacBook:~ hirofumiyashima$
MacBook Terminal
HirofumiYashima-no-MacBook:~ hirofumiyashima$ ln -s /usr/local/Cellar/opencv3/3.1.0_3/lib/python3.5/site-packages/cv2.cpython-35m-darwin.so /Users/hirofumiyashima/.pyenv/versions/3.5.2/lib/python3.5/site-packages/cv2.so
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

Python3.5.2 起動して、import cv3 実行成功

Python3.5.2(pyenv)
HirofumiYashima-no-MacBook:~ hirofumiyashima$ python
Python 3.5.2 (default, Jul 23 2016, 14:25:12) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import cv2
>>> 
>>> cv2.__version__
'3.1.0'
>>> 
>>> print(cv2.__version__)
3.1.0
>>> 
>>> quit()
HirofumiYashima-no-MacBook:~ hirofumiyashima$ 

【 今後の学習 】

以下のウェブページなどを見て、まずは Python & OpenCV3 の 基本的な使い方に手をなじませていきたい。

Python
import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()