SoFunction
Updated on 2024-11-19

Tensorflow Simple Captcha Recognition Application

Simple Tensorflow CAPTCHA recognition application for your reference is as follows

Installation of theSimple, so I won't go into it here.

2. training set training setAs well as testing and as follows (handmade, so the number is not large).

3. Realize the code part(Referred to some implementations on the web to accomplish this)

(The main neural network code)

from gen_check_code import gen_captcha_text_and_image_new,gen_captcha_text_and_image
from gen_check_code import number
from test_check_code import get_test_captcha_text_and_image
import numpy as np
import tensorflow as tf

text, image = gen_captcha_text_and_image_new()
print("Captcha image channel:", ) # (60, 160, 3) 
# image size
IMAGE_HEIGHT = [0]
IMAGE_WIDTH = [1]
image_shape = 
MAX_CAPTCHA = len(text)
print("Maximum number of characters in CAPTCHA text", MAX_CAPTCHA) # CAPTCHA up to 4 characters long; I fixed all to 4, can not be fixed. If the length of CAPTCHA is less than 4, use '_' to make it up.


# Convert color images to grayscale (color is not useful for CAPTCHA recognition)
# Degreeing is the process of converting the three components into the same value
def convert2gray(img):
 if len() > 2:
  gray = (img, -1)
  # The above is a quicker way to turn, and the regular way to turn is as follows
  # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2] 
  # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
  # int gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);
  return gray
 else:
  return img


""" 
cnnAfter the image size is2The highest performance is achieved at multiples of, If you use an image size other than2multiple of sth.,Possible to patch useless pixels at image edges。 
(image,((2,3),(2,2)), 'constant', constant_values=(255,)) # 2 rows above the image, 3 rows below, 2 rows left, 2 rows right
"""


char_set = number # If CAPTCHA length is less than 4, '_' is used to fill in the blanks.
CHAR_SET_LEN = len(char_set)

# Text steering
def text2vec(text):
 text_len = len(text)
 if text_len > MAX_CAPTCHA:
  raise ValueError('Captcha up to 4 characters long')

 vector = (MAX_CAPTCHA * CHAR_SET_LEN)

 def char2pos(c):
  try:
   k = ord(c)-ord('0')
  except:
   raise ValueError('No Map')
  return k

 for i, c in enumerate(text):
  idx = i * CHAR_SET_LEN + char2pos(c)
  vector[idx] = 1
 return vector


# Vector to text
def vec2text(vec):
 char_pos = ()[0]
 text = []
 for i, c in enumerate(char_pos):
  char_at_pos = i # c/63
  char_idx = c % CHAR_SET_LEN
  if char_idx < 10:
   char_code = char_idx + ord('0')
  elif char_idx < 36:
   char_code = char_idx - 10 + ord('A')
  elif char_idx < 62:
   char_code = char_idx - 36 + ord('a')
  elif char_idx == 62:
   char_code = ord('_')
  else:
   raise ValueError('error')
  (chr(char_code))
 return "".join(text)


# Generate a training batch
def get_next_batch(batch_size=128):
 batch_x = ([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH])
 batch_y = ([batch_size, MAX_CAPTCHA * CHAR_SET_LEN])

 # Sometimes the generated image size is not (60, 160, 3)
 def wrap_gen_captcha_text_and_image():
  while True:
   text, image = gen_captcha_text_and_image_new()

   if  == image_shape:
    return text, image

 for i in range(batch_size):
  text, image = wrap_gen_captcha_text_and_image()
  image = convert2gray(image)


  batch_x[i, :] = () / 255 # (()-128)/128 mean is 0
  batch_y[i, :] = text2vec(text)

 return batch_x, batch_y


####################################################################

X = (tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH])
Y = (tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN])
keep_prob = (tf.float32) # dropout


# Define the CNN
def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1):
 x = (X, shape=[-1, IMAGE_HEIGHT, IMAGE_WIDTH, 1])

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

 # Define a three-layer convolutional neural network

 # Define the first layer of a convolutional neural network
 # Define the first layer of weights
 w_c1 = (w_alpha * tf.random_normal([3, 3, 1, 32]))
 # Define the bias of the first layer
 b_c1 = (b_alpha * tf.random_normal([32]))
 # Define the excitation function for the first layer
 conv1 = (.bias_add(.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1))
 # conv1 as input ksize means use 2*2 pooling, i.e. convert 2*2 color blocks into 1*1 color blocks
 conv1 = .max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 # dropout prevents overfitting.
 conv1 = (conv1, keep_prob)

 # Define the second layer of the convolutional neural network
 w_c2 = (w_alpha * tf.random_normal([3, 3, 32, 64]))
 b_c2 = (b_alpha * tf.random_normal([64]))
 conv2 = (.bias_add(.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2))
 conv2 = .max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 conv2 = (conv2, keep_prob)

 # Define the third layer of the convolutional neural network
 w_c3 = (w_alpha * tf.random_normal([3, 3, 64, 64]))
 b_c3 = (b_alpha * tf.random_normal([64]))
 conv3 = (.bias_add(.conv2d(conv2, w_c3, strides=[1, 1, 1, 1], padding='SAME'), b_c3))
 conv3 = .max_pool(conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
 conv3 = (conv3, keep_prob)

 # Fully connected layer
 # Randomly generated weights
 w_d = (w_alpha * tf.random_normal([1536, 1024]))
 # Randomly generated bias
 b_d = (b_alpha * tf.random_normal([1024]))
 dense = (conv3, [-1, w_d.get_shape().as_list()[0]])
 dense = (((dense, w_d), b_d))
 dense = (dense, keep_prob)

 w_out = (w_alpha * tf.random_normal([1024, MAX_CAPTCHA * CHAR_SET_LEN]))
 b_out = (b_alpha * tf.random_normal([MAX_CAPTCHA * CHAR_SET_LEN]))
 out = ((dense, w_out), b_out)
 # out = (out)
 return out


# Training
def train_crack_captcha_cnn():
 # X = (tf.float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH])
 # Y = (tf.float32, [None, MAX_CAPTCHA * CHAR_SET_LEN])
 # keep_prob = (tf.float32) # dropout
 output = crack_captcha_cnn()
 # loss 
 # loss = tf.reduce_mean(.softmax_cross_entropy_with_logits(output, Y))
 loss = tf.reduce_mean(.sigmoid_cross_entropy_with_logits(output, Y))
 # What is the difference between softmax and sigmoid used for classification in the last layer?
 # optimizer To speed up training the learning_rate should start large and then slowly decay.
 optimizer = (learning_rate=0.001).minimize(loss)

 predict = (output, [-1, MAX_CAPTCHA, CHAR_SET_LEN])
 max_idx_p = (predict, 2)
 max_idx_l = ((Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
 correct_pred = (max_idx_p, max_idx_l)
 accuracy = tf.reduce_mean((correct_pred, tf.float32))

 saver = ()
 with () as sess:
   (tf.global_variables_initializer())

   step = 0
   while True:
    batch_x, batch_y = get_next_batch(64)
    _, loss_ = ([optimizer, loss], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75})
    print(step, loss_)

    # Calculate accuracy every 100 steps
    if step % 100 == 0:
     batch_x_test, batch_y_test = get_next_batch(100)
     acc = (accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})
     print(step, acc)
     # If accuracy is greater than 50%, save model and complete training
     if acc > 0.99:
      (sess, "./crack_capcha.model", global_step=step)
      break
    step += 1

## Train (remove the comment on the following line if you want to train)
train_crack_captcha_cnn()


def crack_captcha():
 output = crack_captcha_cnn()

 saver = ()
 with () as sess:
  (sess, .latest_checkpoint('.'))

  predict = ((output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2)
  count = 0
  # Because the test set totaled 40... It's sloppy.
  for i in range(40):
   text, image = get_test_captcha_text_and_image(i)
   image = convert2gray(image)
   captcha_image = () / 255
   text_list = (predict, feed_dict={X: [captcha_image], keep_prob: 1})
   predict_text = text_list[0].tolist()
   predict_text = str(predict_text)
   predict_text = predict_text.replace("[", "").replace("]", "").replace(",", "").replace(" ","")
   if text == predict_text:
    count += 1
    check_result = "that the prediction turned out to be correct."
   else:
    check_result = "that the prediction was incorrect."
    print("properly: {} anticipate: {}".format(text, predict_text) + check_result)

  print("Correctness:" + str(count) + "/40")
# Test (remove the comment on the following line if you want to test)
# crack_captcha()

gen_check_code.py(get the input of training set, need to pay attention to modify root_dir as the input folder of training set, the same below)

from  import ImageCaptcha # pip install captcha
import numpy as np
from PIL import Image
import random
# import  as plt
import os
from random import choice

# Characters in the CAPTCHA, no more Chinese characters.
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']

root_dir = "d:\\train"

# CAPTCHA is generally case-insensitive; CAPTCHA length 4 characters
def random_captcha_text(char_set=number, captcha_size=4):
 captcha_text = []
 for i in range(captcha_size):
  c = (char_set)
  captcha_text.append(c)
 return captcha_text


# Generate character-corresponding CAPTCHA
def gen_captcha_text_and_image():
 image = ImageCaptcha()

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

 captcha = (captcha_text)
 # (captcha_text, captcha_text + '.jpg') # Write to file

 captcha_image = (captcha)
 captcha_image = (captcha_image)
 return captcha_text, captcha_image


def gen_list():
 img_list = []
 for parent, dirnames, filenames in (root_dir): # Three parameters: respectively return 1. parent directory 2. all folder names (without path) 3. all file names
  for filename in filenames: # Output file information
   img_list.append((".gif",""))
   # print("parent is:" + parent)
   # print("filename is:" + filename)
   # print("the full name of the file is:" + (parent, filename)) # Output file path information
 return img_list
img_list = gen_list()
def gen_captcha_text_and_image_new():
 img = choice(img_list)
 captcha_image = (root_dir + "\\" + img + ".gif")
 captcha_image = (captcha_image)
 return img, captcha_image


# if __name__ == '__main__':
# # Testing
#  # text, image = gen_captcha_text_and_image()
#  #
#  # f = ()
#  # ax = f.add_subplot(111)
#  # (0.1, 0.9, text, ha='center', va='center', transform=)
#  # (image)
#  # ()
#  #
#
#  text, image = gen_captcha_text_and_image_new()
#
#  f = ()
#  ax = f.add_subplot(111)
#  (0.1, 0.9, text, ha='center', va='center', transform=)
#  (image)
#  ()

test_check_code.py (get test set input)

from  import ImageCaptcha # pip install captcha
import numpy as np
from PIL import Image
import random
import  as plt
import os
from random import choice


root_dir = "d:\\test"



img_list = []
def gen_list():

 for parent, dirnames, filenames in (root_dir): # Three parameters: respectively return 1. parent directory 2. all folder names (without path) 3. all file names
  for filename in filenames: # Output file information
   img_list.append((".gif",""))
   # print("parent is:" + parent)
   # print("filename is:" + filename)
   # print("the full name of the file is:" + (parent, filename)) # Output file path information
 return img_list

img_list = gen_list()
def get_test_captcha_text_and_image(i=None):
 img = img_list[i]
 captcha_image = (root_dir + "\\" + img + ".gif")
 captcha_image = (captcha_image)
 return img, captcha_image


4. Effects

Recognition rate on the test set

5.Related Documents Download

Training set as well as test setdownloading

This is the whole content of this article.