当前位置:   article > 正文

ddddocr验证码识别-java&nodejs&python_nodejs ddddocr

nodejs ddddocr
1. java版本
参考 https://gitee.com/GCSZHN/ddddocr-for-java?_from=gitee_search java 11
改造使用 jdk1.8 
https://gitee.com/bestman_456/ddddocr4j
  • 1
  • 2
  • 3

引入依赖

 <dependency>
    <groupId>com.yl</groupId>
    <artifactId>ddddocr4j</artifactId>
    <version>1.0.0</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

DDDDOcrUtil

package com.yl.util;

import com.yl.ddddocr.OCREngine;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * DDDDOcrUtil
 *
 * @author liuxb
 * @date 2023/4/28 0:09
 */
@Slf4j
public class DDDDOcrUtil {
    private static final OCREngine ocrEngine = OCREngine.instance();

    /**
     * 获取识别的验证码
     *
     * @param base64 验证码base64字符串
     * @return
     */
    public static String getCode(String base64) {
        byte[] bytes = Base64.getDecoder().decode(base64);
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        try {
            BufferedImage bufferedImage = ImageIO.read(bis);
            return ocrEngine.recognize(bufferedImage);
        } catch (IOException e) {
            log.error("识别验证码异常", e);
            return StringUtils.EMPTY;
        }
    }

    /**
     * 获取识别的验证码
     *
     * @param inputStream 验证码图片输入流
     * @return
     */
    public static String getCode(InputStream inputStream) {
        try {
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            return ocrEngine.recognize(bufferedImage);
        } catch (IOException e) {
            log.error("识别验证码异常", e);
            return StringUtils.EMPTY;
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
2. nodejs版本
安装包
npm install ddddocr
安装的包,报错,需要 修改 ddddocr/dist/index.js 最后一行为  module.exports = DdddOcr;

测试
const Ddddocr  = require('ddddocr')

var fs = require('fs') //引入fs模块
var path = require('path')


var imgDir = "D:/verifies"

var array = fs.readdirSync(imgDir)
array.forEach(item => {
    var file = path.join(imgDir, item)

    Ddddocr.create().then(async ddddocr => {
		// 也可传入 文件路径	
		const verifyCode = await ddddocr.classification(file);
		console.log(file, verifyCode)
	})

})

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
3. Python版本
pip install ddddocr

import os

import ddddocr

# common_old.onnx,  beta=True common.onnx
ocr = ddddocr.DdddOcr()

# dirPath = r'D:\upload\img'
dirPath = r'D:\verifies'
for ff in os.listdir(dirPath):
    path = os.path.join(dirPath, ff)
    with open(path, 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    print('路径:' + path + ', 识别出的验证码为:' + res)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/539460
推荐阅读
相关标签
  

闽ICP备14008679号