FACE COUNTING (COUNT THE NUMBER OF FACES PRESENT IN AN IMAGE)














































FACE COUNTING (COUNT THE NUMBER OF FACES PRESENT IN AN IMAGE)



untitled (2)

FACE COUNTING (count the number of faces present in an image)

Face recognition is the computer vision task, which takes place in two steps :

  1. Face Extraction (number of faces in the given image)
  2. Recognizing / Identifying each and every faces in the image

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

mtcnn.jpg

Requirements

  1. Python 3.4 onwards
  2. OpenCV >= 4.1
  3. Keras >= 2.0.0 (any tensorflow supported by Keras)

!pip install mtcnn to install MTCNN package

In []:
!pip install mtcnn
Collecting mtcnn
  Downloading https://files.pythonhosted.org/packages/67/43/abee91792797c609c1bf30f1112117f7a87a713ebaa6ec5201d5555a73ef/mtcnn-0.1.0-py3-none-any.whl (2.3MB)
     |%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588%u2588| 2.3MB 4.1MB/s 
Requirement already satisfied: opencv-python>=4.1.0 in /usr/local/lib/python3.6/dist-packages (from mtcnn) (4.1.2.30)
Requirement already satisfied: keras>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from mtcnn) (2.4.3)
Requirement already satisfied: numpy>=1.11.3 in /usr/local/lib/python3.6/dist-packages (from opencv-python>=4.1.0->mtcnn) (1.18.5)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from keras>=2.0.0->mtcnn) (3.13)
Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras>=2.0.0->mtcnn) (2.10.0)
Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.6/dist-packages (from keras>=2.0.0->mtcnn) (1.4.1)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from h5py->keras>=2.0.0->mtcnn) (1.15.0)
Installing collected packages: mtcnn
Successfully installed mtcnn-0.1.0

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

In []:
# 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
In []:
# CSV file containing image name as an input and number of faces as an output
dataset= pd.read_csv('.../face_count.csv')
In []:
dataset.head()

Name HeadCount
0 10001.jpg 4
1 10002.jpg 4
2 10003.jpg 2
3 10004.jpg 3
4 10006.jpg 4
In []:
print('Dataset Shape : {}'.format(dataset.shape))
Dataset Shape : (1000, 2)
In []:
# image directory
filename = '.../image_data'
In []:
#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]))
Number of Faces : 4

The detector returns a list of JSON objects. Each JSON object contains three main keys: %u2018box%u2019, %u2018confidence%u2019 and %u2018keypoints%u2019:

  1. The bounding box is formatted as [x, y, width, height] under the key %u2018box%u2019.
  2. The confidence is the probability for a bounding box to be matching a face.
  3. JSON object with the keys %u2018left_eye%u2019, %u2018right_eye%u2019, %u2018nose%u2019, %u2018mouth_left%u2019, %u2018mouth_right%u2019. Each keypoint is identified by a pixel position (x, y).

Format of JSON object is grht.PNG

In []:
#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']))
box : [252, 165, 36, 45]
confidence : 0.9999933242797852
keypoints : {'left_eye': (263, 179), 'right_eye': (278, 186), 'nose': (266, 190), 'mouth_left': (257, 194), 'mouth_right': (271, 201)}

box : [332, 175, 29, 43]
confidence : 0.998878538608551
keypoints : {'left_eye': (337, 192), 'right_eye': (350, 190), 'nose': (342, 200), 'mouth_left': (339, 207), 'mouth_right': (351, 206)}

box : [202, 200, 31, 39]
confidence : 0.9988225102424622
keypoints : {'left_eye': (210, 213), 'right_eye': (224, 216), 'nose': (214, 220), 'mouth_left': (207, 227), 'mouth_right': (220, 230)}

box : [362, 181, 20, 34]
confidence : 0.9950502514839172
keypoints : {'left_eye': (364, 196), 'right_eye': (367, 192), 'nose': (361, 200), 'mouth_left': (367, 208), 'mouth_right': (369, 206)}
In []:
# 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))    
In []:
# converting output list into dataframe 
output = np.array(output)
output = pd.DataFrame(output)
output.columns = ['Predicted']
output.head()

Predicted
0 4
1 4
2 1
3 3
4 4
In []:
# actual output 
actual = pd.DataFrame(dataset['HeadCount'])
In []:
actual.head()

HeadCount
0 4
1 4
2 2
3 3
4 4
In []:
print('output shape : {}'.format(output.shape))
output shape : (1000, 1)
In []:
# 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
In []:
num_of_images = output.shape[0]   # total number of images

accuracy = get_acc(output, actual, num_of_images)
print('accuracy : {}'.format(accuracy))
accuracy : 0.85

More Articles of ML_coderzz unknown:

Name Views Likes
Facial Attribute Analysis 509 1
Generate QR Codes with Python and add Style 434 1
Create a simple chatbot in Python 226 1
Currency Converter using python 208 1
Pixel Level Image Comparison 1752 1
Neural Style Transfer With 2 Lines Of Code 341 1
Graph Based Text Representation 274 1
Semantic Segmentation With 2 Lines Of Code 336 1
Pose Estimation With 4 Lines Of Code 300 1
Question the Text and Summarize it 408 1
Cartooning an Image 247 1
Image to Sketch 245 1
Fine Tuning BERT for Text classification 569 1
Encoding-Decoding Text-to-Image 758 1
Create Pattern in Python 195 1
Spell Checker 419 1
Cropping Part of an image using OpenCV (grabcut algorithm) 1253 2
Transformers : All In One 449 1
Deep Convolutional GAN (DCGAN) 515 1
Spectral Normalization for Generative Adversarial Networks (SN-GAN) 712 1
ENGLISH DICTIONARY using PYTHON 357 2
GENDER CLASSIFICATION 590 1
India Air Quality Data Analysis 526 1
WINE QUALITY PREDICTION 1456 1
PREDICT NEXT N WORDS TO GENERATE COMPLETE SENTENCE 409 2
Denoising colored image 360 1
Print Emojis in Python 256 0
EMOTION DETECTION FROM TEXT 407 2
WORD EMBEDDING REPRESENTATION AND VISUALISATION (using GLOVE vector) 1016 2
Age Detection of Actors 1067 2
IMAGE CLASSIFICATION USING PRE-TRAINED MODEL 611 1
MNIST HANDWRITTEN DIGIT CLASSIFICATION (end to end project using TensorFlowJS) 668 2
IMAGE SIMILARITY USING SIAMESE NETWORK WITH TRIPLET LOSS 3199 1
CONDITIONAL GAN 1020 3
SIMILAR SENTENCE GENERATION 1533 1
TEXT TO SPEECH using gTTS 385 2
PLANT DISEASE CLASSIFICATION (end to end project) 745 1
WASSERSTEIN GAN WITH GRADIENT PENALTY (WGAN GP) 909 1
TRAFFIC SIGN CLASSIFICATION 721 1
K-MEANS CLUSTERING OVER IMAGES 1073 1
Hindi OCR 984 1
GENERATING HANDWRITTEN MNIST DIGITS USING GAN 461 0
CURVE SIMILARITY MEASUREMENT 3048 2
Stock Price Prediction and Forecasting using LSTM 1267 1
LANGUAGE TRANSLATION 603 2
FACIAL EXPRESSION RECOGNITION 684 1
FACE COUNTING (COUNT THE NUMBER OF FACES PRESENT IN AN IMAGE) 2183 2
Image Quality Assessment 3099 1
Text Extraction From An Image 1128 2
NAMED ENTITY RECOGNITION 532 2

Comments

  • ML_coderzz
    14-Mar-2021 03:16:05 PM
    video link:
    https://www.youtube.com/watch?v=kW9JtCkvCz8