Dlib による顔検出を行う Python プログラム(Dlib, Python を使用)(Windows 上)
【目次】
- 前準備
- Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
- Dlib による顔検出を行う Python プログラム(Dlib に付属のプログラムを使用)
- Dlib による顔検出を行う Python プログラム(手持ちの画像ファイルや,パソコンのカメラで顔検出を行う)
【サイト内の関連ページ】
- 説明資料: Dlib の機能概要 [PDF], [パワーポイント]
- 顔情報処理の Python プログラム(Dlib,face_recognition を使用) について: 別ページ »にまとめ
- Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア): 別ページ »にまとめている.
【用語説明】
1. 前準備
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まとめ »
- 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
- python: python
- pip: python -m pip または pip
- Jupyter Qt Console: jupyter qtconsole
- Jupyter ノートブック (Jupyter Notebook): jupyter notebook
- Jupyter Lab: jupyter lab あるいは python -m jupyter lab
- Nteract: jupyter nteract あるいは python -m jupyter nteract
- Spyder: spyder
- まずは、DLib に同封の顔画像ファイルに何があるかを、簡単にみておく
%HOMEPATH%\dlib\examples\faces の下の顔画像のファイルを確認する
- cnn_face_detector.py をエディタなどで開いて、中身を確認
Dlib には Convolutional Network による顔検出の機能があり、顔検出させるためのプログラムは実質2行. 画面を開く、画像ファイルを読み込む、画像データを表示する、顔部分を四角で描くといったことも簡単なコマンド.
- Dlib では、顔のサイズは 80 x 80 であるとして、学習済みデータが配布されている。
より小さな顔を検出したいときは、アップサンプルを行う。アップサンプルを行うと、動作は遅くなる。
cnn_face_detector.py の「dets = cnn_face_detector(img, 1)」の「1」を「2」や「3」に変える。 そして、再び、cnn_face_detector.py を実行する。
上で使用した画像を、縦、横 0.4 倍した画像で試してみる。 まず、「dets = cnn_face_detector(img, 1)」のとき
「dets = cnn_face_detector(img, 2)」のとき
「dets = cnn_face_detector(img, 3)」のとき
- Windows で,管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー >
cmd
と入力 > 右クリック > 「管理者として実行」)。 - opencv-python のインストール
* 「pip install ...」は,Python パッケージをインストールするための操作
python -m pip install -U opencv-python opencv-contrib-python
- 処理したい画像ファイルを準備
ファイル名は a.png にする. %HOMEPATH%\dlib\python_examples に置く.
cd C:\dlib\python_examples curl -O https://www.kkaneko.jp/sample/face/126.png move 126.png a.png
- Python プログラムファイルの作成
notepad hoge.py
ソースコードは次の通り.
import dlib import cv2 import numpy as np def box_label(img, x1, y1, x2, y2, label): cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 1, 1) cv2.rectangle(img, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1) cv2.putText(img, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1) # 「mmod_human_face_detector.bat」のところは、学習済みモデルのファイル名. face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat') # 画像ファイル名を a.png のところに設定 img = cv2.imread('a.png') if img is None: print("画像ファイルがない") exit() # 顔検出を行う. faces = face_detector(img, 1) # 顔検出で得られた顔(複数あり得る)それぞれについて、四角を書く for i, f in enumerate(faces): box_label(img, f.rect.left(), f.rect.top(), f.rect.right(), f.rect.bottom(), 'face') # 画面に描画 cv2.imshow('',img) cv2.waitKey(0) cv2.destroyAllWindows() # ファイルに保存 cv2.imwrite("result.png", img)
- Python プログラムの実行
python hoge.py
- Python プログラムファイルの作成
notepad hoge2.py
ソースコードは次の通り.
import dlib import cv2 import numpy as np def box_label(img, x1, y1, x2, y2, label): cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 1, 1) cv2.rectangle(img, (int(x1), int(y1-25)), (x2, y1), (255,255,255), -1) cv2.putText(img, label, (x1, int(y1-5)), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0,0,0), 1) # ディープラーニングを使わない.精度は低下し,性能は上がるとされている. face_detector = dlib.get_frontal_face_detector() # ビデオカメラ v = cv2.VideoCapture(0) while(v.isOpened()): r, img = v.read() if ( r == False ): break # 顔検出を行う faces = face_detector(img, 1) for i, f in enumerate(faces): # 四角を書く box_label(img, f.left(), f.top(), f.right(), f.bottom(), 'face') print(d) cv2.imshow("", img) if cv2.waitKey(1) & 0xFF == ord('q'): cv2.destroyAllWindows() break
- Python プログラムの実行
python hoge2.py
* 途中で止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる
【関連する外部サイト】
【サイト内の関連ページ】
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
)
2. Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
Dlib による顔検出を行う Python プログラム(Dlib に付属のプログラムを使用)
Dlib による顔検出 を行う.
Python プログラムの実行(Windows 上)
Dlib による顔検出を行う Python プログラム(手持ちの画像ファイルや,パソコンのカメラで顔検出を行う)
Python の opencv-python のインストール
手持ちの画像ファイルで顔検出を行う.
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた.
パソコンのカメラで顔検出を行う.
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた.