top of page

Machine Learning Age Detection Using Images | Machine Learning Assignment Help | Realcode4you

In this blog we will learn how to detect age using the given images, we are providing machine learning assignment help, and other related areas help of machine learning:

Reference link:

Step 1 : Import all related libraries

import os

import random

from import loadmat

import pandas as pd

from scipy.misc import imread

from sklearn.model_selection import train_test_split

Step 2:

root_dir = os.path.abspath('.')

data_dir = '/mnt/hdd/datasets/misc'

train = pd.read_csv(os.path.join(data_dir, 'train.csv'))

test = pd.read_csv(os.path.join(data_dir, 'test.csv'))

fileData = loadmat('wiki_labeled.mat')

Show file data:


## meta information of each of the 60327 images

ID: identification number of the subject (starting from 2002)

• dob: the date of birth of the subject. (It is Matlab’s datenum value calculated based on total number of days since January 0, 0000.)

• dob_str: the DD-MMM-YYYY format dob value.

• photo_taken: when the photo was taken (only the year value)

• full_path: directory path, including filename of the image

• gender: Gender of the subject (0: female, 1: male, NaN if unknown)

• name: name of the subject

• face_location: location of the face.

• face_score: detector score (the higher the better). Inf implies that no face was found in the image, and the face_location then just returns the entire image.

• second_face_score: detector score of the face with the second highest score. This is useful to ignore images with more than one face. second_face_score is NaN (not a number) if no second face was detected.

• age: age of the person (in years), and was calculated based on the “dob” value and the “photo_taken” values.


dob = fileData['wiki_labeled'][0][0][0]

photo_taken = fileData['wiki_labeled'][0][0][1]

full_path = fileData['wiki_labeled'][0][0][2]

gender = fileData['wiki_labeled'][0][0][3]

name = fileData['wiki_labeled'][0][0][4]

face_location = fileData['wiki_labeled'][0][0][5]

face_score = fileData['wiki_labeled'][0][0][6]

second_face_score = fileData['wiki_labeled'][0][0][7]

dob_str = fileData['wiki_labeled'][0][0][8]

age = fileData['wiki_labeled'][0][0][9]

IDs = fileData['wiki_labeled'][0][0][10]

Show Data:

print("IDs: ",IDs)











Choose Selected Column:

columns = ["id","dob","photo_taken","dob_str","full_path","gender","name","face_location","face_score","second_face_score","age"]

Show Using Dataframe:

df = pd.DataFrame({'id': IDs[0], 'dob': dob[0],"photo_taken":photo_taken[0],'dob_str':dob_str[0],"full_path":full_path[0],"gender":gender[0],"name":name[0],


"second_face_score":second_face_score[0],'age':age[0]}, columns=columns)

Print Selected data


## Displaying some images

import imageio

images= df['full_path'].values


data_dir = 'wiki_labeled/'

#displaying 10 pictures

for i in range(len(images[:10])):

img_name = images[i][0]

img = imageio.imread(os.path.join(data_dir, img_name))


#print(‘Age: ‘, train.Class[i])

Split Data

x = df.iloc[:,:-1]

y = df['age']

xTrain, xTest, yTrain, yTest = train_test_split(x, y, test_size = 0.2, random_state = 0)

Training first 200 images to reduce execution time you can set your own desired size

Backgrounds on PCA

Step 1: Obtain the 2D face images, I1,I2,...,Im (the training faces). All faces must be of the same resolution.

#from scipy.misc import imresize

from skimage.transform import resize

images= xTrain['full_path'].values

data_dir = 'wiki_labeled/'

temp = []

# training 200 images

m = 200

for i in range(m):

img_name = images[i][0]

img = imageio.imread(os.path.join(data_dir, img_name))

#img = resize(img, (32, 32))

img = img.astype('float32') # this will help us in later stage


#train_x = np.stack(temp)

def plot_gallery(images, h, w, rows=10, cols=10):


for i in range(rows * cols):

plt.subplot(rows, cols, i + 1)

plt.imshow(images[i].reshape((h, w)),




Other Realcode4you Services

<Realcode4you> Assignment Help

<Realcode4you> Web Assignment Help

Are you looking machine learning help related to this, please contact us.

16 views0 comments


bottom of page