Face recognition in Python

Viewed 93

I'm experimenting with face recognition in Python. I watched a tutorial and wrote a code but I'm curious if there is an option to do it using database. I don't want to use webcam and I couldn't find anything.

import face_recognition
from PIL import Image, ImageDraw

image_of_keanu = face_recognition.load_image_file('KeanuR.jpg')
keanu_face_encoding = face_recognition.face_encodings(image_of_keanu)[0]

image_of_shakira = face_recognition.load_image_file('Shakira.jpg')
shakira_face_encoding = face_recognition.face_encodings(image_of_shakira)[0]

image_of_trump = face_recognition.load_image_file('DTrump.jpg')
trump_face_encoding = face_recognition.face_encodings(image_of_trump)[0]

image_of_beyonce = face_recognition.load_image_file('Beyonce.jpg')
beyonce_face_encoding = face_recognition.face_encodings(image_of_beyonce)[0]

known_face_encodings = [keanu_face_encoding, shakira_face_encoding, trump_face_encoding, beyonce_face_encoding]

known_face_names = ["Keanu Reeves", "Shakira", "Donald Trump", "Beyonce"]

test_image = face_recognition.load_image_file('unknown6.jpg')

face_locations = face_recognition.face_locations(test_image)
face_encodings = face_recognition.face_encodings(test_image, face_locations)

pil_image = Image.fromarray(test_image)

draw = ImageDraw.Draw(pil_image)

for(top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
    matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
    
     
    name = "Unknown person"
    
    if True in matches:
        first_match_index = matches.index(True)
        name = known_face_names[first_match_index]
        
    draw.rectangle(((left, top), (right, bottom)), outline=(5,5,5))
        
    text_width, text_height = draw.textsize(name)
    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill = (0,0,0), 
                       outline=(0,0,0))
    draw.text((left + 6, bottom - text_height - 5), name, fill=(255,255,255,255))
        
del draw     
    
pil_image.show()
0 Answers
Related