Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
Dlib を用いて、次のことを行う
参考 Web ページ: https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/
【サイト内の関連ページ】
先人に感謝
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 にインストールされているとして,以下,説明する.
mkdir c:\pytools cd c:\pytools rmdir /s /q imutils
cd c:\pytools git clone https://github.com/jrosebr1/imutils cd imutils python setup.py build python setup.py install
Windows のコマンドプロンプトで、次のコマンドを実行
python -c "import imutils; print( imutils.__version__ )"
ここで使用する mp4 形式ビデオファイル: sample1.mp4
http://dlib.net/files/ を開き、 shape_predictor_68_face_landmarks.dat.bz2をダウンロード
※ Windows での展開(解凍)のためのソフトには,「7-Zip」などがある.
※ c:\dlib が無いときは作る
Python プログラムを動かす.
Python プログラムを動かすために, Windows では「python」, Ubuntu では「python3」などのコマンドを使う.
あるいは, 開発環境や Python コンソール(Jupyter Qt Console,Spyder,PyCharm,PyScripter など)の利用も便利である.
あるいは,オンラインで動くGoogle Colaboratory のノートブックの利用も,場合によっては便利である.
import numpy as np
import dlib
import cv2
import imutils
from imutils import face_utils
img = cv2.imread("c:/image/126.png")
mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("", mono)
cv2.waitKey(0)
cv2.destroyAllWindows()
※ 「import dlib」でエラーメッセージが出るときの対処
Dlib のインストールを行うこと.(このWebページにある「前準備」も行うこと)
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
引き続き,次のプログラムを実行
detector = dlib.get_frontal_face_detector() rects = detector(mono, 1) print(rects)
結果が数値で表示される
※ 「C:/pytools/dlib/python_examples」のところには、さきほどファイルを置いた作業用ディレクトリを設定すること
from imutils import face_utils
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)
disp = img.copy()
predictor = dlib.shape_predictor('C:/pytools/dlib/python_examples/shape_predictor_68_face_landmarks.dat')
for (i, rect) in enumerate(rects):
shape = predictor(mono, rect)
shape = face_utils.shape_to_np(shape)
(x, y, w, h) = face_utils.rect_to_bb(rect)
box_label(disp, x, y, x + w, y + h, str(i))
j = 0
for (x, y) in shape:
j = j + 1
cv2.putText(disp, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.circle(disp, (x, y), 2, (0, 255, 0), -1)
cv2.imshow("", disp)
cv2.waitKey(0)
cv2.destroyAllWindows()
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
「if ( j > 36 and j < 49): 」を追加
from imutils import face_utils
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)
disp = img.copy()
predictor = dlib.shape_predictor('C:/pytools/dlib/python_examples/shape_predictor_68_face_landmarks.dat')
for (i, rect) in enumerate(rects):
shape = predictor(mono, rect)
shape = face_utils.shape_to_np(shape)
(x, y, w, h) = face_utils.rect_to_bb(rect)
box_label(disp, x, y, x + w, y + h, str(i))
j = 0
for (x, y) in shape:
j = j + 1
if ( j > 36 and j < 49):
cv2.putText(disp, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.circle(disp, (x, y), 2, (0, 255, 0), -1)
cv2.imshow("", disp)
cv2.waitKey(0)
cv2.destroyAllWindows()
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
import numpy as np
import dlib
import cv2
import imutils
from imutils import face_utils
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)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('C:/pytools/dlib/python_examples/shape_predictor_68_face_landmarks.dat')
v = cv2.VideoCapture("c:/image/sample1.mp4")
while(v.isOpened()):
r, disp = v.read()
if ( r == False ):
break
mono = cv2.cvtColor(disp, cv2.COLOR_BGR2GRAY)
rects = detector(mono, 1)
for (i, rect) in enumerate(rects):
shape = predictor(mono, rect)
shape = face_utils.shape_to_np(shape)
(x, y, w, h) = face_utils.rect_to_bb(rect)
box_label(disp, x, y, x + w, y + h, str(i))
j = 0
for (x, y) in shape:
j = j + 1
if ( j > 36 and j < 49):
cv2.putText(disp, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.circle(disp, (x, y), 2, (0, 255, 0), -1)
cv2.imshow("", disp)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
v.release()
cv2.destroyAllWindows()
※ 止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる
今度は、USB接続できるビデオカメラを準備し,パソコンに接続しておく.
「v = cv2.VideoCapture(0)」のように設定している.他は前のプログラムと同じ
import numpy as np
import dlib
import cv2
import imutils
from imutils import face_utils
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)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('C:/pytools/dlib/python_examples/shape_predictor_68_face_landmarks.dat')
v = cv2.VideoCapture(0)
while(v.isOpened()):
r, disp = v.read()
if ( r == False ):
break
mono = cv2.cvtColor(disp, cv2.COLOR_BGR2GRAY)
rects = detector(mono, 1)
for (i, rect) in enumerate(rects):
shape = predictor(mono, rect)
shape = face_utils.shape_to_np(shape)
(x, y, w, h) = face_utils.rect_to_bb(rect)
box_label(disp, x, y, x + w, y + h, str(i))
j = 0
for (x, y) in shape:
j = j + 1
if ( j > 36 and j < 49):
cv2.putText(disp, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.circle(disp, (x, y), 2, (0, 255, 0), -1)
cv2.imshow("", disp)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
v.release()
cv2.destroyAllWindows()
※ 止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる