face_recognition による顔検出,顔のランドマーク,顔検証の Python プログラム(Dlib,ageitgey/face_recognition,Python を使用)(Windows 上)
【サイト内の関連ページ】
- 説明資料: Dlib の機能概要 [PDF], [パワーポイント]
- 顔情報処理の Python プログラム(Dlib,face_recognition を使用) について: 別ページ »にまとめ
- Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア): 別ページ »にまとめている.
【用語説明】
- Dlib
Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
【関連する外部ページ】
ageitgey/face_recognition のページ: https://github.com/ageitgey/face_recognition
前準備
Python のインストール(Windows上)
注:既にPython(バージョン3.12を推奨)がインストール済みの場合は,この手順は不要である.
winget(Windowsパッケージマネージャー)を使用してインストールを行う
- Windowsで,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。 - winget(Windowsパッケージマネージャー)が利用可能か確認する:
winget --version
- Pythonのインストール(下のコマンドにより Python 3.12 がインストールされる).
reg add "HKLM\SYSTEM\CurrentControlSet\Control\FileSystem" /v LongPathsEnabled /t REG_DWORD /d 1 /f REM Python をシステム領域にインストール winget install --scope machine --id Python.Python.3.12 -e --silent REM Python のパス set "INSTALL_PATH=C:\Program Files\Python312" echo "%PATH%" | find /i "%INSTALL_PATH%" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%" /M >nul echo "%PATH%" | find /i "%INSTALL_PATH%\Scripts" >nul if errorlevel 1 setx PATH "%PATH%;%INSTALL_PATH%\Scripts" /M >nul
- Python詳細ガイド:Pythonまとめ »
【関連する外部サイト】
【サイト内の関連ページ】
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
Git のインストール(Windows 上)
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】 Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】 Git の公式ページ: https://git-scm.com/
Gitのインストール
管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要となる。
REM Git をシステム領域にインストール
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
)
Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。
- 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
- Python の dlib パッケージがインストールできたことの確認
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import dlib; print( dlib.__version__ )"
- Dlib のソースコード等のダウンロード
次のコマンドを実行.
cd C:\
rmdir /s /q dlib
git clone https://github.com/davisking/dlib
- 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
- Dlib の動作確認のため,次を実行.エラーメッセージが出ずに,顔検出の結果が表示されれば OK とする.
cd C:\dlib
cd python_examples
python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
ageitgey/ageitgey の face_recognition のインストール
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。
- numpy, scikit-image, scikit-learn のインストール
次のコマンドを実行する.
python -m pip install -U numpy scikit-image scikit-learn
- 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
顔検出,顔ランドマーク,顔検証の Python プログラム
ファイルとディレクトリの準備
- Windows のコマンドプロンプトを開く
- 2つのディレクトリ known_people, unknown_pictures を作る
コマンドプロンプトで次のコマンドを実行
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
- Windows で、ディレクトリ known_people の下に 2つの画像ファイルがあることを確認
-
%HOMEPATH%\face_recognition\examples
にある顔画像のファイル
「two_people.jpg」を,先ほど作成した
ディレクトリ unknown_pictures の下にコピー
copy %HOMEPATH%\face_recognition\examples\two_people.jpg %HOMEPATH%\face_recognition\unknown_pictures
- Windows で、ディレクトリ unknown_pictures の下に画像ファイルがあることを確認
顔検出の Python プログラム
Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
Python 開発環境(Jupyter Qt Console,
Jupyter ノートブック (Jupyter Notebook),
Jupyter Lab,
Nteract,
Spyder,
PyCharm,
PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
python
「face_recognition/unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.
コマンドプロンプトで次を実行
cd /d c:%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()
顔検出と画像表示の Python プログラム
プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
のものを一部変更したものを掲載している
- Python プログラムファイルの作成
ここでは,ファイル名は a.py とする.
cd /d c:%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()
- Python プログラムの実行
python a.py
画像が表示されるので確認.
このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
顔ランドマークの Python プログラム
Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
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 /d c:%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()
顔ランドマークと画像表示の Python プログラム
プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py
のものを一部変更したものを掲載している
- Python プログラムファイルの作成
ここでは,ファイル名は a.py とする.
cd /d c:%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()
- Python プログラムの実行
python a.py
画像が表示されるので確認.
このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
顔のランドマークの Python プログラムで、左目(lefteye)と左の眉(left_eyrbrow)を表示
Python プログラムの実行
Python プログラムの実行
- Windows では python (Python ランチャーは py)
- Ubuntu では python3
Python 開発環境(Jupyter Qt Console,
Jupyter ノートブック (Jupyter Notebook),
Jupyter Lab,
Nteract,
Spyder,
PyCharm,
PyScripterなど)も便利である.
Python のまとめ: 別ページ »にまとめ
python
コマンドプロンプトで次を実行
cd /d c:%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()
左目(lefteye)と左の眉(left_eyrbrow)のランドマークについて画像表示
- Python プログラムファイルの作成
ここでは,ファイル名は a.py とする.
cd /d c:%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()
- Python プログラムの実行
python a.py
画像が表示されるので確認.
このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
顔検証 (face verification) の Python プログラム
2つの顔画像が同一人物かを識別する Python プログラム
コマンドプロンプトで次を実行
cd /d c:%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()
2つの顔画像が同一人物かを識別する Python プログラム(別の顔で動かす)
https://github.com/ageitgey/face_recognition に掲載の写真から切り取った、次の写真を使ってみる
-
コマンドプロンプトで次を実行
(ここでは、上の画像ファイルを、unknown_pictures の下に,104.png という名前で保存している)
cd /d c:%HOMEPATH%
curl -O https://www.kkaneko.jp/ai/dlib/104.png
copy 104.png face_recognition\unknown_pictures
python
- 次の 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()