Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
Dlib の次の機能を使う
Convolutional Neural Network (CNN) による顔検出を行う. Dlib で公開されている学習済みモデルを使う.
次の画像について顔検出を行うとする
顔検出が行われ,顔を囲むようなバウンディングボックス (bounding box) が表示される.
バウンディングボックスの座標値が数値データとして得られる
利用条件などは利用者において確認してください
【サイト内の関連ページ】
先人に感謝
dlib の Web ページ: http://dlib.net/
システム Python を使うことができる(その場合,Python のインストールは行わない)
システム Python を用いるときは,pip, setuptools の更新は次のコマンドで行う.
sudo apt -y update sudo apt -y install python3-pip python3-setuptools
Ubuntu で,システム Python 以外の Python をインストールしたい場合は pyenv が便利である: 別ページで説明している.
Python の URL: http://www.python.org/
【Python, pip の使い方】
Python, pip は,次のコマンドで起動できる.
【Python 開発環境のインストール】
JupyterLab, spyder, nteract (Python 開発環境) のインストールは, Windows でコマンドプロンプトを管理者として実行し, 次のコマンドを実行.
python -m pip install -U pip setuptools jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder
詳しくは,: 別ページで説明している.
JupyterLab, spyder, nteract (Python 開発環境) のインストール: : 別ページで説明している.
Dlib は c:\dlib にインストールされているとして,以下,説明する.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U numpy scikit-image opencv-python
http://dlib.net/files/ を開き、 mmod_human_face_detector.dat.bz2をダウンロード
※ Windows での展開(解凍)のためのソフトには,「7-Zip」などがある.
※ c:\dlib が無いときは作る
Dlib の顔検出 を行う.
c:\dlib\examples\faces の下の顔画像のファイルを確認する
Python プログラムを動かすために, Windows では「python」, Ubuntu では「python3」などのコマンドを使う. あるいは, 開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である.
cd c:\dlib\python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2007_007763.jpg
結果が表示されるまで少し待つ
cd c:\dlib\python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001009.jpg
cd c:\dlib\python_examples python cnn_face_detector.py mmod_human_face_detector.dat ..\examples\faces\2008_001322.jpg
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 matplotlib
前準備として,学習済みモデルのダウンロードを行う.
Windows では, コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install dlib opencv-python matplotlib curl -O http://dlib.net/files/mmod_human_face_detector.dat.bz2 e mmod_human_face_detector.dat.bz2
Python プログラムを動かす.
Python プログラムを動かすために, Windows では「python」, Ubuntu では「python3」などのコマンドを使う.
あるいは, 開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である.
あるいは,オンラインで動くGoogle Colaboratory のノートブックの利用も,場合によっては便利である.
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた. ファイルを作成するので,cd コマンドで,書き込み権限のあるディレクトリに移動してから実行すること.
import dlib
import cv2
import matplotlib.pyplot as plt
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')
# 画面に描画
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# ファイルに保存
cv2.imwrite("result.png", img)
Python プログラムを動かす.
Python プログラムを動かすために, Windows では「python」, Ubuntu では「python3」などのコマンドを使う.
あるいは, 開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である.
あるいは,オンラインで動くGoogle Colaboratory のノートブックの利用も,場合によっては便利である.
dlib に付属の「face_detector.py」を参考にして、次のプログラムを作成してみた. ファイルを作成するので,cd コマンドで,書き込み権限のあるディレクトリに移動してから実行すること.
import dlib
import cv2
import matplotlib.pyplot as plt
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
※ 途中で止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる