Face matching and code implementation

In this article, I will detail how to use face_recognition and pilot are modules that extract and recognize faces from a pile of photos. I want to owe it to Brad traversi because the idea was originally from a Traversy media video that I have bookmarked for some time and recently re viewed (link below this article). Using the same functionality, I adjusted the script to allow the input of a photo directory, which will eventually be incorporated into my second PDM Django application.

They are very crude and ready-made scripts to prove the concept, which I demonstrated to Bob in a weekly code check-in call.

I created two scripts, one for extracting faces from a pile of photos and storing them as jpg files in the specified directory. The second script takes a set of known faces (I guess this is the control set) and compares them with photos to identify faces in random photos. In general, it is very accurate and fast in recognizing known faces.

Face extraction
The first script is fairly simple to implement. We have an image directory in which we want to extract all the images of the face. We call this directory unknown directory. The script essentially scans each photo, recognizes the face, and stores the face image as a new jpeg in another directory, which we call "extraction". The file is created with the title of the original image and the position of the face in the image.

import face_recognition
from PIL import Image
import os

unknown_faces = os.listdir("../frec/unknown/")

for image in unknown_faces:
    image_of_people = face_recognition.load_image_file(f"../frec/unknown/{image}")
    unknown_face_locations = face_recognition.face_locations(image_of_people)

    for face_location in unknown_face_locations:
        top, right, bottom, left = face_location

        face_image = image_of_people[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)

So from this image:

You will get two separate images in the extraction directory, as follows:

After testing this on my own photos, the module is so good that I found a face on a poster in my photos, and you can see it if you download the code from the repository.

Face recognition time
The second script does all the smart things, as long as it will use the image directory of unknown faces, compare them with known face images, and then 'draw' a square around the face and the name of the recognized person on the original image. If it does recognize a face, it will draw a "unknown person" around the face A square in place of the name.

import face_recognition
import os
from PIL import Image, ImageDraw

unknown_faces = os.listdir("../frec/unknown/")
Bob_image = face_recognition.load_image_file("../frec/known/Bob.jpeg")
Bob_encoding = face_recognition.face_encodings(Bob_image)[0]
Julian_image = face_recognition.load_image_file("../frec/known/Julian.jpeg")
Julian_encoding = face_recognition.face_encodings(Julian_image)[0]

known_face_encodings = [

known_face_names = [

for ukface in unknown_faces:
    ukimage = face_recognition.load_image_file(f"../frec/unknown/{ukface}")
    ukface_locations = face_recognition.face_locations(ukimage)
    ukface_encodings = face_recognition.face_encodings(ukimage, ukface_locations)

    # Convert to PIL format
    pil_image = Image.fromarray(ukimage)

    # Set up drawing on image
    draw = ImageDraw.Draw(pil_image)
    for (top, right, bottom, left), ukface_encoding in zip(
        ukface_locations, ukface_encodings
        matches = face_recognition.compare_faces(known_face_encodings, ukface_encoding)

        name = "Unknown Person"

        if True in matches:
            first_match_index = matches.index(True)
            name = known_face_names[first_match_index]

        # Draw Box
            ((left - 10, top - 10), (right + 10, bottom + 10)), outline=(227, 236, 75)

        # Draw Label
        text_width, text_height = draw.textsize(name)
            ((left - 10, bottom - text_height + 2), (right + 10, bottom + 10)),
            fill=(227, 236, 75),
            outline=(227, 236, 75),
        draw.text((left, bottom - text_height + 5), name, fill=(0, 0, 0, 0))

    del draw

I played the 'dra. Rectangle' function and tried to capture as many faces inside the square as possible, because the original face was covered by the square.

Therefore, from a given image with an unknown face:

Two different images, Bob and Julian, are used for matching:

You will eventually:

As I said earlier, the accuracy is amazing, and you can adjust it_ Recognition a package that expands or narrows the matching range. It is also very fast when scanning the image directory and displaying the results on the screen or saving the results to another directory. It is very easy to adjust the script slightly.

I believe you can also use GPU processing, but I can't test this in my current environment.

I have extended the scripts in the repo to include more examples of known / unknown faces, so I can download and play them at will.

For those interested in the material I modified, please take a look at Brad's video YouTube Without these, I wouldn't have found it_ Recognition module. I hope you can modify the code like me to suit your own purpose.

I also had to adjust my script to increase the chance of matching, otherwise Todd Dewey from the ice truck company was identified as me (I'm not sure who would be flattered!) this is model and num_ The jitters option is used for scripts found in the buy back.

Tags: Python AI Deep Learning

Posted by jkewlo on Tue, 28 Sep 2021 12:57:38 +0530