【目次】
【サイト内の関連ページ】
【用語説明】
Windows での Git のインストール: 別ページ »で説明
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
Windows での 7-Zip のインストール: 別ページ »で説明
【関連する外部ページ】
7-Zip の公式ページ: https://sevenzip.osdn.jp/
Windows での Python 3.10,関連パッケージ,Python 開発環境のインストール: 別ページ »で説明
【サイト内の関連ページ】
Python のまとめ: 別ページ »にまとめ
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
Windows での Visual Studio Community 2022 のインストール: 別ページ »で説明
Visual Studio Community 2022 に, Build Tools for Visual Studio 2022の機能が含まれている.
Windows での Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022) のインストール: 別ページ »で説明
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行.
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
バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import dlib; print( dlib.__version__ )"
次のコマンドを実行.
cd C:\ rmdir /s /q dlib git clone https://github.com/davisking/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
cd C:\dlib cd python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
Dlib による顔検出 を行う.
Python プログラムの実行(Windows 上)
%HOMEPATH%\dlib\examples\faces の下の顔画像のファイルを確認する
Dlib には Convolutional Network による顔検出の機能があり、顔検出させるためのプログラムは実質2行. 画面を開く、画像ファイルを読み込む、画像データを表示する、顔部分を四角で描くといったことも簡単なコマンド.
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)」のとき
コマンドプロンプトを管理者として実行: 別ページ »で説明
※ 「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
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 hoge.py
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた.
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 hoge2.py
※ 途中で止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる