• 下载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)
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐