Celebrity Face Recognition

Original article was published on Artificial Intelligence on Medium

Celebrity Face Recognition

Some times it happens you don’t know the name of the celebrity in the picture or in real life so this article gonna help you to build a face recognizer.

Recognizing celebrity face using transfer learning for this purpose we will have to use FaceNet model to extract features from the face.

The tasks we need to accomplish :

  • Getting the data
  • Getting the pre-trained model (FaceNet)
  • Detecting face boundaries and cropping
  • Generating embedding for data
  • Applying the classification algorithm
  • My fav part Prediction

Let’s go

Getting the data for training

There are many ways to get celebrity data :

  • Downloading images from google
  • create an Instagram scrapper to scrap images
  • Search for an existing dataset

I use Instagram to download images I only collect 15–20 images for each

Getting the pre-trained model (FaceNet)

The model can be downloaded from here:

Place the model inside the working directory

Detecting face boundaries and cropping

For detecting face boundaries, I have used Dlib

  • Loading dlib face detector CNN model
import dlibface_detect=dlib.cnn_face_detection_model_v1(“Data/model/mmod_human_ face_detector.dat”)
  • Detecting face and cropping it
def get_image_face(img):
faces = face_detector(img, 1)
crop_faces=[]
for face in faces:
crop_faces.append(img[face.rect.top():face.rect.bottom(),
face.rect.left():face.rect.right()])
return crop_faces[0]
Image after cropping

Generating embedding for data

For Generating Embeddings we load over Keras model and pass the image through the model

def generate_embeddings(face_pixels):
face_pixels=cv2.resize(face_pixels,(160,160))
# scale pixel values
face_pixels = face_pixels.astype(‘float32’)
# standardize pixel values across channels (global)
mean, std = face_pixels.mean(), face_pixels.std()
face_pixels = (face_pixels — mean) / std
# transform face into one sample
samples = expand_dims(face_pixels, axis=0)
# make prediction to get embedding
yhat = model.predict(samples)
return yhat[0]

The Embedding is 128 bit and can be easily used in the classification task.

Applying the classification algorithm

I simply used KNN for classification task you can use any model which you prefer.

Prediction

Classified Image

If you have gotten this far into the blog give yourself a pat on the back because guess what? You’re awesome. The whole working repository is available on GitHub.

“We challenge each other, and leave as friends”. Hit me up on LinkedIn for any collaborations on the topic or edits of this article.