欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【百度大脑新品体验】新版增值税发票识别

程序员文章站 2022-07-12 20:51:21
...

【百度大脑新品体验】新版增值税发票识别
作者:才能我浪费99

1.功能描述:

在日常工作中经常要用到增值税发票。在使用的时候需要对增值税发表进行检查,验真,录入等很多工作。使用增值税发票识别技术,实现对增值税普票或专票各字段信息的识别和录入,可应用于企业税务核算及内部报销等场景,能够有效减少人工核算工作量,降低人力成本,实现财税报销的自动化

2.平台接入

增值税发票识别接入网址:https://console.bce.baidu.com/ai/#/ai/imageprocess/overview/index

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:
http://ai.baidu.com/forum/topic/show/943327

3.调用攻略(Python3)及评测

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

http://ai.baidu.com/docs#/Auth/top

具体Python3代码如下:

-- coding: utf-8 --

#!/usr/bin/env python

import urllib
import base64
import json
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=’ + client_id + ‘&client_secret=’ + client_secret
request = urllib.request.Request(host)
request.add_header(‘Content-Type’, ‘application/json; charset=UTF-8’)
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content)
token_key = token_info[‘access_token’]
return token_key
3.2增值税发票识别分析接口调用:

详细说明请参考: https://ai.baidu.com/docs#/OCR-API/5099e085

说明的比较清晰,这里就不重复了。

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice
图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式

Python3调用代码如下:

#增值税发票
#filename:图片名(本地存储包括路径)
def vat_invoice(filename):
request_url = “https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice”

# 二进制方式打开图片文件
f = open(filename, 'rb')
img = base64.b64encode(f.read())

params = dict()
params['image'] = img
params['show'] = 'true'
params = urllib.parse.urlencode(params).encode("utf-8")
#params = json.dumps(params).encode('utf-8')

access_token = get_token()
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
if content:
    #print(content)
    content=content.decode('utf-8')
    #print(content)
    data = json.loads(content)
    #print(data)
    words_result=data['words_result']
    print ('发票号码:',words_result['InvoiceNum'])
    print ('开票日期:',words_result['InvoiceDate'])
    print ('合计金额:',words_result['TotalAmount'])
    print ('合计税额:',words_result['TotalTax'])
    print ('销售方名称:',words_result['SellerName'])
    print ('销售方纳税人识别号:',words_result['SellerRegisterNum'])
    print ('购方名称:',words_result['PurchaserName'])
    print ('购方纳税人识别号:',words_result['PurchaserRegisterNum'])

vat_invoice(‘invoice.jpg’)
4.功能评测:
选用不同的数据对效果进行测试,具体效果如下(以下发票例子均来自网上):
【百度大脑新品体验】新版增值税发票识别

发票号码: 01234567
开票日期: 2016年03月15日
合计金额: 120.00
合计税额: 30.00

测试下来,整体感觉处理的很不错,此功能会大大的发票处理的工作量。