#VSCode – Let’s do some #FaceRecognition with 20 lines in #Python (2/N)

Buy Me A Coffee

Hi!

Yesterday I explained how to write a couple of lines in Python to perform live face detection in a webcam feed [Post]. Check the resources section to find more about the tools I’m using.

Today, I’ll add some more code to perform face recognition. And as usual, I’ll work with my kids to test this out. I’ll start adding 2 face encodings for Valentino and myself. The code is simple enough, and I use a simple 300×300 head-shot photo to train and get the face encoding.

def LoadFaces():
bruno_image = face_recognition.load_image_file("d:\Faces\Bruno1.jpg")
bruno_face_encoding = face_recognition.face_encodings(bruno_image)[0]
valentino_image = face_recognition.load_image_file("d:\Faces\Valen1.jpg")
valentino_face_encoding = face_recognition.face_encodings(valentino_image)[0]
known_face_encodings = [
bruno_face_encoding,
valentino_face_encoding
]
known_face_names = [
"Bruno",
"Valentino"
]
return known_face_encodings, known_face_names;

The previous function returns an set of arrays with the face encodings and the face names. In the complete file, I’ll use this to analyze the camera frame (line 31) and later to check the matches for faces (lines 34 * 36)

import face_recognition
import cv2
import numpy as np
def LoadFaces():
bruno_image = face_recognition.load_image_file("d:\Faces\Bruno1.jpg")
bruno_face_encoding = face_recognition.face_encodings(bruno_image)[0]
valentino_image = face_recognition.load_image_file("d:\Faces\Valen1.jpg")
valentino_face_encoding = face_recognition.face_encodings(valentino_image)[0]
known_face_encodings = [
bruno_face_encoding,
valentino_face_encoding
]
known_face_names = [
"Bruno",
"Valentino"
]
return known_face_encodings, known_face_names;
video_capture = cv2.VideoCapture(0)
known_face_encodings, known_face_names = LoadFaces()
while True:
ret, frame = video_capture.read()
rgb_frame = frame[:, :, ::1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
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"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom 25), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
cv2.putText(frame, name, (left + 6, bottom 6), font, 0.7, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

Last lines are cosmetic to mostly draw the frames for the detected faces, and show the names.

The complete project is available here https://github.com/elbruno/Blog/tree/master/20190521%20Python%20FaceRecognition

Happy Coding!

Greetings @ Burlington

El Bruno

Resources

My Posts

Advertisement

5 comments

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: