潜江市网站建设_网站建设公司_域名注册_seo优化
2025/12/26 19:50:17 网站建设 项目流程

python识别图片验证码,现在用得比较多,这是主流OCR识别对比如下:

下面就针对ddddocr进行使用

直接上代码:

import ddddocr def pngresult(): data_map = {} try: with open(f"doc/pngtext.txt", 'r') as file: data_map = {line.strip().split('=', 1)[0]: line.strip().split('=', 1)[1] for line in file if line.strip() and '=' in line} except FileNotFoundError: print("文件 doc/pngtext.txt 不存在") return {} return data_map def ocr1(): ocr = ddddocr.DdddOcr() with open(f"doc/images/1.png", 'rb') as f: img_bytes = f.read() res = ocr.classification(img_bytes) print(res) def ocrbatch(): ocr = ddddocr.DdddOcr() # 移除分号,只创建一次实例 data_result = pngresult() true_num = 0 for i in range(1, 501): filename = f"doc/images/{i}.png" try: with open(filename, 'rb') as f: img_bytes = f.read() res = ocr.classification(img_bytes) # 修复键名格式,应该是 "i.png" 而不是 "i+.png" expected_key = f"{i}.png" is_true=res.upper() == data_result.get(expected_key, "").upper() if is_true: true_num += 1 print(f"{i}//识别结果:{res.upper()},正确验证码为{data_result.get(expected_key, "").upper()}//识别结果:{is_true}") except FileNotFoundError: print(f"文件 {filename} 不存在") if true_num > 0: print(f"正确率:{true_num/500:.2%}") else: print("正确率:0%") # 执行下载 if __name__ == "__main__": # ocr1() ocrbatch()

识别图片如下:

各种验证码都有,总共500张,测试结果如下:

完整项目下载地址http://www.zrscsoft.com/sitepic/12159.html

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询