84. Identify VAT Invoices for Cloud Services#
84.1. Introduction#
In this challenge, you need to call the VAT invoice recognition interface provided by Baidu Cloud to complete the task of recognizing VAT invoice pictures.
84.2. Key Points#
Cloud service invocation
Invoice recognition
In the previous experiments, we learned and used the AI services provided by Baidu Cloud. Meanwhile, with the help of the SDK provided by Baidu Cloud, we completed several examples of natural language processing tasks and image recognition tasks. In this challenge, you need to independently complete the learning and invocation of the existing APIs, but this is slightly different from the experimental content.
{exercise-start}
:label: chapter10_07_1
Open-ended Challenge
Challenge: Find the VAT invoice recognition interface provided by Baidu Cloud and complete the task of recognizing VAT invoice pictures.
Requirement: You cannot use the SDK provided by Baidu Cloud. You can only complete this challenge through the HTTP request method given in the official technical documentation.
{exercise-end}
First, the challenge provides a sample VAT invoice picture as follows:

The picture URL link is:
https://cdn.aibydoing.com/aibydoing/files/invoice-demo.png
Next, you need to build a function for recognizing this VAT invoice picture, complete the task of recognizing the picture with the input link, and return the recognition information. It is worth noting that there is no recognition interface for VAT invoice pictures in Baidu’s existing SDK, so you cannot call it as quickly as in the experiment. However, the method of calling the interface using the HTTP request method is provided in the relevant documentation.
You need to register by yourself for a Baidu Cloud account, apply for a VAT invoice recognition authorization code, and finally complete the task of recognizing the input picture link. The challenge will not provide any reference materials and you need to complete it independently.
# 请在 Notebook 中完成本次挑战
Solution to
import base64
import requests
from PIL import Image
from io import BytesIO
# Request network image
def vat_ocr(url):
response = requests.get(url)
image = base64.b64encode(response.content) # The image needs to be processed into Base64 encoding according to the API
# Obtain the access_token authorization code using AK and SK with reference to the official documentation
API_KEY = '0HUECBnp7enxo2EpiI8kqP5I'
SECRET_KEY = 'DdomRyxgwZepSX4mSlo1mroZu2YLN8jN'
token_url = f'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={API_KEY}&client_secret={SECRET_KEY}'
access_token = requests.get(url=token_url).json()['access_token']
# Add the access_token to the request URL parameters
vat_url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice?access_token={access_token}"
# Request the VAT invoice recognition API
r = requests.post(url=vat_url, data={'image': image, 'accuracy': 'normal'})
return r.json()
vat_ocr("https://cdn.aibydoing.com/aibydoing/files/invoice-demo.png")
Expected output: