Cainvas

Indian Currency Notes Classifier

Credit: AITS Cainvas Community

Photo by Alexander Barton for NJI Media on Dribbble

Using the images of the currency notes in circulation to classify them. This application can be of use to the visually impaired in their everyday lives.

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from keras import layers, optimizers, models, preprocessing, losses, callbacks
import os
import random
from PIL import Image
import tensorflow as tf
import keras

The dataset

On Kaggle by Gaurav Rajesh Sahani

The dataset contains 195 images of 7 categories of Indian Currency Notes - Tennote, Fiftynote, Twentynote, 2Thousandnote, 2Hundrednote, Hundrednote, 1Hundrednote.

There are 2 folders in the dataset - train and test, each with 7 sub-folders corresponding to the currency categories.

In [2]:
!wget -N https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/indian-currency-notes-classifier.zip

!unzip -qo indian-currency-notes-classifier.zip -d currency
--2021-03-06 08:08:16--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/indian-currency-notes-classifier.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.64.100
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.64.100|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 21008172 (20M) [application/zip]
Saving to: ‘indian-currency-notes-classifier.zip.1’

indian-currency-not 100%[===================>]  20.03M  58.7MB/s    in 0.3s    

2021-03-06 08:08:16 (58.7 MB/s) - ‘indian-currency-notes-classifier.zip.1’ saved [21008172/21008172]

A peek into the number of images in the folders -

In [3]:
data_dir = 'currency'

print("Number of samples")
for f in os.listdir(data_dir + '/'):
    print()
    if os.path.isdir(data_dir + '/' + f):
        print(f.upper())
        for fx in os.listdir(data_dir + '/' + f):
            if os.path.isdir(data_dir + '/' + f + '/' + fx):
                print(fx, " : ", len(os.listdir(data_dir + '/' + f +'/' + fx)))
Number of samples

TEST
Tennote  :  6
Fiftynote  :  6
Twentynote  :  6
2Thousandnote  :  6
2Hundrednote  :  6
5Hundrednote  :  6
1Hundrednote  :  6

TRAIN
Tennote  :  22
Fiftynote  :  22
Twentynote  :  22
2Thousandnote  :  21
2Hundrednote  :  22
5Hundrednote  :  22
1Hundrednote  :  22

This is a balanced dataset.

In [4]:
# Loading the dataset

path = 'currency/'
input_shape = (256, 256, 3)    # default input shape while loading the images

batch = 64

# The train and test datasets
print("Train dataset")
train_ds = preprocessing.image_dataset_from_directory(path+'Train', batch_size=batch, label_mode='categorical')#, color_mode='grayscale')

print("Test dataset")
test_ds = preprocessing.image_dataset_from_directory(path+'Test', batch_size=batch, label_mode='categorical')#, color_mode='grayscale')
Train dataset
Found 153 files belonging to 7 classes.
Test dataset
Found 42 files belonging to 7 classes.
In [5]:
# Looking into the class labels

class_names = train_ds.class_names

print("Train class names: ", train_ds.class_names)
print("Test class names: ", test_ds.class_names)
Train class names:  ['1Hundrednote', '2Hundrednote', '2Thousandnote', '5Hundrednote', 'Fiftynote', 'Tennote', 'Twentynote']
Test class names:  ['1Hundrednote', '2Hundrednote', '2Thousandnote', '5Hundrednote', 'Fiftynote', 'Tennote', 'Twentynote']

Visualization

In [6]:
num_samples = 4    # the number of samples to be displayed in each class

for x in class_names:
    plt.figure(figsize=(20, 20))

    filenames = os.listdir(path + 'Train/' + x)

    for i in range(num_samples):
        ax = plt.subplot(1, num_samples, i + 1)
        img = Image.open(path +'Train/' + x + '/' + filenames[i])
        plt.imshow(img)
        plt.title(x)
        plt.axis("off")