Using Face_Recognition Library in Google Colab
This library is one of the basic ways to create a deep learning project. The project will use the face_recognition library to detect and recognize faces in photos. All of the process will be conducted in Google Colab, a free cloud-based-like Jupyter notebook. This is perfect to those who would like to experiment deep learning concepts without requiring a high-performance computer.
Setting up
Step 1: Open and make create a new notebook in Google Colab
Open Google Colab
Create a new notebook by clicking File -> New notebook
Step 2: Connect the notebook to Google Drive
To connect your Google Drive to Google Colab, click the folder icon at the lowest left side -> Mount Drive -> Connect to Google Drive
Step 3: Change runtime type to GPU
Google Colab have a subscription model if you opt for a more reliable use of online GPU but free tier is also available.
To change runtime type to GPU, click Runtime -> Change runtime type -> select GPU from the Hardware accelerator option -> Save
Running the code
Step 1: Install the face recognition library
Install face_recognition library to the notebook which makes each of the photos go through a pre-trained network and generate 128 unique numbered measurements for each of the faces.
!pip install face_recognition
Step 2: Make the known directory
I have chosen 5 different types of faces and skin tones. These are Joe Biden, Tim Cook, Tsai YingWen, Queen Elizabeth, and Kim Kardashian. You may choose your own set of known faces. Just paste the link of your chosen individual and add -O known/mark.jpg (or any other name to designate the photo) after the pasted link.
!mkdir known
!wget https://cloudfront-us-east-2.images.arcpublishing.com/reuters/QES3ISDRWFPMDGNEZRRTFLNWZE.jpg -O known/joe.jpg
!wget https://www.outsideonline.com/wp-content/uploads/2021/02/03/tim-cook-apple-park_h.jpg -O known/tim.jpg
!wget https://storage.googleapis.com/afs-prod/media/438bbc560e924b1a8a72b9b519915415/3000.jpeg -O known/tsai.jpg
!wget https://images5.alphacoders.com/694/694607.jpg -O known/eli.jpg
!wget https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/kim-kardashian-before-after-2014-1599644228.jpg -O known/kim.jpg
Step 3: Make the unknown directory
I have choose 6 different unknown photos with 5 of these people are from the known photos. I chose a more challenging photos especially with the same people but having different hair type, facial expression and even their younger selves. Make sure to add in increments -O unknown/1.jpg at the end of the pasted link.
!mkdir unknown
!wget https://betanews.com/wp-content/uploads/2015/11/timcookface-600x597.jpg -O unknown/1.jpg
!wget https://cdn.i-scmp.com/sites/default/files/d8/images/methode/2020/01/15/3e7bc792-375a-11ea-9933-e21be988cd59_image_hires_172353.jpg -O unknown/3.jpg
!wget https://images.indianexpress.com/2015/03/kimkardashian-platinumhair480.jpg -O unknown/4.jpg
!wget https://www.objeko.com/wp-content/uploads/2021/07/lady-gaga-sublime-dans-une-superbe-robe-tres-moulante-ses-fans-sont-subjugues.jpg -O unknown/5.jpg
!wget https://content.time.com/time/2010/10_pol_prodigies/biden.jpg -O unknown/6.jpg
Step 4: Import all other necessary libraries
import face_recognition
import cv2
import os
from google.colab.patches import cv2_imshow
Step 5: Read and resize the images
Some of the selected images size are not not alike. In order to match the size of the photos, it needs to be resized first.
def read_img(path):
img = cv2.imread(path)
(h, w) = img.shape[:2]
width = 500
ratio = width / float(w)
height = int(h * ratio)
return cv2.resize(img, (width, height))
Step 6: Storing and handling of the known images
known_encodings = []
known_names = []
known_dir = ‘known’
Step 7: Iterating the known images
for file in os.listdir(known_dir):
img = read_img(known_dir + ‘/’ + file)
img_enc = face_recognition.face_encodings(img)[0]
known_encodings.append(img_enc)
known_names.append(file.split(‘.’)[0])
Step 8: Comparing images by generating the unknown encodings
unknown_dir = ‘unknown’
for file in os.listdir(unknown_dir):
print(“Processing”, file)
img = read_img(unknown_dir + ‘/’ + file)
img_enc = face_recognition.face_encodings(img)[0]
Step 9: Check which of the faces in known and unknown photos are similar.
results = face_recognition.compare_faces(known_encodings, img_enc)
for i in range(len(results)):
if results[i]:
name = known_names[i]
(top, right, bottom, left) = face_recognition.face_locations(img)[0]
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(img, name, (left+2, bottom+20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
cv2_imshow(img)
Results
Mostly accurate results
Of the 6 unknown photos that has been selected, the results shows that 4 of those photos are the same as the ones in the known photos. The left side of the photos below are from the known directory while the right side are from the unknown directory.
The outlier
This project is inspired by Adam Geitgey (https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78) and explained by Adarsh Menon (https://www.youtube.com/watch?v=987QtKPZ-P0).