-
Notifications
You must be signed in to change notification settings - Fork 0
/
captcha_cnn.py
executable file
·44 lines (36 loc) · 1.39 KB
/
captcha_cnn.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
# coding:utf-8
import tensorflow as tf
from captchaCnn.cnn_train import cnn_graph
from captchaCnn.captcha_gen import gen_captcha_text_and_image
from captchaCnn.util import vec2text, convert2gray
from captchaCnn.util import CAPTCHA_LIST, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, CAPTCHA_LEN
import matplotlib.pyplot as plt
def captcha2text(image_list, height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH):
"""
#验证码图片转化为文本
:param image_list:
:param height:
:param width:
:return:
"""
x = tf.placeholder(tf.float32, [None, height * width])
keep_prob = tf.placeholder(tf.float32)
y_conv = cnn_graph(x, keep_prob, (height, width))
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, tf.train.latest_checkpoint('.'))
predict = tf.argmax(tf.reshape(y_conv, [-1, CAPTCHA_LEN, len(CAPTCHA_LIST)]), 2)
vector_list = sess.run(predict, feed_dict={x: image_list, keep_prob: 1})
vector_list = vector_list.tolist()
text_list = [vec2text(vector) for vector in vector_list]
return text_list
if __name__ == '__main__':
text, image = gen_captcha_text_and_image()
plt.figure('color')
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.show()
image = convert2gray(image)
image = image.flatten() / 255
pre_text = captcha2text([image])
print('Label:', text, ' Predict:', pre_text)