阿里云给的ocr示例不支持python3.0+吧?有没有3.9的示例代码?
python3的在这https://github.com/ALIBABAOCR/OCR_EXAMPLE #!/usr/bin/env python
import sys,os import base64 import json
from urllib.parse import urlparse import urllib.request import base64
ENCODING = 'utf-8'
def get_img_base64(img_file): with open(img_file, 'rb') as infile: s = infile.read() return base64.b64encode(s).decode(ENCODING)
def predict(url, appcode, img_base64, kv_configure): param = {} param['image'] = img_base64 if kv_configure is not None: param['configure'] = json.dumps(kv_configure) body = json.dumps(param) data = bytes(body, "utf-8")
headers = {'Authorization' : 'APPCODE %s' % appcode}
request = urllib.request.Request(url = url, headers = headers, data = data)
try:
response = urllib.request.urlopen(request, timeout = 10)
return response.code, response.headers, response.read()
except urllib.request.HTTPError as e:
return e.code, e.headers, e.read()
def demo(): appcode = '你的APPCODE' url = 'http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json' img_file = '图片文件路径/图片url' configure = {'side':'face'} #如果没有configure字段,configure设为None #configure = None
img_base64data = get_img_base64(img_file)
stat, header, content = predict( url, appcode, img_base64data, configure)
if stat != 200:
print('Http status code: ', stat)
print('Error msg in header: ', header['x-ca-error-message'] if 'x-ca-error-message' in header else '')
print('Error msg in body: ', content)
exit()
result_str = content
print(result_str.decode(ENCODING))
#result = json.loads(result_str)
if name == 'main': demo(),此回答整理自钉群“【官方】阿里云OCR公共云客户交流群”
阿里云OCR提供了Python 3.x版本的SDK,您可以在阿里云官网的文档中找到相应的示例代码和SDK包。以下是使用Python 3.9的示例代码,供您参考:
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import base64
client = AcsClient('<accessKeyId>', '<accessSecret>', '<regionId>')
request = CommonRequest()
request.set_domain('ocr.cn-hangzhou.aliyuncs.com')
request.set_version('2019-12-30')
request.set_product('ocr')
request.set_action_name('RecognizeBusinessCard')
image_path = '<image_path>'
# 图片转base64
with open(image_path, 'rb') as f:
image_bytes = f.read()
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
# 设置请求参数
request.add_query_param('ImageURL', '')
request.add_query_param('ImageContent', image_base64)
request.add_query_param('Side', 'face')
response = client.do_action_with_exception(request)
print(response.decode('utf-8'))
需要注意的是,您需要在代码中替换 <accessKeyId>
、<accessSecret>
、<regionId>
和 <image_path>
这些参数为您自己的阿里云账号信息和识别的图片路径。同时,您需要在运行代码之前先安装 aliyun-python-sdk-core
和 aliyun-python-sdk-ocr
这两个Python SDK包。您可以使用以下命令安装这两个包:
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-ocr
希望能对您有所帮助!
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。