読者です 読者をやめる 読者になる 読者になる

MacにOpenCV3を入れてAKAZA特徴量

OS X Yosemite 10.10.5

OpenCV 3

MacOpenCV 3を入れてAKAZE特徴量を試してみたかったのでメモ。

↓の記事を参考にOpenCV3をインストール。
https://blog.ymyzk.com/2015/07/os-x-opencv-3-python-2-3/

brew tap homebrew/science
brew tap homebrew/python
brew install python
brew install python3
brew install numpy --with-python3
brew list
brewでnumpyのエラー
brew install numpy --with-python3

Error: No available formula with the name "numpy" 
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
This formula was found in a tap:
homebrew/python/numpy
To install it, run:
  brew install homebrew/python/numpy
==> You haven't updated Homebrew in a while.
A formula for numpy might have been added recently.
Run `brew update` to get the latest Homebrew updates!

Error: No available formula for numpy · Issue #36556 · Homebrew/legacy-homebrew · GitHub

brew tap homebrew/python

をしていなかった。

エラー2

brew で numpy を入れようとしたらエラー。

brew install numpy --with-python3
==> Installing numpy from homebrew/python
==> 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.11.0.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.11.0/libexec/nose --single-version-ext
==> python setup.py build --fcompiler=gnu95 --parallel=4 install --prefix=/usr/local/Cellar/numpy/1.11.0 --single-version-external
==> 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.11.0/libexec/nose --single-version-ex
==> python3 setup.py build --fcompiler=gnu95 --parallel=4 install --prefix=/usr/local/Cellar/numpy/1.11.0 --single-version-externa
Error: parent directory is world writable but not sticky
Please report this bug:
    https://git.io/brew-troubleshooting
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tmpdir.rb:92:in `mktmpdir'
/usr/local/Library/Homebrew/utils/fork.rb:6:in `safe_fork'
/usr/local/Library/Homebrew/formula_installer.rb:574:in `build'
/usr/local/Library/Homebrew/formula_installer.rb:236:in `install'
/usr/local/Library/Homebrew/cmd/install.rb:275:in `install_formula'
/usr/local/Library/Homebrew/cmd/install.rb:149:in `block in install'
/usr/local/Library/Homebrew/cmd/install.rb:149:in `each'
/usr/local/Library/Homebrew/cmd/install.rb:149:in `install'
/usr/local/Library/brew.rb:87:in `<main>'

https://github.com/Homebrew/legacy-homebrew/issues/39475

brew doctor

Warning: /tmp is world-writable but does not have the sticky bit set. Please execute sudo chmod +t /tmp in your Terminal.

sudo chmod +t /tmp

で直った。
以前にbrewをsudo brewで実行していたのかもしれない。

エラー3

対話式Pythonでnumpyをimportしたらエラーになった。

python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python2.7/site-packages/numpy/core/__init__.py", line 58, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "/usr/local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 12, in <module>
    from . import decorators as dec
  File "/usr/local/lib/python2.7/site-packages/numpy/testing/decorators.py", line 21, in <module>
    from .utils import SkipTest
  File "/usr/local/lib/python2.7/site-packages/numpy/testing/utils.py", line 15, in <module>
    from tempfile import mkdtemp, mkstemp
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
    import io as _io
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/io.py", line 51, in <module>
    import _io
ImportError: dlopen(/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so, 2): Symbol not found: __PyCodecInfo_GetIncrementalDecoder
  Referenced from: /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so
  Expected in: flat namespace
 in /usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/_io.so

brew doctor
の警告メッセージにしたがって、.zshrcに設定を追加。

Warning: Homebrew's sbin was not found in your PATH but you have installed
formulae that put executables in /usr/local/sbin.
Consider setting the PATH for example like so
  echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc

新しくTerminalを立ち上げて、Pythonを実行したら直った。
sourceで~/.zshrcを読み込んでもいいらしい。

virtualenv

virtualenvの使い方を忘れた。。
~/.virtualenvs/
の下に前に作ったvirtualenv環境があるけど、新しく作れない。
virtualenvをインストールした。

pip install virtualenv
cd
cd .virtualenvs/
virtualenv -p /usr/local/bin/python2.7 cv
source cv/bin/activate

以前は、PyCharmからvirtualevnを作ったのかもしれない。

cd ~/.virtualenvs/cv/lib/python2.7
ln -s /usr/local/Cellar/opencv3/3.1.0_3/lib/python2.7/site-packages/cv2.so
~/Documents/opencv% python
Python 2.7.11 (default, Jan 22 2016, 08:28:37) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> 
>>> cv2.__version__
'3.1.0'
>>> 

AKAZE

↓の記事のコードをコピペしてAKAZEを試す。
python/OpenCV3.0でAKAZE特徴量を用いたマッチング - Qiita

Wikipediaから、とりあえずペンギンの画像を撮ってきて保存。
PyCharmで実行。

img_path
img_path2
を書き換えて実行。

f:id:kubotti:20160519165630p:plain