【目次】
【サイト内の関連ページ】
【用語説明】
Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
Dlib には,Convolutional Neural Network (CNN) による顔検出の機能がある.
Dlib による顔検出の例
顔検出が行われ,顔を囲むようなバウンディングボックス (bounding box) が表示される.
バウンディングボックスの座標値が数値データとして得られる
顔のランドマーク検知が行われ,顔を囲むようなバウンディングボックス (bounding box) と,顔のランドマークが表示される.
Gitは,バージョン管理システム.ソースコードの管理や複数人での共同に役立つ.
【サイト内の関連ページ】
Windows での Git のインストール: 別ページ »で説明している.
【関連する外部ページ】
Git の公式ページ: https://git-scm.com/
7-Zip 23.01 のインストール
7-Zipは,ファイルの圧縮や展開のツール.さまざまなフォーマットに対応している.
Windows では, コマンドプロンプトを管理者として開き, 次のコマンドを実行することにより, 7-Zip 23.01 のインストールを行うことができる.
mkdir %HOMEPATH%\7zip cd %HOMEPATH%\7zip curl -O https://www.7-zip.org/a/7z2301-x64.exe .\7z2301-x64.exe powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\7-Zip\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
【サイト内の関連ページ】
Windows での 7-Zip のインストール: 別ページ »で説明している.
【関連する外部ページ】
7-Zip の公式ページ: https://sevenzip.osdn.jp/
【サイト内の関連ページ】
【関連する外部ページ】
Python の公式ページ: https://www.python.org/
【サイト内の関連ページ】
Windows での Visual Studio Community 2022 のインストール: 別ページ »で説明している.なお,Visual Studio には,ビルドツール(Build Tools)が含まれている.
Windows での ビルドツール for Visual Studio 2022 (Build Tools for Visual Studio 2022) のインストール: 別ページ »で説明している.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
cd %HOMEPATH% rmdir /s /q imutils
cd %HOMEPATH% git clone https://github.com/jrosebr1/imutils cd imutils python setup.py build python setup.py install
python -c "import imutils; print( imutils.__version__ )"
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行.
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
mp4 形式動画ファイル: sample1.mp4
次のコマンドを実行.
cd C:\dlib cd python_examples curl -O https://www.kkaneko.jp/sample/face/126.png curl -O https://www.kkaneko.jp/sample/face/127.png curl -O https://www.kkaneko.jp/sample/face/sample1.mp4
顔画像ファイル 126.png を読み込んで、濃淡画像に変換してみる
Python プログラムを実行する
コマンドプロンプトで次を実行
cd C:\dlib python
次の Python プログラムを実行
import numpy as np import dlib import cv2 import imutils from imutils import face_utils img = cv2.imread("./python_examples/126.png") mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imshow("", mono) cv2.waitKey(0) cv2.destroyAllWindows() exit()
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
Python プログラムを実行する
コマンドプロンプトで次を実行
cd C:\dlib python
次の Python プログラムを実行
import numpy as np import dlib import cv2 import imutils from imutils import face_utils img = cv2.imread("./python_examples/126.png") mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) detector = dlib.get_frontal_face_detector() rects = detector(mono, 1) print(rects) exit()
結果が数値で表示される
Python プログラムを実行する
コマンドプロンプトで次を実行
cd C:\dlib python
次の Python プログラムを実行
import numpy as np import dlib import cv2 import imutils from imutils import face_utils img = cv2.imread("./python_examples/126.png") mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) detector = dlib.get_frontal_face_detector() rects = detector(mono, 1) print(rects) 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('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() exit()
68 ランドマークの結果を表示
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
Python プログラムを実行する
コマンドプロンプトで次を実行
python
コマンドプロンプトで次を実行
cd C:\dlib python
次の Python プログラムを実行
「if ( j > 36 and j < 49): 」を追加
import numpy as np import dlib import cv2 import imutils from imutils import face_utils img = cv2.imread("./python_examples/126.png") mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) detector = dlib.get_frontal_face_detector() rects = detector(mono, 1) print(rects) 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('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」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
Python プログラムを実行する
コマンドプロンプトで次を実行
cd C:\dlib python
次の Python プログラムを実行
OpenCV による動画表示を行う.
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('python_examples/shape_predictor_68_face_landmarks.dat') v = cv2.VideoCapture("./python_examples/sample1.mp4") while(v.isOpened()): r, f = v.read() if ( r == False ): break mono = cv2.cvtColor(f, 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(f, 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(f, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) cv2.circle(f, (x, y), 2, (0, 255, 0), -1) cv2.imshow("", f) # Press Q to exit 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('python_examples/shape_predictor_68_face_landmarks.dat') v = cv2.VideoCapture(0) while(v.isOpened()): r, f = v.read() if ( r == False ): break mono = cv2.cvtColor(f, 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(f, 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(f, str(j), (x, y - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) cv2.circle(f, (x, y), 2, (0, 255, 0), -1) cv2.imshow("", f) # Press Q to exit if cv2.waitKey(1) & 0xFF == ord('q'): break v.release() cv2.destroyAllWindows()
※ 止めたいとき,右上の「x」をクリックしない.画面の中をクリックしてから,「q」のキーを押して閉じる