【目次】
【サイト内の関連ページ】
【用語説明】
Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
Dlib には,Convolutional Neural Network (CNN) による顔検出の機能がある.
Dlib による顔検出の例
顔検出が行われ,顔を囲むようなバウンディングボックス (bounding box) が表示される.
バウンディングボックスの座標値が数値データとして得られる
顔のランドマーク検知が行われ,顔を囲むようなバウンディングボックス (bounding box) と,顔のランドマークが表示される.
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
【サイト内の関連ページ】
Windows での cmake のインストール: 別ページ »で説明
【関連する外部ページ】
cmake の公式ダウンロードページ: https://cmake.org/download/
7-Zip 22.01 のインストール
Windows では, コマンドプロンプトを管理者として開き, 次のコマンドを実行することにより, 7-Zip 22.01 のインストールを行うことができる.
mkdir %HOMEPATH%\7zip cd %HOMEPATH%\7zip curl -O https://www.7-zip.org/a/7z2201-x64.exe .\7z2201-x64.exe call powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\7-Zip\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
【サイト内の関連ページ】
Windows での 7-Zip のインストール: 別ページ »で説明
【関連する外部ページ】
7-Zip の公式ページ: https://sevenzip.osdn.jp/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
Windows での Visual Studio Community 2022 のインストール: 別ページ »で説明
Visual Studio Community 2022 に, Build Tools for Visual Studio 2022の機能が含まれている.
Windows での Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022) のインストール: 別ページ »で説明
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行.
「python -m pip install -U dlib」はエラーが出やすい可能性があるため, よりエラーが出にくいと思われる,次の手順でインストールする.
python -m pip uninstall -y dlib cd C:\ rmdir /s /q dlib git clone https://github.com/davisking/dlib cd C:\dlib python setup.py build --no DLIB_GIF_SUPPORT python setup.py install --no DLIB_GIF_SUPPORT
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import dlib; print( dlib.__version__ )"
次のコマンドを実行.
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
cd C:\dlib cd python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
Dlib に付属のサンプルプログラムは,examplesの下にある.次の手順でビルドする.
http://dlib.net/compile.html の記述に従う
コマンドプロンプトを管理者として実行: 別ページ »で説明
cd C:\dlib cd examples rmdir /s /q build mkdir build cd build cmake .. -G "Visual Studio 17 2022" -A x64 -T host=x64 ^ -DCUDA_SDK_ROOT_DIR="c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8" ^ -DCUDA_TOOLKIT_ROOT_DIR="c:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.8" ^ -DCUDA_NVCC_FLAGS="-allow-unsupported-compiler" ^ -DCUDA_NVCC_FLAGS_DEBUG="-allow-unsupported-compiler" ^ -DCMAKE_INSTALL_PREFIX=c:/dlib ^ -DDLIB_GIF_SUPPORT=OFF
エラーメッセージが出ていないこと
cmake --build . --config RELEASE
画像分類:画像(1つ)から、所定のラベル(複数)のうち1つを推定すること。あるいは、複数を推定すること。推定するとき、その確率とともに得ることもある
Dlib は C:\dlib にインストールされているとする
curl でなく wget を使いたいときは, 「wget http://dlib.net/files/resnet34_1000_imagenet_classifier.dnn.bz2」のように操作する.
cd C:\dlib cd examples\build curl -O http://dlib.net/files/resnet34_1000_imagenet_classifier.dnn.bz2 "c:\Program Files\7-Zip\7z.exe" x resnet34_1000_imagenet_classifier.dnn.bz2
画像分類される画像は,「faces\*.jpg」で指定している.
cd C:\dlib cd examples\build for %i in (..\faces\*.jpg) do .\release\dnn_imagenet_ex %i
画像分類される画像
物体検出:画像から、ある種類の物体を検出し、その位置と大きさを得る。1つの画像の中に、物体が複数あることもある。
Max-Margin 物体検出法が利用されている(文献は次の通り) それにより,Dlib による文体検出が行われる.
Davis E. King, Max-Margin Object Detection, CoRR, abs/1502.00046, 2015.
学習済みモデルのダウンロード
curl でなく wget を使いたいときは, 「wget http://dlib.net/files/mmod_rear_end_vehicle_detector.dat.bz2」のように操作する.
cd C:\dlib cd examples\build curl -O http://dlib.net/files/mmod_rear_end_vehicle_detector.dat.bz2 "c:\Program Files\7-Zip\7z.exe" x mmod_rear_end_vehicle_detector.dat.bz2
物体検出の実行(車の正面)
cd C:\dlib cd examples\build .\release\dnn_mmod_find_cars_ex
学習済みモデルのダウンロード
curl でなく wget を使いたいときは, 「wget http://dlib.net/files/mmod_front_and_rear_end_vehicle_detector.dat.bz2」のように操作する.
cd C:\dlib cd examples\build curl -O http://dlib.net/files/mmod_front_and_rear_end_vehicle_detector.dat.bz2 "c:\Program Files\7-Zip\7z.exe" x mmod_front_and_rear_end_vehicle_detector.dat.bz2
物体検出の実行(車の正面と背面)
cd C:\dlib cd examples\build .\release\dnn_mmod_find_cars2_ex
cd C:\dlib cd examples\build .\release\video_tracking_ex ..\video_frames
学習済みモデルのダウンロード
cd C:\dlib cd examples\build curl -O http://dlib.net/files/mmod_dog_hipsterizer.dat.bz2 "c:\Program Files\7-Zip\7z.exe" x mmod_dog_hipsterizer.dat.bz2
実行してみる
cd C:\dlib cd examples\build .\release\dnn_mmod_dog_hipsterizer mmod_dog_hipsterizer.dat ..\faces\dogs.jpg
Dlib による顔検出 を行う.
%HOMEPATH%\dlib\examples\faces の下の顔画像のファイルを確認する
Python プログラムの実行(Windows 上)
cd C:\dlib cd python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
結果が表示されるまで少し待つ
cd C:\dlib cd python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001009.jpg
cd C:\dlib cd python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001322.jpg
顔ランドマークの検出 (face landmark detector) のサンプルプログラムを実行してみる
顔ごとに,最大で 68 のランドマーク (68 landmarks) が得られる.
cd C:\dlib cd python_examples python face_landmark_detection.py shape_predictor_68_face_landmarks.dat ..\examples\faces\
利用条件などは各自で確認のこと
Python プログラムの実行
Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
cd C:\dlib cd python_examples python face_alignment.py shape_predictor_68_face_landmarks.dat ..\examples\faces\2007_007763.jpg
増量は、学習で重要となる
cd C:\dlib cd python_examples python face_jitter.py shape_predictor_68_face_landmarks.dat
元画像として何を使うかは、プログラム中で設定されている
cd C:\dlib cd python_examples python face_landmark_detection.py shape_predictor_5_face_landmarks.dat ..\examples\faces
cd C:\dlib cd python_examples python face_landmark_detection.py shape_predictor_68_face_landmarks.dat ..\examples\faces
cd C:\dlib cd python_examples python face_recognition.py shape_predictor_5_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ..\examples\faces
顔ごとに 128個の数値(特徴量ベクトル)が得られることを確認
cd C:\dlib cd python_examples python face_recognition.py shape_predictor_68_face_landmarks.dat dlib_face_recognition_resnet_model_v1.dat ..\examples\faces
顔ごとに 128個の数値(特徴量ベクトル)が得られることを確認
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行する.
python -m pip install -U numpy scikit-image scikit-learn
cd %HOMEPATH% rmdir /s /q face_recognition git clone https://github.com/ageitgey/face_recognition cd face_recognition copy C:\dlib\python_examples\shape_predictor_68_face_landmarks.dat . python setup.py build python setup.py install
コマンドプロンプトで次のコマンドを実行
mkdir %HOMEPATH%\face_recognition\known_people mkdir %HOMEPATH%\face_recognition\unknown_pictures
%HOMEPATH%\face_recognition\examples にある顔画像のファイル 「biden.jpg」と「obama.jpg」を,先ほど作成した ディレクトリ known_people の下にコピー
copy %HOMEPATH%\face_recognition\examples\biden.jpg %HOMEPATH%\face_recognition\known_people copy %HOMEPATH%\face_recognition\examples\obama.jpg %HOMEPATH%\face_recognition\known_people
copy %HOMEPATH%\face_recognition\examples\two_people.jpg %HOMEPATH%\face_recognition\unknown_pictures
cd %HOMEPATH%\face_recognition face_detection --model cnn unknown_pictures
表示される4つの数字は、顔領域の座標値
unknown_pictures の下にある画像ファイルを、 known_people 下の画像ファイルを照合して顔識別を行うコマンド
cd %HOMEPATH%\face_recognition face_recognition --show-distance true known_people unknown_pictures
※ その他,Dlib を用いた顔検出、顔識別などについては, 別ページ »にまとめ