山内セミナー(2019/06/19)

関連サイトと資料

サンプル画像


obama.jpg


jared_1.jpg


jared_2.jpg


jared_3.jpg


jared_4.jpg

サンプルプログラム(1)

encode_face_dlib.py
import cv2
import dlib
import numpy as np
	
pose_predictor_5_point = dlib.shape_predictor("shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
detector = dlib.get_frontal_face_detector()
	
def face_encodings(face_image, number_of_times_to_upsample=1, num_jitters=1):
    face_locations = detector(face_image, number_of_times_to_upsample)
    raw_landmarks = [pose_predictor_5_point(face_image, face_location) for face_location in face_locations]
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for
            raw_landmark_set in raw_landmarks]
	
image = cv2.imread("jared_1.jpg")
rgb = image[:, :, ::-1]
encodings = face_encodings(rgb)
print(encodings[0])

サンプルプログラム(2)

compare_faces_dlib.py
import cv2
import dlib
import numpy as np
	
pose_predictor_5_point = dlib.shape_predictor("shape_predictor_5_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")
detector = dlib.get_frontal_face_detector()
	
def compare_faces_ordered(encodings, face_names, encoding_to_check):
    distances = list(np.linalg.norm(encodings - encoding_to_check, axis=1))
    return zip(*sorted(zip(distances, face_names)))
	
def compare_faces(encodings, encoding_to_check):
    return list(np.linalg.norm(encodings - encoding_to_check, axis=1))
	
def face_encodings(face_image, number_of_times_to_upsample=1, num_jitters=1):
    face_locations = detector(face_image, number_of_times_to_upsample)
    raw_landmarks = [pose_predictor_5_point(face_image, face_location) for face_location in face_locations]
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for
            raw_landmark_set in raw_landmarks]
	
known_image_1 = cv2.imread("jared_1.jpg")
known_image_2 = cv2.imread("jared_2.jpg")
known_image_3 = cv2.imread("jared_3.jpg")
known_image_4 = cv2.imread("obama.jpg")
unknown_image = cv2.imread("jared_4.jpg")
	
known_image_1 = known_image_1[:, :, ::-1]
known_image_2 = known_image_2[:, :, ::-1]
known_image_3 = known_image_3[:, :, ::-1]
known_image_4 = known_image_4[:, :, ::-1]
unknown_image = unknown_image[:, :, ::-1]
	
names = ["jared_1.jpg", "jared_2.jpg", "jared_3.jpg", "obama.jpg"]
known_image_1_encoding = face_encodings(known_image_1)[0]
known_image_2_encoding = face_encodings(known_image_2)[0]
known_image_3_encoding = face_encodings(known_image_3)[0]
known_image_4_encoding = face_encodings(known_image_4)[0]
known_encodings = [known_image_1_encoding, known_image_2_encoding, known_image_3_encoding, known_image_4_encoding]
unknown_encoding = face_encodings(unknown_image)[0]
	
computed_distances = compare_faces(known_encodings, unknown_encoding)
computed_distances_ordered, ordered_names = compare_faces_ordered(known_encodings, names, unknown_encoding)
	
print(computed_distances)
print(computed_distances_ordered)
print(ordered_names)

サンプルプログラム(3)

encode_face_fr.py
import face_recognition
import cv2
	
image = cv2.imread("jared_1.jpg")
image = image[:, :, ::-1]
encodings = face_recognition.face_encodings(image)
	
print(encodings[0])

サンプルプログラム(4)

compare_faces_fr.py
import face_recognition
	
known_image_1 = face_recognition.load_image_file("jared_1.jpg")
known_image_2 = face_recognition.load_image_file("jared_2.jpg")
known_image_3 = face_recognition.load_image_file("jared_3.jpg")
known_image_4 = face_recognition.load_image_file("obama.jpg")
	
names = ["jared_1.jpg", "jared_2.jpg", "jared_3.jpg", "obama.jpg"]
unknown_image = face_recognition.load_image_file("jared_4.jpg")
	
known_image_1_encoding = face_recognition.face_encodings(known_image_1)[0]
known_image_2_encoding = face_recognition.face_encodings(known_image_2)[0]
known_image_3_encoding = face_recognition.face_encodings(known_image_3)[0]
known_image_4_encoding = face_recognition.face_encodings(known_image_4)[0]
known_encodings = [known_image_1_encoding, known_image_2_encoding, known_image_3_encoding, known_image_4_encoding]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
	
results = face_recognition.compare_faces(known_encodings, unknown_encoding)
print(results)