Dlibは,機械学習のアルゴリズムの機能を持つソフトウエア.
利用条件などは利用者において確認してください
【サイト内の関連ページ】
謝辞
Dlib の作者に感謝します
システム Python を使うことができる(その場合,Python のインストールは行わない)
システム Python を用いるときは,pip, setuptools の更新は次のコマンドで行う.
sudo apt -y update sudo apt -y install python3-pip python3-setuptools
Ubuntu で,システム Python 以外の Python をインストールしたい場合は pyenv が便利である: 別ページで説明している.
Python の URL: http://www.python.org/
【Python, pip の使い方】
Python, pip は,次のコマンドで起動できる.
【Python 開発環境のインストール】
JupyterLab, spyder, nteract (Python 開発環境) のインストールは, Windows でコマンドプロンプトを管理者として実行し, 次のコマンドを実行.
python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
詳しくは,: 別ページで説明している.
JupyterLab, spyder, nteract (Python 開発環境) のインストール: : 別ページで説明している.
Dlib は c:\dlib にインストールされているとして,以下,説明する.
cmake のダウンロード URL: https://cmake.org/download/
システム環境変数Pathに追加を忘れないこと: C:\Program Files (x86)\GnuWin32\bin
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U numpy scikit-image
端末で,次のコマンドを実行.
sudo apt -y update sudo apt -y install python3-numpy python3-skimage
前準備として、Windows のときは マイクロソフト C++ ビルドツール (Build Tools) のインストールが終わっていること
cd c:\dlib cd tools\imglab rmdir /s /q build mkdir build cd build cmake -G "Visual Studio 16 2019" -A x64 -T host=x64 ^ -DCMAKE_INSTALL_PREFIX="c:\dlib" .. cmake --build . --config RELEASE cmake --build . --config RELEASE --target INSTALL
結果を確認.エラーメッセージが出ていないこと
※ ここで,「c:\dlib」は,Dlib をインストールしたディレクトリに読み替えること。
※ まだ Dlib のインストールを行っていないときは、 Dlib のインストールを行うこと.
c:\dlib\tools\imglab\Release
Windowsのコマンドプロンプトを開き、次のコマンドを実行.エラーメッセージが出ないことを確認.
「.\imglab.exe -c mydata.xml .」は、.xml 形式ファイルを作るコマンド.ファイル名 mydata.xml は何でもよい
C: cd c:\dlib\tools\imglab\Release .\imglab.exe -c mydata.xml .
Windowsのコマンドプロンプトで、次のコマンドを実行.
.\imglab.exe mydata.xml
imglab は,画像ビューワになっている
シフトキー(SHIFT キー)を押しながら、マウスをドラッグ<
「File」→「Save」」と操作する
指定した領域についての情報が保存される
写真は https://ccphotosearch.com/ を利用して、クリエイティブコモンズのものを使用.領域指定は手作業で実施.
※ Windows での展開(解凍)のためのソフトには,「7-Zip」などがある.
14枚の画像について、 領域を指定済み
Google Chrome ではうまく表示できない.下の図では Firefox での表示結果を示している.
Windowsのコマンドプロンプトで、次のコマンドを実行.
C: cd c:\dlib\tools\imglab\Release .\imglab.exe training.xml
最後の「.」を忘れないこと
python c:\dlib\python_examples\train_object_detector.py .
謝辞:http://tadaoyamaoka.hatenablog.com/entry/2016/09/21/184955 のものを参考に(少し変更して)使っています
import os
import sys
import glob
import dlib
from skimage import io
if len(sys.argv) != 2:
exit()
f = sys.argv[1]
detector = dlib.simple_object_detector("detector.svm")
win_det = dlib.image_window()
win_det.set_image(detector)
win = dlib.image_window()
print("Processing file: {}".format(f))
img = io.imread(f)
dets = detector(img)
print("Number of objects detected: {}".format(len(dets)))
for k, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue()
python hoge.py 5920652345_5be61ae940.jpg
以上、学習と、画像分類を示した(上の犬の写真では、画像分類がうまくいっていない)
.\imglab training.xml