开发者社区 > 视觉智能 > 文字识别 > 正文

阿里云给的ocr示例不支持python3.0+吧?有没有3.9的示例代码?

阿里云给的ocr示例不支持python3.0+吧?有没有3.9的示例代码?

展开
收起
真的很搞笑 2023-04-26 23:23:42 126 0
2 条回答
写回答
取消 提交回答
  • python3的在这https://github.com/ALIBABAOCR/OCR_EXAMPLE #!/usr/bin/env python

    -- coding: utf-8 --

    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公共云客户交流群”

    2023-04-27 12:41:59
    赞同 展开评论 打赏
  • 阿里云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-corealiyun-python-sdk-ocr 这两个Python SDK包。您可以使用以下命令安装这两个包:

    pip install aliyun-python-sdk-core
    pip install aliyun-python-sdk-ocr
    

    希望能对您有所帮助!

    2023-04-27 08:03:42
    赞同 展开评论 打赏

文字识别技术可以灵活应用于证件文字识别、发票文字识别、文档识别与整理等行业场景,满足认证、鉴权、票据流转审核等业务需求。

热门讨论

热门文章

相关电子书

更多
阿里云智能-印刷文字识别OCR-产品介绍 立即下载
阿里巴巴读光OCR 立即下载
印刷文字识别算法设计与在线服务 立即下载