前言:
此时大家对“python执行不了baidu aip”大体比较讲究,朋友们都想要剖析一些“python执行不了baidu aip”的相关资讯。那么小编在网摘上搜集了一些对于“python执行不了baidu aip””的相关文章,希望咱们能喜欢,朋友们快快来了解一下吧!import urllibimport redisimport requestsAPP_ID = '*******'API_KEY = '**************'SECRET_KEY = '**************'pool_redis = redis.ConnectionPool(host='127.0.0.1', port=6379, decode_responses=True)redis_client = redis.StrictRedis(connection_pool=pool_redis)def orc_look(base64_ima): data = { 'image': base64_ima } headers = { 'Content-Type': 'application/x-www-form-urlencoded' } url = "" + get_access_token() r = requests.post(url, params=headers, data=data).json() # {'log_id': 4298270195334760861, 'words_result_num': 1, 'words_result': [{'words': '24518'}]} words = r['words_result'][0]['words'] return words# 获取百度的access_token,缓存在redis里面,不用每次都取新的def get_access_token(): redis_key = 'baiduCloud:ocr_access_token' access_token = redis_client.get(redis_key) if not access_token: # client_id 为官网获取的AK, client_secret 为官网获取的SK host = f'{API_KEY}&client_secret={SECRET_KEY}' request = urllib.request.Request(host) request.add_header('Content-Type', 'application/json; charset=UTF-8') response = urllib.request.urlopen(request) content = response.read() content = eval(content) access_token = content['access_token'] # print(type(content)) # print(content) redis_client.setex(redis_key, content['expires_in'], access_token) return access_token# 注意需要去掉 data:image/bmp;base64, 这样的字符base64_text = """这里是base64编码的图片,自己替换"""print(orc_look(base64_text))
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #python执行不了baidu aip