顔識別 | python3Xのブログ

python3Xのブログ

ここでは40代、50代の方が日々の生活で役に立つ情報や私の趣味であるプログラム、Excelや科学に関する内容で投稿する予定です。

完全ではないので

チョットっ躊躇もしますが

様々な表情や変装(メガネ)を見抜き

個人を特定するプログラムです

今回使ったFaceRecognizerは3種類だけです

①EnginFaceRecognizer

②FisherFaceRecognizer

③LBPHFaceRecognizer

の順にその結果を記します

 

コード

import cv2, os
import numpy as np
from PIL import Image
# train images
train_path = 'yalefaces'
# test images
test_path = 'test'
# Haar-like:Feature Classification
cascadePath = r"C:\Users\nakano\Anaconda3\pkgs\opencv-4.0.1-py36hb76ac4c_200\Library\etc\haarcascades\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
# facial recognition for OpenCV 4
# EigenFace
#recognizer = cv2.face.EigenFaceRecognizer_create()
# FisherFace
#recognizer = cv2.face.FisherFaceRecognizer_create()
# LBPH

recognizer = cv2.face.LBPHFaceRecognizer_create()
# images are taken in
def get_images_and_labels(path):
    # arrangement (for images stocks)
    images = []
    # arrangement (for labels stocks)
    labels = []
    # arrangement (for files stocks)
    files = []
    for f in os.listdir(path):
        # path of images
        image_path = os.path.join(path, f)
        # read by gary scale
        image_pil = Image.open(image_path).convert('L')
        # stocked in arrangement of NumPy
        image = np.array(image_pil, 'uint8')
        # Haar-like:facial detection(feature classification)
        faces = faceCascade.detectMultiScale(image)
        # processing
        for (x, y, w, h) in faces:
            # resize(200x200)
            roi = cv2.resize(image[y: y + h, x: x + w], (200, 200), interpolation=cv2.INTER_LINEAR)
            # images(in the arrangement)
            images.append(roi)
            # labels are taken in from file names.
            labels.append(int(f[7:9]))
            # file names are stocked in arrangement
            files.append(f)
    return images, labels, files

# load images
images, labels, files = get_images_and_labels(train_path)
# tarin
recognizer.train(images, np.array(labels))
# test
test_images, test_labels, test_files = get_images_and_labels(test_path)
i = 0
while i < len(test_labels):
    # predict
    label, confidence = recognizer.predict(test_images[i])
    # console
    print("Test Image: {}, Predicted Label: {}, Confidence: {}".format(test_files[i], label, confidence))
    # view
    cv2.imshow("test image", test_images[i])
    cv2.waitKey(300)
    i += 1
# complete process
cv2.destroyAllWindows()
テスト画像: subject01.wink,            識別結果:  1, 信頼度: 5474.734256667496
テスト画像: subject
02.sad,             識別結果:  2, 信頼度: 4080.2906923090754
テスト画像: subject
03.glasses,        識別結果:  3, 信頼度: 10713.237107610594
テスト画像: subject
04.leftlight,        識別結果: 14, 信頼度: 6517.747574106187     XX
テスト画像: subject05.surprised,     識別結果:  5, 信頼度: 4953.66583480945
テスト画像: subject
06.centerlight,   識別結果: 12, 信頼度: 9683.138230892655      XX
テスト画像: subject07.sleepy,         識別結果:  7, 信頼度: 2904.4042060749457
テスト画像: subject
08.rightlight,      識別結果:  6, 信頼度: 9377.025972104246      XX
テスト画像: subject09.happy,         識別結果:  9,  信頼度: 6348.283675760366
テスト画像: subject
10.noglasses,    識別結果: 10, 信頼度: 3475.939782319625
テスト画像: subject
11.normal,        識別結果: 11, 信頼度: 3320.4110942691127
テスト画像: subject
12.wink,           識別結果: 12, 信頼度: 7857.9288764710645
テスト画像: subject
13.sad,            識別結果: 13, 信頼度: 3466.441368103875
テスト画像: subject
14.glasses,       識別結果: 14, 信頼度: 4978.5364595813135
テスト画像: subject
15.surprised,    識別結果: 15, 信頼度: 5994.86461890028
テスト画像: subject01.wink,           識別結果:  1, 信頼度: 1938.3969044828814
テスト画像: subject
02.sad,            識別結果:  2, 信頼度: 960.3256558136222
テスト画像: subject
03.glasses,       識別結果:  3, 信頼度: 1936.7866179022017
テスト画像: subject
04.leftlight,       識別結果: 14, 信頼度: 2443.0329406278593   X
テスト画像: subject05.surprised,     識別結果:  5, 信頼度: 1278.6880037908086
テスト画像: subject
06.centerlight,   識別結果:  6, 信頼度: 2195.097741822751
テスト画像: subject
07.sleepy,         識別結果:  7, 信頼度: 1049.5206933474653
テスト画像: subject
08.rightlight,     識別結果: 10, 信頼度: 2939.7327618152226   X
テスト画像: subject09.happy,         識別結果:  9, 信頼度: 1659.6121939094476
テスト画像: subject
10.noglasses,   識別結果: 10, 信頼度: 1033.0179560907186
テスト画像: subject
11.normal,       識別結果: 11, 信頼度: 1625.9333166879533
テスト画像: subject
12.wink,          識別結果: 12, 信頼度: 1534.1928425193505
テスト画像: subject
13.sad,           識別結果: 13, 信頼度: 974.9455922795806
テスト画像: subject
14.glasses,      識別結果: 14, 信頼度: 820.1833794622077
テスト画像: subject
15.surprised,   識別結果: 15, 信頼度: 1573.6617546912541
テスト画像: subject01.wink,          識別結果:  1, 信頼度: 29.069779601766207
テスト画像: subject
02.sad,           識別結果:  2, 信頼度: 25.376160132147987
テスト画像: subject
03.glasses,      識別結果:  3, 信頼度: 46.77730386814718
テスト画像: subject
04.leftlight,      識別結果:  4, 信頼度: 44.40023715023647
テスト画像: subject
05.surprised,   識別結果:  5, 信頼度: 26.470641545194894
テスト画像: subject
06.centerlight, 識別結果: 15, 信頼度: 47.39389413863307    X
テスト画像: subject07.sleepy,       識別結果:  7, 信頼度: 18.20026479764992
テスト画像: subject
08.rightlight,    識別結果:  9, 信頼度: 55.11161195359889     X
テスト画像: subject09.happy,        識別結果:  9, 信頼度: 28.677786927225895
テスト画像: subject
10.noglasses,   識別結果: 10, 信頼度: 16.11567730160056
テスト画像: subject
11.normal,       識別結果: 11, 信頼度: 21.40005124927871
テスト画像: subject
12.wink,          識別結果: 12, 信頼度: 36.34764808476766
テスト画像: subject
13.sad,           識別結果: 13, 信頼度: 23.004445169567926
テスト画像: subject
14.glasses,      識別結果: 14, 信頼度: 25.16146896533009
テスト画像: subject
15.surprised,   識別結果: 15, 信頼度: 30.641585044949483