1 Verification code introduction

CAPTCHA is a public automatic program that distinguishes a user from a computer or a human. In CAPTCHA tests, the computer acting as the server automatically generates a question for the user to answer. This question can be generated and judged by a computer, but only a human can answer it. Since computers can’t answer CAPTCHA’s questions, users who do are considered human.

2 CNN verification code recognition

The traditional method is to carry out text recognition through two unrelated steps: 1) position the text in the picture, and then cut the text in the picture by “small box” segmentation, and 2) recognition. But in today’s captchas, such “segmentation” is not useful when the text in the image to be identified is written on top of each other. So convolutional neural network (CNN) is used to identify these unintelligible scripts. Such CNN is composed of one or more convolution layers and the top fully connected layer (corresponding to the classical neural network) to recognize the image. The CNN training model requires a large number of manually annotated images for training, but the method in this paper is to automatically generate random characters and generate corresponding pictures to adjust parameters during operation. This article focuses on captcha images with 4 characters. Each character is represented as 62 neurons at the output layer. We can assume a mapping function


To the corresponding


That is:


The first 62 neurons are assigned to the first character in the sequence, and the second batch of 62 neurons to the second character in the sequence. So, for the character xi

The index of corresponding neuron is


There are a total of 4*62=128 output layers. If the index of the first predicted character is C0 =52, the predicted character can therefore be inferred as


3 Implementation Steps

1 Verification code generation

1 Characters in the verification code

number = ['0'.'1'.'2'.'3'.'4'.'5'.'6'.'7'.'8'.'9']
ALPHABET = ['A'.'B'.'C'.'D'.'E'.'F'.'G'.'H'.'I'.'J'.'K'.'L'.'M'.'N'.'O'.'P'.'Q'.'R'.'S'.'T'.'U'.'V'.'W'.'X'.'Y'.'Z']
alphabet = ['a'.'b'.'c'.'d'.'e'.'f'.'g'.'h'.'i'.'j'.'k'.'l'.'m'.'n'.'o'.'p'.'q'.'r'.'s'.'t'.'u'.'v'.'w'.'x'.'y'.'z']

gen_char_set = number + ALPHABET  The data set used to generate the captcha
Copy the code

2 Generate the characters of the verification code

  # char_set=number + alphabet + ALPHABET,
        char_set=gen_char_set,
        # char_set=number,
        captcha_size=4) :""" Generates a random string of 4 bits :param char_set: :param captcha_size: :return: """
    captcha_text = []
    for i in range(captcha_size):
        c = random.choice(char_set)
        captcha_text.append(c)
    return captcha_text
Copy the code

3 Generate verification codes based on characters

def gen_captcha_text_and_image(a):
    """ Generate the verification code corresponding to the character :return: """
    image = ImageCaptcha()

    captcha_text = random_captcha_text()
    captcha_text = ' '.join(captcha_text)

    captcha = image.generate(captcha_text)

    captcha_image = Image.open(captcha)
    captcha_image = np.array(captcha_image)
    return captcha_text, captcha_image
Copy the code

4 training

def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1):
    ""1 defines CNN CNN performs best when the image size is a multiple of 2. If you are using an image that is not a multiple of 2, you can fill in pixels at the edges of the image. Pad (image,(2,3),(2,2)), 'constant', constant values=(255,))

    x = tf.reshape(X, shape=[- 1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])

    # w_c1_alpha = np.sqrt(2.0/(IMAGE_HEIGHT*IMAGE_WIDTH)) #
    # w_c2_alpha = np. SQRT (2.0 / (3 * 3 * 32))
    # w_c3_alpha = np. SQRT (2.0 / (3 * 3 * 64))
    # w_d1_alpha = np. SQRT (2.0 / (8 * 32 * 64))
    # out_alpha = np. SQRT (2.0/1024)

    # 3 conv layer
    w_c1 = tf.Variable(w_alpha * tf.random_normal([3.3.1.32]))
    b_c1 = tf.Variable(b_alpha * tf.random_normal([32]))
    conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1.1.1.1], padding='SAME'), b_c1))
    conv1 = tf.nn.max_pool(conv1, ksize=[1.2.2.1], strides=[1.2.2.1], padding='SAME')
    conv1 = tf.nn.dropout(conv1, keep_prob)

    w_c2 = tf.Variable(w_alpha * tf.random_normal([3.3.32.64]))
    b_c2 = tf.Variable(b_alpha * tf.random_normal([64]))
    conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1.1.1.1], padding='SAME'), b_c2))
    conv2 = tf.nn.max_pool(conv2, ksize=[1.2.2.1], strides=[1.2.2.1], padding='SAME')
    conv2 = tf.nn.dropout(conv2, keep_prob)

    w_c3 = tf.Variable(w_alpha * tf.random_normal([3.3.64.64]))
    b_c3 = tf.Variable(b_alpha * tf.random_normal([64]))
    conv3 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv2, w_c3, strides=[1.1.1.1], padding='SAME'), b_c3))
    conv3 = tf.nn.max_pool(conv3, ksize=[1.2.2.1], strides=[1.2.2.1], padding='SAME')
    conv3 = tf.nn.dropout(conv3, keep_prob)

    # Fully connected layer
    w_d = tf.Variable(w_alpha * tf.random_normal([8 * 20 * 64.1024]))
    b_d = tf.Variable(b_alpha * tf.random_normal([1024]))
    dense = tf.reshape(conv3, [- 1, w_d.get_shape().as_list()[0]])
    dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
    dense = tf.nn.dropout(dense, keep_prob)

    w_out = tf.Variable(w_alpha * tf.random_normal([1024. MAX_CAPTCHA * CHAR_SET_LEN])) b_out = tf.Variable(b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN])) out = tf.add(tf.matmul(dense, w_out), b_out)* 4 # 36
    0 # out = tf_0 (Out, (CHAR_SET_LEN, MAX_CAPTCHA)) # 0 0 0 0
    # out = tf.nn.softmax(out)
    return out
Copy the code

Due to the limitation of time and equipment, I deleted English letters in the string generated by the verification code and only left numbers for training. Otherwise you can calculate the end of time or 3% accuracy. Below is a screenshot of the results of gen_char_set = number + ALPHABET training over 10,000 steps

5 concludes

In this paper, “living CNN” is adopted for verification code recognition, which can avoid a large number of manual annotation steps and greatly improve work efficiency.


The text/JoeCDC

Math enthusiast

Sound/fluorspar

This article has been authorized by the author, the copyright belongs to chuangyu front. Welcome to indicate the source of this article. Link to this article: knownsec-fed.com/2018-09-28-…

To subscribe for more sharing from the front line of KnownsecFED development, please search our wechat official account KnownsecFED. Welcome to leave a comment to discuss, we will reply as far as possible.

Thank you for reading.