Python 腾讯云 OCR识别

网上找了下Python 做OCR识别的,如果是用本地识别,通常用 tesseract,
但是好像识别率不高,

找了下还是用腾讯云的 OCR 实现比较快,而且每个月有1000的免费额度,但是网上的示例不多,用Python的还是Python2.7,代码也都是拼接的乱七八糟, 花了点时间改成Python3的

Python 3.10

import hashlib, hmac, json, os, sys, time
import requests

from datetime import datetime


def heads():
    # 密钥参数
    secret_id = "AKIDI1SfDx6vqcAHtnG14JjQpC*****"
    secret_key = "OH3ZSLemhaOs6yGqRSJwPFwlY****"

    service = "ocr"
    host = "ocr.tencentcloudapi.com"
    endpoint = "https://" + host
    region = "ap-guangzhou"
    action = "GeneralBasicOCR"
    version = "2018-11-19"
    algorithm = "TC3-HMAC-SHA256"
    timestamp = int(time.time())
    # timestamp = 1551113065
    date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
    params = {
        "ImageUrl": "https://main.qcloudimg.com/raw/929bf9094ce2012473bcc4233a383e01.png"
    }

    # ************* 步骤 1:拼接规范请求串 *************
    http_request_method = "POST"
    canonical_uri = "/"
    canonical_querystring = ""
    ct = "application/json; charset=utf-8"
    payload = json.dumps(params)
    canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
    signed_headers = "content-type;host"
    hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
    canonical_request = (http_request_method + "\n" +
                         canonical_uri + "\n" +
                         canonical_querystring + "\n" +
                         canonical_headers + "\n" +
                         signed_headers + "\n" +
                         hashed_request_payload)
    print(canonical_request)

    # ************* 步骤 2:拼接待签名字符串 *************
    credential_scope = date + "/" + service + "/" + "tc3_request"
    hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
    string_to_sign = (algorithm + "\n" +
                      str(timestamp) + "\n" +
                      credential_scope + "\n" +
                      hashed_canonical_request)
    print(string_to_sign)

    # ************* 步骤 3:计算签名 *************
    # 计算签名摘要函数
    def sign(key, msg):
        return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()

    secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
    secret_service = sign(secret_date, service)
    secret_signing = sign(secret_service, "tc3_request")
    signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
    print(signature)

    # ************* 步骤 4:拼接 Authorization *************
    authorization = (algorithm + " " +
                     "Credential=" + secret_id + "/" + credential_scope + ", " +
                     "SignedHeaders=" + signed_headers + ", " +
                     "Signature=" + signature)
    print(authorization)

    print('curl -X POST ' + endpoint
          + ' -H "Authorization: ' + authorization + '"'
          + ' -H "Content-Type: application/json; charset=utf-8"'
          + ' -H "Host: ' + host + '"'
          + ' -H "X-TC-Action: ' + action + '"'
          + ' -H "X-TC-Timestamp: ' + str(timestamp) + '"'
          + ' -H "X-TC-Version: ' + version + '"'
          + ' -H "X-TC-Region: ' + region + '"'
          + " -d '" + payload + "'")
    return timestamp, authorization


def req():
    timestamp, authorization = heads()
    headers = {
        'Authorization': '%s' % authorization,
        'Content-Type': 'application/json; charset=utf-8',
        'Host': 'ocr.tencentcloudapi.com',
        'X-TC-Action': 'GeneralBasicOCR',
        'X-TC-Timestamp': '%s' % timestamp,
        'X-TC-Version': '2018-11-19',
        'X-TC-Region': 'ap-guangzhou',
    }

    data = '{"ImageUrl": "https://main.qcloudimg.com/raw/929bf9094ce2012473bcc4233a383e01.png"}'
    r = requests.post('https://ocr.tencentcloudapi.com', headers=headers, data=data)
    # json 输出
    status_code = r.status_code
    print(status_code)
    r_json = r.json()
    text_list = r_json["Response"]["TextDetections"]
    text_ret = map(lambda x: x["DetectedText"], text_list)
    ret = ''.join(text_ret)
    print(ret)

    # 字符串输出
    # responseinfo = r.content
    # data = responseinfo.decode('utf-8')
    # print(data)


req()

Base64 图片上传

import hashlib, hmac, json, os, sys, time
import requests

from datetime import datetime


def heads(image_path):
    # 密钥参数
    secret_id = "AKIDI1SfDx6vqcAHtnG14JjQpC0p******"
    secret_key = "OH3ZSLemhaOs6yGqRSJwPFwlY*****"

    service = "ocr"
    host = "ocr.tencentcloudapi.com"
    endpoint = "https://" + host
    region = "ap-guangzhou"
    action = "GeneralBasicOCR"
    version = "2018-11-19"
    algorithm = "TC3-HMAC-SHA256"
    timestamp = int(time.time())
    date = datetime.utcfromtimestamp(timestamp).strftime("%Y-%m-%d")
    # 图片编码
    image_base64 = base64(image_path)
    params = {
        "ImageBase64": image_base64
    }

    # ************* 步骤 1:拼接规范请求串 *************
    http_request_method = "POST"
    canonical_uri = "/"
    canonical_querystring = ""
    ct = "application/json; charset=utf-8"
    payload = json.dumps(params)
    canonical_headers = "content-type:%s\nhost:%s\n" % (ct, host)
    signed_headers = "content-type;host"
    hashed_request_payload = hashlib.sha256(payload.encode("utf-8")).hexdigest()
    canonical_request = (http_request_method + "\n" +
                         canonical_uri + "\n" +
                         canonical_querystring + "\n" +
                         canonical_headers + "\n" +
                         signed_headers + "\n" +
                         hashed_request_payload)
    print(canonical_request)

    # ************* 步骤 2:拼接待签名字符串 *************
    credential_scope = date + "/" + service + "/" + "tc3_request"
    hashed_canonical_request = hashlib.sha256(canonical_request.encode("utf-8")).hexdigest()
    string_to_sign = (algorithm + "\n" +
                      str(timestamp) + "\n" +
                      credential_scope + "\n" +
                      hashed_canonical_request)
    print(string_to_sign)

    # ************* 步骤 3:计算签名 *************
    # 计算签名摘要函数
    def sign(key, msg):
        return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()

    secret_date = sign(("TC3" + secret_key).encode("utf-8"), date)
    secret_service = sign(secret_date, service)
    secret_signing = sign(secret_service, "tc3_request")
    signature = hmac.new(secret_signing, string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()
    print(signature)

    # ************* 步骤 4:拼接 Authorization *************
    authorization = (algorithm + " " +
                     "Credential=" + secret_id + "/" + credential_scope + ", " +
                     "SignedHeaders=" + signed_headers + ", " +
                     "Signature=" + signature)
    print(authorization)

    print('curl -X POST ' + endpoint
          + ' -H "Authorization: ' + authorization + '"'
          + ' -H "Content-Type: application/json; charset=utf-8"'
          + ' -H "Host: ' + host + '"'
          + ' -H "X-TC-Action: ' + action + '"'
          + ' -H "X-TC-Timestamp: ' + str(timestamp) + '"'
          + ' -H "X-TC-Version: ' + version + '"'
          + ' -H "X-TC-Region: ' + region + '"'
          + " -d '" + payload + "'")
    return timestamp, authorization, payload


def req(image_path):
    timestamp, authorization, data = heads(image_path)
    headers = {
        'Authorization': '%s' % authorization,
        'Content-Type': 'application/json; charset=utf-8',
        'Host': 'ocr.tencentcloudapi.com',
        'X-TC-Action': 'GeneralBasicOCR',
        'X-TC-Timestamp': '%s' % timestamp,
        'X-TC-Version': '2018-11-19',
        'X-TC-Region': 'ap-guangzhou',
    }

    # data = '{"ImageUrl": "https://main.qcloudimg.com/raw/929bf9094ce2012473bcc4233a383e01.png"}'
    r = requests.post('https://ocr.tencentcloudapi.com', headers=headers, data=data)
    # json 输出
    status_code = r.status_code
    print(status_code)
    r_json = r.json()
    print("json:", r_json)
    text_list = r_json["Response"]["TextDetections"]
    text_ret = map(lambda x: x["DetectedText"], text_list)
    ret = ''.join(text_ret)
    print(ret)

    # 字符串输出
    # responseinfo = r.content
    # data = responseinfo.decode('utf-8')
    # print(data)


def base64(file_path):
    import base64
    # file_path = '1.jpg'
    file = open(file_path, 'rb')
    encoded = base64.b64encode(file.read()).decode()
    return "data:image/png;base64," + encoded


req("test_ocr.png")

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值