金子邦彦研究室人工知能顔情報処理の Python プログラム(Dlib,face_recognition を使用)face_recognition による顔検出,顔のランドマーク,顔検証の Python プログラム(Dlib,ageitgey/face_recognition,Python を使用)(Windows 上)

face_recognition による顔検出,顔のランドマーク,顔検証の Python プログラム(Dlib,ageitgey/face_recognition,Python を使用)(Windows 上)

サイト内の関連ページ

用語説明

関連する外部ページ

ageitgey/face_recognition のページ: https://github.com/ageitgey/face_recognition

前準備

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

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

サイト内の関連ページ

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

関連する外部ページ

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

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

7-Zipは,ファイル圧縮・展開(解凍)ツール

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

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

  2. 次のコマンドを実行

    次のコマンドは,7-Zip圧縮・展開(解凍)ツールをインストールするものである.

    winget install --scope machine 7zip.7zip
    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\7-Zip\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    

関連する外部ページ

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

サイト内の関連ページ

関連する外部ページ

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

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

インストールの判断

Build Tools for Visual Studio は,開発ツールセットである. Visual Studio は統合開発環境であり,いくつかの種類があり,Build Tools for Visual Studioの機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:

不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.

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 
    

顔検出,顔ランドマーク,顔検証の 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]

顔検出の Python プログラム

  • Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    python
    

    face_recognition/unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.

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

    cd %HOMEPATH%
    python
    

    次の Python プログラムを実行

    import face_recognition
    image = face_recognition.load_image_file("face_recognition/unknown_pictures/two_people.jpg")
    face_locations = face_recognition.face_locations(image, model="cnn")
    print(face_locations)
    exit()
    

    [image]

    顔検出と画像表示の Python プログラム

    プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py  のものを一部変更したものを掲載している

    1. Python プログラムファイルの作成

      ここでは,ファイル名は a.py とする.

      cd %HOMEPATH%
      cd face_recognition
      notepad a.py
      

      ソースコードは次の通り.

      face_recognition/unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.

      import face_recognition
      import PIL
      import cv2
      
      F = "unknown_pictures/two_people.jpg"
      
      image = face_recognition.load_image_file(F)
      face_locations = face_recognition.face_locations(image, model="cnn")
      
      def box_label(bgr, x1, y1, x2, y2, label): 
          cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
          cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
          cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
      
      bgr = cv2.imread(F)
      for face_location in face_locations:
          top, right, bottom, left = face_location
          box_label(bgr, left, top, right, bottom, 'face')
      
      cv2.imshow('', bgr)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
    2. Python プログラムの実行

      Python プログラムの実行: 別ページ »で説明

      Python のまとめ: 別ページ »にまとめ

      python a.py
      

      画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる

      [image]

    顔ランドマークの Python プログラム

  • Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    python
    

    chin, left_eyebrow, right_eyebrow, nose_bridge, left_eye, right_eye の情報が表示される

    「face_landmarks_list = face_recognition.face_landmarks(image)」の行を, 次のように書き換えると改善するかどうかは未確認(いずれ確認したい)
    
    

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

    cd %HOMEPATH%
    python
    

    次の Python プログラムを実行

    import face_recognition
    image = face_recognition.load_image_file("face_recognition/unknown_pictures/two_people.jpg")
    face_locations = face_recognition.face_locations(image, model="cnn")
    face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations)
    for i in face_landmarks_list:
        print(i)
    
    exit()
    

    [image]

    顔ランドマークと画像表示の Python プログラム

    プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py  のものを一部変更したものを掲載している

    1. Python プログラムファイルの作成

      ここでは,ファイル名は a.py とする.

      cd %HOMEPATH%
      cd face_recognition
      notepad a.py
      

      ソースコードは次の通り.

      unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.

      import face_recognition
      import PIL
      import cv2
      
      F = "unknown_pictures/two_people.jpg"
      
      image = face_recognition.load_image_file(F)
      face_locations = face_recognition.face_locations(image, model="cnn")
      face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations)
      
      def box_label(bgr, x1, y1, x2, y2, label): 
          cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
          cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
          cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
      
      bgr = cv2.imread(F)
      for i in face_landmarks_list:
          for j in i.keys():
              for k in (i[j]):
                  cv2.circle(bgr, (k[0], k[1]), 2, (255, 0, 0), -1)
      
      cv2.imshow('', bgr)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
    2. Python プログラムの実行

      Python プログラムの実行: 別ページ »で説明

      Python のまとめ: 別ページ »にまとめ

      python a.py
      

      画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる

      [image]

    顔のランドマークの Python プログラムで、左目(lefteye)と左の眉(left_eyrbrow)を表示

  • Python プログラムの実行

    Python プログラムの実行

    Python 開発環境(Jupyter Qt Console, Jupyter ノートブック (Jupyter Notebook), Jupyter Lab, Nteract, Spyder, PyCharm, PyScripterなど)も便利である.

    Python のまとめ: 別ページ »にまとめ

    python
    

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

    cd %HOMEPATH%
    python
    

    次の Python プログラムを実行

    import face_recognition
    image = face_recognition.load_image_file("unknown_pictures/two_people.jpg")
    face_locations = face_recognition.face_locations(image, model="cnn")
    face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations)
    for i in face_landmarks_list:
        print(i['left_eye'])
        print(i['left_eyebrow'])
    
    exit()
    

    [image]

    左目(lefteye)と左の眉(left_eyrbrow)のランドマークについて画像表示

    1. Python プログラムファイルの作成

      ここでは,ファイル名は a.py とする.

      cd %HOMEPATH%
      cd face_recognition
      notepad a.py
      

      ソースコードは次の通り.

      プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py  のものを一部変更したものを掲載している

      unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.

      import face_recognition
      import PIL
      import cv2
      
      F = "unknown_pictures/two_people.jpg"
      
      image = face_recognition.load_image_file(F)
      face_locations = face_recognition.face_locations(image, model="cnn")
      face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations)
      
      def box_label(bgr, x1, y1, x2, y2, label): 
          cv2.rectangle(bgr, (x1, y1), (x2, y2), (255, 0, 0), 1, 1)
          cv2.rectangle(bgr, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1)
          cv2.putText(bgr, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1)
      
      bgr = cv2.imread(F)
      for i in face_landmarks_list:
          for j in i['left_eye']:
              cv2.circle(bgr, (j[0], j[1]), 2, (0, 255, 0), -1)
          for j in i['left_eyebrow']:
              cv2.circle(bgr, (j[0], j[1]), 2, (0, 0, 255), -1)
      
      
      cv2.imshow('', bgr)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      
    2. Python プログラムの実行

      Python プログラムの実行: 別ページ »で説明

      Python のまとめ: 別ページ »にまとめ

      python a.py
      

      画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる

      [image]

    顔検証 (face verification) の Python プログラム

    2つの顔画像が同一人物かを識別する Python プログラム

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

    cd %HOMEPATH%
    python
    

    次の Python プログラムを実行

    import face_recognition
    picture_of_obama = face_recognition.load_image_file("face_recognition/known_people/obama.jpg")
    obama_face_encoding = face_recognition.face_encodings(picture_of_obama)[0]
    
    biden_picture = face_recognition.load_image_file("face_recognition/known_people/biden.jpg")
    biden_face_encoding = face_recognition.face_encodings(biden_picture)[0]
    
    results = face_recognition.compare_faces([obama_face_encoding], biden_face_encoding)
    print(results)
    exit()
    

    [image]

    2つの顔画像が同一人物かを識別する Python プログラム(別の顔で動かす)

    https://github.com/ageitgey/face_recognition に掲載の写真から切り取った、次の写真を使ってみる

    [image]
    1. コマンドプロンプトで次を実行

      (ここでは、上の画像ファイルを、unknown_pictures の下に,104.png という名前で保存している)

      cd %HOMEPATH%
      curl -O https://www.kkaneko.jp/ai/dlib/104.png
      copy 104.png face_recognition\unknown_pictures
      python
      

    2. 次の Python プログラムを実行

      (今度は True と表示される)

      import face_recognition
      picture_of_obama = face_recognition.load_image_file("face_recognition/known_people/obama.jpg")
      obama_face_encoding = face_recognition.face_encodings(picture_of_obama)[0]
      
      another_picture = face_recognition.load_image_file("face_recognition/unknown_pictures/104.png")
      another_face_encoding = face_recognition.face_encodings(another_picture)[0]
      
      results = face_recognition.compare_faces([obama_face_encoding], another_face_encoding)
      print(results)
      exit()
      
      [image]