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) と,顔のランドマーク表示される.

  • 1. 前準備

    Python 3.12 のインストール

    インストール済みの場合は実行不要。

    管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要である。

    REM Python をシステム領域にインストール
    winget install --scope machine --id Python.Python.3.12 -e --silent
    REM Python のパス設定
    set "PYTHON_PATH=C:\Program Files\Python312"
    set "PYTHON_SCRIPTS_PATH=C:\Program Files\Python312\Scripts"
    echo "%PATH%" | find /i "%PYTHON_PATH%" >nul
    if errorlevel 1 setx PATH "%PATH%;%PYTHON_PATH%" /M >nul
    echo "%PATH%" | find /i "%PYTHON_SCRIPTS_PATH%" >nul
    if errorlevel 1 setx PATH "%PATH%;%PYTHON_SCRIPTS_PATH%" /M >nul

    関連する外部ページ

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

    AI エディタ Windsurf のインストール

    Pythonプログラムの編集・実行には、AI エディタの利用を推奨する。ここでは,Windsurfのインストールを説明する。

    管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行して、Windsurfをシステム全体にインストールする。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。

    winget install --scope machine Codeium.Windsurf -e --silent

    関連する外部ページ

    Windsurf の公式ページ: https://windsurf.com/

    Gitのインストール

    管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。

    
    REM Git をシステム領域にインストール
    winget install --scope machine --id Git.Git -e --silent
    REM Git のパス設定
    set "GIT_PATH=C:\Program Files\Git\cmd"
    if exist "%GIT_PATH%" (
        echo "%PATH%" | find /i "%GIT_PATH%" >nul
        if errorlevel 1 setx PATH "%PATH%;%GIT_PATH%" /M >nul
    )
    

    CMakeのインストール

    管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。

    
    REM CMake をシステム領域にインストール
    winget install --scope machine --id Kitware.CMake -e --silent
    REM CMake のパス設定
    set "GMAKE_PATH=C:\Program Files\CMake\bin"
    if exist "%GMAKE_PATH%" (
        echo "%PATH%" | find /i "%GMAKE_PATH%" >nul
        if errorlevel 1 setx PATH "%PATH%;%GMAKE_PATH%" /M >nul
    )
    

    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
    

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

    1. Windows で,管理者権限コマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)。
    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
      

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

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

      python -c "import dlib; print( dlib.__version__ )"
      
    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
      
    5. Dlib の動作確認のため,次を実行.エラーメッセージが出ずに,顔検出の結果が表示されれば OK とする.
      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
      

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

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

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

    1. Windows で,管理者権限コマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)。.

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

      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
      
    2. 結果の確認

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

    3. ビルド操作
      cmake --build . --config RELEASE
      
    4. 結果を確認.エラーメッセージが出ていないこと

    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
      
    2. 画像分類の実行

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

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

    画像分類される画像

    物体検出(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
    

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

    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
    

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

    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
    

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

    Dlib による顔検出 を行う.

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

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

    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
      

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

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

      • 次のように「RuntimeError: Unable to open mmod_human_face_detector.dat for reading」というエラーメッセージが出たときは、 上の「学習済みモデルのダウンロード」をやり直して、もう一度実行.
      • 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
      
    5. さらに別の画像で試してみる
      cd C:\dlib
      cd python_examples
      python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001322.jpg
      

    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\
    

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

    顔のアラインメント

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

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

      Python プログラムの実行

      【サイト内の関連ページ】 Python のまとめ: 別ページ »

      cd C:\dlib
      cd python_examples
      python face_alignment.py shape_predictor_68_face_landmarks.dat ..\examples\faces\2007_007763.jpg
      
    3. Enter キーを複数回押して、結果を確認

    顔データのデータ拡張

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

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

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

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

    5ランドマーク

    1. 実行してみる
      cd C:\dlib
      cd python_examples
      python face_landmark_detection.py shape_predictor_5_face_landmarks.dat ..\examples\faces
      
    2. Enter キーを複数回押して、結果を確認

    68 ランドマーク

    1. 実行してみる
      cd C:\dlib
      cd python_examples
      python face_landmark_detection.py shape_predictor_68_face_landmarks.dat ..\examples\faces
      
    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個の数値(特徴量ベクトル)が得られることを確認

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

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

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

    1. Windows で,管理者権限コマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)。
    2. numpy, scikit-image, scikit-learn のインストール

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

      python -m pip install -U numpy scikit-image scikit-learn
      
    3. face_recognition のインストール
      cd /d c:%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
      
    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
      
    4. Windows で、ディレクトリ known_people の下に 2つの画像ファイルがあることを確認
    5. %HOMEPATH%\face_recognition\examples にある顔画像のファイル 「two_people.jpg」を,先ほど作成した ディレクトリ unknown_pictures の下にコピー
      copy %HOMEPATH%\face_recognition\examples\two_people.jpg %HOMEPATH%\face_recognition\unknown_pictures
      
    6. Windows で、ディレクトリ unknown_pictures の下に画像ファイルがあることを確認

    顔検出を行ってみる

    cd /d c:%HOMEPATH%\face_recognition
    face_detection --model cnn unknown_pictures
    

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

    顔識別を行ってみる

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

    cd /d c:%HOMEPATH%\face_recognition
    face_recognition --show-distance true known_people unknown_pictures
    

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