Showing posts with label 【PYTHON OPENCV】This script makes used of dlib library to calculate the 128D descriptor to be used for face recognition and compare the faces using some distance metrics. Show all posts
Showing posts with label 【PYTHON OPENCV】This script makes used of dlib library to calculate the 128D descriptor to be used for face recognition and compare the faces using some distance metrics. Show all posts

Thursday, 15 April 2021

【PYTHON OPENCV】This script makes used of dlib library to calculate the 128D descriptor to be used for face recognition and compare the faces using some distance metrics

 """

This script makes used of dlib library to calculate the 128D descriptor to be used for face recognition and compare the faces using some distance metrics """ # Import required packages: import cv2 import dlib import numpy as np # Load shape predictor, face enconder and face detector using dlib library: 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): """Returns the ordered distances and names when comparing a list of face encodings against a candidate 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): """Returns the distances when comparing a list of face encodings against a candidate 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): """Returns the 128D descriptor for each face in the image""" # Detect faces: face_locations = detector(face_image, number_of_times_to_upsample) # Detected landmarks: raw_landmarks = [pose_predictor_5_point(face_image, face_location) for face_location in face_locations] # Calculate the face encoding for every detected face using the detected landmarks for each one: return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks] # Load images: 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") # Convert image from BGR (OpenCV format) to RGB (dlib format): 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] # Crate names for each loaded image: names = ["jared_1.jpg", "jared_2.jpg", "jared_3.jpg", "obama.jpg"] # Create the encodings: 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] # Compare faces: computed_distances = compare_faces(known_encodings, unknown_encoding) computed_distances_ordered, ordered_names = compare_faces_ordered(known_encodings, names, unknown_encoding) # Print obtained results: print(computed_distances) print(computed_distances_ordered) print(ordered_names)

BREAKING: North Carolina automotive group acquires 7 Upstate dealerships

Breaking news from GSA Business Report Click here to view this message in a browser window. ...