Amazing-Python-Scripts
188 строк · 5.4 Кб
1from keras.datasets import mnist2import matplotlib.pyplot as plt3import cv24import numpy as np5from keras.models import Sequential6from keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout7from keras.optimizers import SGD, Adam8from keras.callbacks import ReduceLROnPlateau, EarlyStopping9from keras.utils import to_categorical10import pandas as pd11import numpy as np12from sklearn.model_selection import train_test_split13from keras.utils import np_utils14import matplotlib.pyplot as plt15from tqdm import tqdm_notebook16from sklearn.utils import shuffle17
18
19# Read the data...
20data = pd.read_csv(21r"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
24X = data.drop('0', axis=1)25y = data['0']26
27
28# Reshaping the data in csv file so that it can be displayed as an image...
29
30train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.2)31train_x = np.reshape(train_x.values, (train_x.shape[0], 28, 28))32test_x = np.reshape(test_x.values, (test_x.shape[0], 28, 28))33
34
35print("Train data shape: ", train_x.shape)36print("Test data shape: ", test_x.shape)37
38# Dictionary for getting characters from index values...
39word_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',4013: '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'}41
42
43# Plotting the number of alphabets in the dataset...
44
45train_yint = np.int0(y)46count = np.zeros(26, dtype='int')47for i in train_yint:48count[i] += 149
50alphabets = []51for i in word_dict.values():52alphabets.append(i)53
54fig, ax = plt.subplots(1, 1, figsize=(10, 10))55ax.barh(alphabets, count)56
57plt.xlabel("Number of elements ")58plt.ylabel("Alphabets")59plt.grid()60plt.show()61
62
63# Shuffling the data ...
64shuff = shuffle(train_x[:100])65
66fig, ax = plt.subplots(3, 3, figsize=(10, 10))67axes = ax.flatten()68
69for i in range(9):70axes[i].imshow(np.reshape(shuff[i], (28, 28)), cmap="Greys")71plt.show()72
73
74# Reshaping the training & test dataset so that it can be put in the model...
75
76train_X = train_x.reshape(77train_x.shape[0], train_x.shape[1], train_x.shape[2], 1)78print("New shape of train data: ", train_X.shape)79
80test_X = test_x.reshape(test_x.shape[0], test_x.shape[1], test_x.shape[2], 1)81print("New shape of train data: ", test_X.shape)82
83
84# Converting the labels to categorical values...
85
86train_yOHE = to_categorical(train_y, num_classes=26, dtype='int')87print("New shape of train labels: ", train_yOHE.shape)88
89test_yOHE = to_categorical(test_y, num_classes=26, dtype='int')90print("New shape of test labels: ", test_yOHE.shape)91
92
93# CNN model...
94
95model = Sequential()96
97model.add(Conv2D(filters=32, kernel_size=(3, 3),98activation='relu', input_shape=(28, 28, 1)))99model.add(MaxPool2D(pool_size=(2, 2), strides=2))100
101model.add(Conv2D(filters=64, kernel_size=(3, 3),102activation='relu', padding='same'))103model.add(MaxPool2D(pool_size=(2, 2), strides=2))104
105model.add(Conv2D(filters=128, kernel_size=(3, 3),106activation='relu', padding='valid'))107model.add(MaxPool2D(pool_size=(2, 2), strides=2))108
109model.add(Flatten())110
111model.add(Dense(64, activation="relu"))112model.add(Dense(128, activation="relu"))113
114model.add(Dense(26, activation="softmax"))115
116
117model.compile(optimizer=Adam(learning_rate=0.001),118loss='categorical_crossentropy', metrics=['accuracy'])119reduce_lr = ReduceLROnPlateau(120monitor='val_loss', factor=0.2, patience=1, min_lr=0.0001)121early_stop = EarlyStopping(122monitor='val_loss', min_delta=0, patience=2, verbose=0, mode='auto')123
124
125history = model.fit(train_X, train_yOHE, epochs=1, callbacks=[126reduce_lr, early_stop], validation_data=(test_X, test_yOHE))127
128
129model.summary()130model.save(r'model_hand.h5')131
132
133# Displaying the accuracies & losses for train & validation set...
134
135print("The validation accuracy is :", history.history['val_accuracy'])136print("The training accuracy is :", history.history['accuracy'])137print("The validation loss is :", history.history['val_loss'])138print("The training loss is :", history.history['loss'])139
140
141# Making model predictions...
142
143pred = model.predict(test_X[:9])144print(test_X.shape)145
146
147# Displaying some of the test images & their predicted labels...
148
149fig, axes = plt.subplots(3, 3, figsize=(8, 9))150axes = axes.flatten()151
152for i, ax in enumerate(axes):153img = np.reshape(test_X[i], (28, 28))154ax.imshow(img, cmap="Greys")155pred = word_dict[np.argmax(test_yOHE[i])]156ax.set_title("Prediction: "+pred)157ax.grid()158
159
160# Prediction on external image...
161
162img = cv2.imread(163r'E:\VSCODE\19115065\handwritten-character-recognition-code\code-files\image\img-m.jpg')164img_copy = img.copy()165
166img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)167img = cv2.resize(img, (400, 440))168
169img_copy = cv2.GaussianBlur(img_copy, (7, 7), 0)170img_gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)171_, img_thresh = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV)172
173img_final = cv2.resize(img_thresh, (28, 28))174img_final = np.reshape(img_final, (1, 28, 28, 1))175
176
177img_pred = word_dict[np.argmax(model.predict(img_final))]178
179cv2.putText(img, "Prediction: " + img_pred, (20, 410),180cv2.FONT_HERSHEY_DUPLEX, 1.3, color=(255, 0, 30))181cv2.imshow('handwritten character recognition _ _ _ ', img)182
183
184while (1):185k = cv2.waitKey(1) & 0xFF186if k == 27:187break188cv2.destroyAllWindows()189