OCR验证码识别

1. java生成验证码

1.1 使用谷歌的验证码包

    <!-- https://mvnrepository.com/artifact/com.github.penggle/kaptcha -->
    <dependency>
      <groupId>com.github.penggle</groupId>
      <artifactId>kaptcha</artifactId>
      <version>2.3.2</version>
    </dependency>

1.2 生成验证码方法

/**
   * 创建图片
   * @author  heliming
   * @date    2023/8/29-16:29
   * @throws IOException
   */
  public static void createImg() throws IOException {
    Properties properties = new Properties();
    //  是否有边框
    properties.put("kaptcha.border", "no");
    //  设置文字颜色
    properties.put("kaptcha.textproducer.font.color", "black");
    //  字体大小,默认40
    properties.setProperty("kaptcha.textproducer.font.size", "40");
    //  字符长度,默认为5
    properties.setProperty("kaptcha.textproducer.char.length", "5");
    //  字符间距 默认为2
    properties.setProperty("kaptcha.textproducer.char.space", "4");
    // 干扰 颜色
    properties.setProperty("kaptcha.noise.color", "black");
    //  验证码图片宽度 默认为200
    properties.setProperty("kaptcha.image.width", "160");
    //  验证码图片高度 默认为40
    properties.setProperty("kaptcha.image.height", "50");
    //  图片样式
    properties.setProperty("kaptcha.obscurificator.impl",
        "com.google.code.kaptcha.impl.WaterRipple");
    //  背景颜色渐变,开始颜色
    properties.setProperty("kaptcha.background.clear.from", "white");
    //  背景颜色渐变,结束颜色
    properties.setProperty("kaptcha.background.clear.to", "white");

    Config config = new Config(properties);
    DefaultKaptcha defaultkaptcha = new DefaultKaptcha();
    defaultkaptcha.setConfig(config);
    BufferedImage image = defaultkaptcha.createImage("bupy");
    ImageIO.write(image, "jpg", new File("F:\\demo1\\aa.jpg"));
  }
  
public static void main(String[] args) throws TesseractException, IOException {

    createImg();

  }

1.3 生成后的验证码

2. python识别验证码

查看python版本,我用的是Python 3.11.4

python -V

查看pip版本,我用的是23.2.1

pip -V

2.1 安装ddddocr

pip3 install ddddocr

2.2 有些时候需要升级pip

pip install --upgrade pip

2.3 执行ocrtest2.py脚本

import ddddocr
 
ocr = ddddocr.DdddOcr()
 
with open('F:/demo1/aa.jpg', 'rb') as f:
    img_bytes = f.read()
 
res = ocr.classification(img_bytes)
print(res)

2.4 如果执行失败 报错,python 属性错误:模块“PIL.Image”没有属性“ANTIALIAS”

是Pillow10.0没这个方法了,推荐改为9.5版本

pip uninstall Pillow
pip install Pillow==9.5.0

执行python .\ocrtest2.py

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章