-
Notifications
You must be signed in to change notification settings - Fork 0
/
font_recognition.py
338 lines (251 loc) · 9.28 KB
/
font_recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# -*- coding: utf-8 -*-
"""Font Recognition.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1huaDe0tWBUEm4biimzJDqoadHdsL2Lti
"""
import tensorflow as tf
# Commented out IPython magic to ensure Python compatibility.
from matplotlib.pyplot import imshow
import matplotlib.cm as cm
import matplotlib.pylab as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import PIL
from PIL import Image
from PIL import ImageFilter
import cv2
import itertools
import random
import keras
import imutils
from imutils import paths
import os
from tensorflow.keras import optimizers
from tensorflow.keras.preprocessing.image import img_to_array
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import callbacks
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D , UpSampling2D ,Conv2DTranspose
from tensorflow.keras import backend as K
# %matplotlib inline
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
if 'COLAB_TPU_ADDR' in os.environ:
TF_MASTER = 'grpc://{}'.format(os.environ['COLAB_TPU_ADDR'])
else:
TF_MASTER=''
tpu_address = TF_MASTER
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu_address)
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
print("Number of devices: ", len(tf.config.list_logical_devices('TPU')))
strategy = tf.distribute.TPUStrategy(resolver)
from google.colab import drive
drive.mount('/content/drive')
def pil_image(img_path):
pil_im =PIL.Image.open(img_path).convert('L')
pil_im=pil_im.resize((105,105))
#imshow(np.asarray(pil_im))
return pil_im
"""# Augumentation Steps
* Noise
* Blur
* Perpective Rotation
* Shading
* Variable Character Spacing
* Variable Aspect Ratio
## Noise
"""
def noise_image(pil_im):
# Adding Noise to image
img_array = np.asarray(pil_im)
mean = 0.0 # some constant
std = 5 # some constant (standard deviation)
noisy_img = img_array + np.random.normal(mean, std, img_array.shape)
noisy_img_clipped = np.clip(noisy_img, 0, 255)
noise_img = PIL.Image.fromarray(np.uint8(noisy_img_clipped)) # output
#imshow((noisy_img_clipped ).astype(np.uint8))
noise_img=noise_img.resize((105,105))
return noise_img
def blur_image(pil_im):
#Adding Blur to image
blur_img = pil_im.filter(ImageFilter.GaussianBlur(radius=3)) # ouput
#imshow(blur_img)
blur_img=blur_img.resize((105,105))
return blur_img
def affine_rotation(img):
#img=cv2.imread(img_path,0)
rows, columns = img.shape
point1 = np.float32([[10, 10], [30, 10], [10, 30]])
point2 = np.float32([[20, 15], [40, 10], [20, 40]])
A = cv2.getAffineTransform(point1, point2)
output = cv2.warpAffine(img, A, (columns, rows))
affine_img = PIL.Image.fromarray(np.uint8(output)) # affine rotated output
#imshow(output)
affine_img=affine_img.resize((105,105))
return affine_img
def gradient_fill(image):
#image=cv2.imread(img_path,0)
laplacian = cv2.Laplacian(image,cv2.CV_64F)
laplacian = cv2.resize(laplacian, (105, 105))
return laplacian
"""## Preparing Dataset"""
data_path = "drive/MyDrive/font_patch/"
data=[]
labels=[]
imagePaths = sorted(list(paths.list_images(data_path)))
random.seed(42)
random.shuffle(imagePaths)
def conv_label(label):
if label == 'Lato':
return 0
elif label == 'Raleway':
return 1
elif label == 'Roboto':
return 2
elif label == 'Sansation':
return 3
elif label == 'Walkway':
return 4
augument=["blur","noise","affine","gradient"]
a=itertools.combinations(augument, 4)
for i in list(a):
print(list(i))
counter=0
for imagePath in imagePaths:
label = imagePath.split(os.path.sep)[-2]
label = conv_label(label)
pil_img = pil_image(imagePath)
#imshow(pil_img)
# Adding original image
org_img = img_to_array(pil_img)
#print(org_img.shape)
data.append(org_img)
labels.append(label)
augument=["noise","blur","affine","gradient"]
for l in range(0,len(augument)):
a=itertools.combinations(augument, l+1)
for i in list(a):
combinations=list(i)
print(len(combinations))
temp_img = pil_img
for j in combinations:
if j == 'noise':
# Adding Noise image
temp_img = noise_image(temp_img)
elif j == 'blur':
# Adding Blur image
temp_img = blur_image(temp_img)
#imshow(blur_img)
elif j == 'affine':
open_cv_affine = np.array(pil_img)
# Adding affine rotation image
temp_img = affine_rotation(open_cv_affine)
elif j == 'gradient':
open_cv_gradient = np.array(pil_img)
# Adding gradient image
temp_img = gradient_fill(open_cv_gradient)
temp_img = img_to_array(temp_img)
data.append(temp_img)
labels.append(label)
data = np.asarray(data, dtype="float") / 255.0
labels = np.array(labels)
print("Success")
# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data,labels, test_size=0.25, random_state=42)
trainX.shape
trainY.shape
testX.shape
testY.shape
# convert the labels from integers to vectors
trainY = to_categorical(trainY, num_classes=5)
testY = to_categorical(testY, num_classes=5)
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,horizontal_flip=True)
K.image_data_format()
def create_model():
model=Sequential()
# Cu Layers
model.add(Conv2D(64, kernel_size=(48, 48), activation='relu', input_shape=(105,105,1)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, kernel_size=(24, 24), activation='relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2DTranspose(128, (24,24), strides = (2,2), activation = 'relu', padding='same', kernel_initializer='uniform'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Conv2DTranspose(64, (12,12), strides = (2,2), activation = 'relu', padding='same', kernel_initializer='uniform'))
model.add(UpSampling2D(size=(2, 2)))
#Cs Layers
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Conv2D(256, kernel_size=(12, 12), activation='relu'))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2383,activation='relu'))
model.add(Dense(5, activation='softmax'))
return model
#with strategy.scope():
# batch_size = 128
# epochs = 50
# model= create_model()
# sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
# model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])
#model.summary()
early_stopping=callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=0, mode='min')
filepath="top_model.h5"
checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [early_stopping,checkpoint]
with strategy.scope():
batch_size = 128
epochs = 50
model= create_model()
sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mean_squared_error', steps_per_execution = 50, optimizer=sgd, metrics=['accuracy'])
epochs = 50
model.fit(trainX, trainY,shuffle=True,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(testX, testY),callbacks=callbacks_list)
score = model.evaluate(testX, testY, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
from keras.models import load_model
model = load_model('top_model.h5')
score = model.evaluate(testX, testY, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
img_path="drive/MyDrive/sample/sample.jpg"
pil_im =PIL.Image.open(img_path).convert('L')
pil_im=pil_image(img_path)
org_img = img_to_array(pil_im)
print(org_img.shape)
def rev_conv_label(label):
if label == 0 :
return 'Lato'
elif label == 1:
return 'Raleway'
elif label == 2 :
return 'Roboto'
elif label == 3 :
return 'Sansation'
elif label == 4:
return 'Walkway'
data=[]
data.append(org_img)
data = np.asarray(data, dtype="float") / 255.0
y = model.predict(data)
y = np.round(y).astype(int)
label = rev_conv_label(y[0,0])
fig, ax = plt.subplots(1)
ax.imshow(pil_im, interpolation='nearest', cmap=cm.gray)
ax.text(5, 5, label , bbox={'facecolor': 'white', 'pad': 10})
plt.show()