The two key facial attributes, age and gender, play a foundational role in social interactions, making age and gender estimation from a single face image an important task in intelligent applications, such as access control, human-computer interaction, law enforcement, marketing intelligence and visual surveillance, etc.
Used YouTube URL link for data, recognizing age, gender and face detection by pre trained models.
We will be doing face recognition, face detection stuff and furthermore. We will be using CNN (Convolutional Neural Networks) for age and gender predictions from a YouTube video. You don’t need to download the video, just the video URL is fine. The interesting part will be the usage of CNN for age and gender predictions on video URLs.
Steps to follow :
1. Get the video URL from YouTube.
2. Face detection with Haar cascades.
3. Gender Recognition with CNN.
4. Age Recognition with CNN.
Process:
1. Get a video URL from YouTube:
Get the Youtube video URL and try to get the attributes of the video using pafy as explained above.
2. Face detection with Haar cascades:
This is a the part most of us at least have heard of. OpenCV/JavaCV provide direct methods to import Haar-cascades and use them to detect faces. We will not be explaining this part in depth. You can refer this article to know more about face detection using OpenCV.
3. Gender Recognition with CNN:
Gender recognition using OpenCV’s fisherfaces implementation is quite popular and some of you must have tried or read about it. But, in this example, we will be using a different approach to recognize gender.
4. Age Recognition with CNN
This is almost similar to the gender detection part except that the corresponding prototxt file and the caffe model file are “deploy_agenet.prototxt” and “age_net.caffemodel”. Furthermore, the CNN’s output layer (probability layer) in this CNN consists of 8 values for 8 age classes (“0–2”, “4–6”, “8–13”, “15–20”, “25–32”, “38–43”, “48–53” and “60-”)
A caffe model has 2 associated files,
1 .prototxt — The definition of CNN goes in here. This file defines the layers in the neural network, each layer’s inputs, outputs and functionality.
2 .caffemodel — This contains the information of the trained neural network (trained model).
Steps:
Step 1: Import all the required libraries.
Step 2: Get the Youtube video URL and create an object ‘play’ which contains the best resolution of the video in webm/mp4 format.
Step 3: Often, we have to capture live stream with a camera. OpenCV provides a very simple interface to this. We can capture the video from the camera, convert it into grayscale video and display it. Just a simple task to get started.
Step 4: Using set() we’ll set the height and width of our video frame. cap.set(propId, value)
Step 5: Create 3 separate lists for storing Model_Mean_Values, Age, and Gender.
Step 6: We have defined a function to load caffemodel and prototxt of both age and gender detector. These are basically pre-trained CNN models which will do the detection.
Step 7: Now we will perform Face detection, Age detection, and Gender detection and for that create a function video_detector(age_net, gender_net) inside your main function and pass age_net and gender_net as its parameters.
Step 8: Read the cap object which is created from VideoCapture() in step 3.
Step 9: Convert the image to gray image as OpenCV face detector expects gray images.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Step 10: Load the pre-built model for facial detection.
face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_alt.xml’)
Step 11: Now, how do we detect a face from an image using the CascadeClassifier
Below are arguments which should pass to detectMultiScale().
This is a general function to detect objects, in this case, it’ll detect faces since we called in the face cascade. If it finds a face, it returns a list of positions of said face in the form “Rect(x,y,w,h).”, if not, then returns “None”.
Image: The first input is the grayscale image.
Step 12: Loop through the list of faces and draw rectangles on the human faces in the video. Here basically we’re finding faces, splitting the faces, their sizes, and drawing rectangles.
Step 13 : OpenCV provides a function to facilitate image preprocessing for deep learning classification: blobFromImage(). It performs :
Step 14: Predict the gender.
#Predict Gender
gender_net.setInput(blob)
gender_preds = gender_net.forward()
Step 15: Predict the Age.
#Predict Age
age_net.setInput(blob)
age_preds = age_net.forward()
Step 16: Now we have to put text on our output frame using putText() module of openCV.
Step 17: Finally print your final output.
Results of Dicky-Fuller test
Youtube video 1:
Video URL: https://www.youtube.com/watch?v=qLNhVC296YI
You can find the complete code here : https://github.com/dltk-ai/future-work/tree/master/Age-and-Gender-prediction-master
Prof. Sanjay Verma is area chair for aligning IT Business at IIM-A and has been mentoring fortunate few on developing great IT products for business.
Dr. Sanjay Verma holds his doctorate in space of Artificial Intelligence and is mentoring CIOs of variety of businesses. Government has appointed him as Independent director for one of India’s largest Public sector bank.
Mr. Sada Iyer played pivotal role in establishing HPE in India. He redefined Service Integration space in India. Sada is considered encyclopaedia of Banking across the globe and has lead globally BFSI division in world class firms like HPE and Oracle.
Sada has been sounding board to several banking and Insurance policy makers.
Experienced in Internal Audits, Risk Management , Corporate Governance and Business Advisory Services. He is a Certified Internal Auditor from The Institute of Internal Auditors, (USA), Certified Information Systems Auditor from ISACA (USA) , Certified Fraud Examiner from Association of Certified Fraud Examiners, (USA) & Specializes in Organizational Transformation, Risk Management and Corporate Governance.
Prof. K.C. John established Qualcomm in India. He is associated to World Economic Forum’s Sustainability Chapter. He has demonstrated a massive success in startup space by establishing successful firms back to back.
Currently he is on advisory board at Qubit AI and mentoring startups associated to Great Lakes Institute of Management and considered finest Professor to impart leadership lessons to Chief Executives.
Highly’ experienced in Research & Development, Strong knowledge Systems, product development, interpretation of National & International standards. Identifying product requirements / risks. Can solve any mechanical & electrical problems related to product development. Very Good at learning new things & implementing. Have 3 international patents.
Skilled in Product Management, AI/ML/DL,Domain expertise in various domains,Design and Lead AI COE, Skilled AI Trainer, Designing courses, Graduated Business Analytics professional from ISB.
Professional Chartered Accountant with experience in both Audit and Finance.