讯飞和百度文字识别接口(基于python)

  • 下载baidu-aip接口
from aip import AipOcr
def baiduOCR(picfile):#百度api
        """利用百度api识别文本,并保存提取的文字
        picfile:    图片文件名
        outfile:    输出文件
        """
        APP_ID = 'XXX'  # 刚才获取的 ID,下同
        API_KEY = 'XXXCh0ZmWe6o1'
        SECRECT_KEY = 'XXXXXXXX5UqbZElQ3lrp7Nu20OLtwNiI'


        # APP_ID, API_KEY, SECRECT_KEY = account
        client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)
        text_list = []
        time_out = 0
        while time_out < 5:
            try:

                message = client.basicGeneral(picfile)  # 通用文字识别
                # elif flag == 1:
                #     message = client.basicAccurate(picfile)  # 通用文字高精度识别,
                break
            except:
                time_out += 1
        if time_out == 5:
            print('识别出错,程序退出')
            return '网络错误'
        try:
            for text in message['words_result']:
                text = text['words']

                print(text)
                text_list.append(text)

        except KeyError or TypeError:
            if message['error_code'] == 17:
                return 'limit'
            elif message['error_code'] == 18:

                return 'qps'
def xunfeiOCR(picfile):#讯飞接口
  

    # from urllib import parse
    # 印刷文字识别 webapi 接口地址
    # URL = "http://webapi.xfyun.cn/v1/service/v1/ocr/general"
    URL = "https://webapi.xfyun.cn/v1/service/v1/ocr/handwriting"  # 手写体识别12.11过期
    # 应用ID (必须为webapi类型应用,并印刷文字识别服务,参考帖子如何创建一个webapi应用:http://bbs.xfyun.cn/forum.php?mod=viewthread&tid=36481)
    APPID = "xxxxxf46"
    # 接口密钥(webapi类型应用开通印刷文字识别服务后,控制台--我的应用---印刷文字识别---服务的apikey)
    API_KEY = "xxxxxxef7879e"

    def getHeader():
        #  当前时间戳
        curTime = str(int(time.time()))
        #  支持语言类型和是否开启位置定位(默认否)
        param = {"language": "cn|en", "location": "false"}
        param = js.dumps(param)
        paramBase64 = base64.b64encode(param.encode('utf-8'))

        m2 = hashlib.md5()
        str1 = API_KEY + curTime + str(paramBase64, 'utf-8')
        m2.update(str1.encode('utf-8'))
        checkSum = m2.hexdigest()
        # 组装http请求头
        header = {
            'X-CurTime': curTime,
            'X-Param': paramBase64,
            'X-Appid': APPID,
            'X-CheckSum': checkSum,
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
        }
        return header

    # 上传文件并进行base64位编码
    # with open(r'E:\9-19\图像出题\4.jpg', 'rb') as f:
    #     f1 = f.read()

    f1_base64 = str(base64.b64encode(picfile), 'utf-8')

    data = {
        'image': f1_base64
    }
    try:
        r = requests.post(URL, data=data, headers=getHeader())
        result = js.loads(str(r.content, 'utf-8'))
    except:
        time.sleep(5)
        r = requests.post(URL, data=data, headers=getHeader())
        result = js.loads(str(r.content, 'utf-8'))
    # 错误码链接:https://www.xfyun.cn/document/error-code (code返回错误码时必看)

    text_list = []
    # text = self.reader.readtext(picfile, detail=0)
    # text = text[0]
    if result['data']['block'][0]['line']:
        text = result['data']['block'][0]['line'][0]['word'][0]['content']

        print(text)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值