Skip to content

Instantly share code, notes, and snippets.

@KevinPatel04
Last active April 26, 2020 09:40
Show Gist options
  • Save KevinPatel04/4cbd9208a7ba24b000ab506af1a81cef to your computer and use it in GitHub Desktop.
Save KevinPatel04/4cbd9208a7ba24b000ab506af1a81cef to your computer and use it in GitHub Desktop.
Age Prediction Blog
# importing required libraries
import cv2
import numpy as np
from keras.preprocessing import image
from keras.models import model_from_json
import face_recognition
# only for google colab
# from google.colab.patches import cv2_imshow
# declare age range list
age_label_list = ['0-2','4-6','8-12','15-20','25-32','38-43','48-53','60-100']
# initialize the model
age_prototxt = "models/age_deploy.prototxt"
age_caffemodel = "models/age_net.caffemodel"
age_cov_net = cv2.dnn.readNet(age_caffemodel, age_prototxt)
# importing required libraries
import cv2
import numpy as np
from keras.preprocessing import image
from keras.models import model_from_json
import face_recognition
# only for google colab
# from google.colab.patches import cv2_imshow
# declare age range list
age_label_list = ['0-2','4-6','8-12','15-20','25-32','38-43','48-53','60-100']
# initialize the model
age_prototxt = "models/age_deploy.prototxt"
age_caffemodel = "models/age_net.caffemodel"
age_cov_net = cv2.dnn.readNet(age_caffemodel, age_prototxt)
# load the image to be detected
img = cv2.imread('test/modi-obama-1.jpg');
# find all face locations using face_locations() function
# model can be "cnn" or "hog"
# number_of_times_to_upsample = 1 higher and detect more faces
all_face_locations = face_recognition.face_locations(img, model="hog")
# loop over each face detected in the image
for index, face_location in enumerate(all_face_locations):
top_pos, right_pos, bottom_pos, left_pos = face_location
#-----------------------------------------------------------------------------
# Age Detection
#-----------------------------------------------------------------------------
# Slice frame image array by positions face_locations
face_image = img[top_pos:bottom_pos, left_pos:right_pos]
# The 'AGE_GENDER_MODEL_MEAN_VALUES' calculated by using the numpy.mean()
AGE_GENDER_MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
# create blob of sliced face image
face_image_blob = cv2.dnn.blobFromImage(face_image, 1, (227, 227), AGE_GENDER_MODEL_MEAN_VALUES, swapRB=False)
# give input to age detection model
age_cov_net.setInput(face_image_blob)
# get the predicted label from the model
age_prediction = age_cov_net.forward()
# find the max value of predicted index
# pass the index to age_label_list to get the associated label text
age_label = age_label_list[age_prediction[0].argmax()]
# draw rectangle around each face
cv2.rectangle(img, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2)
# disply the name of age as text along the face
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(img, (left_pos-1, bottom_pos + 18), (right_pos+1, bottom_pos), (0, 0, 255), -1)
cv2.putText(img, age_label, (left_pos+2,bottom_pos+11), font, 0.5, (255,255,255), 1)
#-----------------------------------------------------------------------------
# show image
cv2.imshow("Image Age Prediction",image)
# only for google colab
# cv2_imshow(img)
# load the image to be detected
img = cv2.imread('test/modi-obama-1.jpg');
# find all face locations using face_locations() function
# model can be "cnn" or "hog"
# number_of_times_to_upsample = 1 higher and detect more faces
all_face_locations = face_recognition.face_locations(img, model="hog")
# loop over each face detected in the image
for index, face_location in enumerate(all_face_locations):
top_pos, right_pos, bottom_pos, left_pos = face_location
#-----------------------------------------------------------------------------
# Age Detection
#-----------------------------------------------------------------------------
# Slice frame image array by positions face_locations
face_image = img[top_pos:bottom_pos, left_pos:right_pos]
# The 'AGE_GENDER_MODEL_MEAN_VALUES' calculated by using the numpy.mean()
AGE_GENDER_MODEL_MEAN_VALUES = (78.4263377603, 87.7689143744, 114.895847746)
# create blob of sliced face image
face_image_blob = cv2.dnn.blobFromImage(face_image, 1, (227, 227), AGE_GENDER_MODEL_MEAN_VALUES, swapRB=False)
# give input to age detection model
age_cov_net.setInput(face_image_blob)
# get the predicted label from the model
age_prediction = age_cov_net.forward()
# find the max value of predicted index
# pass the index to age_label_list to get the associated label text
age_label = age_label_list[age_prediction[0].argmax()]
# draw rectangle around each face
cv2.rectangle(img, (left_pos,top_pos), (right_pos,bottom_pos), (0,0,255), 2)
# disply the name of age as text along the face
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(img, (left_pos-1, bottom_pos + 18), (right_pos+1, bottom_pos), (0, 0, 255), -1)
cv2.putText(img, age_label, (left_pos+2,bottom_pos+11), font, 0.5, (255,255,255), 1)
#-----------------------------------------------------------------------------
# show image
cv2.imshow("Image Age Prediction",image)
# only for google colab
# cv2_imshow(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment