self.client.FlightInvoiceOCR(req)
https://cloud.tencent.com/document/product/1131/42337
ImageBase64
base64.b64encode
TC3-HMAC-SHA256
作为APIcaller识别发票信息
https://blog.youkuaiyun.com/weixin_41552148/article/details/102741282
tencentcloud-sdk-python
主要使用的核心库,集中了最主要的命令
https://cloud.tencent.com/document/sdk/Python
#安装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
#导入PIL的Image,打开图片,压缩图片用
from PIL import Image
#获取目录下的文件,以及改名必备
import os
#图片转base64,必备
import base64
#腾讯云返回的信息是json格式,我要转回python的字典,才可以方便的操作
import json
import time
#面向对象思维,创建类
class recognition:
def __init__(self):
#requestId和key是腾讯云开通后申请的,我这里保护下隐私,隐藏起来
self.requestId = "AKIDiBNIFx8aU7JE8n**************"
self.key = "VllY9VbR1OUJtDH**************"
#必备参数
self.Action = "ap-beijing"
#这个方法是把图片转化为base64的方法,并添加类的属性
def getBase64(self, name):
with open(name, "rb") as f:
self.base64_data = base64.b64encode(f.read()).decode()
#核心部分,图片解析,根据腾讯云的文档来做即可
def getInformation(self):
try:
cred = credential.Credential(self.requestId, self.key)
httpProfile = HttpProfile()
httpProfile.endpoint = "ocr.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
req = models.VatInvoiceOCRRequest()
dic = {"ImageBase64": self.base64_data}
params = json.dumps(dic)
req.from_json_string(params)
self.resp = client.VatInvoiceOCR(req)
except TencentCloudSDKException as err:
print(err)
#解析返回的数据,这个是根据返回的json来做的,他的格式是一个字典列表,即元素是字典的列表,需要遍历,获得想要的发票数据
def analysis(self):
self.data = json.loads(self.resp.to_json_string())
self.fatherName = self.data['VatInvoiceInfos'][6]['Value']
for i in self.data['VatInvoiceInfos']:
if i['Name'] == "小写金额":
self.value = i['Value']
self.money = self.value.replace("¥", "")
break
#方法调用,这里可以开多线程其实,封装在函数里
def start(self, name):
self.getBase64(name)
self.getInformation()
self.analysis()
os.rename(name, self.fatherName + "-" + self.money + ".jpg")
print(self.data)
print(self.fatherName)
print(self.money)
#主函数部分应该不用解释了,很easy的方法。
if __name__ == "__main__":
nameList = os.listdir()
nameList.remove("main.py")
for name in nameList:
sImg = Image.open(name)
w, h = sImg.size
dImg = sImg.resize((int(w/2), int(h/2)), Image.ANTIALIAS)
dImg.save(name)
try:
re = recognition()
re.start(name)
except AttributeError:
print(name + "错误")
ImageBase64
client.VatInvoiceOCR
高拍仪
[TencentCloudSDKException] code:AuthFailure.SignatureFailure message:由于请求包大小超过限制,请求签名验证失败,请使用新的签名方法 TC3-HMAC-SHA256
try :
aaa
except Exception as e:
print (e)
增值税发票
增值税发票识别
client.VatInvoiceOCR(req)
机票行程单识别
self.client.FlightInvoiceOCR
{“ImageBase64”: