|
| 1 | +from keras.datasets import mnist |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | +from keras.models import Sequential |
| 6 | +from keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout |
| 7 | +from keras.optimizers import SGD, Adam |
| 8 | +from keras.callbacks import ReduceLROnPlateau, EarlyStopping |
| 9 | +from keras.utils import to_categorical |
| 10 | +import pandas as pd |
| 11 | +import numpy as np |
| 12 | +from sklearn.model_selection import train_test_split |
| 13 | +from keras.utils import np_utils |
| 14 | +import matplotlib.pyplot as plt |
| 15 | +from tqdm import tqdm_notebook |
| 16 | +from sklearn.utils import shuffle |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +# Read the data... |
| 21 | +data = pd.read_csv(r"E:\VSCODE\19115065\handwritten-character-recognition-code\code-files\A_Z Handwritten Data.csv").astype('float32') |
| 22 | + |
| 23 | +# Split data the X - Our data , and y - the prdict label |
| 24 | +X = data.drop('0',axis = 1) |
| 25 | +y = data['0'] |
| 26 | + |
| 27 | + |
| 28 | +# Reshaping the data in csv file so that it can be displayed as an image... |
| 29 | + |
| 30 | +train_x, test_x, train_y, test_y = train_test_split(X, y, test_size = 0.2) |
| 31 | +train_x = np.reshape(train_x.values, (train_x.shape[0], 28,28)) |
| 32 | +test_x = np.reshape(test_x.values, (test_x.shape[0], 28,28)) |
| 33 | + |
| 34 | + |
| 35 | +print("Train data shape: ", train_x.shape) |
| 36 | +print("Test data shape: ", test_x.shape) |
| 37 | + |
| 38 | +# Dictionary for getting characters from index values... |
| 39 | +word_dict = {0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I',9:'J',10:'K',11:'L',12:'M',13:'N',14:'O',15:'P',16:'Q',17:'R',18:'S',19:'T',20:'U',21:'V',22:'W',23:'X', 24:'Y',25:'Z'} |
| 40 | + |
| 41 | + |
| 42 | +# Plotting the number of alphabets in the dataset... |
| 43 | + |
| 44 | +train_yint = np.int0(y) |
| 45 | +count = np.zeros(26, dtype='int') |
| 46 | +for i in train_yint: |
| 47 | + count[i] +=1 |
| 48 | + |
| 49 | +alphabets = [] |
| 50 | +for i in word_dict.values(): |
| 51 | + alphabets.append(i) |
| 52 | + |
| 53 | +fig, ax = plt.subplots(1,1, figsize=(10,10)) |
| 54 | +ax.barh(alphabets, count) |
| 55 | + |
| 56 | +plt.xlabel("Number of elements ") |
| 57 | +plt.ylabel("Alphabets") |
| 58 | +plt.grid() |
| 59 | +plt.show() |
| 60 | + |
| 61 | + |
| 62 | +#Shuffling the data ... |
| 63 | +shuff = shuffle(train_x[:100]) |
| 64 | + |
| 65 | +fig, ax = plt.subplots(3,3, figsize = (10,10)) |
| 66 | +axes = ax.flatten() |
| 67 | + |
| 68 | +for i in range(9): |
| 69 | + axes[i].imshow(np.reshape(shuff[i], (28,28)), cmap="Greys") |
| 70 | +plt.show() |
| 71 | + |
| 72 | + |
| 73 | +#Reshaping the training & test dataset so that it can be put in the model... |
| 74 | + |
| 75 | +train_X = train_x.reshape(train_x.shape[0],train_x.shape[1],train_x.shape[2],1) |
| 76 | +print("New shape of train data: ", train_X.shape) |
| 77 | + |
| 78 | +test_X = test_x.reshape(test_x.shape[0], test_x.shape[1], test_x.shape[2],1) |
| 79 | +print("New shape of train data: ", test_X.shape) |
| 80 | + |
| 81 | + |
| 82 | +# Converting the labels to categorical values... |
| 83 | + |
| 84 | +train_yOHE = to_categorical(train_y, num_classes = 26, dtype='int') |
| 85 | +print("New shape of train labels: ", train_yOHE.shape) |
| 86 | + |
| 87 | +test_yOHE = to_categorical(test_y, num_classes = 26, dtype='int') |
| 88 | +print("New shape of test labels: ", test_yOHE.shape) |
| 89 | + |
| 90 | + |
| 91 | +# CNN model... |
| 92 | + |
| 93 | +model = Sequential() |
| 94 | + |
| 95 | +model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1))) |
| 96 | +model.add(MaxPool2D(pool_size=(2, 2), strides=2)) |
| 97 | + |
| 98 | +model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same')) |
| 99 | +model.add(MaxPool2D(pool_size=(2, 2), strides=2)) |
| 100 | + |
| 101 | +model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding = 'valid')) |
| 102 | +model.add(MaxPool2D(pool_size=(2, 2), strides=2)) |
| 103 | + |
| 104 | +model.add(Flatten()) |
| 105 | + |
| 106 | +model.add(Dense(64,activation ="relu")) |
| 107 | +model.add(Dense(128,activation ="relu")) |
| 108 | + |
| 109 | +model.add(Dense(26,activation ="softmax")) |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +model.compile(optimizer = Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy']) |
| 114 | +reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=1, min_lr=0.0001) |
| 115 | +early_stop = EarlyStopping(monitor='val_loss', min_delta=0, patience=2, verbose=0, mode='auto') |
| 116 | + |
| 117 | + |
| 118 | +history = model.fit(train_X, train_yOHE, epochs=1, callbacks=[reduce_lr, early_stop], validation_data = (test_X,test_yOHE)) |
| 119 | + |
| 120 | + |
| 121 | +model.summary() |
| 122 | +model.save(r'model_hand.h5') |
| 123 | + |
| 124 | + |
| 125 | +# Displaying the accuracies & losses for train & validation set... |
| 126 | + |
| 127 | +print("The validation accuracy is :", history.history['val_accuracy']) |
| 128 | +print("The training accuracy is :", history.history['accuracy']) |
| 129 | +print("The validation loss is :", history.history['val_loss']) |
| 130 | +print("The training loss is :", history.history['loss']) |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +#Making model predictions... |
| 135 | + |
| 136 | +pred = model.predict(test_X[:9]) |
| 137 | +print(test_X.shape) |
| 138 | + |
| 139 | + |
| 140 | +# Displaying some of the test images & their predicted labels... |
| 141 | + |
| 142 | +fig, axes = plt.subplots(3,3, figsize=(8,9)) |
| 143 | +axes = axes.flatten() |
| 144 | + |
| 145 | +for i,ax in enumerate(axes): |
| 146 | + img = np.reshape(test_X[i], (28,28)) |
| 147 | + ax.imshow(img, cmap="Greys") |
| 148 | + pred = word_dict[np.argmax(test_yOHE[i])] |
| 149 | + ax.set_title("Prediction: "+pred) |
| 150 | + ax.grid() |
| 151 | + |
| 152 | + |
| 153 | +# Prediction on external image... |
| 154 | + |
| 155 | +img = cv2.imread(r'E:\VSCODE\19115065\handwritten-character-recognition-code\code-files\image\img-m.jpg') |
| 156 | +img_copy = img.copy() |
| 157 | + |
| 158 | +img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 159 | +img = cv2.resize(img, (400,440)) |
| 160 | + |
| 161 | +img_copy = cv2.GaussianBlur(img_copy, (7,7), 0) |
| 162 | +img_gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY) |
| 163 | +_, img_thresh = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV) |
| 164 | + |
| 165 | +img_final = cv2.resize(img_thresh, (28,28)) |
| 166 | +img_final =np.reshape(img_final, (1,28,28,1)) |
| 167 | + |
| 168 | + |
| 169 | +img_pred = word_dict[np.argmax(model.predict(img_final))] |
| 170 | + |
| 171 | +cv2.putText(img, "Prediction: " + img_pred, (20,410), cv2.FONT_HERSHEY_DUPLEX, 1.3, color = (255,0,30)) |
| 172 | +cv2.imshow('handwritten character recognition _ _ _ ', img) |
| 173 | + |
| 174 | + |
| 175 | +while (1): |
| 176 | + k = cv2.waitKey(1) & 0xFF |
| 177 | + if k == 27: |
| 178 | + break |
| 179 | +cv2.destroyAllWindows() |
| 180 | + |
0 commit comments