金子邦彦研究室人工知能顔情報処理の Python プログラム(Dlib,face_recognition を使用)face_recognition による顔識別.訓練データによる訓練と,検証データによる検証(Dlib,ageitgey/face_recognition,Python を使用)(Windows 上)

face_recognition による顔識別.訓練データによる訓練と,検証データによる検証(Dlib,ageitgey/face_recognition,Python を使用)(Windows 上)

サイト内の関連ページ

用語説明

前準備

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

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

サイト内の関連ページ

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

関連する外部ページ

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

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 上)

サイト内の関連ページ

関連する外部ページ

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

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

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

  2. Dlib のパッケージのインストール

    Windowspip を実行するときは,コマンドプロンプト管理者として開き,それを使って pip を実行することにする.

    次のコマンドを実行.

    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:\
    rmdir /s /q dlib
    git clone https://github.com/davisking/dlib
    

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

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 
    

face_recognition による顔識別.訓練データによる訓練と,検証データによる検証(Dlib,ageitgey/face_recognition,Python を使用)

ここで行うこと

knn アルゴリズム。

  1. ファイル %HOMEPATH%\face_recognition\examples\face_recognition_knn.py を分かりやすいディレクトリ(フォルダ)にコピー

    ここでは,c:\w にコピーしている.

    [image]
  2. そこに、「knn_examples」という名前のディレクトリを作る.

    [image]
  3. 「knn_examples」の下に、「train」という名前のディレクトリを作る.

    [image]
  4. 「knn_examples」の下に、「test」という名前のディレクトリを作る.

    [image]
  5. 「train」の下に、さらに、複数のディレクトリを作る.

    ディレクトリ名は何でも良いが、英語または数字のみを使い、分かりやすい名前がよい.

    次の例では2つ作っているが、3つ以上作ってもよい。

    [image]
  6. いま作ったディレクトリの下に画像ファイルを置く。

    画像ファイルの拡張子は .png もしくは .jpg もしくは .jpeg であること。

    [image]

    [image]
  7. 今度は「test」の下に、顔認識させたい画像を置く

    [image]
  8. Windows でコマンドプロンプトを開く。cd コマンドを使い、 ダウンロードしたディレクトリ(フォルダ)に、カレントディレクトリを移す
  9. python face_recognition_knn.py を実行

    [image]

    このプログラムでは,モデルとして hog を使う. ディープラーニングの CNN を使うように変更する場合には, face_locaions のところを,「face_locations(..., model="cnn")」のように書き換える

  10. 結果を確認。

    画像サイズを変える(解像度を変える)など、少しの工夫で検出漏れがなくなったり、顔認識の精度が向上する可能性がある。

    [image]
  11. 結果をファイルに保存したい場合

    python face_recognition_knn.py を次のように書き換え.

    「pil_image.save('result' + os.path.basename(img_path))」を追加

    [image]

    python face_recognition_knn.py を実行すると、次のように、結果がファイルに保存される.

    [image]