Пример #1
0
def multi_test(height=CAPTCHA_HEIGHT, width=CAPTCHA_WIDTH):
    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('.'))
        while 1:
            text, image = get_random_captcha_text_and_image()
            image = convert2gray(image)
            image = image.flatten() / 255
            image_list = [image]
            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]
            pre_text = text_list[0]
            flag = u'错误'
            if text == pre_text:
                flag = u'正确'
            print u"实际值(actual):%s, 预测值(predict):%s, 预测结果:%s" % (
                text,
                pre_text,
                flag,
            )
Пример #2
0
def test_crack_captcha(test_step):
    output = crack_captcha_cnn()

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, tf.train.latest_checkpoint('./models'))
        predict = tf.argmax(
            tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
        sum_correct = 0
        # for _ in range(test_step):
        for g_image in gen_image('./captcha_image'):

            text_source, image = g_image
            # print(text_source, image)
            image = convert2gray(image)
            captcha_image = image.flatten() / 255
            text_list = sess.run(predict,
                                 feed_dict={
                                     X: [captcha_image],
                                     keep_prob: 1
                                 })
            # text_list = sess.run(predict, feed_dict={X: np.reshape(captcha_image,(-1,4000)), keep_prob: 1})
            text = text_list[0].tolist()
            vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN)
            i = 0
            for n in text:
                vector[i * CHAR_SET_LEN + n] = 1
                i += 1
            predict_text = vec2text(vector)
            print("正确: {}  预测: {}".format(text_source, predict_text))

            if text_source.lower() == predict_text.lower():
                sum_correct += 1
        print('sum_correct:%s' % sum_correct)
        print('成功率:%s' % (sum_correct / test_step))
Пример #3
0
def crack_captcha():
    output = crack_captcha_cnn()

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, tf.train.latest_checkpoint('models'))

        predict = tf.argmax(
            tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
        count_out = 0
        right_number = 0
        while (True):
            root_text, image = gen_captcha_text_and_image()
            image = convert2gray(image)
            captcha_image = image.flatten() / 255
            text_list = sess.run(predict,
                                 feed_dict={
                                     X: [captcha_image],
                                     keep_prob: 1
                                 })

            text = text_list[0].tolist()
            vector = np.zeros(MAX_CAPTCHA * CHAR_SET_LEN)
            i = 0
            for n in text:
                vector[i * CHAR_SET_LEN + n] = 1
                i += 1
            predict_text = vec2text(vector)
            print("正确: {}  预测: {}".format(root_text, predict_text))
            if count_out == 1000:
                break
            count_out += 1
Пример #4
0
def test_captcha_model(test_step):
    sum_correct = 0
    for _ in range(test_step):
        # text, image = gen_captcha_text_and_image()
        text, image = gen_image('./captcha_image')
        image = convert2gray(image)
        image = image.flatten() / 255
        predict_text = crack_captcha(image)
        print("正确: {}  预测: {}".format(text, predict_text))
        if text == predict_text:
            sum_correct += 1
    print('sum_correct:%s' % sum_correct)
    print('成功率:%s' % (sum_correct / test_step))
Пример #5
0
def get_name_and_image():
    list1 = glob.glob(r"C:\Users\Administrator\Desktop\text\text\*.png")
    num  =random.randint(0,len(list1)-1)

    path = list1[num]
    print(path)
    p, image_text = path.split('_')
    captcha_text, type = image_text.split('.')

    captcha_image = Image.open(path)

    captcha_image = np.array(captcha_image)

    image = convert2gray(captcha_image)
    image = image.flatten() / 255

    return captcha_text, image
Пример #6
0
def test():
    img_base64 = request.form['img_base64']
    # return img_base64
    img_base64 = re.sub('^data:image/.+;base64,', '', img_base64)
    img_base64 = base64.b64decode(img_base64)
    buffer = cStringIO.StringIO(img_base64)
    img = Image.open(buffer)
    img = np.array(img)
    img = convert2gray(img)
    img = img.flatten() / 255
    text_list = sess.run(pre, feed_dict={X: [img], keep_prop: 1})
    text = text_list[0].tolist()
    vector = np.zeros(6 * 36)
    i = 0
    for n in text:
        vector[i * 36 + n] = 1
        i += 1
    text = vec2text(vector)
    res = {"code": 200, "data": text, "msg": None}
    return json.dumps(res)
Пример #7
0
            flag = u'错误'
            if text == pre_text:
                flag = u'正确'
            print u"实际值(actual):%s, 预测值(predict):%s, 预测结果:%s" % (
                text,
                pre_text,
                flag,
            )


if __name__ == '__main__':
    try:
        # 多个测试
        multi_test()
        exit()

        text, image = get_random_captcha_text_and_image()
        image = convert2gray(image)
        image = image.flatten() / 255
        pre_text = captcha_to_text([image])
        flag = u'错误'
        if text == pre_text:
            flag = u'正确'
        print u"实际值(actual):%s, 预测值(predict):%s, 预测结果:%s" % (
            text,
            pre_text,
            flag,
        )
    except KeyboardInterrupt as e:
        print e.message
Пример #8
0
from gen_captcha import gen_captcha_text_and_image
from gen_captcha import number
from gen_captcha import alphabet
from gen_captcha import ALPHABET


IMAGE_HEIGHT = 60
IMAGE_WIDTH = 160
MAX_CAPTCHA = 4

# 文本转向量
char_set = number + alphabet + ALPHABET + ['_']  # 如果验证码长度小于4, '_'用来补齐
CHAR_SET_LEN = len(char_set)
def crack_captcha(captcha_image):
    output = crack_captcha_cnn()

    saver = tf.train.Saver()
    predict = tf.argmax(tf.reshape(output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
    with tf.Session() as sess:
        saver.restore(sess, "./Model/model.ckpt")

        text_list = sess.run(predict, feed_dict={X: [captcha_image], keep_prob: 1})
        text = text_list[0].tolist()
        return text


text, image = gen_captcha_text_and_image()
image = convert2gray(image)
image = image.flatten() / 255
predict_text = crack_captcha(image)
print("正确: {}  预测: {}".format(text, predict_text))