Face recognition is the computer vision task, which takes place in two steps :
In this article we are going to deal with "face extraction" mechanism for which we will use MTCNN ( Multi-Task Cascaded Convolutional Neural Network).
It is based on the paper Zhang, K et al. (2016). For more information visit https://pypi.org/project/mtcnn/#zhang2016
Requirements
!pip install mtcnn to install MTCNN package
!pip install mtcnn
In this article I am going to use face count dataset of 1000 images to check performance of MTCNN. So that you can use it for task like face recognition, counting number of faces, etc. Visit given link to download the dataset https://www.kaggle.com/vin1234/count-the-number-of-faces-present-in-an-image
# Loading required libraries
from PIL import Image
from numpy import asarray
from mtcnn.mtcnn import MTCNN
import tensorflow.keras as Keras
import matplotlib.pyplot as plt
import os
import pandas as pd
import numpy as np
# CSV file containing image name as an input and number of faces as an output
dataset= pd.read_csv('.../face_count.csv')
dataset.head()
print('Dataset Shape : {}'.format(dataset.shape))
# image directory
filename = '.../image_data'
#loading sample image
dir = filename + '/' + dataset['Name'][0] # loading first image of csv file
image = Image.open(dir) #loading image from given path
image = image.convert('RGB') # converting image into 3D (RGB) image
pixels = asarray(image)
plt.imshow(pixels)
plt.axis('off')
print('Number of Faces : {}'.format( dataset['HeadCount'][0]))
The detector returns a list of JSON objects. Each JSON object contains three main keys: %u2018box%u2019, %u2018confidence%u2019 and %u2018keypoints%u2019:
Format of JSON object is
#face extraction using MTCNN
detector = MTCNN()
results = detector.detect_faces(pixels)
# As we already know that there are 4 faces so results variable contains %u2018box%u2019, %u2018confidence%u2019
# and %u2018keypoints%u2019of each face
total_faces = len(results)
for face in range(total_faces):
print('box : {}'.format(results[face]['box']))
print('confidence : {}'.format(results[face]['confidence']))
print('keypoints : {}'.format(results[face]['keypoints']))
# similarly now applying over 1000 images to measure performance of MTCNN
# storing number of heads in each and every image
output = []
# looping through CSV file
for img in dataset['Name']:
# diretory of image
dir = filename + '/' + img
image = Image.open(dir) #loading image from given path
image = image.convert('RGB') # converting image into 3D (RGB) image
pixels = asarray(image)
# face extraction
detector = MTCNN()
results = detector.detect_faces(pixels)
# len(results) returns number of faces in an image
output.append(len(results))
# converting output list into dataframe
output = np.array(output)
output = pd.DataFrame(output)
output.columns = ['Predicted']
output.head()
# actual output
actual = pd.DataFrame(dataset['HeadCount'])
actual.head()
print('output shape : {}'.format(output.shape))
# Now we have to compare actual output with the predicted output
# accuracy = (predicted[i] == actual[i]) / number_of_images
def get_acc(predicted, actual, num_of_images):
correct = 0
for idx in range(num_of_images):
if output['Predicted'][idx] == actual['HeadCount'][idx]:
correct += 1
accuracy = correct / num_of_images
return accuracy
num_of_images = output.shape[0] # total number of images
accuracy = get_acc(output, actual, num_of_images)
print('accuracy : {}'.format(accuracy))
Comments
ML_coderzz
14-Mar-2021 03:16:05 PM