Skip to content

Face Recognition

recognize(image, datasets='/home/runner/work/facereg/facereg/datasets', encodings='/home/runner/work/facereg/facereg/facereg/encodings.pickle', detection_method='cnn')

Recognize face from given image path.

Parameters:

Name Type Description Default
image str

Image path from file system.

required
datasets str

Datasets path from file system.

'/home/runner/work/facereg/facereg/datasets'
encodings str

Encodings path from file system.

'/home/runner/work/facereg/facereg/facereg/encodings.pickle'
detection_method str

Face detection method. Options: cnn or hog.

'cnn'

Returns:

Type Description
List

names (list): List of names.

Source code in facereg/recognize_faces.py
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
67
68
69
70
71
72
73
74
75
76
77
78
def recognize(
    image,
    datasets=datasets_path,
    encodings=encodings_path,
    detection_method=detection_method,
) -> List:
    """Recognize face from given image path.
    Args:
      image (str):
        Image path from file system.
      datasets (str):
        Datasets path from file system.
      encodings (str):
        Encodings path from file system.
      detection_method (str):
        Face detection method. Options: `cnn` or `hog`.
    Returns:
      names (list):
        List of names.
    """

    if not os.path.isfile(image):
        print("[recognize_faces:recognize] no image found on given image path!")
        return []
    # encode faces
    face_encoder.encode_faces(
        datasets=datasets, encodings=encodings, detection_method=detection_method
    )
    # load the known faces and embeddings
    print("[recognize_faces:recognize] loading encodings...")
    with open(encodings, "rb") as handle:
        data = pickle.load(handle)
    # load the input image and convert it from BGR to RGB
    image = cv2.imread(image)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # bounding boxes corresponding to each face in the input image
    print("[recognize_faces:recognize] recognizing faces...")
    boxes = face_recognition.face_locations(rgb, model=detection_method)
    encodings = face_recognition.face_encodings(rgb, boxes)
    # initialize the list of names for each face detected
    names = []
    # facial embeddings
    for encoding in encodings:
        # compare input image to our known encodings
        matches = face_recognition.compare_faces(data["encodings"], encoding)
        name = "Unknown"

        if True in matches:
            # matched faces indexes
            matched_idxs = [i for (i, b) in enumerate(matches) if b]
            counts = {}
            # maintain a count for each recognized face
            for i in matched_idxs:
                name = data["names"][i]
                counts[name] = counts.get(name, 0) + 1

            # find the name with largest vote
            name = max(counts, key=counts.get)
        # update the list of names
        names.append(name)
    return names