加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ocr.py 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
Famio 提交于 2020-08-19 18:22 . 开源释出
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
# OCR识别封装
class OCR(object):
def img_to_excel(self,
output_file_name,
image_path,
secret_id,
secret_key):
# 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
cred = credential.Credential(
secret_id,
secret_key
)
# 实例化client对象
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
clientProfile.signMethod = "TC3-HMAC-SHA256"
client = ocr_client.OcrClient(cred, "ap-shanghai", clientProfile)
# 实例化一个请求对象
req = models.GeneralFastOCRRequest()
# 读取图片数据,使用Base64编码
with open(image_path, 'rb') as f:
image = f.read()
image_base64 = str(base64.b64encode(image), encoding='utf-8')
req.ImageBase64 = image_base64
# 通过client对象调用访问接口,传入请求对象
resp = client.TableOCR(req)
# 获取返回数据(Data为Base64编码后的Excel数据)
data = resp.Data
# 转换为Excel
output_file_name = str(output_file_name)
path_excel = output_file_name+".xlsx"
with open(path_excel, 'wb') as f:
f.write(base64.b64decode(data))
return path_excel
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化