Gender Detection on real time video streaming

Gender Detection on real time video streaming


                                         - (Using CNN, Python, Keras and Open CV…)


Here, we will learn how to build a Gender Detector model using Keras, Tensorflow, and Open CV. We will also see how to apply this on a Live Video Camera.

Step 1) 

First and foremost we need to collect the data to train our model. I have taken the data images from Kaggle and google images. You can add some other additional images as well to your dataset. You can download the Kaggle dataset from here [https://www.kaggle.com/gmlmrinalini/genderdetectionface].

Step 2) 

Here we will be creating one  file where we will pre-process our data and will convert all the images into array.

We will create 2 list — one with the image data in the form of array and second to store the labels for that corresponding image.

Importing the necessary libraries:

from tensorflow.keras.preprocessing.image import 
from tensorflow.keras.optimizers import 
from tensorflow.keras.preprocessing.image import 
from tensorflow.keras.utils import 
from tensorflow.keras.models import 
from tensorflow.keras.layers import 
from tensorflow.keras import as 
from sklearn.model_selection import 
import matplotlib.pyplot as 
import as 
import 
import 
import 
import 

initial parameters:
epochs lrbatch_size img_dims 

data = []
labels = []

Now we are loading the dataset images and shuffling the same to train our model in a better way and to get more accurate results:

= [f for f in glob.glob(r’C:\Files\gender_dataset_face’ + “/**/*”, recursive=True) if not os.path.isdir(f)]
random.(image_files)

Here we are converting images to arrays and labeling the categories and then converting those to the ndarray to pass as an input to our model:

Image for post
Converting images to arrays and labels

 Now, we will split our dataset into training and testing part and converting them to the categorical data.

Image for post
Splitting the dataset into training and testing sets

 Now we will use the to train our model with more number of images , by rotating them , shifting left and right…

Image for post
ImageDataGenerator to add more images to our model

Step 3) 

Now we will create a function separately to build the model using Keras CNN to train it with our input images.

Image for post

 Building the model using above function:

= buildModel(width=img_dims[0], height=img_dims[1], depth=img_dims[2],
classes=2)

Now we will compile our model with  which will change our learning rate after each epoch it performs to get the more accuracy:

opt = (lr=lr, decay=lr/epochs)
.(loss=”binary_crossentropy”, optimizer=opt, metrics=[“accuracy”])

  •  Now, we will train our model and will save the same for later usage while testing:

Image for post
Training and Saving the model

 Now , we will plot and see the training and testing accuracy and loss analysis in the form of graphs using matplotlib:

Image for post
Plotting loss and accuracy of our model
Image for post
An example of above code execution

Step 4) 

 Initializing all the required libraries and loading our model which e have created in above steps:

from tensorflow.keras.preprocessing.image import 
from tensorflow.keras.models import 
import as 
import 
import 
import as 


model = load_model(‘’)

 Open webcam and declare both the output classes:
webcam = (0)
= [‘man’,’woman’]

 Now , We are looping through the frames getting captured by camera, reading frames from camera and then applying the face detection technique to detect the faces.

After that we will loop through all those detected frames, will crop them, will draw a rectangle over detected faces and then at the end will be applying our model we have created on it to detect the gender of that particular person.

Image for post
Capturing Frames and Predicting the results

Now we are showing the final image frame captured above with rectangle and predicted class label on it and once we press “Q” from out keyboard the camera window will get closed:

cv2.(“gender detection”, frame)
if cv2.(1) & 0xFF == ord(‘q’):
break

 Releasing all the resources now:

webcam.()
cv2.()

For the entire code please checkout .

Thank you! and Happy Coding!!.

Comments

Popular posts from this blog

Random Forest (Easily Explained)

Handle Imbalanced Dataset