Dlib による顔検出を行う Python プログラム(Dlib, Python を使用)(Windows 上)
【目次】
- 前準備
- Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
- Dlib による顔検出を行う Python プログラム(Dlib に付属のプログラムを使用)
- Dlib による顔検出を行う Python プログラム(手持ちの画像ファイルや,パソコンのカメラで顔検出を行う)
【サイト内の関連ページ】
- 説明資料: Dlib の機能概要 [PDF], [パワーポイント]
- 顔情報処理の Python プログラム(Dlib,face_recognition を使用) について: 別ページ »にまとめ
- Windows で動く人工知能関係 Pythonアプリケーション,オープンソースソフトウエア): 別ページ »にまとめている.
【用語説明】
1. 前準備
Git のインストール(Windows 上)
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】 Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】 Git の公式ページ: https://git-scm.com/
7-Zip のインストール(Windows 上)
7-Zipは,ファイル圧縮・展開(解凍)ツール
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- 次のコマンドを実行
次のコマンドは,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\")"
【関連する外部ページ】
- 7-Zip の公式ページ: https://7-zip.opensource.jp/
Python のインストール(Windows 上)
【サイト内の関連ページ】
- Windows での Python 3.10,関連パッケージ,Python 開発環境のインストール(winget を使用しないインストール): 別ページ »で説明
- Windows での Anaconda3 のインストール: 別ページ »で説明
- Python のまとめ: 別ページ »にまとめている.
【関連する外部ページ】 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の機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:
- Build Tools for Visual Studio の機能のみが必要な場合
- Visual Studio の機能が必要である,あるいは,よく分からない場合
Visual Studio 2022 をインストールする際に,「C++ によるデスクトップ開発」を選択することで, Build Tools for Visual Studio 2022 の機能も一緒にインストールされる.
不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.
Build Tools for Visual Studio 2022 のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行
次のコマンドは,Build Tools for Visual Studio 2022と VC2015 再配布可能パッケージをインストールするものである.
- Build Tools for Visual Studio 2022 での C++ によるデスクトップ開発,CLI,ATL,MFC のインストール(Windows 上)
- Visual Studio Installer の起動
起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.
- Visual Studio Build Tools 2022 で「変更」を選ぶ.
- 「C++ によるデスクトップ開発」をクリック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」,「ATL」,「MFC」をチェックする.その後,「変更」をクリック.
- Visual Studio Installer の起動
Visual Studio のインストール(Windows 上)
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行
次のコマンドは,Visual Studio Community 2022と VC2015 再配布可能パッケージをインストールするものである.
- Visual Studio での C++ によるデスクトップ開発,CLI のインストール(Windows 上)
- Visual Studio Installer の起動
起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.
- Visual Studio Community 2022 で「変更」を選ぶ.
- 「C++ によるデスクトップ開発」をチェック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」をチェックする.その後,「インストール」をクリック.
- Visual Studio Installer の起動
2. Dlib Python のインストール,Dlib のソースコード等と,Dlib の学習済みモデルのダウンロード
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- 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
Dlib による顔検出を行う Python プログラム(Dlib に付属のプログラムを使用)
Dlib による顔検出 を行う.
Python プログラムの実行(Windows 上)
- 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)」のとき
Dlib による顔検出を行う Python プログラム(手持ちの画像ファイルや,パソコンのカメラで顔検出を行う)
Python の opencv-python のインストール
- Windows で,コマンドプロンプトを管理者として実行
コマンドプロンプトを管理者として実行: 別ページ »で説明
- opencv-python のインストール
* 「pip install ...」は,Python パッケージをインストールするための操作
python -m pip install -U opencv-python opencv-contrib-python
手持ちの画像ファイルで顔検出を行う.
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた.
- 処理したい画像ファイルを準備
ファイル名は 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
パソコンのカメラで顔検出を行う.
dlib に付属の「face_detector.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」のキーを押して閉じる