# -*- coding: utf-8 -*- import base64 from tencentcloud.common import credential from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException # 导入对应产品模块的client models。 from tencentcloud.ecc.v20181213 import ecc_client, models from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile def tencent_ecc(img_mode): img, mode = img_mode try: # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey cred = credential.Credential("AKIDBPOs761NcaK3YxrxveEXaOIkyxhCqdT2", "mIabM1QhhiU1IIJ6dtEWdoKaJM8C1f5D") # 实例化一个http选项,可选的,没有特殊需求可以跳过。 httpProfile = HttpProfile() httpProfile.reqMethod = "POST" # post请求(默认为post请求) # httpProfile.reqTimeout = 90 # 请求超时时间,单位为秒(默认60秒) httpProfile.endpoint = "ecc.tencentcloudapi.com" # 指定接入地域域名(默认就近接入) httpProfile.keepAlive = True # 实例化一个client选项,可选的,没有特殊需求可以跳过。 clientProfile = ClientProfile() clientProfile.signMethod = "TC3-HMAC-SHA256" # 指定签名算法(默认为HmacSHA256) # clientProfile.unsignedPayload = True clientProfile.httpProfile = httpProfile client = ecc_client.EccClient(cred, "", clientProfile) req = models.EHOCRRequest() # req.Grade = "grade9" req.InputType = 1 req.ServerType = 1 # 图片批改 0:OCR结果 req.Image = img if mode == "file": req.Image = base64.b64encode(open(img, 'rb').read()).rstrip().decode('utf-8') resp = client.EHOCR(req) # 输出json格式的字符串回包 return resp.to_json_string() except TencentCloudSDKException as err: print("作文批改接口报错:{}".format(err)) return None