腾讯OCR接口调用——python

相较于百度的接口,腾讯的稍微麻烦一点。首先是旧版的,这个是从别人那儿整的,忘了是那个博客了,作者连应用ID和Key都写了出来:

// An highlighted block
import base64
import hashlib
import time
import uuid
from urllib import parse

import requests

APP_ID = "2115425241"
APP_KEY = "IS1oUQtC7Vhy91YZ"
OCR = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr'


def __get_sign(params):
    # 参数升序
    param_list = list(params.keys())
    param_list.sort()
    param_str = ''
    for param in param_list:
        # 空值不参与签名
        value = params[param]
        if value:
            if param_str:
                param_str += '&'
            #    对 value进行编码
            parse_quote = list(parse.quote_plus(str(value)))
            i = 0
            for a in parse_quote:
                if a == '%':
                    parse_quote[i + 1] = str(parse_quote[i + 1]).upper()
                    parse_quote[i + 2] = str(parse_quote[i + 2]).upper()
                i += 1
            param_str += str(param) + '=' + (''.join(parse_quote))

    key_ = ("%s&app_key=%s" % (param_str, APP_KEY)).encode(encoding='utf-8')
    return hashlib.md5(key_).hexdigest().upper()


def getWork(image_path):
    params = {
        'app_id': APP_ID,
        'time_stamp': int(time.time()),
        'nonce_str': str(uuid.uuid4()).replace('-', '')[0:31]
    }
    with open(image_path, 'rb') as f:
        params['image'] = str(base64.b64encode(f.read()), 'utf-8').replace(r'\r\n', '')
    params['sign'] = __get_sign(params)


    result = requests.post(OCR, headers={"Content-Type": "application/x-www-form-urlencoded"}, data=params).json()
    print(result)
    if result['ret'] == 0:
        item_list = []
        for item in result['data']['item_list']:
            print(item['itemstring'])
            item_list.append(item['itemstring'])
        return item_list
    else:
        print('系统忙:', result['msg'])
        return result['msg']


if __name__ == '__main__':
    import glob
    import os
    path_pic='pic'
    path_save='tengxun_OCR'
    if not os.path.exists(path_save):
        os.mkdir(path_save)
    extensions=['jpg', 'JPG', 'jpeg', 'JPEG']
    for extension in extensions:
        for path in glob.glob(os.path.join(path_pic, '*.'+extension)):
            A=getWork(path)
            path_txt = path_save +'/' + path.split('/')[-1].split('.')[0]+ '.txt'
            with open(path_txt, 'w+') as file:
                for item in A:
                    file.write(item)
                    file.write('\n')

        # print('1')

另外一个就是调用的新版的通用识别,是改的官方代码,第20行改地区,22行和36行改接口模块。具体的腾讯相应的API接口那儿有文档说明。

#pip install tencentcloud-sdk-python

from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.ocr.v20181119 import ocr_client, models
import base64
import json
SecretId='xxxxxxxx'#自己去腾讯云上看,需要实名认证
SecretKey='xxxxxxxxxx'
def get_json(path):
    try:
        cred = credential.Credential(SecretId, SecretKey) #密钥
        httpProfile = HttpProfile()
        httpProfile.endpoint = "ocr.tencentcloudapi.com"

        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = ocr_client.OcrClient(cred, "ap-shanghai", clientProfile)

        req = models.GeneralAccurateOCRRequest()#这个通用还是高精度自己看着来
        #对本地图片进行base64转码【本地图片解析需要先转成base64编码】
        with open(path, 'rb') as f:
            # base64_data = base64.b64encode(f.read())
            # s = base64_data.decode()
            # ImageBase64_value = 'data:image/jpeg;base64,%s'%s
            # #params是字符串,以下进行拼接
            # params = '{"ImageBase64":"' + ImageBase64_value + '"}' #以图片Base64编码发送请求

            base64data = base64.b64encode(f.read())  # 得到 byte 编码的数据
            base64data = str(base64data, 'utf-8')  # 重新编码数据
            params = '{"ImageBase64":"' + base64data + '"}'

        req.from_json_string(params)
        resp = client.GeneralAccurateOCR(req)#GeneralAccurateOCR#GeneralBasicOCR#高精度和通用
        # A=resp.TextDetections
        # for line in A:
        #     print(line.DetectedText)
        # resp = resp.to_json_string()
        return resp
    except TencentCloudSDKException as err:
        print(err)



if __name__ == '__main__':
    import glob
    import os
    path_pic='pic'
    path_save='tengxun_OCR_23'
    if not os.path.exists(path_save):
        os.mkdir(path_save)
    extensions=['jpg', 'JPG', 'jpeg', 'JPEG']
    for extension in extensions:
        for path in glob.glob(os.path.join(path_pic, '*.'+extension)):
            path_txt = path_save + '/' + path.split('/')[-1].split('.')[0] + '.txt'
            resp = get_json(path)
            results = resp.TextDetections
            print(results)
            with open(path_txt,'w') as file:
                for line in results:
                    file.write(line.DetectedText)
                    file.write('\n')

我这也有点奇怪了,之前跑得好好的,现在返回接口返回值是None,base64编码不知道为啥出问题了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 。 下面是一个简单的腾讯云OCR API接口调用python代码的示例: ``` import requests import json def ocr_tencent(image_path): # API的请求地址 url = "https://ocr.tencentcloudapi.com/" # API的请求参数 params = { "Action": "OCR", "Version": "2018-11-19", "Region": "ap-guangzhou", "SecretId": "YOUR_SECRET_ID", "Timestamp": int(time.time()), "Nonce": int(time.time()), "SignatureMethod": "HmacSHA1", "ImageUrl": image_path } # 计算请求签名 sign = calculate_signature(params, "YOUR_SECRET_KEY") params["Signature"] = sign # 发送请求 response = requests.get(url, params=params) # 解析响应 result = json.loads(response.text) return result ``` 请注意,在上面的代码中,您需要替换`YOUR_SECRET_ID`和`YOUR_SECRET_KEY`为您的腾讯云帐户的密钥。 ### 回答2: 当然可以帮您写腾讯云OCR模型API接口调用Python代码!下面是一个简单的示例: ```python import requests import base64 # 设置请求地址和API密钥 url = "https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr" app_key = "你的APP_KEY" app_id = "你的APP_ID" # 读取要识别的图片文件 with open("要识别的图片.jpg", "rb") as file: image_data = file.read() base64_image = base64.b64encode(image_data).decode("utf-8") # 构造请求参数 data = { "app_id": app_id, "app_key": app_key, "image": base64_image, } # 发送POST请求 response = requests.post(url, data=data) # 处理响应结果 if response.status_code == 200: res_json = response.json() if res_json["ret"] == 0: for item in res_json["data"]["item_list"]: print(item["itemstring"]) else: print("OCR识别失败,错误码:%s,错误信息:%s" % (res_json["ret"], res_json["msg"])) else: print("请求失败,响应码:%s" % response.status_code) ``` 上述代码示例中,首先需要设置请求地址和API密钥,即`url`、`app_key`和`app_id`。接着,读取要识别的图片文件并通过`base64`编码转化为base64格式。然后,构造请求参数,其中`app_id`、`app_key`和`image`是必须的字段,`image`字段为转化后的base64编码。最后,发送POST请求并处理响应结果。如果识别成功,可以通过`res_json["data"]["item_list"]`获取到识别结果列表,并输出每个识别结果;如果识别失败,可以通过`res_json["ret"]`和`res_json["msg"]`获取错误码和错误信息。 请注意替换示例中的`你的APP_KEY`、`你的APP_ID`和`要识别的图片.jpg`为您自己的实际值和文件路径。 希望这段代码能帮到您!如果还有其他问题,请随时追问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值