Dlibは,機械学習のアルゴリズムやトールの機能を持つソフトウエア.
Dlib を用いて、次のことを行う
参考 Web ページ: https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/
サイト内の関連ページ
先人に感謝
dlib の Web ページ: http://dlib.net/
sudo apt -y install python3-dev python3-pip python3-numpy
Dlib は C:\pytools\dlib にインストールされているとする
sudo apt -y install git
※ ここには,Windows でのインストール手順を示す.Ubuntuでも同様の手順になる.
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 形式ビデオファイル: sample2.mp4 (30秒)
http://dlib.net/files/ を開き、 shape_predictor_68_face_landmarks.dat.bz2をダウンロード
※ Windows での展開(解凍)のためのソフトには,「7-Zip」などがある.
※ C:\pytools\dlib が無いときは作る
Python プログラムを動かす.
※ Python プログラムを動かすために, Windows では,「python」コマンドを使う. Ubuntu では「python3」コマンドを使う.
開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)も便利である.
import numpy as np import dlib import cv2 import imutils from imutils import face_utils img = cv2.imread("C:/face-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:/face-image/sample2.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」のキーを押して閉じる
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ)