金子邦彦研究室人工知能Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア)Dlib Python,face_recognition のインストールと動作確認(顔検出,顔ランドマーク,顔識別)(Python を使用)(Windows 上)

Dlib Python,face_recognition のインストールと動作確認(顔検出,顔ランドマーク,顔識別)(Python を使用)(Windows 上)

目次

  1. 前準備
  2. Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
  3. Dlib に付属のサンプルプログラム(example 下)のビルド
  4. 画像分類,物体検出,トラッキング・ビジョン,犬の顔画像について眼鏡をあてはめる(Dlib に付属のサンプルプログラムを使用)
  5. Dlib による顔検出(Python を使用)
  6. Dlib による顔ランドマークの検出(Python を使用)
  7. Dlib による顔のアラインメント,顔データのデータ拡張,5ランドマーク,68 ランドマーク,顔のコード化(5ランドマークを使用),顔のコード化(68 ランドマークを使用)(Python を使用)
  8. ageitgey/ageitgey の face_recognition のインストール
  9. ageitgey の face_recognition で顔識別を行ってみる(Python を使用)

サイト内の関連ページ

用語説明

  • 顔ランドマークの検出 (face landmark detector): 画像から 68 のランドマーク (68 landmarks) を得る. tree structured part model により,最大 68 のランドマークを得る

    顔のランドマーク検知が行われ,顔を囲むようなバウンディングボックス (bounding box) と,顔のランドマーク表示される.

    [image]
  • 1. 前準備

    Git のインストール(Windows 上)

    Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.

    サイト内の関連ページ

    Windows での Git のインストール: 別ページ »で説明している.

    関連する外部ページ

    Git の公式ページ: https://git-scm.com/

    CMake のインストール(Windows 上)

    CMake はビルドツールである.

    サイト内の関連ページ

    Windows での cmake のインストール: 別ページ »で説明している.

    関連する外部ページ

    CMake の公式ダウンロードページ: https://cmake.org/download/

    7-Zip のインストール(Windows 上)

    7-Zip 23.01 のインストール

    7-Zipは,ファイルの圧縮や展開のツール.さまざまなフォーマットに対応している.

    Windows では, コマンドプロンプトを管理者として開き, 次のコマンドを実行することにより, 7-Zip 23.01 のインストールを行うことができる.

    mkdir %HOMEPATH%\7zip
    cd %HOMEPATH%\7zip
    curl -O https://www.7-zip.org/a/7z2301-x64.exe
    .\7z2301-x64.exe
    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 のインストール(Windows 上)

    サイト内の関連ページ

    関連する外部ページ

    Python の公式ページ: https://www.python.org/

    Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022),Visual Studio 2022 のインストール(Windows 上)

    サイト内の関連ページ

    関連する外部ページ

    2. Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード

    1. Windows で,コマンドプロンプト管理者として実行

      コマンドプロンプトを管理者として実行: 別ページ »で説明

    2. Dlib Python のインストール

      次のコマンドを実行.

      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
      

      [image]
      (以下省略)
    3. Python の dlib パッケージがインストールできたことの確認

      バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.

      python -c "import dlib; print( dlib.__version__ )"
      

      [image]
    4. 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
      

      [image]
    5. Dlib の動作確認のため,次を実行.エラーメッセージが出ずに,顔検出の結果が表示されれば OK とする.
      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
      

      [image]

      [image]

    3. Dlib に付属のサンプルプログラム(example 下)のビルド

    Dlib に付属のサンプルプログラムは,examplesの下にある.次の手順でビルドする.

    http://dlib.net/compile.html の記述に従う

    1. Windows で,コマンドプロンプト管理者として実行.

      コマンドプロンプトを管理者として実行: 別ページ »で説明

      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
      

      [image]
    2. 結果の確認

      エラーメッセージが出ていないこと

      [image]
    3. ビルド操作
      cmake --build . --config RELEASE
      

      [image]
    4. 結果を確認.エラーメッセージが出ていないこと

      [image]

    4. 画像分類,物体検出,トラッキング・ビジョン,犬の顔画像について眼鏡をあてはめる(Dlib に付属のサンプルプログラムを使用)

    画像分類

    画像分類:画像(1つ)から、所定のラベル(複数)のうち1つを推定すること。あるいは、複数を推定すること。推定するとき、その確率とともに得ることもある

    Dlib は C:\dlib にインストールされているとする

    1. 学習済みモデルのダウンロード

      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
      

      [image]
    2. 画像分類の実行

      画像分類される画像は,「faces\*.jpg」で指定している.

      cd C:\dlib
      cd examples\build
      for %i in (..\faces\*.jpg) do .\release\dnn_imagenet_ex %i
      

      [image]

    画像分類される画像

    [image] [image] [image] [image] [image] [image] [image] [image] [image] [image] [image] [image]

    物体検出(Max-Margin Object Detection by Davis E. King を使用)

    物体検出:画像から、ある種類の物体を検出し、その位置と大きさを得る。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
    

    [image]

    物体検出の実行(車の正面)

    cd C:\dlib
    cd examples\build
    .\release\dnn_mmod_find_cars_ex
    

    [image]

    [image]

    学習済みモデルのダウンロード

    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
    

    [image]

    物体検出の実行(車の正面と背面)

    cd C:\dlib
    cd examples\build
    .\release\dnn_mmod_find_cars2_ex
    

    [image]

    トラッキング・ビジョン(correlation_tracker を使用)

    cd C:\dlib
    cd examples\build
    .\release\video_tracking_ex ..\video_frames
    

    [image]

    犬の顔画像について眼鏡をあてはめる(顔のランドマーク検出による)

    学習済みモデルのダウンロード

    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
    

    [image]

    実行してみる

    cd C:\dlib
    cd examples\build
    .\release\dnn_mmod_dog_hipsterizer mmod_dog_hipsterizer.dat ..\faces\dogs.jpg
    

    [image]

    [image]

    5. Dlib による顔検出(Python を使用)

    Dlib による顔検出 を行う.

    1. まずは、DLib に同封の顔画像ファイルに何があるかを、簡単にみておく

      %HOMEPATH%\dlib\examples\faces の下の顔画像のファイルを確認する

      [image]
    2. Windowsコマンドプロンプトを開く
    3. Python プログラムの実行

      Python プログラムの実行(Windows 上)

      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
      

      結果が表示されるまで少し待つ

      [image]

      [image]

      うまく動かないときのヒント

      • 次のように「RuntimeError: Unable to open mmod_human_face_detector.dat for reading」というエラーメッセージが出たときは、 上の「学習済みモデルのダウンロード」をやり直して、もう一度実行.

        [image]
      • python にパスが通っていないときは、うまくいかない. Windows で,python にパスが通っているかは、次のコマンドで確認
        where python
        
    4. 別の画像で試してみる
      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001009.jpg
      

      [image]

      [image]
    5. さらに別の画像で試してみる
      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001322.jpg
      

      [image]

      [image]

    6. Dlib による顔ランドマークの検出(Python を使用)

    顔ランドマークの検出 (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\
    

    [image]

    [image]

    7. Dlib による顔のアラインメント,顔データのデータ拡張,5ランドマーク,68 ランドマーク,顔のコード化(5ランドマークを使用),顔のコード化(68 ランドマークを使用)(Python を使用)

    顔のアラインメント

    利用条件などは各自で確認のこと

    1. Windowsコマンドプロンプトを開く
    2. Python プログラムの実行

      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
      

      [image]
    3. Enter キーを複数回押して、結果を確認

    顔データのデータ拡張

    増量は、学習で重要となる

    1. 実行してみる
      cd C:\dlib
      cd python_examples
      python face_jitter.py shape_predictor_68_face_landmarks.dat 
      

      元画像として何を使うかは、プログラム中で設定されている

      [image]

      [image]
    2. Enter キーを複数回押して、結果を確認

    5ランドマーク

    1. 実行してみる
      cd C:\dlib
      cd python_examples
      python face_landmark_detection.py shape_predictor_5_face_landmarks.dat ..\examples\faces
      

      [image]

      [image]
    2. Enter キーを複数回押して、結果を確認

    68 ランドマーク

    1. 実行してみる
      cd C:\dlib
      cd python_examples
      python face_landmark_detection.py shape_predictor_68_face_landmarks.dat ..\examples\faces
      

      [image]

      [image]
    2. Enter キーを複数回押して、結果を確認

    顔のコード化(5ランドマークを使用)

    1. 実行してみる
      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個の数値(特徴量ベクトル)が得られることを確認

      [image]

      [image]
    2. Enter キーを複数回押して、結果を確認

    顔のコード化(68 ランドマークを使用)

    1. 実行してみる
      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個の数値(特徴量ベクトル)が得られることを確認

      [image]

      [image]
    2. Enter キーを複数回押して、結果を確認

    8. ageitgey/ageitgey の face_recognition のインストール

    1. Windows で,コマンドプロンプト管理者として実行

      コマンドプロンプトを管理者として実行: 別ページ »で説明

    2. numpy, scikit-image, scikit-learn のインストール

      次のコマンドを実行する.

      python -m pip install -U numpy scikit-image scikit-learn
      
    3. face_recognition のインストール
      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 
      

    9. ageitgey の face_recognition で顔識別を行ってみる(Python を使用)

    ファイルとディレクトリの準備

    1. Windows のコマンドプロンプトを開く
    2. 2つのディレクトリ known_people, unknown_pictures を作る

      コマンドプロンプトで次のコマンドを実行

      mkdir %HOMEPATH%\face_recognition\known_people
      mkdir %HOMEPATH%\face_recognition\unknown_pictures
      

      [image]
    3. 顔画像の準備

      %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
      

      [image]
    4. Windows で、ディレクトリ known_people の下に 2つの画像ファイルがあることを確認

      [image]
    5. %HOMEPATH%\face_recognition\examples にある顔画像のファイル 「two_people.jpg」を,先ほど作成した ディレクトリ unknown_pictures の下にコピー
      copy %HOMEPATH%\face_recognition\examples\two_people.jpg %HOMEPATH%\face_recognition\unknown_pictures
      

      [image]
    6. Windows で、ディレクトリ unknown_pictures の下に画像ファイルがあることを確認

      [image]

    顔検出を行ってみる

    cd %HOMEPATH%\face_recognition
    face_detection --model cnn unknown_pictures
    

    表示される4つの数字は、顔領域の座標値

    [image]

    顔識別を行ってみる

    unknown_pictures の下にある画像ファイルを、 known_people 下の画像ファイルを照合して顔識別を行うコマンド

    cd %HOMEPATH%\face_recognition
    face_recognition --show-distance true known_people unknown_pictures
    

    [image]

    ※ その他,Dlib を用いた顔検出、顔識別などについては, 別ページ »にまとめ