社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
pip install baidu-aip
注册百度账号,百度云管理中心创建应用,生成AppID、App Key、Secret Key
参数:
APP_ID: 必须参数,固定为AppID;
API_KEY: 必须参数,应用的API Key;
SECRET_KEY: 必须参数,应用的Secret Key
# coding=utf-8
# Author: Allan
import os
from PIL import Image
from aip import AipOcr
def get_image(path, filename, img, positions=None):
#通过Image处理图像
im = Image.open(os.path.join(path, filename))
im = im.crop(positions)
im.save(os.path.join(path, img))
return 'PASS'
def GetCaptchaV(path, filename):
#获取验证码
APP_ID = 'xxx'
API_KEY = 'yyy'
SECRET_KEY = 'zzz'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(path, filename):
filename = os.path.join(path, filename)
with open(filename, 'rb') as fp:
return fp.read()
image = get_file_content(path, filename)
""" 调用通用文字识别, 图片参数为本地图片 """
ret = client.basicGeneral(image)
code = ret["words_result"][0]["words"]
print("Verify Code:",code)
# 去掉空格
if ' ' in code:
code = code.replace(' ', '')
return code
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!