Creating Face Recognition Model Using CNN Architectures.

Face Recognition and send Mail, SMS, WhatsApp message once face gets recognized using Python.

Kothapally Gnana praneeth
5 min readJun 19, 2021

--

  1. Face Recognition
  2. Sending Notification via different platforms using Python.

GitHub link:

https://github.com/Praneeth102/face_recognision.git

Face Recognition:

In a Convolution Neural Network(CNN) there are different types of architectures are created. Depending upon the use cases we need to use them.

  1. LeNet
  2. VGG
  3. AlexNet
  4. ResNet etc.,

Here I use LeNet architecture for creating a face recognition model. I made some changes in the architecture to reach the desired accuracy by hit and trial.

LeNet Architecture:

LeNet consists of 7 layers alternatingly 2 convolutional and 2 average pooling layers, and then 2 fully connected layers and the output layer with activation function softmax.

In my architecture, I added one more convolution and max-pooling layer. Using Softmax as an activation function in the output layer.

I created one python code to capture so many images using a haarcascade face detection model and by applying loops for collecting datasets.

I collected datasets of 2 different persons and one dog. Now I am using Keras to create Neural Network, coming to the code import all the required libraries.

from keras.models import Sequential

from keras.layers import Dense, Dropout, Activation, Flatten

from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D

from keras.layers.normalization import BatchNormalization

import cv2

If any module is not found use the pip command to install them. This is the architecture for my face recognition model.

model=Sequential()

model.add(Conv2D(20,(5,5),input_shape=(64,64,3),padding=”same”))

model.add(Activation(“relu”))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Activation(“relu”))

model.add(Conv2D(40,(5,5),padding=”same”))

model.add(Activation(“relu”))

model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Activation(“relu”))

model.add(Conv2D(45,(5,5),padding=”same”))

model.add(Activation(“relu”))

model.add(MaxPooling2D(pool_size=(2,2),strides = (2, 2)))

model.add(Activation(“relu”))

model.add(Flatten())

model.add(Dense(units=500))

model.add(Activation(“relu”))

model.add(Dense(units=64))

model.add(Activation(“relu”))

model.add(Dense(units=3,activation=’softmax’))

I used a sequential model from Keras and relu as the activation function for inner layers. Three alternating convolution and padding layers are used. convolution layer helps in detecting edges in the image once edges get detected pooling layer down the sampling of the images. The above layer gives the 2D output we need to convert 2D into 1D by using Flatten layer. By using the Dense function I added two hidden layers and one output layer. Using relu as the activation function for hidden layers and outer layer Softmax as activation function because it comes under the Multi Classification category.

Here I have three classifications in the dataset so I added three units in the output layer.

from keras.optimizers import Adam

model.compile(loss=’categorical_crossentropy’,optimizer=’Adam’,metrics=[‘accuracy’])

Use Adam as an optimizer and categorical_crossentropy as a loss. These help in the decrease in loss and increase accuracy.

model.summary()

The final architecture of our CNN:

Model: “sequential” _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 64, 64, 20) 1520 _________________________________________________________________ activation (Activation) (None, 64, 64, 20) 0 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 32, 32, 20) 0 _________________________________________________________________ activation_1 (Activation) (None, 32, 32, 20) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 32, 32, 40) 20040 _________________________________________________________________ activation_2 (Activation) (None, 32, 32, 40) 0 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 16, 16, 40) 0 _________________________________________________________________ activation_3 (Activation) (None, 16, 16, 40) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 16, 16, 45) 45045 _________________________________________________________________ activation_4 (Activation) (None, 16, 16, 45) 0 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 8, 8, 45) 0 _________________________________________________________________ activation_5 (Activation) (None, 8, 8, 45) 0 _________________________________________________________________ flatten (Flatten) (None, 2880) 0 _________________________________________________________________ dense (Dense) (None, 500) 1440500 _________________________________________________________________ activation_6 (Activation) (None, 500) 0 _________________________________________________________________ dense_1 (Dense) (None, 64) 32064 _________________________________________________________________ activation_7 (Activation) (None, 64) 0 _________________________________________________________________ dense_2 (Dense) (None, 3) 195 ================================================================= Total params: 1,539,364 Trainable params: 1,539,364 Non-trainable params: 0

from keras_preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(

rescale=1./255,

shear_range=0.2,

zoom_range=0.2,

horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(

‘/content/drive/MyDrive/face_detec/train’,

target_size=(64, 64),

batch_size=32,

class_mode=’categorical’)

validation_generator = test_datagen.flow_from_directory(

‘/content/drive/MyDrive/face_detec/test’,

target_size=(64, 64),

batch_size=32,

class_mode=’categorical’)

Imagedatagenerator generates more images by making minor changes in our image view like angle rotation, zoom in and out. It also converts all images in the same given size.

model.fit(

train_generator,

steps_per_epoch=10,

epochs=10,

validation_data=validation_generator,

validation_steps=40)

Now model gets trained by using the above data generator and created Neural Network. while training the model only it gets validates by this we can easily know validation accuracy and helps in creating the model.

Epoch 1/10
10/10 [==============================] - 81s 7s/step - loss: 1.0531 - accuracy: 0.4560 - val_loss: 1.0499 - val_accuracy: 0.4050
Epoch 2/10
10/10 [==============================] - 37s 4s/step - loss: 0.6886 - accuracy: 0.7134 - val_loss: 0.8518 - val_accuracy: 0.6729
Epoch 3/10
10/10 [==============================] - 40s 4s/step - loss: 0.4042 - accuracy: 0.8443 - val_loss: 0.3582 - val_accuracy: 0.8563
Epoch 4/10
10/10 [==============================] - 36s 4s/step - loss: 0.2891 - accuracy: 0.8882 - val_loss: 0.2227 - val_accuracy: 0.9305
Epoch 5/10
10/10 [==============================] - 38s 4s/step - loss: 0.1528 - accuracy: 0.9598 - val_loss: 0.1786 - val_accuracy: 0.9383
Epoch 6/10
10/10 [==============================] - 35s 4s/step - loss: 0.1602 - accuracy: 0.9572 - val_loss: 0.0874 - val_accuracy: 0.9619
Epoch 7/10
10/10 [==============================] - 32s 4s/step - loss: 0.1275 - accuracy: 0.9545 - val_loss: 0.0631 - val_accuracy: 0.9830
Epoch 8/10
10/10 [==============================] - 40s 4s/step - loss: 0.1485 - accuracy: 0.9402 - val_loss: 0.0706 - val_accuracy: 0.9768
Epoch 9/10
10/10 [==============================] - 38s 4s/step - loss: 0.1046 - accuracy: 0.9572 - val_loss: 0.1481 - val_accuracy: 0.9489
Epoch 10/10
10/10 [==============================] - 40s 4s/step - loss: 0.0568 - accuracy: 0.9816 - val_loss: 0.0946 - val_accuracy: 0.9586
<keras.callbacks.History at 0x7fae61170d50>

My model gets trained with 98% accuracy and 95% validation accuracy. Getting this much accuracy is really good.

model.save(‘face_det_lenet4.h5’)

once the model gets saved we can use it any time by loading using Keras.

The code file(“final_sms_mail_wp.ipynb”) uploaded in GitHub tells how to convert predicting images into certainly required dimensions and classifying the outputs with names.

I added how to send SMS, mail, Whatsapp once the image gets detected in the same file.

SMS

for sending SMS twilio library helps us. Initially create an account in twilio by using the key, token and number SMS gets send. code available in GitHub.

WhatsApp Message:

WhatsApp messages can easily send by using the pywhatkit library. No need to create any account in pywhatkit.

Email:

For sending an email using python code we need to decrease the security of email. For Gmail turn off “two-step verification” and turn on “allow less secure apps”.Decreasing security to Gmail is risky. By using the “smtplib” library mail can be sent easily.

Please check GitHub for detailed code of the face recognition model and messages sending using different platforms.

--

--

No responses yet