macでdlibのimglabというGUIツールを使用する際の作業メモです。
■環境
macOS High Siera(10.13.3)
■インストール手順
●python3にする(必要ないかもしれませんが)
1 |
pyenv install 3.6.0 |
下記のようなエラーが出る場合は、
1 |
zipimport.ZipImportError: can't decompress data; zlib not available |
1 |
xcode-select --install |
また、下記のようなエラーの場合、
1 |
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib? |
1 |
CFLAGS="-I$(brew --prefix openssl)/include" LDFLAGS="-L$(brew --prefix openssl)/lib" pyenv install -v 3.6.0 |
でインストールを試みると入ります。
エラーが出なければ一度確認してみます。
1 2 3 4 |
$ pyenv versions system * 2.7.11 (set by /Users/***/.pyenv/version) 3.6.0 |
3.6.0に切り替えて確認。
1 2 3 4 5 6 7 8 |
$ pyenv global 3.6.0 $ pyenv rehash $ pyenv versions system 2.7.11 * 3.6.0 (set by /Users/***/.pyenv/version) $ python --version Python 3.6.0 |
●boostをインストール
1 |
brew install boost |
最後にこんなログが出ます。
1 2 3 4 |
==> Downloading https://homebrew.bintray.com/bottles/boost-1.67.0_1.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring boost-1.67.0_1.high_sierra.bottle.tar.gz /usr/local/Cellar/boost/1.67.0_1: 13,506 files, 451.0MB |
次に、
1 |
$brew install boost-python |
問題なければ下記のようなログが出ます。
1 2 3 4 |
==> Downloading https://homebrew.bintray.com/bottles/boost-python-1.67.0.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring boost-python-1.67.0.high_sierra.bottle.tar.gz /usr/local/Cellar/boost-python/1.67.0: 459 files, 17.3MB |
●cmakeをインストール
1 |
brew install cmake |
ログはこんな感じです。
1 2 3 4 5 6 7 8 |
==> Downloading https://homebrew.bintray.com/bottles/cmake-3.11.4.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring cmake-3.11.4.high_sierra.bottle.tar.gz ==> Caveats Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/cmake ==> Summary 🍺 /usr/local/Cellar/cmake/3.11.4: 2,363 files, 32.7MB |
●opencvをインストール
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$ brew install opencv ログはこんな感じです。 ==> Summary 🍺 /usr/local/Cellar/python@2/2.7.15_1: 4,673 files, 82.8MB ==> Installing opencv dependency: numpy ==> Downloading https://homebrew.bintray.com/bottles/numpy-1.14.5_1.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring numpy-1.14.5_1.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/numpy/1.14.5_1: 939 files, 24.9MB ==> Installing opencv dependency: tbb ==> Downloading https://homebrew.bintray.com/bottles/tbb-2018_U5.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring tbb-2018_U5.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/tbb/2018_U5: 133 files, 2.7MB ==> Installing opencv ==> Downloading https://homebrew.bintray.com/bottles/opencv-3.4.1_6.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring opencv-3.4.1_6.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/opencv/3.4.1_6: 600 files, 214.8MB |
●pipで必要そうなものを入れる
1 2 |
pip install --upgrade setuptools pip install opencv-python |
ついでなので、
1 2 3 4 |
pip install numpy pip install matplotlib pip install pillow pip install scikit-image |
この辺りも入れておきます。
※pipが古いと言われたらアップデート
1 |
pip install --upgrade pip |
●dlibをインストール
dlibを使うだけなら pip install dlibで入るのですが、今回はimglabを使う事が目的ですので、ソースからビルドします。
1 2 3 |
http://dlib.net/ から最新版(2018.07.01時点ではdlib-19.13)をダウンロードして解凍 cd dlib-19.13 sudo python setup.py install |
色々ログが出て最後に、
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[100%] Built target dlib_python creating build/bdist.macosx-10.13-x86_64 creating build/bdist.macosx-10.13-x86_64/egg copying build/lib.macosx-10.13-x86_64-3.6/dlib.cpython-36m-darwin.so -> build/bdist.macosx-10.13-x86_64/egg creating stub loader for dlib.cpython-36m-darwin.so byte-compiling build/bdist.macosx-10.13-x86_64/egg/dlib.py to dlib.cpython-36.pyc creating build/bdist.macosx-10.13-x86_64/egg/EGG-INFO copying dlib.egg-info/PKG-INFO -> build/bdist.macosx-10.13-x86_64/egg/EGG-INFO copying dlib.egg-info/SOURCES.txt -> build/bdist.macosx-10.13-x86_64/egg/EGG-INFO copying dlib.egg-info/dependency_links.txt -> build/bdist.macosx-10.13-x86_64/egg/EGG-INFO copying dlib.egg-info/not-zip-safe -> build/bdist.macosx-10.13-x86_64/egg/EGG-INFO copying dlib.egg-info/top_level.txt -> build/bdist.macosx-10.13-x86_64/egg/EGG-INFO writing build/bdist.macosx-10.13-x86_64/egg/EGG-INFO/native_libs.txt creating dist creating 'dist/dlib-19.13.0-py3.6-macosx-10.13-x86_64.egg' and adding 'build/bdist.macosx-10.13-x86_64/egg' to it removing 'build/bdist.macosx-10.13-x86_64/egg' (and everything under it) Processing dlib-19.13.0-py3.6-macosx-10.13-x86_64.egg creating /Users/***/.pyenv/versions/3.6.0/lib/python3.6/site-packages/dlib-19.13.0-py3.6-macosx-10.13-x86_64.egg Extracting dlib-19.13.0-py3.6-macosx-10.13-x86_64.egg to /Users/***/.pyenv/versions/3.6.0/lib/python3.6/site-packages Adding dlib 19.13.0 to easy-install.pth file Installed /Users/***/.pyenv/versions/3.6.0/lib/python3.6/site-packages/dlib-19.13.0-py3.6-macosx-10.13-x86_64.egg Processing dependencies for dlib==19.13.0 Finished processing dependencies for dlib==19.13.0 |
こんな感じになります。
次にimglabをビルドするのですが、いきなりやると下記のようなエラーが出る場合があります。
1 2 3 4 5 6 |
dlib/dlib-19.13/dlib/../dlib/gui_core/gui_core_kernel_2.h:11:2: error: "DLIB_NO_GUI_SUPPORT is defined so you can't use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it." #error "DLIB_NO_GUI_SUPPORT is defined so you can't use the GUI code. Turn DLIB_NO_GUI_SUPPORT off if you want to use it." ^ dlib/dlib-19.13/dlib/../dlib/gui_core/gui_core_kernel_2.h:12:2: error: "Also make sure you have libx11-dev installed on your system" #error "Also make sure you have libx11-dev installed on your system" |
この場合は、XQuartzのインストールとCmakeファイルの編集が必要です。
●XQuartzをインストール
1 2 3 4 5 |
https://www.xquartz.org/ からXQuartzというソフトウェアをダウンロードしてインストールする。 export CPPFLAGS=-I/opt/X11/include ln -s /opt/X11/include/X11 /usr/local/include/X11 |
●CMakeCache.txtの編集
1 2 3 4 |
dlib-19.13/tools/imglab/build/CMakeCache.txt の DLIB_NO_GUI_SUPPORT:STRING=OFF の OFF を ON に変更します |
●imglabのビルド
1 2 3 4 5 6 |
cd dlib-19.13/tools/imglab mkdir build cd build cmake .. cmake --build . --config Release sudo make install |
問題なければ、ログは下記のような感じです。
1 2 3 4 5 6 7 |
[ 91%] Built target dlib [100%] Built target imglab Install the project... -- Install configuration: "Release" -- Installing: /usr/local/bin/imglab -- Installing: /usr/local/bin/convert_imglab_paths_to_relative -- Installing: /usr/local/bin/copy_imglab_dataset |
一度確認してみます。
1 2 3 4 5 |
$ ./imglab -v imglab v1.14 Compiled: 18:26:49 Jul 1 2018 Written by Davis King Check for updates at http://dlib.net |
■imglab使い方
学習させたい画像を集めたディレクトリ(仮にimagesとします)を作成して
1 2 3 |
cd images imglab -c imagedata.xml . imglab imagedata.xml |
XQuartzのウィンドウが立ちあがって、ファイル一覧とともに画像が表示されるアプリが起動するので、shiftを押しながらマウスドラッグで領域を指定していって、最後にsaveすると、
imagedata.xml
image_metadata_stylesheet.xsl
の二つのファイルができている。
※領域失敗した時はダブルクリックして(枠が水色になる)deleteキーで削除できます。
また、矩形の中にパーツを指定する場合は、起動オプションに –parts “01 02 03 04 05 06 ….”のようにパーツのラベルを指定します。
1 2 |
imglab --parts "01 02 03 04 05 06 ...." imagedata.xml というようにパーツのポイントのラベルを指定する |
矩形をダブルクリックして右クリックすると引数で指定したラベル一覧がリスト表示されるので、選択するとその点がポイントになる。
トレーニングは、python_examples/train_object_detector.pyの中身を今回作成したxmlやディレクトリを処理するように変更すれば、.svm ファイルが作成されます。