03-12306验证码文字 识别

将切割后的12306验证码,文字部分传入 函数,函数调用百度文字识别接口,识别12306图片中的文字。

# utf-8 __*__
import base64
import numpy as np
import cv2 as cv
import urllib.parse
import requests
import json

imgF  = 'F:\\python\\data\\title.png'
API_key = '********'  #从百度云中心获取
Secret_key = '**********' # 从百度云中心获取
get_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' + 'client_id=' + API_key + '&client_secret=' + Secret_key


def getORC_words(imgF, API_key, Secret_key):
    """
    传入带 文字的图片,调用 百度文字识别API借口,识别图片中的问题并返回识别出来的文字
    :param imgF:
    :param API_key:
    :param Secret_key:
    :return:
    """
    data = {
        'image_type': 'BASE64',
        'image': '',
        'group_id': 'gropu001',
        'user_id': '0001'
    }

    # 打开图片文件,对文件进行BASE64转码
    img_file = open(imgF, 'rb')
    img_b664encode = base64.b64encode(img_file.read())
    img_file.close()
    data['image'] = img_b664encode

    # 合成token获取URL,获取token并合成文字识别 请求API链接
    get_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' + 'client_id=' + API_key + '&client_secret=' + Secret_key
    header = {'Content-Type': 'application/x-www-form-urlencoded'}
    r = requests.post(get_token_url)
    orc_url = ' https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=' + json.loads(r.text)['access_token']

    # 识别文字,并返回结果
    orc = requests.post(orc_url, headers=header, data=data)
    try:
        words = json.loads(orc.text)['words_result'][0]['words']
        return words
    except Exception as e:
        print(e)
        return -1

print(getORC_words(imgF, API_key, Secret_key))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值