竞赛选题 题目:基于python的验证码识别 - 机器视觉 验证码识别

文章目录

  • 0 前言
  • 1 项目简介
  • 2 验证码识别步骤
    • 2.1 灰度处理&二值化
    • 2.2 去除边框
    • 2.3 图像降噪
    • 2.4 字符切割
    • 2.5 识别
  • 3 基于tensorflow的验证码识别
    • 3.1 数据集
    • 3.2 基于tf的神经网络训练代码
  • 4 最后

0 前言

🔥 优质竞赛项目系列,今天要分享的是

基于python的验证码识别

该项目较为新颖,适合作为竞赛课题方向,学长非常推荐!

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate

1 项目简介

在python爬虫爬取某些网站的验证码的时候可能会遇到验证码识别的问题,现在的验证码大多分为四类:

  • 1、计算验证码

  • 2、滑块验证码

  • 3、识图验证码

  • 4、语音验证码

学长这主要写的就是识图验证码,识别的是简单的验证码,要想让识别率更高,识别的更加准确就需要花很多的精力去训练自己的字体库。

2 验证码识别步骤

1、灰度处理

2、二值化

3、去除边框(如果有的话)

4、降噪

5、切割字符或者倾斜度矫正

6、训练字体库
7、识别

这6个步骤中前三个步骤是基本的,4或者5可根据实际情况选择是否需要,并不一定切割验证码,识别率就会上升很多有时候还会下降

这篇博客不涉及训练字体库的内容,请自行搜索。同样也不讲解基础的语法。

用到的几个主要的python库: Pillow(python图像处理库)、OpenCV(高级图像处理库)、pytesseract(识别库)

2.1 灰度处理&二值化

灰度处理,就是把彩色的验证码图片转为灰色的图片。

二值化,是将图片处理为只有黑白两色的图片,利于后面的图像处理和识别

在OpenCV中有现成的方法可以进行灰度处理和二值化,处理后的效果:

在这里插入图片描述

# 自适应阀值二值化
def _get_dynamic_binary_image(filedir, img_name):
  filename =   './out_img/' + img_name.split('.')[0] + '-binary.jpg'
  img_name = filedir + '/' + img_name
  print('.....' + img_name)
  im = cv2.imread(img_name)
  im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) #灰值化
  # 二值化
  th1 = cv2.adaptiveThreshold(im, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 21, 1)
  cv2.imwrite(filename,th1)
  return th1

2.2 去除边框

如果验证码有边框,那我们就需要去除边框,去除边框就是遍历像素点,找到四个边框上的所有点,把他们都改为白色,我这里边框是两个像素宽

注意:在用OpenCV时,图片的矩阵点是反的,就是长和宽是颠倒的

代码:

# 去除边框
def clear_border(img,img_name):
  filename = './out_img/' + img_name.split('.')[0] + '-clearBorder.jpg'
  h, w = img.shape[:2]
  for y in range(0, w):
    for x in range(0, h):
      if y < 2 or y > w - 2:
        img[x, y] = 255
      if x < 2 or x > h -2:
        img[x, y] = 255

  cv2.imwrite(filename,img)
  return img

效果

在这里插入图片描述

2.3 图像降噪

降噪是验证码处理中比较重要的一个步骤,我这里使用了点降噪和线降噪

在这里插入图片描述
线降噪的思路就是检测这个点相邻的四个点(图中标出的绿色点),判断这四个点中是白点的个数,如果有两个以上的白色像素点,那么就认为这个点是白色的,从而去除整个干扰线,但是这种方法是有限度的,如果干扰线特别粗就没有办法去除,只能去除细的干扰线

# 干扰线降噪
def interference_line(img, img_name):
  filename =  './out_img/' + img_name.split('.')[0] + '-interferenceline.jpg'
  h, w = img.shape[:2]
  # !!!opencv矩阵点是反的
  # img[1,2] 1:图片的高度,2:图片的宽度
  for y in range(1, w - 1):
    for x in range(1, h - 1):
      count = 0
      if img[x, y - 1] > 245:
        count = count + 1
      if img[x, y + 1] > 245:
        count = count + 1
      if img[x - 1, y] > 245:
        count = count + 1
      if img[x + 1, y] > 245:
        count = count + 1
      if count > 2:
        img[x, y] = 255
  cv2.imwrite(filename,img)
  return img

点降噪的思路和线降噪的差不多,只是会针对不同的位置检测的点不一样,注释写的很清楚了

# 点降噪
def interference_point(img,img_name, x = 0, y = 0):
    """
    9邻域框,以当前点为中心的田字框,黑点个数
    :param x:
    :param y:
    :return:
    """
    filename =  './out_img/' + img_name.split('.')[0] + '-interferencePoint.jpg'
    # todo 判断图片的长宽度下限
    cur_pixel = img[x,y]# 当前像素点的值
    height,width = img.shape[:2]

    for y in range(0, width - 1):
      for x in range(0, height - 1):
        if y == 0:  # 第一行
            if x == 0:  # 左上顶点,4邻域
                # 中心点旁边3个点
                sum = int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x + 1, y]) \
                      + int(img[x + 1, y + 1])
                if sum <= 2 * 245:
                  img[x, y] = 0
            elif x == height - 1:  # 右上顶点
                sum = int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x - 1, y]) \
                      + int(img[x - 1, y + 1])
                if sum <= 2 * 245:
                  img[x, y] = 0
            else:  # 最上非顶点,6邻域
                sum = int(img[x - 1, y]) \
                      + int(img[x - 1, y + 1]) \
                      + int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x + 1, y]) \
                      + int(img[x + 1, y + 1])
                if sum <= 3 * 245:
                  img[x, y] = 0
        elif y == width - 1:  # 最下面一行
            if x == 0:  # 左下顶点
                # 中心点旁边3个点
                sum = int(cur_pixel) \
                      + int(img[x + 1, y]) \
                      + int(img[x + 1, y - 1]) \
                      + int(img[x, y - 1])
                if sum <= 2 * 245:
                  img[x, y] = 0
            elif x == height - 1:  # 右下顶点
                sum = int(cur_pixel) \
                      + int(img[x, y - 1]) \
                      + int(img[x - 1, y]) \
                      + int(img[x - 1, y - 1])

                if sum <= 2 * 245:
                  img[x, y] = 0
            else:  # 最下非顶点,6邻域
                sum = int(cur_pixel) \
                      + int(img[x - 1, y]) \
                      + int(img[x + 1, y]) \
                      + int(img[x, y - 1]) \
                      + int(img[x - 1, y - 1]) \
                      + int(img[x + 1, y - 1])
                if sum <= 3 * 245:
                  img[x, y] = 0
        else:  # y不在边界
            if x == 0:  # 左边非顶点
                sum = int(img[x, y - 1]) \
                      + int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x + 1, y - 1]) \
                      + int(img[x + 1, y]) \
                      + int(img[x + 1, y + 1])

                if sum <= 3 * 245:
                  img[x, y] = 0
            elif x == height - 1:  # 右边非顶点
                sum = int(img[x, y - 1]) \
                      + int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x - 1, y - 1]) \
                      + int(img[x - 1, y]) \
                      + int(img[x - 1, y + 1])

                if sum <= 3 * 245:
                  img[x, y] = 0
            else:  # 具备9领域条件的
                sum = int(img[x - 1, y - 1]) \
                      + int(img[x - 1, y]) \
                      + int(img[x - 1, y + 1]) \
                      + int(img[x, y - 1]) \
                      + int(cur_pixel) \
                      + int(img[x, y + 1]) \
                      + int(img[x + 1, y - 1]) \
                      + int(img[x + 1, y]) \
                      + int(img[x + 1, y + 1])
                if sum <= 4 * 245:
                  img[x, y] = 0
    cv2.imwrite(filename,img)
    return img

效果:
在这里插入图片描述

其实到了这一步,这些字符就可以识别了,没必要进行字符切割了,现在这三种类型的验证码识别率已经达到50%以上了

2.4 字符切割

字符切割通常用于验证码中有粘连的字符,粘连的字符不好识别,所以我们需要将粘连的字符切割为单个的字符,在进行识别

字符切割的思路就是找到一个黑色的点,然后在遍历与他相邻的黑色的点,直到遍历完所有的连接起来的黑色的点,找出这些点中的最高的点、最低的点、最右边的点、最左边的点,记录下这四个点,认为这是一个字符,然后在向后遍历点,直至找到黑色的点,继续以上的步骤。最后通过每个字符的四个点进行切割

在这里插入图片描述

图中红色的点就是代码执行完后,标识出的每个字符的四个点,然后就会根据这四个点进行切割(图中画的有些误差,懂就好)

但是也可以看到,m2是粘连的,代码认为他是一个字符,所以我们需要对每个字符的宽度进行检测,如果他的宽度过宽,我们就认为他是两个粘连在一起的字符,并将它在从中间切割

确定每个字符的四个点代码:

def cfs(im,x_fd,y_fd):
  '''用队列和集合记录遍历过的像素坐标代替单纯递归以解决cfs访问过深问题
  '''

  # print('**********')

  xaxis=[]
  yaxis=[]
  visited =set()
  q = Queue()
  q.put((x_fd, y_fd))
  visited.add((x_fd, y_fd))
  offsets=[(1, 0), (0, 1), (-1, 0), (0, -1)]#四邻域

  while not q.empty():
      x,y=q.get()

      for xoffset,yoffset in offsets:
          x_neighbor,y_neighbor = x+xoffset,y+yoffset

          if (x_neighbor,y_neighbor) in (visited):
              continue  # 已经访问过了

          visited.add((x_neighbor, y_neighbor))

          try:
              if im[x_neighbor, y_neighbor] == 0:
                  xaxis.append(x_neighbor)
                  yaxis.append(y_neighbor)
                  q.put((x_neighbor,y_neighbor))

          except IndexError:
              pass
  # print(xaxis)
  if (len(xaxis) == 0 | len(yaxis) == 0):
    xmax = x_fd + 1
    xmin = x_fd
    ymax = y_fd + 1
    ymin = y_fd

  else:
    xmax = max(xaxis)
    xmin = min(xaxis)
    ymax = max(yaxis)
    ymin = min(yaxis)
    #ymin,ymax=sort(yaxis)

  return ymax,ymin,xmax,xmin

def detectFgPix(im,xmax):
  '''搜索区块起点
  '''

  h,w = im.shape[:2]
  for y_fd in range(xmax+1,w):
      for x_fd in range(h):
          if im[x_fd,y_fd] == 0:
              return x_fd,y_fd

def CFS(im):
  '''切割字符位置
  '''

  zoneL=[]#各区块长度L列表
  zoneWB=[]#各区块的X轴[起始,终点]列表
  zoneHB=[]#各区块的Y轴[起始,终点]列表

  xmax=0#上一区块结束黑点横坐标,这里是初始化
  for i in range(10):

      try:
          x_fd,y_fd = detectFgPix(im,xmax)
          # print(y_fd,x_fd)
          xmax,xmin,ymax,ymin=cfs(im,x_fd,y_fd)
          L = xmax - xmin
          H = ymax - ymin
          zoneL.append(L)
          zoneWB.append([xmin,xmax])
          zoneHB.append([ymin,ymax])

      except TypeError:
          return zoneL,zoneWB,zoneHB

  return zoneL,zoneWB,zoneHB

切割粘连字符代码:

def cutting_img(im,im_position,img,xoffset = 1,yoffset = 1):
  filename =  './out_img/' + img.split('.')[0]
  # 识别出的字符个数
  im_number = len(im_position[1])
  # 切割字符
  for i in range(im_number):
    im_start_X = im_position[1][i][0] - xoffset
    im_end_X = im_position[1][i][1] + xoffset
    im_start_Y = im_position[2][i][0] - yoffset
    im_end_Y = im_position[2][i][1] + yoffset
    cropped = im[im_start_Y:im_end_Y, im_start_X:im_end_X]
    cv2.imwrite(filename + '-cutting-' + str(i) + '.jpg',cropped)

效果:

在这里插入图片描述

2.5 识别

识别用的是typesseract库,主要识别一行字符和单个字符时的参数设置,识别中英文的参数设置,代码很简单就一行,我这里大多是filter文件的操作

# 识别验证码
      cutting_img_num = 0
      for file in os.listdir('./out_img'):
        str_img = ''
        if fnmatch(file, '%s-cutting-*.jpg' % img_name.split('.')[0]):
          cutting_img_num += 1
      for i in range(cutting_img_num):
        try:
          file = './out_img/%s-cutting-%s.jpg' % (img_name.split('.')[0], i)
          # 识别字符
          str_img = str_img + image_to_string(Image.open(file),lang = 'eng', config='-psm 10') #单个字符是10,一行文本是7
        except Exception as err:
          pass
      print('切图:%s' % cutting_img_num)
      print('识别为:%s' % str_img)

最后这种粘连字符的识别率是在30%左右,而且这种只是处理两个字符粘连,如果有两个以上的字符粘连还不能识别,但是根据字符宽度判别的话也不难,有兴趣的可以试一下

无需切割字符识别的效果:

在这里插入图片描述

需要切割字符的识别效果:

在这里插入图片描述

3 基于tensorflow的验证码识别

  • python库: tensorflow, opencv, pandas, gpu机器。

  • 训练集: 10w 图片, 200step左右开始收敛。

  • 策略: 切分图片,训练单字母识别。预测时也是同样切分。(ps:不切分训练及识别,跑了一夜,没有收敛)

  • 准确率: 在区分大小写的情况下,单字母识别率98%, 整体识别率75%+。

3.1 数据集

在这里插入图片描述

数据集预处理

package com;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
 
import org.patchca.color.ColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;
 
public class CreatePatcha {
    private static Random random = new Random();
    private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
    static {
        // cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));
        cs.setColorFactory(new ColorFactory() {
            @Override
            public Color getColor(int x) {
                int[] c = new int[3];
                int i = random.nextInt(c.length);
                for (int fi = 0; fi < c.length; fi++) {
                    if (fi == i) {
                        c[fi] = random.nextInt(71);
                    } else {
                        c[fi] = random.nextInt(256);
                    }
                }
                return new Color(c[0], c[1], c[2]);
            }
        });
        RandomWordFactory wf = new RandomWordFactory();
//      wf.setCharacters("23456789abcdefghigklmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
        wf.setCharacters("0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ");
        wf.setMaxLength(4);
        wf.setMinLength(4);
         
        cs.setWordFactory(wf);
    }
 
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 100; i++) {
            switch (random.nextInt(5)) {
            case 0:
                cs.setFilterFactory(new CurvesRippleFilterFactory(cs
                        .getColorFactory()));
                break;
            case 1:
                cs.setFilterFactory(new MarbleRippleFilterFactory());
                break;
            case 2:
                cs.setFilterFactory(new DoubleRippleFilterFactory());
                break;
            case 3:
                cs.setFilterFactory(new WobbleRippleFilterFactory());
                break;
            case 4:
                cs.setFilterFactory(new DiffuseRippleFilterFactory());
                break;
            }
 
            OutputStream out = new FileOutputStream(new File(i + ".png"));
            String token = EncoderHelper.getChallangeAndWriteImage(cs, "png",
                    out);
            out.close();
            File f = new File(i+".png");
            f.renameTo(new File("checkdata/" + token +"_" + i+".png"));
            System.out.println(i+"验证码=" + token);
        }
    }
}

3.2 基于tf的神经网络训练代码

#coding:utf-8from gen_captcha import gen_captcha_text_and_imagefrom gen_captcha import numberfrom gen_captcha import alphabetfrom gen_captcha import ALPHABETimport numpy as npimport tensorflow as tfimport osos.environ["CUDA_VISIBLE_DEVICES"] = "0"text, image = gen_captcha_text_and_image()print("验证码图像channel:", image.shape)  # (70, 160, 3)# 图像大小IMAGE_HEIGHT = 70IMAGE_WIDTH = 70MAX_CAPTCHA = len(text)print("验证码文本最长字符数", MAX_CAPTCHA)   # 验证码最长4字符; 我全部固定为4,可以不固定. 如果验证码长度小于4,用'_'补齐# 把彩色图像转为灰度图像(色彩对识别验证码没有什么用)def convert2gray(img):    if len(img.shape) > 2:        gray = np.mean(img, -1)        # 上面的转法较快,正规转法如下        # r, g, b = img[:,:,0], img[:,:,1], img[:,:,2]        # gray = 0.2989 * r + 0.5870 * g + 0.1140 * b        return gray    else:        return img"""cnn在图像大小是2的倍数时性能最高, 如果你用的图像大小不是2的倍数,可以在图像边缘补无用像素。np.pad(image【,((2,3),(2,2)), 'constant', constant_values=(255,))  # 在图像上补2行,下补3行,左补2行,右补2行"""# 文本转向量# char_set = number + alphabet + ALPHABET + ['_']  # 如果验证码长度小于4, '_'用来补齐char_set = number + alphabet + ALPHABET # 如果验证码长度小于4, '_'用来补齐CHAR_SET_LEN = len(char_set) #26*2+10+1=63def text2vec(text):    text_len = len(text)    if text_len > MAX_CAPTCHA:        raise ValueError('验证码最长4个字符')    vector = np.zeros(MAX_CAPTCHA*CHAR_SET_LEN)    def char2pos(c):        if c =='_':            k = 62            return k        k = ord(c)-48        if k > 9:            k = ord(c) - 55            if k > 35:                k = ord(c) - 61                if k > 61:                    raise ValueError('No Map')        return k    for i, c in enumerate(text):        idx = i * CHAR_SET_LEN + char2pos(c)        vector[idx] = 1    return vector# 向量转回文本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)"""#向量(大小MAX_CAPTCHA*CHAR_SET_LEN)用0,1编码 每63个编码一个字符,这样顺利有,字符也有vec = text2vec("F5Sd")text = vec2text(vec)print(text)  # F5Sdvec = text2vec("SFd5")text = vec2text(vec)print(text)  # SFd5"""# 生成一个训练batchdef get_next_batch(batch_size=128, train = True):    batch_x = np.zeros([batch_size, IMAGE_HEIGHT*IMAGE_WIDTH])    batch_y = np.zeros([batch_size, MAX_CAPTCHA*CHAR_SET_LEN])    # 有时生成图像大小不是(70, 160, 3)    def wrap_gen_captcha_text_and_image(train):        while True:            text, image = gen_captcha_text_and_image(train)            if image.shape == (70, 70, 3):                return text, image    for i in range(batch_size):        text, image = wrap_gen_captcha_text_and_image(train)        image = convert2gray(image)        batch_x[i,:] = image.flatten() / 255 # (image.flatten()-128)/128  mean为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# 定义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)    # 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([9*9*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)    #out = tf.nn.softmax(out)    return out# 训练def train_crack_captcha_cnn():    output = crack_captcha_cnn()    # loss    #loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(output, Y))    with tf.device('/gpu:0'):        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=Y))            # 最后一层用来分类的softmax和sigmoid有什么不同?        # optimizer 为了加快训练 learning_rate应该开始大,然后慢慢衰        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.reduce_mean(tf.cast(correct_pred, tf.float32))    saver = tf.train.Saver()    config = tf.ConfigProto(allow_soft_placement=True)    config.gpu_options.allow_growth = True    with tf.Session(config=config) as sess:        sess.run(tf.global_variables_initializer())        step = 0        while True:            batch_x, batch_y = get_next_batch(256)            _, loss_ = sess.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.75})            # 每100 step计算一次准确率            if step % 100 == 0:                batch_x_test, batch_y_test = get_next_batch(100, False)                acc = sess.run(accuracy, feed_dict={X: batch_x_test, Y: batch_y_test, keep_prob: 1.})                print('step:%d,loss:%g' % (step, loss_))                print('step:%d,acc:%g'%(step, acc))                # 如果准确率大于50%,保存模型,完成训练                if acc > 0.98:                    saver.save(sess, "crack_capcha.model", global_step=step)                    break            step += 1def crack_captcha(captcha_image):    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)        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        return vec2text(vector)if __name__ == '__main__':    #text, image = gen_captcha_text_and_image()    #image = convert2gray(image)    #image = image.flatten() / 255    #predict_text = crack_captcha(image)    #print("正确: {}  预测: {}".format(text, predict_text))    train_crack_captcha_cnn()

在这里插入图片描述

4 最后

🧿 更多资料, 项目分享:

https://gitee.com/dancheng-senior/postgraduate

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/188056.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2022年12月 Scratch(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

Scratch等级考试(1~4级)全部真题・点这里 一、单选题(共25题,每题2分,共50分) 第1题 默认小猫角色和气球角色都是显示状态,小猫程序如下图所示,气球没有程序,点击绿旗,舞台上最终显示的效果是?( ) A:可能出现6个不同位置的小猫和6个小球 B:可能出现6个不同位…

RabbitMq使用与整合

MQ基本概念 MQ概述 MQ全称 Message Queue&#xff08;[kjuː]&#xff09;&#xff08;消息队列&#xff09;&#xff0c;是在消息的传输过程中保存消息的容器。多用于分布式系统之间进行通信。 &#xff08;队列是一种容器&#xff0c;用于存放数据的都是容器&#xff0c;存…

Leetcode—45.跳跃游戏II【中等】

2023每日刷题&#xff08;四十&#xff09; Leetcode—45.跳跃游戏II 贪心法思想 实现代码 #define MAX(a, b) (a > b ? (a) : (b))int jump(int* nums, int numsSize) {int start 0;int end 1;int ans 0;int maxStride 0;while(end < numsSize) {maxStride 0;fo…

2016年12月13日 Go生态洞察:2016年Go用户调查与企业问卷

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

以“防方视角”观文件上传功能

为方便您的阅读&#xff0c;可点击下方蓝色字体&#xff0c;进行跳转↓↓↓ 01 案例概述02 攻击路径03 防方思路 01 案例概述 这篇文章来自微信公众号“NearSec”&#xff0c;记录的某师傅接到一个hw项目&#xff0c;在充分授权的情况下&#xff0c;针对客户的系统进行渗透测试…

Java 简单解决 返回日期格式带 ‘T‘ 问题

问题描述 接口返回数据给前端时&#xff0c;返回的日期带 ‘T’ 解决方案&#xff1a; 在返回的实体类字段中&#xff0c;使用JsonFormat(pattern "yyyy-MM-dd",timezone"GMT8")格式化日期

springboot2.0 集成swagger3+Knife4j导出离线API 配置

springboot 版本2.3.1 一、集成swagger3 引入swagger依赖包 <!--swagger3集成--><dependency><groupId>org.springframework.plugin</groupId><artifactId>spring-plugin-core</artifactId><version>2.0.0.RELEASE</version>…

BGP联邦及路由反射器配置

需求 1 AS1存在两个环回&#xff0c;一个地址为192.168.1.0/24&#xff0c;该地址不能再任何协议中宣告 AS3存在两个环回&#xff0c;一个地址为192.168.2.0/24&#xff0c;该地址不能再任何协议中宣告 AS1还有一个环回地址为10.1.1.0/24&#xff0c;AS3另一个环回地址是11.1.1…

微服务 BFF 架构设计

文章目录 一、什么是 BFF二、典型的进程间微服务架构薄 BFF厚 BFF微服务模式对比 三、BFF 的问题四、BFF 的治理方向五、总结 在现代软件开发中&#xff0c;由于程序、团队、数据规模太大&#xff0c;需要把企业的业务能力进行复用&#xff0c;将领域服务剥离&#xff0c;提供通…

[PyTorch][chapter 66][强化学习-值函数近似]

前言 现实强化学习任务面临的状态空间往往是连续的,无穷多个。 这里主要针对这种连续的状态空间处理。后面DQN 也是这种处理思路。 目录&#xff1a; 1&#xff1a; 原理 2&#xff1a; 梯度更新 3&#xff1a; target 和 预测值 4 流程 一 原理 强化学习最重要的是得到 …

大中小协作 共筑科学梦——华中科技大学附属花城中学举办首届科技节

为普及科学知识&#xff0c;张扬科学精神&#xff0c;创设浓郁的科学氛围&#xff0c;11月24日&#xff0c;华中科技大学附属花城中学举办了以“走近科学&#xff0c;触碰未来”为主题的首届科技节暨科创文化展示周活动。学生们在学习中感受科技的魅力&#xff0c;在“玩”中感…

ChatGPT文章批量改写伪原创软件说明文档

大家好&#xff0c;我是淘小白~ 最近有很多朋友咨询&#xff0c;chatGPT文章改写插件和改写软件&#xff0c;这个软件之前已经做出来了&#xff0c;用的朋友不是很多&#xff0c;这几天有不少咨询的&#xff0c;现在把说明文档补一下&#xff0c;(#^.^#) 1、软件语言 Pytho…

初出茅庐的小李之C语言必备知识预处理

编译预处理 编译预处理就是在编译源代码之前进行的一系列处理&#xff0c;将源程序中的一些特殊命令进行展开或处理&#xff0c;生成扩展的源代码。这些特殊命令通常以“#”开头&#xff0c;占单独的行&#xff0c;语句尾部不需要加分号。 宏定义 (#define)是一种常见的编译…

Kotlin学习——流程控制,when,循环,range工具 kt里的equals if实现类似三元表达式的效果

Kotlin 是一门现代但已成熟的编程语言&#xff0c;旨在让开发人员更幸福快乐。 它简洁、安全、可与 Java 及其他语言互操作&#xff0c;并提供了多种方式在多个平台间复用代码&#xff0c;以实现高效编程。 https://play.kotlinlang.org/byExample/01_introduction/02_Functio…

张弛声音变现课,惊悚电影配音篇

在提供惊悚片的声音配音服务时&#xff0c;配音员旨在制造一种让观众的心率加快、情绪紧张的气氛。惊悚片侧重于心理层面的紧张和预期的恐怖&#xff0c;声音在塑造这种心理效应中起到了至关重要的作用。演员需通过对声音的精细雕琢和调整来强化电影的悬念和紧迫感。以下是为惊…

C语言SO EASY(ZZULIOJ1220: SO EASY)

题目描述 Superbin最近在研究初等数论&#xff0c;初等数论 是研究数的规律&#xff0c;特别是整数性质的数学分支。它是数论的一个最古老的分支。它以算术方法为主要研究方法&#xff0c;主要内容有整数的整除理论、同余理论、连分数理论和某些特殊不定方程。 是定义在正整数…

2017年2月16日 Go生态洞察:Go 1.8版本的革新

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

rocky8.9配置K8S集群kubernetes,centos同理

rocky8.9配置K8S集群 节点主机名IP地址mastertang1192.168.211.101node1tang2192.168.211.102node2tang3192.168.211.103 1&#xff09;准备工作 全部主机都配置静态ip vi /etc/sysconfig/network-scriptsTYPEEthernet PROXY_METHODnone BROWSER_ONLYno BOOTPROTOstatic DE…

华为ensp:单臂路由

通过单臂路由实现vlan之间的互通 将vlan和trunk配置好&#xff0c;我直接就在r1上演示单臂路由 我们要在r1的e0/0/0上面随便配置个ip&#xff0c;如果你不在接口上配置ip那就无法开启协议 R1 interface e0/0/0 进入真实接口随便配置个ip ip add 192.168.10.1 24 再进入子接…
最新文章