1. 安装模块
pip install ddddocr -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
2. 编写代码
import ddddocr # 导入orc模块
import logging # 导入日志
logging.getLogger().setLevel(logging.INFO) # 设置日志级别
def ComputeCode(path):try:logging.info("数字字母验证码图片识别路径: %s", str(path))ocr = ddddocr.DdddOcr() # 创建ocr对象with open(path, 'rb') as f: # 读取图片二进制字节img_bytes = f.read()res = ocr.classification(img_bytes) # 识别图片logging.info("数字字母验证码识别结果: %s", str(res))return res # 返回结果except Exception as e:# 异常处理logging.exception("异常信息: %s", str(e))return None
ComputeCode("ocr.png")
注意:图片路径和命名不要含有中文路径,否则出现未知错误