Amazing-Python-Scripts

Форк
0
188 строк · 5.4 Кб
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
# Read the data...
20
data = pd.read_csv(
21
    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',
40
             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'}
41

42

43
# Plotting the number of alphabets in the dataset...
44

45
train_yint = np.int0(y)
46
count = np.zeros(26, dtype='int')
47
for i in train_yint:
48
    count[i] += 1
49

50
alphabets = []
51
for i in word_dict.values():
52
    alphabets.append(i)
53

54
fig, ax = plt.subplots(1, 1, figsize=(10, 10))
55
ax.barh(alphabets, count)
56

57
plt.xlabel("Number of elements ")
58
plt.ylabel("Alphabets")
59
plt.grid()
60
plt.show()
61

62

63
# Shuffling the data ...
64
shuff = shuffle(train_x[:100])
65

66
fig, ax = plt.subplots(3, 3, figsize=(10, 10))
67
axes = ax.flatten()
68

69
for i in range(9):
70
    axes[i].imshow(np.reshape(shuff[i], (28, 28)), cmap="Greys")
71
plt.show()
72

73

74
# Reshaping the training & test dataset so that it can be put in the model...
75

76
train_X = train_x.reshape(
77
    train_x.shape[0], train_x.shape[1], train_x.shape[2], 1)
78
print("New shape of train data: ", train_X.shape)
79

80
test_X = test_x.reshape(test_x.shape[0], test_x.shape[1], test_x.shape[2], 1)
81
print("New shape of train data: ", test_X.shape)
82

83

84
# Converting the labels to categorical values...
85

86
train_yOHE = to_categorical(train_y, num_classes=26, dtype='int')
87
print("New shape of train labels: ", train_yOHE.shape)
88

89
test_yOHE = to_categorical(test_y, num_classes=26, dtype='int')
90
print("New shape of test labels: ", test_yOHE.shape)
91

92

93
# CNN model...
94

95
model = Sequential()
96

97
model.add(Conv2D(filters=32, kernel_size=(3, 3),
98
          activation='relu', input_shape=(28, 28, 1)))
99
model.add(MaxPool2D(pool_size=(2, 2), strides=2))
100

101
model.add(Conv2D(filters=64, kernel_size=(3, 3),
102
          activation='relu', padding='same'))
103
model.add(MaxPool2D(pool_size=(2, 2), strides=2))
104

105
model.add(Conv2D(filters=128, kernel_size=(3, 3),
106
          activation='relu', padding='valid'))
107
model.add(MaxPool2D(pool_size=(2, 2), strides=2))
108

109
model.add(Flatten())
110

111
model.add(Dense(64, activation="relu"))
112
model.add(Dense(128, activation="relu"))
113

114
model.add(Dense(26, activation="softmax"))
115

116

117
model.compile(optimizer=Adam(learning_rate=0.001),
118
              loss='categorical_crossentropy', metrics=['accuracy'])
119
reduce_lr = ReduceLROnPlateau(
120
    monitor='val_loss', factor=0.2, patience=1, min_lr=0.0001)
121
early_stop = EarlyStopping(
122
    monitor='val_loss', min_delta=0, patience=2, verbose=0, mode='auto')
123

124

125
history = model.fit(train_X, train_yOHE, epochs=1, callbacks=[
126
                    reduce_lr, early_stop],  validation_data=(test_X, test_yOHE))
127

128

129
model.summary()
130
model.save(r'model_hand.h5')
131

132

133
# Displaying the accuracies & losses for train & validation set...
134

135
print("The validation accuracy is :", history.history['val_accuracy'])
136
print("The training accuracy is :", history.history['accuracy'])
137
print("The validation loss is :", history.history['val_loss'])
138
print("The training loss is :", history.history['loss'])
139

140

141
# Making model predictions...
142

143
pred = model.predict(test_X[:9])
144
print(test_X.shape)
145

146

147
# Displaying some of the test images & their predicted labels...
148

149
fig, axes = plt.subplots(3, 3, figsize=(8, 9))
150
axes = axes.flatten()
151

152
for i, ax in enumerate(axes):
153
    img = np.reshape(test_X[i], (28, 28))
154
    ax.imshow(img, cmap="Greys")
155
    pred = word_dict[np.argmax(test_yOHE[i])]
156
    ax.set_title("Prediction: "+pred)
157
    ax.grid()
158

159

160
# Prediction on external image...
161

162
img = cv2.imread(
163
    r'E:\VSCODE\19115065\handwritten-character-recognition-code\code-files\image\img-m.jpg')
164
img_copy = img.copy()
165

166
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
167
img = cv2.resize(img, (400, 440))
168

169
img_copy = cv2.GaussianBlur(img_copy, (7, 7), 0)
170
img_gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
171
_, img_thresh = cv2.threshold(img_gray, 100, 255, cv2.THRESH_BINARY_INV)
172

173
img_final = cv2.resize(img_thresh, (28, 28))
174
img_final = np.reshape(img_final, (1, 28, 28, 1))
175

176

177
img_pred = word_dict[np.argmax(model.predict(img_final))]
178

179
cv2.putText(img, "Prediction: " + img_pred, (20, 410),
180
            cv2.FONT_HERSHEY_DUPLEX, 1.3, color=(255, 0, 30))
181
cv2.imshow('handwritten character recognition _ _ _ ', img)
182

183

184
while (1):
185
    k = cv2.waitKey(1) & 0xFF
186
    if k == 27:
187
        break
188
cv2.destroyAllWindows()
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.