python实现百度ocr接口进行验证码识别

运行环境:

Python3.11       

Windows10操作系统 

baidu-aip 4.16.11

from aip import AipOcr

def get_file_content(filePath):
    """
    输入图片路径,返回图片的二进制码
    :param filePath: 图片路径
    :return:图片的二进制码
    """
    with open(filePath, 'rb') as fp:
        return fp.read()


def init():
    """
    初始化,构造ocr对象
    :return:
    """
    # 从百度ocr申请的ID、Key和Secret_key
    APP_ID = ''
    API_KEY = ''
    SECRET_KEY = ''
    # 构造对象
    return AipOcr(APP_ID, API_KEY, SECRET_KEY)


def verify_cap(image_path):
    """
    获取验证码
    :param image_path: 验证码图片路径
    """
    client = init()
    # 获取二进制码
    image_byte = get_file_content(image_path)
    # 进行识别,返回一个dict
    result = client.basicGeneral(image_byte)
    # 输出返回值字典
    for key in result.items():
        print(key)

    captcha = ''
    # 根据识别出的结果数量判断
    if result['words_result_num'] == 0:
        print('未识别出验证码!')
    else:
        # 存在多个返回值,则进行拼接
        for key in result['words_result']:
            captcha = captcha + key["words"]
        # 去除验证码中的空格
        captcha = captcha.replace(' ', '')
    print('验证码:', captcha)

    return captcha


print(verify_cap(r'C:\Users\Administrator\Desktop\yzm1.png'))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值