日日行,不怕千万里;常常做,不怕千万事|

__username

园龄:2年5个月粉丝:12关注:2

📂python
🔖Python
2023-08-04 09:05阅读: 24评论: 0推荐: 0

识别图片验证码

一个demo

import ddddocr
ocr = ddddocr.DdddOcr()
# 简单的图片数字英文识别
with open('code.jpg', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
print(res)
# 其它写法
ocr = ddddocr.DdddOcr()
with open('code.png', 'rb') as f:
img_bytes = f.read()
res = ocr.classification(img_bytes)
logger.info(f'验证码识别结果-----> {res}')
# if len(res) != 4 or re.search(r'[\u4e00-\u9fff]', res):
# logger.warning('验证码识别结果-----> 长度不符 或者 含有中文 失败')
# return None
if not re.match(r'^[A-Za-z0-9]{4}$', res):
logger.warning('验证码识别结果-----> 长度不符或者含有非法字符')
return None

可能出现的问题解决

pip install Pillow==9.5.0

demo2 2024.1.11更新

import os
import ddddocr
# 图片识别函数
def img2text(img_file):
ocr = ddddocr.DdddOcr() # 法1
# ocr = ddddocr.DdddOcr(det=True) #法2
with open(img_file, 'rb') as f:
img_bytes = f.read()
# 获取文字
res = ocr.classification(img_bytes)
# print('res:', res)
return res
if __name__ == '__main__':
jpg_ch_file = "captchaWord.png"
res = img2text(jpg_ch_file)
print(res)
posted @   __username  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起