tencent.py 2.0 KB

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