Project 2: DoppelGanger Find your Celebrity Look-Alike
Here I am learning OpenCV in another course from Dr. Satya Malik, I have to do several projects, today this blog reports on project number 2.
In this
project you get a lot of images from celebs in a file celeb_mini.zip, when
unzip it, it has 1166 sub-folders and each sub-folder has five images of a
celebrity. The names of the sub-folders are numbers, so they give you a file
called celeb_mapping.npy, it is a dictionary, with the name (number) of the sub-folder as a key, and you can get the name of the celeb in the photos from that
sub-folder. They suggest using the library dlib, so you also get the files shape_predictor_68_face_landmarks.dat
to get the landmarks and dlib_face_recognition_resnet_model_v1.dat for the
recognition. For the face detector, we are going to use the dlib face detector.
Besides there is a
folder with two images inside, the test-images.zip, we have to use these images
to find a celebrity look alike to them.
There is this program “enrollDlibFaceRec.py” that enrolls the celeb images in the face predictor de Dlib and the program “testDlibFaceRecImage.py”, to find the celebrity look-alike to the test-image.
What did I do?
I wanted to play with the files in my system, I like to have the control, so I uploaded the notebook “Project2_DoppelGanger_Find_Celebrity_Look_Alike.ipynb” to the lab and run the next notebook code to load and unzip the required files:
Next, I downloaded the next files from the lab to my pc .
shape_predictor_68_face_landmarks.dat
dlib_face_recognition_resnet_model_v1.dat
celeb_mapping.npy
celeb_mini.zip
test-images.zip
Next, I move the files
to my python-project2 folder and unzip there next files:
celeb_mini.zip
test-images.zip
I modified the files “enrollDlibFaceRec.py” and “testDlibFaceRecImage.py”, so these programs could read all the files required when saved in the folder “python-project2”.
My system is four years old; it has a processor Intel(R) Core(TM) i7-6700HQ CPU @ 2.6GHz 2.59 GHz, an Nvidia GEFORCE GTX 960M and 20.0 GB RAM.
It takes 44 minutes to enroll all the faces in the celeb_mini folder and generate a file called “descriptors.npy” of 5.7 MB with the information about the images found in the celeb_mini folder. Besides it saves a file called index.pkl of 62 kB.
Next I run the “testDlibFaceRecImage.py” with the image “sofia-solares.jpg” with a threshold of 0.5 and I got “unknown”, I modified the threshold to 0.6 and I got the label n00002238 that is the folder for American Singer Selena Gomez. I did the same with the image “shashikant-pedwal.jpg” and I got the label n00000102 that is the folder for Indian Film Actor Amitabh Bachchan.
Next I left the folder celeb_mini with only two subfolders n00000102 and n00002238, doing that, I can test the changes and degub the code in least than one minute each time, because this way it enrolls only ten images from two folders.
I copied and pasted the python code from the notebook “Project2_DoppelGanger_Find_Celebrity_Look_Alike.ipynb” to a python “.py” script file that I saved to my python-project2 folder with the name Project2_DoppelGanger_Find_Celebrity_Look_Alike.py.
I copied and pasted the code necessary from the files “enrollDlibFaceRec.py” and “testDlibFaceRecImage.py”, into the script Project2_DoppelGanger_Find_Celebrity_Look_Alike.py.
I changed the input and output details in Project2_DoppelGanger_Find_Celebrity_Look_Alike.py, so it runs as the specifications are asking for.
Here, I run the program
on celeb_mini folder with 1166 sub-folders to get these screen captures:
The project
specification give advice to upload the Jupiter notebook to Google colab, so, I
uploaded the notebook to colab, insert my code in the right places (# TODO) and
run each cell, I debug some lines of code from my python script, and here is a
screen capture from Google colab:
First time I have used Google colab, I liked Google colab a lot, it enrolled the 6000 images in less than three minutes Vs 44 minutes in my system, that is 15 times faster.
How does this program work?
I got a Jupiter notebook with some code missing; the
project is to write that “TODO” code
Cell 2.- import libraries
Cell 3.- import and setup the matplotlib with some
parameters
Cell 4.- Iniciate face detector and download dlib
models: shape predictor and face recognizer
# Download dlib models
# Landmark model
# Face recognition model
# Initialize face detector, facial landmarks detector
# and face recognizer
Cell 5.- load the files images to be enrolled and
unzip them, the label mapping and the test images to find celebrity look alike.
Cell 6.-assign the string name to the
faceDatasetFolder and load the labelMap
Cell 7.- We are going to read all the images from a
random celebrity and show the File path and the Celeb Name.
Here we get several images from a random celebrity like this:
Cell 8.- My code to Enrollment of Celeb Images it’s an excerpt from “enrollDlibFaceRec.py” with little changes
Enrolling a person is done by simply passing a few
images of the person through the network to obtain 128 dimensional feature
descriptor from the Dlib CNN, corresponding to each image. Given a new image of
the same person, we can verify if it is the same person by checking the
distance between the enrolled face and the new face in the 128 dimensional
space. Remember the goal with
the loss function is that images of the same class are clustered together and
images of different classes are separated by a large distance using a metric.
# #YOUR CODE HERE
# read subfolders in folder "celeb_mini"
# nameLabelMap is dict with keys as person's name
# and values as integer label assigned to this person
# labels contain integer labels for corresponding image in imagePaths
# Process
images one by one
# We will store face descriptors in an ndarray
(faceDescriptors)
# and their corresponding labels in dictionary (index)
The printed output of the enrollment process:
2. Testing (The specifications)
Once you have enrolled the celebs, you should be able to use them to find the closest celeb to the given test image using minimum distance rule.
You need to find the embeddings of the test images and compare it with the enrolled celebs to find the look-alike.
Finally, display the celeb along side the input image. Partial code is given.
Cell 9.- Change the parameters of matplolib to get bigger images
Cell 12.- Load the test images and feed the face
recognizer to get 128 numbers and make the metric comparative with the enrollments.
# read image(s) from test-images folder
# change test images color space from BGR to RGB
compatible con Dlib
# Begin to insert my code adapted from “testDlibFaceRecImage.py”
# the faceDetector gets the images one by one
# Compute face descriptor using neural network defined in Dlib using facial landmark shape
# Calculate Euclidean distances between face
descriptor calculated on face dectected
# in current frame with all the face descriptors
we calculated while enrolling faces
# Calculate minimum distance and index of this
face
# If minimum distance if less than threshold (0.6)
# end
of my code
####################
# Plot the test image
#TODO - display celeb image which looks like the
test image instead of the black image.
# Create a
empty list, append to this list all the images of the celeb int the sub-folder
label
# Read the first image from the list and show it with its name from labelMap
No hay comentarios:
Publicar un comentario