-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
66 lines (53 loc) · 2.56 KB
/
utils.py
File metadata and controls
66 lines (53 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import dlib
from glob import glob
import cv2
import numpy as np
import os
# load the face detector, landmark predictor, and face recognition model
face_detector = dlib.get_frontal_face_detector()
shape_predictor = dlib.shape_predictor("models/shape_predictor_68_face_landmarks.dat")
face_encoder = dlib.face_recognition_model_v1("models/dlib_face_recognition_resnet_model_v1.dat")
# change this to include other image formats you want to support (e.g. .webp)
VALID_EXTENSIONS = ['.png', '.jpg', '.jpeg']
def get_image_paths(root_dir, class_names):
""" grab the paths to the images in our dataset"""
image_paths = []
# loop over the class names
for class_name in class_names:
# grab the paths to the files in the current class directory
class_dir = os.path.sep.join([root_dir, class_name])
class_file_paths = glob(os.path.sep.join([class_dir, '*.*']))
# loop over the file paths in the current class directory
for file_path in class_file_paths:
# extract the file extension of the current file
ext = os.path.splitext(file_path)[1]
# if the file extension is not in the valid extensions list, ignore the file
if ext.lower() not in VALID_EXTENSIONS:
print("Skipping file: {}".format(file_path))
continue
# add the path to the current image to the list of image paths
image_paths.append(file_path)
return image_paths
def face_rects(image):
# convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# detect faces in the grayscale image
rects = face_detector(gray, 1)
# return the bounding boxes
return rects
def face_landmarks(image):
return [shape_predictor(image, face_rect) for face_rect in face_rects(image)]
def face_encodings(image):
# compute the facial embeddings for each face
# in the input image. the compute_face_descriptor
# function returns a 128-d vector that describes the face in an image
return [np.array(face_encoder.compute_face_descriptor(image, face_landmark))
for face_landmark in face_landmarks(image)]
def nb_of_matches(known_encodings, unknown_encoding):
# compute the euclidean distance between the current face encoding
# and all the face encodings in the database
distances = np.linalg.norm(known_encodings - unknown_encoding, axis=1)
# keep only the distances that are less than the threshold
small_distances = distances <= 0.6
# return the number of matches
return sum(small_distances)