私的AI研究会 > FaceRec1a
パッケージ(dlib/face_recognition) のサイトにある Pythonコードのサンプルを動かし実用的な「顔認証/顔認識 (Face recognition)」プログラム開発の方法を検討する。
face_recognition サイトからダウンロードしコードを修正する。
オリジナル・ディレクトリ「workspace_py37/face_recognition-master」とする。
プロジェクト・ディレクトリ「workspace_py37/face_rec_try」を実行テストのディレクトリとする。
/workspace_py37 ├── face_recognition-master ← オリジナル ├── face_rec_try ← オリジナルのサンプルコードをコピー/修正 ├── face_rec ← 新規開発プロジェクト :
(base) PS > Set-Alias -Name python3 python
(base) PS > conda activate py37y (py37y) PS > cd /anaconda_win/workspace_py37/face_rec
from PIL import Image import face_recognition # Load the jpg file into a numpy array image = face_recognition.load_image_file("face_rec_try_full.jpg") # Find all the faces in the image using the default HOG-based model. # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated. # See also: find_faces_in_picture_cnn.py face_locations = face_recognition.face_locations(image) print("I found {} face(s) in this photograph.".format(len(face_locations))) for face_location in face_locations: # Print the location of each face in this image top, right, bottom, left = face_location print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # You can access the actual face itself like this: face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) pil_image.show()
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python find_faces_in_picture.py I found 5 face(s) in this photograph. A face is located at pixel location Top: 1427, Left: 810, Bottom: 1889, Right: 1272 A face is located at pixel location Top: 590, Left: 1146, Bottom: 975, Right: 1531 A face is located at pixel location Top: 1488, Left: 2044, Bottom: 1874, Right: 2429 A face is located at pixel location Top: 718, Left: 1830, Bottom: 1103, Right: 2215 A face is located at pixel location Top: 1282, Left: 3510, Bottom: 1505, Right: 3733・解像度「1280x857」コピーして入力画像を修正 find_faces_in_picture_s.py
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python find_faces_in_picture_s.py I found 5 face(s) in this photograph. A face is located at pixel location Top: 477, Left: 270, Bottom: 632, Right: 425 A face is located at pixel location Top: 182, Left: 368, Bottom: 311, Right: 497 A face is located at pixel location Top: 498, Left: 689, Bottom: 605, Right: 796 A face is located at pixel location Top: 247, Left: 629, Bottom: 355, Right: 736 A face is located at pixel location Top: 420, Left: 1158, Bottom: 495, Right: 1232※ 解像度の違いによる認識の差はない。
from PIL import Image import face_recognition # Load the jpg file into a numpy array image = face_recognition.load_image_file("face_rec_try_full.jpg") # Find all the faces in the image using a pre-trained convolutional neural network. # This method is more accurate than the default HOG model, but it's slower # unless you have an nvidia GPU and dlib compiled with CUDA extensions. But if you do, # this will use GPU acceleration and perform well. # See also: find_faces_in_picture.py face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn") print("I found {} face(s) in this photograph.".format(len(face_locations))) for face_location in face_locations: # Print the location of each face in this image top, right, bottom, left = face_location print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # You can access the actual face itself like this: face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) pil_image.show()
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python find_faces_in_picture_cnn.py I found 7 face(s) in this photograph. A face is located at pixel location Top: 1412, Left: 834, Bottom: 1820, Right: 1242 A face is located at pixel location Top: 1454, Left: 2031, Bottom: 1861, Right: 2439 A face is located at pixel location Top: 566, Left: 1134, Bottom: 906, Right: 1473 A face is located at pixel location Top: 1292, Left: 3502, Bottom: 1489, Right: 3699 A face is located at pixel location Top: 669, Left: 1866, Bottom: 1077, Right: 2274 A face is located at pixel location Top: 1460, Left: 457, Bottom: 1624, Right: 621 A face is located at pixel location Top: 1356, Left: 283, Bottom: 1451, Right: 378・解像度「1280x857」コピーして入力画像を修正 find_faces_in_picture_cnn_s.py
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python find_faces_in_picture_cnn_s.py I found 5 face(s) in this photograph. A face is located at pixel location Top: 238, Left: 628, Bottom: 351, Right: 742 A face is located at pixel location Top: 191, Left: 374, Bottom: 305, Right: 488 A face is located at pixel location Top: 470, Left: 671, Bottom: 607, Right: 807 A face is located at pixel location Top: 470, Left: 270, Bottom: 607, Right: 406 A face is located at pixel location Top: 420, Left: 1148, Bottom: 499, Right: 1227※ 解像度が高い方が認識する顔の数が多い。
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python face_recognition_knn.py Training KNN classifier... Training complete! Looking for faces in izutsu_1.jpg - Found izutsu at (245, 400) Looking for faces in nitta_1.jpg - Found nitta at (348, 348) Looking for faces in face_rec_try.jpg (eog:19651): EOG-CRITICAL **: 04:19:01.762: eog_list_store_get_pos_by_image: assertion 'EOG_IS_IMAGE (image)' failed (eog:19651): EOG-CRITICAL **: 04:19:01.809: eog_image_get_file: assertion 'EOG_IS_IMAGE (img)' failed (eog:19651): GLib-GIO-CRITICAL **: 04:19:01.809: g_file_equal: assertion 'G_IS_FILE (file1)' failed - Found izutsu at (270, 477) - Found izutsu at (368, 182) - Found okegawa at (689, 498) - Found okegawa at (629, 247) - Found unknown at (1158, 420) Looking for faces in okegawa_1.jpg - Found okegawa at (572, 263) Looking for faces in yaoi_1.jpg - Found yaoi at (502, 297)※ ほぼ認識はできるものの複数の顔の入ったものは誤認識もある。
(py37y) PS > cd /anaconda_win/workspace_py37/face_rec_try/ (py37y) PS > python face_recognition_svm.py Number of faces detected: 5 Found: nitta nitta nitta nitta nitta※ トレーニングデータが少なすぎるようで認識結果は✕。