-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptcha.py
382 lines (311 loc) · 12.5 KB
/
captcha.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
import cv2
import numpy as np
import pandas as pd
from pathlib import Path
from collections import Counter
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
def generate_arrays(df, resize=True, img_height=50, img_width=200):
"""Generates image array and labels array from a dataframe.
Args:
df: dataframe from which we want to read the data
resize (bool) : whether to resize images or not
img_weidth (int): width of the resized images
img_height (int): height of the resized images
Returns:
images (ndarray): grayscale images
labels (ndarray): corresponding encoded labels
"""
num_items = len(df)
images = np.zeros((num_items, img_height, img_width), dtype=np.float32)
labels = [0] * num_items
for i in range(num_items):
img = cv2.imread(df["img_path"][i])
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if resize:
img = cv2.resize(img, (img_width, img_height))
img = (img / 255.).astype(np.float32)
label = df["label"][i]
# Add only if it is a valid captcha
#if is_valid_captcha(label):
images[i, :, :] = img
labels[i] = label
return images, np.array(labels)
class DataGenerator(keras.utils.Sequence):
"""Generates batches from a given dataset.
Args:
data: training or validation data
labels: corresponding labels
char_map: dictionary mapping char to labels
batch_size: size of a single batch
img_width: width of the resized
img_height: height of the resized
downsample_factor: by what factor did the CNN downsample the images
max_length: maximum length of any captcha
shuffle: whether to shuffle data or not after each epoch
Returns:
batch_inputs: a dictionary containing batch inputs
batch_labels: a batch of corresponding labels
"""
def __init__(self,
data,
labels,
char_map,
batch_size=16,
img_width=200,
img_height=50,
downsample_factor=4,
max_length=5, # 글자 개수에 맞춰서 4개 or 5개
shuffle=True
):
self.data = data
self.labels = labels
self.char_map = char_map
self.batch_size = batch_size
self.img_width = img_width
self.img_height = img_height
self.downsample_factor = downsample_factor
self.max_length = max_length
self.shuffle = shuffle
self.indices = np.arange(len(data))
self.on_epoch_end()
def __len__(self):
return int(np.ceil(len(self.data) / self.batch_size))
def __getitem__(self, idx):
# 1. Get the next batch indices
curr_batch_idx = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size]
# 2. This isn't necessary but it can help us save some memory
# as not all batches the last batch may not have elements
# equal to the batch_size
batch_len = len(curr_batch_idx)
# 3. Instantiate batch arrays
batch_images = np.ones((batch_len, self.img_width, self.img_height, 1),
dtype=np.float32)
batch_labels = np.ones((batch_len, self.max_length), dtype=np.float32)
input_length = np.ones((batch_len, 1), dtype=np.int64) * \
(self.img_width // self.downsample_factor - 2)
label_length = np.zeros((batch_len, 1), dtype=np.int64)
for j, idx in enumerate(curr_batch_idx):
# 1. Get the image and transpose it
img = self.data[idx].T
# 2. Add extra dimenison
img = np.expand_dims(img, axis=-1)
# 3. Get the correpsonding label
text = self.labels[idx]
# 4. Include the pair only if the captcha is valid
#if is_valid_captcha(text):
label = [self.char_map[ch] for ch in text]
batch_images[j] = img
batch_labels[j] = label
label_length[j] = len(text)
batch_inputs = {
'input_data': batch_images,
'input_label': batch_labels,
'input_length': input_length,
'label_length': label_length,
}
return batch_inputs, np.zeros(batch_len).astype(np.float32)
def on_epoch_end(self):
if self.shuffle:
np.random.shuffle(self.indices)
class CTCLayer(layers.Layer):
def __init__(self, name=None):
super().__init__(name=name)
self.loss_fn = keras.backend.ctc_batch_cost
def call(self, y_true, y_pred, input_length, label_length):
# Compute the training-time loss value and add it
# to the layer using `self.add_loss()`.
loss = self.loss_fn(y_true, y_pred, input_length, label_length)
self.add_loss(loss)
# On test time, just return the computed loss
return loss
'''
def build_model(opt, lr=0.001):
# Inputs to the model
input_img = layers.Input(shape=(img_width, img_height, 1),
name='input_data',
dtype='float32')
labels = layers.Input(name='input_label', shape=[max_length], dtype='float32')
input_length = layers.Input(name='input_length', shape=[1], dtype='int64')
label_length = layers.Input(name='label_length', shape=[1], dtype='int64')
# First conv block
x = layers.Conv2D(32,
(3,3),
activation='relu',
kernel_initializer='he_normal',
padding='same',
name='Conv1')(input_img)
x = layers.MaxPooling2D((2,2), name='pool1')(x)
# Second conv block
x = layers.Conv2D(64,
(3,3),
activation='relu',
kernel_initializer='he_normal',
padding='same',
name='Conv2')(x)
x = layers.MaxPooling2D((2,2), name='pool2')(x)
# We have used two max pool with pool size and strides of 2.
# Hence, downsampled feature maps are 4x smaller. The number of
# filters in the last layer is 64. Reshape accordingly before
# passing it to RNNs
new_shape = ((img_width // 4), (img_height // 4)*64)
x = layers.Reshape(target_shape=new_shape, name='reshape')(x)
x = layers.Dense(64, activation='relu', name='dense1')(x)
x = layers.Dropout(0.2)(x)
# RNNs
x = layers.Bidirectional(layers.LSTM(128,
return_sequences=True,
dropout=0.2))(x)
x = layers.Bidirectional(layers.LSTM(64,
return_sequences=True,
dropout=0.25))(x)
# Predictions
x = layers.Dense(len(characters)+1,
activation='softmax',
name='dense2',
kernel_initializer='he_normal')(x)
# Calculate CTC
output = CTCLayer(name='ctc_loss')(labels, x, input_length, label_length)
# Define the model
model = keras.models.Model(inputs=[input_img,
labels,
input_length,
label_length],
outputs=output,
name='ocr_model_v1')
# Optimizer
sgd = keras.optimizers.legacy.SGD(learning_rate=lr,
decay=1e-6,
momentum=0.9,
nesterov=True,
clipnorm=5)
Adam = tf.keras.optimizers.Adam(
learning_rate=lr,
beta_1=0.9,
beta_2=0.999,
epsilon=1e-07,
amsgrad=False,
weight_decay=None,
clipnorm=None,
clipvalue=None,
global_clipnorm=None,
use_ema=False,
ema_momentum=0.99,
ema_overwrite_frequency=None,
jit_compile=True)
if opt=='SGD':
opt=sgd
if opt=='Adam':
opt=Adam
# Compile the model and return
model.compile(optimizer=opt, metrics=['accuracy'])
return model
'''
# test 이미지가 저장된 디렉토리 주소 입력
test_data_dir = Path("C:\\Users\\choo0\\Downloads\\")
images = list(test_data_dir.glob("*.png"))
images += list(test_data_dir.glob("*.jpg"))
print("Number of images found: ", len(images))
# Store all the characters in a set
characters = set()
# A list to store the length of each captcha
captcha_length = []
# Store image-label info
dataset = []
# Iterate over the dataset and store the
# information needed
for img_path in images:
# 1. Get the label associated with each image
if str(img_path)[-3:] == 'png':
label = img_path.name.split(".png")[0]
else:
label = img_path.name.split(".jpg")[0]
# 2. Store the length of this cpatcha
captcha_length.append(len(label))
# 3. Store the image-label pair info
dataset.append((str(img_path), label))
# 4. Store the characters present
for ch in label:
characters.add(ch)
# Sort the characters
characters = sorted(characters)
# Convert the dataset info into a dataframe
dataset = pd.DataFrame(dataset, columns=["img_path", "label"], index=None)
# Shuffle the dataset
dataset = dataset.sample(frac=1.).reset_index(drop=True)
# Build test data
test_data, test_labels = generate_arrays(df=dataset)
print("Number of test images: ", test_data.shape)
print("Number of test labels: ", test_labels.shape)
# Map text to numeric labels
char_to_labels = {char:idx for idx, char in enumerate(characters)}
# Map numeric labels to text
labels_to_char = {val:key for key, val in char_to_labels.items()}
# Batch size for training and validation
batch_size = 32
# Desired image dimensions
img_width=200
img_height=50
# Factor by which the image is going to be downsampled
# by the convolutional blocks
downsample_factor=4
# Maximum length of any captcha in the data
# 글자 개수에 맞춰서 4개 or 5개
max_length=5
# Get a generator object for the validation data
test_data_generator = DataGenerator(data=test_data,
labels=test_labels,
char_map=char_to_labels,
batch_size=batch_size,
img_width=img_width,
img_height=img_height,
downsample_factor=downsample_factor,
max_length=max_length,
shuffle=False
)
# 모델 로드
model_addr = '"C:\\Users\\choo0\\Documents\\Repository\\Captcha-Recognition-Chrome-Extension\\Chrome-Extension\\captcha_recognition_32000_model.h5"' # 모델 주소 입력
test_model = tf.keras.models.load_model(model_addr, custom_objects={"CTCLayer": CTCLayer})
prediction_model = keras.models.Model(test_model.get_layer(name='input_data').input,
test_model.get_layer(name='dense2').output)
# A utility to decode the output of the network
def decode_batch_predictions(pred):
pred = pred[:, :-2]
input_len = np.ones(pred.shape[0]) * pred.shape[1]
# Use greedy search. For complex tasks, you can use beam search
results = keras.backend.ctc_decode(pred,
input_length=input_len,
greedy=True)[0][0]
# Iterate over the results and get back the text
output_text = []
for res in results.numpy():
outstr = ''
for c in res:
if c < len(characters) and c >= 0:
outstr += labels_to_char[c]
output_text.append(outstr)
# return final text results
return output_text
p_texts = []
o_texts = []
print('*'*20,'이미지에 대한 예측 결과 확인','*'*20)
# Let's check results on some validation samples
for p, (inp_value, _) in enumerate(test_data_generator):
bs = inp_value['input_data'].shape[0]
X_data = inp_value['input_data']
labels = inp_value['input_label']
preds = prediction_model.predict(X_data)
pred_texts = decode_batch_predictions(preds)
orig_texts = []
for label in labels:
text = ''.join([labels_to_char[int(x)] for x in label])
orig_texts.append(text)
p_texts.append(pred_texts)
o_texts.append(orig_texts)
for i in range(bs):
print(
f'Ground truth: {orig_texts[i]} \t Predicted: {pred_texts[i]} \t Correct: {orig_texts[i] == pred_texts[i]}')
break