imglab を用いた画像のアノテーション(Dlib, Dlib の imglab, Python を使用)(Windows 上)
Dlibは,機械学習のアルゴリズムの機能を持つソフトウェア.
利用条件などは利用者において確認してください
【サイト内の関連ページ】
- 説明資料: Dlib の機能概要 [PDF], [パワーポイント]
謝辞
Dlib の作者に感謝します
前準備
Python のインストール(Windows上)
注:既にPython(バージョン3.12を推奨)がインストール済みの場合は,この手順は不要である.
winget(Windowsパッケージマネージャー)を使用してインストールを行う
- Windowsで,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。 - winget(Windowsパッケージマネージャー)が利用可能か確認する:
winget --version
- Pythonのインストール(下のコマンドにより Python 3.12 がインストールされる).
reg add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f REM Python をシステム領域にインストール winget install --scope machine --id Python.Python.3.12 -e --silent REM Python のパス set "INSTALL_PATH=C:\Program Files\Python312" echo "%PATH%" | find /i "%INSTALL_PATH%" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%" /M >nul echo "%PATH%" | find /i "%INSTALL_PATH%\Scripts" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%\Scripts" /M >nul
- Python詳細ガイド:Pythonまとめ »
【関連する外部サイト】
【サイト内の関連ページ】
Visual Studio 2022 Build Toolsとランタイムのインストール
管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要である。
REM Visual Studio 2022 Build Toolsとランタイムのインストール
winget install --scope machine Microsoft.VisualStudio.2022.BuildTools Microsoft.VCRedist.2015+.x64
set VS_INSTALLER="C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe"
set VS_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
REM C++開発ワークロードのインストール
%VS_INSTALLER% modify --installPath %VS_PATH% ^
--add Microsoft.VisualStudio.Workload.VCTools ^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
--add Microsoft.VisualStudio.Component.Windows11SDK.22621 ^
--includeRecommended --quiet --norestart
Git のインストール(Windows 上)
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】 Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】 Git の公式ページ: https://git-scm.com/
CMake のインストール
CMake の公式ダウンロードページ: https://cmake.org/download/
Gitのインストール
管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。
REM Git をシステム領域にインストール
7-Zip のインストール
管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。
REM 7-Zip をシステム領域にインストール
winget install --scope machine --id 7zip.7zip -e --silent
REM 7-Zip のパス設定
set "SEVENZIP_PATH=C:\Program Files\7-Zip"
if exist "%SEVENZIP_PATH%" (
echo "%PATH%" | find /i "%SEVENZIP_PATH%" >nul
if errorlevel 1 setx PATH "%PATH%;%SEVENZIP_PATH%" /M >nul
)
Visual Studio 2022 Build Toolsとランタイムのインストール
管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要である。
REM Visual Studio 2022 Build Toolsとランタイムのインストール
winget install --scope machine Microsoft.VisualStudio.2022.BuildTools Microsoft.VCRedist.2015+.x64
set VS_INSTALLER="C:\Program Files (x86)\Microsoft Visual Studio\Installer\setup.exe"
set VS_PATH="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools"
REM C++開発ワークロードのインストール
%VS_INSTALLER% modify --installPath %VS_PATH% ^
--add Microsoft.VisualStudio.Workload.VCTools ^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
--add Microsoft.VisualStudio.Component.Windows11SDK.22621 ^
--includeRecommended --quiet --norestart
Dlib のインストール
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。
- 次のコマンドを実行する.
python -m pip install -U dlib
Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。
- Dlib のソースコード等のダウンロード
次のコマンドを実行.
cd C:\
rmdir /s /q dlib
git clone https://github.com/davisking/dlib
- Dlib の学習済みモデルのダウンロード
次のコマンドを実行.
cd C:\dlib
cd python_examples
curl -O http://dlib.net/files/mmod_human_face_detector.dat.bz2
curl -O http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2
curl -O http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2
curl -O http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
"c:\Program Files\7-Zip\7z.exe" x mmod_human_face_detector.dat.bz2
"c:\Program Files\7-Zip\7z.exe" x dlib_face_recognition_resnet_model_v1.dat.bz2
"c:\Program Files\7-Zip\7z.exe" x shape_predictor_5_face_landmarks.dat.bz2
"c:\Program Files\7-Zip\7z.exe" x shape_predictor_68_face_landmarks.dat.bz2
del mmod_human_face_detector.dat.bz2
del dlib_face_recognition_resnet_model_v1.dat.bz2
del shape_predictor_5_face_landmarks.dat.bz2
del shape_predictor_68_face_landmarks.dat.bz2
Python 用 numpy, scikit-image のインストール
Windows の場合
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。
- 次のコマンドを実行する.
python -m pip install -U numpy scikit-image
Ubuntu の場合
Ubuntu を使用する場合は,次のように操作する.
# パッケージリストの情報を更新
sudo apt update
sudo apt -y install python3-numpy python3-skimage
Dlib の imglab のインストール
前準備として、Windows のときは
マイクロソフト C++ ビルドツール (Build Tools) のインストールが終わっていること
結果を確認.エラーメッセージが出ていないこと
ここで,「%HOMEPATH%\dlib」は,Dlib のソースコードを展開したディレクトリに読み替えること。
imglab を使ってみる
- 画像を数枚準備する
- 準備した画像ファイルを,次のディレクトリに置く
%HOMEPATH%\dlib\bin
- 画像ファイルの準備が終わったら、
.xml 形式ファイルを作る.
Windows のコマンドプロンプトを開き,次のコマンドを実行する.エラーメッセージが出ないことを確認.
「.\imglab.exe -c mydata.xml .」は、.xml 形式ファイルを作るコマンド.ファイル名 mydata.xml は何でもよい
C:
cd C:\dlib\bin
.\imglab.exe -c mydata.xml .
- 生成された .xml 形式ファイルをエディタなどで開き確認してみる.
notepad mydata.xml
- imglab を起動してみる
Windows のコマンドプロンプトで、次のコマンドを実行する.
.\imglab.exe mydata.xml
imglab は,画像ビューワになっている
- 複数の画像を簡単に表示できる機能がある。
- ラベル(label)を編集する機能がある (Next Labelのところに書き込む)
- 領域を指定する機能がある (シフトキー(SHIFT キー)を押しながら、マウスをドラッグ)
- imglab で、領域を指定してみる
シフトキー(SHIFT キー)を押しながら、マウスをドラッグ<
- 保存する
「File」→「Save」」と操作する
- 確かに .xml形式ファイルに,結果が保存される
指定した領域についての情報が保存される
Dlib を用いた学習
- 教材ファイル Release.zip をダウンロードし,%HOMEPATH%\dlib\bin に置く
C:
cd C:\dlib\bin
curl -O https://www.kkaneko.jp/sample/dog/Release.zip
"c:\Program Files\7-Zip\7z.exe" x Release.zip
- Web ブラウザで training.xml を開いて、確認してみる
14枚の画像について、
領域を指定済み
Google Chrome ではうまく表示できない.下の図では Firefox での表示結果を示している.
- imglab を起動して確認してみる
Windows のコマンドプロンプトで、次のコマンドを実行する.
C:
cd C:\dlib\bin
.\imglab.exe training.xml
- imglab を閉じる
- 学習を行う(教師データの生成)
最後の「.」を忘れないこと
copy %HOMEPATH%\dlib\examples\faces\*.jpg .
python C:\dlib\python_examples\train_object_detector.py .
- 途中で、Enterキーを押して続行
-
Python プログラムの実行
謝辞:http://tadaoyamaoka.hatenablog.com/entry/2016/09/21/184955 のものを参考に(少し変更して)使っています
Python プログラムの実行(Windows 上)
- python: python
- pip: python -m pip または pip
- Jupyter Qt Console: jupyter qtconsole
- Jupyter ノートブック (Jupyter Notebook): jupyter notebook
- Jupyter Lab: jupyter lab あるいは python -m jupyter lab
- Nteract: jupyter nteract あるいは python -m jupyter nteract
- Spyder: spyder
コマンドプロンプトで次を実行
python
次の Python プログラムを実行
import os
import sys
import glob
import dlib
from skimage import io
f = '5920652345_5be61ae940.jpg'
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()
上の犬の写真では、画像分類がうまくいっていない
- 学習に使う画像を増やすには
.\imglab training.xml