Tensorflow simple verification code recognition application, tensorflow Verification Code
Simple Tensorflow verification code recognition application for your reference. The specific content is as follows:
1. Tensorflow Installation MethodI will not go into details here.
2. Training setAs well as testing and the following (manually created, so the number is small ):
3. Implementation Code(Based on some implementations on the Internet)
Main. py (main neural network code)
From gen_check_code import protocol, gen_captcha_text_and_imagefrom gen_check_code import numberfrom test_check_code import using numpy as npimport tensorflow as tftext, image = Signature () print ("Verification Code image channel:", image. shape) # (60,160, 3) # image size IMAGE_HEIGHT = image. shape [0] IMAGE_WIDTH = image. shape [1] image_shape = image. sha PeMAX_CAPTCHA = len (text) print ("Maximum number of characters in the verification code text", MAX_CAPTCHA) # The maximum length of the verification code is 4 characters. I have fixed it to 4 characters and it can be unfixed. if the verification code length is less than 4, fill it with '_' # convert the color image into a grayscale image (color is useless for the verification code) # degrees: def convert2gray (img): if len (img. shape)> 2: gray = np. mean (img,-1) # The preceding method is faster. The regular method 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: r Eturn img "cnn has the highest performance when the image size is a multiple of 2. If the image size you use is not a multiple of 2, you can add useless pixels at the edge of the image. Np. pad (image, (2, 3), (2, 2), 'constant', constant_values = (255,) # Add 2 rows on the image and 3 rows below, add 2 rows to the left and 2 rows to the right. "" char_set = number # If the verification code length is less than 4, '_' is used to complete CHAR_SET_LEN = len (char_set) # text conversion quantity def text2vec (text): text_len = len (text) if text_len> MAX_CAPTCHA: raise ValueError ('verification Code cannot exceed 4 characters ') vector = np. zeros (MAX_CAPTCHA * CHAR_SET_LEN) def char2pos (c): try: k = ord (c)-ord ('0') handle T: raise ValueError ('no map') return k I, c in enumerate (text): idx = I * CHAR_SET_LEN + char2pos (c) vector [idx] = 1 return vector # vector back text def vec2text (vec ): char_pos = vec. nonzero () [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') text. append (chr (char_code) return "". join (text) # generate a training batchdef get_next_batch (batch_size = 128): batch_x = np. zeros ([batch_size, IMAGE_HEIGHT * IMAGE_WIDTH]) batch_y = np. zeros ([batch_size, MAX_CAPTCHA * CHAR_SET_LEN]) # Sometimes the size of the generated image is not (60,160, 3) def wrap_gen_captcha_text_and_image (): while True: text, image = gen_captcha_t Ext_and_image_new () if image. shape = 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,:] = image. flatten ()/255 # (image. flatten ()-128)/128 mean is 0 batch_y [I,:] = text2vec (text) return batch_x, batch_y ####################################### ############################ X = tf. placeholder (tf. float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) Y = tf. placeholder (tf. float32, [None, MAX_CAPTCHA * CHAR_SET_LEN]) keep_prob = tf. placeholder (tf. float32) # dropout # define CNNdef crack_captcha_cnn (w_alpha = 0.01, B _alpha = 0.1): 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) # define a layer-3 convolutional Neural Network # define the level-1 convolutional Neural Network # define the layer-1 weight w_c1 = tf. variable (w_alpha * tf. random_normal ([3, 3, 1, 32]) # defines the first layer offset B _c1 = tf. variable (B _alpha * tf. random_normal ([32]) # define the first-layer incentive function conv1 = tf. nn. relu (tf. nn. bias_add (tf. nn. conv2d (x, w_c1, strides = [1, 1, 1], padding = 'same'), B _c1) # conv1 indicates 2*2 pools for input ksize, convert the color block of 2*2 to the color block conv1 = of 1*1. Tf. nn. max_pool (conv1, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'same') # dropout prevents overfitting. Conv1 = tf. nn. dropout (conv1, keep_prob) # defines the second layer of the convolutional Neural Network 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], 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_p Rob) # define the third-layer convolutional Neural Network 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], 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 # randomly generated Weight w_d = tf. variable (w_alpha * tf. random_normal ([1536,102 4]) # randomly generated offset 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) # out = tf. nn. softmax (out) return out # Train def train_crack_captcha_cnn (): # X = tf. placeholder (tf. float32, [None, IMAGE_HEIGHT * IMAGE_WIDTH]) # Y = tf. placeholder (tf. float32, [None, MAX_CAPTCHA * CHAR_SET_LEN]) # keep_prob = tf. placeholder (tf. float32) # dropout output = crack_captcha_cnn () # loss = tf. r Educe_mean (tf. nn. softmax_cross_entropy_with_logits (output, Y) loss = tf. performance_mean (tf. nn. sigmoid_cross_entropy_with_logits (output, Y) # What is the difference between softmax and sigmoid used for classification at the last layer? # Optimizer should start to increase learning_rate in order to speed up training, and then slowly decline optimizer = tf. train. adamOptimizer (learning_rate = 0.001 ). minimize (loss) predict = tf. reshape (output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]) max_idx_p = tf. argmax (predict, 2) max_idx_l = tf. argmax (tf. reshape (Y, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) correct_pred = tf. equal (max_idx_p, max_idx_l) accuracy = tf. performance_mean (tf. cast (correct_pred, tf. float32) saver = tf. train. saver () with tf. session () as sess: sess. run (tf. global_variables_initializer () step = 0 while True: batch_x, batch_y = get_next_batch (64) _, loss _ = sess. run ([optimizer, loss], feed_dict = {X: batch_x, Y: batch_y, keep_prob: 0.75}) print (step, loss _) # accuracy calculated every 100 steps if step % 100 = 0: batch_x_test, batch_y_test = get_next_batch (100) acc = sess. run (accuracy, feed_dict = {X: batch_x_test, Y: batch_y_test, keep_prob: 1 .}) print (step, acc) # if the accuracy is greater than 50%, save the model and complete training if acc> 0.99: saver. save (sess ,". /crack_capcha.model ", global_step = step) break step + = 1 # training (if you want to train, remove the following line of comment) Explain () def crack_captcha (): output = crack_captcha_cnn () saver = tf. train. saver () with tf. session () as sess: saver. restore (sess, tf. train. latest_checkpoint ('. ') predict = tf. argmax (tf. reshape (output, [-1, MAX_CAPTCHA, CHAR_SET_LEN]), 2) count = 0 # A total of 40 test sets... the write is sloppy for I in range (40): text, image = get_test_captcha_text_and_image (I) image = convert2gray (image) captcha_image = image. flatten ()/255 text_list = sess. run (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 = ", the prediction result is correct" else: check_result = ", incorrect prediction result "print (" correct: {} prediction :{}". format (text, predict_text) + check_result) print ("correct rate:" + str (count) + "/40") # test (if you want to test, remove the comments of the following line) # crack_captcha ()
Gen_check_code.py (to get the input of the training set, you must change root_dir to the input folder of the training set, the same below)
From captcha. image import ImageCaptcha # pip install captchaimport numpy as npfrom PIL import Imageimport random # import matplotlib. pyplot as pltimport osfrom random import choice # characters in the verification code, no Chinese characters are needed. 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', '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" # The verification code is case insensitive. The verification code consists of four characters: def random_captcha_text (char_set = number, captcha_size = 4 ): captcha_text = [] for I in range (captcha_size): c = random. choice (char_set) captcha_text.append (c) return captcha_text # generate the verification code def encode (): image = ImageCaptcha () captcha_text = random_captcha_text () captcha_text = ''. join (captcha_text) captcha = image. generate (captcha_text) # image. write (captcha_text, captcha_text + '.jpg ') # write to the file captcha_image = Image. open (captcha) captcha_image = np. array (captcha_image) return captcha_text, captcha_imagedef gen_list (): img_list = [] for parent, dirnames, filenames in OS. walk (root_dir): # three parameters: return 1. parent directory 2. all folder names (excluding paths) 3. all file names for filename in filenames: # output file information img_list.append (filename. replace (". gif "," ") # print (" parent is: "+ parent) # print (" filename is: "+ filename) # print ("the full name of the file is:" + OS. path. join (parent, filename) # return img_listimg_list = gen_list () def gen_captcha_text_and_image_new (): img = choice (img_list) captcha_image = Image. open (root_dir + "\" + img + ". gif ") captcha_image = np. array (captcha_image) return img, captcha_image # if _ name _ = '_ main _': ## test ## text, image = gen_captcha_text_and_image () #### f = plt. figure () # ax = f. add_subplot (111) # ax. text (0.1, 0.9, text, ha = 'center', va = 'center', transform = ax. transAxes) # plt. imshow (image) # plt. show () #### text, image = gen_captcha_text_and_image_new () # f = plt. figure () # ax = f. add_subplot (111) # ax. text (0.1, 0.9, text, ha = 'center', va = 'center', transform = ax. transAxes) # plt. imshow (image) # plt. show ()
Test_check_code.py (obtain the test set input)
From captcha. image import ImageCaptcha # pip install captchaimport numpy as npfrom PIL import Imageimport randomimport matplotlib. pyplot as pltimport osfrom random import choiceroot_dir = "d: \ test" img_list = [] def gen_list (): for parent, dirnames, filenames in OS. walk (root_dir): # three parameters: return 1. parent directory 2. all folder names (excluding paths) 3. all file names for filename in filenames: # output file information img_list.append (filename. replace (". gif "," ") # print (" parent is: "+ parent) # print (" filename is: "+ filename) # print ("the full name of the file is:" + OS. path. join (parent, filename) # return img_listimg_list = gen_list () def get_test_captcha_text_and_image (I = None): img = img_list [I] captcha_image = Image. open (root_dir + "\" + img + ". gif ") captcha_image = np. array (captcha_image) return img, captcha_image
4. Results
Recognition Rate on the Test Set
5. Download Related Files
Download training set and Test Set
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.