Beard Detection using Keras in Python

In this attempt, we’ll look at how we can build a beard detection system using Keras in Python. Keras is an open-source neural-network library written in Python. We can use Keras to quickly build much efficient neural networks .  In this tutorial we’ll be using our own dataset for training the model.

Also we’ll use OpenCV and Haar-Cascade detection. Haar feature-based cascade classifiers is an effective object detection method. Firstly we’ll focus on detecting faces in an image or video source. After that we’ll procede further to detect if that face has a beard or not.

Data Preprocessing

Use OpenCV and python’s os module to create directories. Create a dataset folder with two sub-folders like “beard” and “clean shaved”. Add clean-shaved pictures in the “clean shaved” folder and bearded ones in “bearded” folder. The next step is to create these directories in our python file.

import cv2,os

data_path='dataset'
categories=os.listdir(data_path)
labels=[i for i in range(len(categories))]

label_dict=dict(zip(categories,labels)) #empty dictionary

print(label_dict)
print(categories)
print(labels)

This code block will outputs a python dictionary with  index as values and names of folders as keys. {'beard': 0, 'cleanshave': 1}

Now we’ll create two empty list and append the images and their respective labels into these lists. Also resize these images and turn them into grayscale using OpenCV, so that all of our images are of same size and have same color channels.

data=[]
target=[]

for category in categories:
    folder_path=os.path.join(data_path,category)
    img_names=os.listdir(folder_path)
        
    for img_name in img_names:
        img_path=os.path.join(folder_path,img_name)
        img=cv2.imread(img_path)

        try:
            gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)           
            resized=cv2.resize(gray,(img_size,img_size))
            #resizing the gray scale into 100x100, since we need a fixed common size for all the images in the dataset
            data.append(resized)
            target.append(label_dict[category])
        except Exception as e:
            print('Exception:',e)

The next step is to normalize the color range between (0 to 255) to (0 to 1) by dividing the data by 255.0. Also reshape these images in an numpy array of 4 dimension. Coming to the labels , we’ll use “one-hot encoding” to convert them into 0’s and 1’s.  np_utils.to_categoricalis the function we’ll be using to perform one-hot encoding.

import numpy as np

data=np.array(data)/255.0
data=np.reshape(data,(data.shape[0],img_size,img_size,1))
target=np.array(target)

from keras.utils import np_utils
new_target=np_utils.to_categorical(target)

Now switching to the next task which is creating and training the model.

Building and training the Keras model

Firstly we’ll save the data and target numpy array in a numpy file. So that we can use this data and load it in future.

import numpy as np

data=np.load('data.npy')
target=np.load('target.npy')

Now comes the main task which is building the model. We’ll use Keras to build this model using Convolutional Neural Networks.

from keras.models import Sequential
from keras.layers import Dense,Activation,Flatten,Dropout
from keras.layers import Conv2D,MaxPooling2D
from keras.callbacks import ModelCheckpoint

model=Sequential()

model.add(Conv2D(200,(3,3),input_shape=data.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(100,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(50,activation='relu'))
model.add(Dense(2,activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

Now that we’ve compiled our model well check our model’s layers and summary using model.summary() .

The next step is quite crucial , which is performing the train-test split. Train-test split is very necessary in Machine learning to get an idea of how good and accurate our model is on unseen data. A model gains all the features during train and thus it becomes really important to check it’s performance on data it has not even seen yet. We’ll use sklearn to perform train-test split.

from sklearn.model_selection import train_test_split
train_data,test_data,
train_target,test_target=train_test_split(data,target,test_size=0.1)

It’s time to create checkpoints to save the best model and then train our neural network model.

checkpoint = ModelCheckpoint('model-{epoch:03d}.model',monitor='val_loss',verbose=0,save_best_only=True,mode='auto')
history=model.fit(train_data,train_target,epochs=20,callbacks=[checkpoint],validation_split=0.2)

After the training is completed, we can plot the Training accuracy vs Validation accuracy graph and Training accuracy vs Validation Loss graphto chek if there’s any type of over-fitting. Here’s the code for accuracy graph and below are the images of what these look like.

#For accuracy graph
from matplotlib import pyplot as plt
%matplotlib inline
plt.plot(history.history['accuracy'],'r',label='training accuracy')
plt.plot(history.history['val_accuracy'],label='validation accuracy')
plt.xlabel('# epochs')
plt.ylabel('loss')
plt.legend()
plt.show()

accuracy graph

Now we’re moving to the next step which is using OpenCV and Haar-Cascade classifiers to detect faces and then make predictions using our model.

Detecting faces and beard

The first step is to load the model using Keras’s load_model function and use haarcascade_frontface_classifier.xml file to create an OpenCV classifier.

from keras.models import load_model
import cv2
import numpy as np
model = load_model('model-017.model')
face_clsfr=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
source=cv2.VideoCapture(0)
labels_dict={0:'Beard',1:'Clean Shaved'}
color_dict={0:(0,255,0),1:(0,0,255)}

With this code we’re all set to start making predictions on random faces data. We can also use webcams to check the working capability of our model. Below is the code snip from my Github repository using which we can perform real-time detection using our model.

detection

This is what our real-time beard detection looks like using Keras. You check Keras official documentation if you need any other help with the codes.

One response to “Beard Detection using Keras in Python”

  1. manikanta says:

    I want this project for my college project please give me your GitHub repository link.

Leave a Reply

Your email address will not be published. Required fields are marked *