ddddocr是Python第三方库,识别度高达99%
官方:https://pypi.org/project/ddddocr/ 安装库:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ddddocr
import requests
from ddddocr import DdddOcr
def get_code_from_img(img_path):
"""
获取图片上的验证码
:param img_path: 图片
:return: 返回验证码
"""
with open(img_path, 'rb') as f:
img_bytes = f.read()
ocr = DdddOcr()
code = ocr.classification(img_bytes)
return code
def get_code_from_url(img_url):
"""
通过url获取图片 保存在本地 再获取图片上的验证码
:param img_url: 获取图片的url
:return: 返回验证码
"""
img = requests.get(img_url)
with open("code.img", 'wb') as img_file:
img_file.write(img.content)
with open("code.img", 'rb') as f:
img_bytes = f.read()
ocr = DdddOcr()
code = ocr.classification(img_bytes)
return code