こちらのサイトを参考にさせていただき、mac book proにopencv3をインストールしました。 HomebrewでMacにOpenCV3をインストールしてPython3で使う

macの環境はこちら

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.12.6
BuildVersion:   16G1036

インストール事前準備

リポジトリを切り替え

opencvは標準で入っていないので切り替える。

$ brew tap homebrew/science

オプションの確認

$ brew info opencv3
opencv: stable 3.3.1 (bottled)
Open source computer vision library
https://opencv.org/
/usr/local/Cellar/opencv/2.4.13.2 (278 files, 35.6MB) *
  Poured from bottle on 2017-05-03 at 14:14:55
/usr/local/Cellar/opencv/3.2.0 (498 files, 62.9MB)
  Built from source on 2017-06-25 at 19:53:41 with: --with-contrib
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/opencv.rb
==> Dependencies
Build: cmake ✘, pkg-config ✔
Required: eigen ✔, ffmpeg ✘, jpeg ✔, libpng ✔, libtiff ✘, openexr ✔, numpy ✘, tbb ✔
==> Requirements
Required: python ✘, python3 ✔
==> 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 ~/.local/lib/python3.5/site-packages
  echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> ~/.local/lib/python3.5/site-packages/homebrew.pth

オプションを確認すると色々と要求されていたり、 「Build, Required」など×がついているものが多かったので対応した。

対応した時に打ったコマンド

$ brew install cmake pkg-config jpeg libpng libtiff eigen openexr jasper tbb qt5
$ brew upgrade cmake
$ brew upgrade libtiff
$ mkdir -p ~/.local/lib/python3.5/site-packages
(余計だったかも)$ echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> ~/.local/lib/python3.5/site-packages/homebrew.pth
$ brew upgrade ffmpeg (インストールしていない場合は $ brew install ffmpeg)
$ brew upgrade numpy (インストールしていない場合は $ brew install numpy)
$ brew install python

対応後

$ brew info opencv3
opencv: stable 3.3.1 (bottled)
Open source computer vision library
https://opencv.org/
/usr/local/Cellar/opencv/2.4.13.2 (278 files, 35.6MB) *
  Poured from bottle on 2017-05-03 at 14:14:55
/usr/local/Cellar/opencv/3.2.0 (498 files, 62.9MB)
  Built from source on 2017-06-25 at 19:53:41 with: --with-contrib
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/opencv.rb
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: eigen ✔, ffmpeg ✔, jpeg ✔, libpng ✔, libtiff ✔, openexr ✔, numpy ✔, tbb ✔
==> Requirements
Required: python ✔, python3 ✔

全部にチェックがついた。これで勝つる。

インストール

$ brew install opencv3 --with-contrib --with-examples --with-ffmpeg --with-jasper --with-opengl --with-python3 --with-qt5 --with-tbb --without-numpy

実は、すでにopencvが入っている人はこっち。

$ brew upgrade opencv

それなりに時間がかかります。

確認

インストールが終わったらopencvのビルド情報を確認。

$ python -c "import cv2; print(cv2.getBuildInformation())"

General configuration for OpenCV 3.3.1 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /tmp/opencv-20171203-13938-dlhhkn/opencv-3.3.1/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2017-12-03T04:20:37Z
    Host:                        Darwin 16.7.0 x86_64
    CMake:                       3.10.0
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/local/Homebrew/Library/Homebrew/shims/super/gmake
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2
      SSE4_1 (3 files):          + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (8 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      YES
    C++11:                       YES
    C++ Compiler:                /usr/local/Homebrew/Library/Homebrew/shims/super/clang++  (ver 9.0.0.9000038)
    C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/local/Homebrew/Library/Homebrew/shims/super/clang
    C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -DNDEBUG  -DNDEBUG
    C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -fno-omit-frame-pointer -ffunction-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):
    Linker flags (Debug):
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          /usr/local/lib/libtbb.dylib
    3rdparty dependencies:

  OpenCV modules:
    To be built:                 core flann hdf imgproc ml objdetect phase_unwrapping photo plot reg surface_matching video xphoto bgsegm dnn face fuzzy img_hash imgcodecs shape videoio xobjdetect highgui superres bioinspired dpm features2d line_descriptor saliency text calib3d ccalib datasets rgbd stereo structured_light tracking videostab xfeatures2d ximgproc aruco optflow sfm stitching python2 python3
    Disabled:                    js world contrib_world
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java ts viz cnn_3dobj cvv dnn_modern freetype matlab

  Non-free algorithms are enabled

  GUI:
    QT:                          NO
    Cocoa:                       YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O:
    ZLib:                        /usr/lib/libz.dylib (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        /usr/local/lib/libwebp.dylib (ver encoder: 0x020e)
    PNG:                         /usr/local/lib/libpng.dylib (ver 1.6.34)
    TIFF:                        /usr/local/lib/libtiff.dylib (ver 42 - 4.0.8)
    JPEG 2000:                   NO
    OpenEXR:                     /usr/local/lib/libImath.dylib /usr/local/lib/libIlmImf.dylib /usr/local/lib/libIex.dylib /usr/local/lib/libHalf.dylib /usr/local/lib/libIlmThread.dylib (ver 2.2.0)
    GDAL:                        NO
    GDCM:                        NO

  Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES
      avcodec:                   YES (ver 57.107.100)
      avformat:                  YES (ver 57.83.100)
      avutil:                    YES (ver 55.78.100)
      swscale:                   YES (ver 4.8.100)
      avresample:                YES (ver 3.7.0)
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    AVFoundation:                YES
    V4L/V4L2:                    NO/NO
    XIMEA:                       NO
    Intel Media SDK:             NO
    gPhoto2:                     NO

  Parallel framework:            TBB (ver 2018.0 interface 10001)

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Use Intel IPP:               2017.0.3 [2017.0.3]
               at:               /tmp/opencv-20171203-13938-dlhhkn/opencv-3.3.1/build/3rdparty/ippicv/ippicv_mac
    Use Intel IPP IW:            sources (2017.0.3)
                  at:            /tmp/opencv-20171203-13938-dlhhkn/opencv-3.3.1/build/3rdparty/ippicv/ippiw_mac
    Use VA:                      NO
    Use Intel VA-API/OpenCL:     NO
    Use Lapack:                  YES (/System/Library/Frameworks/Accelerate.framework /System/Library/Frameworks/Accelerate.framework)
    Use Eigen:                   YES (ver 3.3.4)
    Use Cuda:                    NO
    Use OpenCL:                  YES
    Use OpenVX:                  NO
    Use custom HAL:              NO

  OpenCL:                        <Link with OpenCL library>
    Link libraries:              -framework OpenCL
    Use AMDFFT:                  NO
    Use AMDBLAS:                 NO

  Python 2:
    Interpreter:                 /usr/local/opt/python/libexec/bin/python (ver 2.7.14)
    Libraries:                   /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib (ver 2.7.14)
    numpy:                       /usr/local/lib/python2.7/site-packages/numpy/core/include (ver 1.13.3)
    packages path:               lib/python2.7/site-packages

  Python 3:
    Interpreter:                 /usr/local/opt/python3/bin/python3 (ver 3.6.3)
    Libraries:                   /usr/local/opt/python3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin/libpython3.6.dylib (ver 3.6.3)
    numpy:                       /usr/local/lib/python3.6/site-packages/numpy/core/include (ver 1.13.3)
    packages path:               lib/python3.6/site-packages

  Python (for build):            /usr/local/opt/python/libexec/bin/python
    Pylint:                      ~/libraries/anaconda2/bin/pylint (ver: 1.6.4, checks: 110)

  Java:
    ant:                         NO
    JNI:                         /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include/darwin /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:                        Matlab not found or implicitly disabled

  Documentation:
    Doxygen:                     NO

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              NO

  Install path:                  /usr/local/Cellar/opencv/3.3.1_1

  cvconfig.h is in:              /tmp/opencv-20171203-13938-dlhhkn/opencv-3.3.1/build
-----------------------------------------------------------------

これで、macでopencv3を使えます!

Anacondaでopencv3を利用する場合

Python3 OpenCV3 for Linux/Mac をインストールする

上記のサイトを参考にさせていただきました。

すでに作ってあった環境で試してみる。

$ source activate face_authentication
$ python -V
Python 3.5.3 :: Continuum Analytics, Inc.

opencvをインストール

$ conda install --channel https://conda.anaconda.org/menpo opencv3

確認(失敗編)

$ python -c "import cv2; print(cv2.getBuildInformation())"
Error in sitecustomize; set PYTHONVERBOSE for traceback:
KeyError: 'PYTHONPATH'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type
  Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
  Expected in: flat namespace
 in /usr/local/lib/python2.7/site-packages/cv2.so

なんかエラーが出てる。

原因

pyenvが壊れた?PYTHONPATHエラーでPython3.xが起動時に2.xのモジュールを参照する件

調べてみると、どうやらhomebrewで設定しているパスがおかしかったみたいです。 ってか最初のopencvのインストールで余計なことをやっていた可能性が高いです。。。

余計なことをやったと思われるコマンド

$ echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> ~/.local/lib/python3.5/site-packages/homebrew.pth

対応

$ cat ~/.local/lib/python3.5/site-packages/homebrew.pth
import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")

環境はpython3.5なので、修正をする。

$ vi ~/.local/lib/python3.5/site-packages/homebrew.pth
import site; site.addsitedir("/usr/local/lib/python3.5/site-packages")

再確認

$ python -c "import cv2; print(cv2.getBuildInformation())"
~~省略~~
Video I/O:
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      NO
      avcodec:                   NO
      avformat:                  NO
      avutil:                    NO
      swscale:                   NO
      avresample:                NO
    GStreamer:                   NO
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    Aravis SDK:                  NO
    AVFoundation:                YES
    V4L/V4L2:                    NO/NO
    XIMEA:                       NO
    gPhoto2:                     NO

ビンゴ!エラーがなくなりました! しかし、VIdeo I/OがNoですね。anaconda環境だとどうやってインストールするのだろうか。 とりあえずインストール自体はできたので、今回はここまで。

気になったのでメモ

Install OpenCV for Python on Mac OS X or Linux

$ pip install moviepy