【サイト内の関連ページ】
【用語説明】
Dlibは,数多くの機能を持つ C++ ライブラリ.機能には,機械学習,数値計算,グラフィカルモデル推論,画像処理,スレッド,通信,GUI,データ圧縮・一貫性,テスト,さまざまなユーティリティなどがある.Python API もある.
【関連する外部ページ】
ageitgey/face_recognition のページ: https://github.com/ageitgey/face_recognition
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) のインストール: 別ページ »で説明している.
【関連する外部ページ】
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行.
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
コマンドプロンプトを管理者として実行: 別ページ »で説明
次のコマンドを実行する.
python -m pip install -U numpy scikit-image scikit-learn
cd %HOMEPATH% rmdir /s /q face_recognition git clone https://github.com/ageitgey/face_recognition cd face_recognition copy C:\dlib\python_examples\shape_predictor_68_face_landmarks.dat . python setup.py build python setup.py install
コマンドプロンプトで次のコマンドを実行
mkdir %HOMEPATH%\face_recognition\known_people mkdir %HOMEPATH%\face_recognition\unknown_pictures
%HOMEPATH%\face_recognition\examples にある顔画像のファイル 「biden.jpg」と「obama.jpg」を,先ほど作成した ディレクトリ known_people の下にコピー
copy %HOMEPATH%\face_recognition\examples\biden.jpg %HOMEPATH%\face_recognition\known_people copy %HOMEPATH%\face_recognition\examples\obama.jpg %HOMEPATH%\face_recognition\known_people
copy %HOMEPATH%\face_recognition\examples\two_people.jpg %HOMEPATH%\face_recognition\unknown_pictures
Python プログラムを実行する
「face_recognition/unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.
コマンドプロンプトで次を実行
cd %HOMEPATH% python
次の Python プログラムを実行
import face_recognition image = face_recognition.load_image_file("face_recognition/unknown_pictures/two_people.jpg") face_locations = face_recognition.face_locations(image, model="cnn") print(face_locations) exit()
プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py のものを一部変更したものを掲載している
ここでは,ファイル名は a.py とする.
cd %HOMEPATH% cd face_recognition notepad a.py
ソースコードは次の通り.
「face_recognition/unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.
import face_recognition import PIL import cv2 F = "unknown_pictures/two_people.jpg" image = face_recognition.load_image_file(F) face_locations = face_recognition.face_locations(image, model="cnn") 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) bgr = cv2.imread(F) for face_location in face_locations: top, right, bottom, left = face_location box_label(bgr, left, top, right, bottom, 'face') cv2.imshow('', bgr) cv2.waitKey(0) cv2.destroyAllWindows()
python a.py
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
Python プログラムを実行する
chin, left_eyebrow, right_eyebrow, nose_bridge, left_eye, right_eye の情報が表示される
コマンドプロンプトで次を実行
cd %HOMEPATH% python
次の Python プログラムを実行
import face_recognition image = face_recognition.load_image_file("face_recognition/unknown_pictures/two_people.jpg") face_locations = face_recognition.face_locations(image, model="cnn") face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations) for i in face_landmarks_list: print(i) exit()
プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py のものを一部変更したものを掲載している
ここでは,ファイル名は a.py とする.
cd %HOMEPATH% cd face_recognition notepad a.py
ソースコードは次の通り.
「unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.
import face_recognition import PIL import cv2 F = "unknown_pictures/two_people.jpg" image = face_recognition.load_image_file(F) face_locations = face_recognition.face_locations(image, model="cnn") face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations) 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) bgr = cv2.imread(F) for i in face_landmarks_list: for j in i.keys(): for k in (i[j]): cv2.circle(bgr, (k[0], k[1]), 2, (255, 0, 0), -1) cv2.imshow('', bgr) cv2.waitKey(0) cv2.destroyAllWindows()
python a.py
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
Python プログラムを実行する
コマンドプロンプトで次を実行
cd %HOMEPATH% python
次の Python プログラムを実行
import face_recognition image = face_recognition.load_image_file("unknown_pictures/two_people.jpg") face_locations = face_recognition.face_locations(image, model="cnn") face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations) for i in face_landmarks_list: print(i['left_eye']) print(i['left_eyebrow']) exit()
ここでは,ファイル名は a.py とする.
cd %HOMEPATH% cd face_recognition notepad a.py
ソースコードは次の通り.
プログラムは、https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py のものを一部変更したものを掲載している
「unknown_pictures」のところは、実際に作成したディレクトリに書き換えて実行すること.ディレクトリの切れ目に「\」でなく「/」を使うのは Python の流儀.
import face_recognition import PIL import cv2 F = "unknown_pictures/two_people.jpg" image = face_recognition.load_image_file(F) face_locations = face_recognition.face_locations(image, model="cnn") face_landmarks_list = face_recognition.face_landmarks(image, face_locations=face_locations) 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) bgr = cv2.imread(F) for i in face_landmarks_list: for j in i['left_eye']: cv2.circle(bgr, (j[0], j[1]), 2, (0, 255, 0), -1) for j in i['left_eyebrow']: cv2.circle(bgr, (j[0], j[1]), 2, (0, 0, 255), -1) cv2.imshow('', bgr) cv2.waitKey(0) cv2.destroyAllWindows()
python a.py
画像が表示されるので確認. このあと,ウインドウの右上の「x」をクリックしない.画面の中をクリックしてから,何かのキーを押して閉じる
コマンドプロンプトで次を実行
cd %HOMEPATH% python
次の Python プログラムを実行
import face_recognition picture_of_obama = face_recognition.load_image_file("face_recognition/known_people/obama.jpg") obama_face_encoding = face_recognition.face_encodings(picture_of_obama)[0] biden_picture = face_recognition.load_image_file("face_recognition/known_people/biden.jpg") biden_face_encoding = face_recognition.face_encodings(biden_picture)[0] results = face_recognition.compare_faces([obama_face_encoding], biden_face_encoding) print(results) exit()
https://github.com/ageitgey/face_recognition に掲載の写真から切り取った、次の写真を使ってみる
(ここでは、上の画像ファイルを、unknown_pictures の下に,104.png という名前で保存している)
cd %HOMEPATH% curl -O https://www.kkaneko.jp/db/dlib/104.png copy 104.png face_recognition\unknown_pictures python
(今度は True と表示される)
import face_recognition picture_of_obama = face_recognition.load_image_file("face_recognition/known_people/obama.jpg") obama_face_encoding = face_recognition.face_encodings(picture_of_obama)[0] another_picture = face_recognition.load_image_file("face_recognition/unknown_pictures/104.png") another_face_encoding = face_recognition.face_encodings(another_picture)[0] results = face_recognition.compare_faces([obama_face_encoding], another_face_encoding) print(results) exit()