Skip to the content.

Day 04: Integrating AI in Your Website

Not the page you want to view? Back to home page

In this theory session, we will be going through the application of AI. We will learn:

Some details about the event can be found in the Event Page. Below are some of the useful links that are relevant to the theory today.


Hands-On: Extracting Receipt Data

This manual is written by Lim Hui Ern and Vanessa Jing Taing, edited by by Lim Jun Yi.

In the workshop today, we will create a Receipt Data Extractor.

You can alternatively refer to these videos and follow along at your own pace:

The workflow for the hands-on session today is shown in the diagram below:

workflow

Step 01: Download Relevant Files

Download the sample receipts and Python code from this repo. To download it, click on the green “Code” button and click “Download ZIP”, or you can:

# Only if you have Git installed
git clone https://github.com/GCPE2023/ReceiptExtractor.git

If you download this repository as a zipped file, unzip it as we will be using the receipt-extractor.py file in this repository.

Step 02: Uploading Receipts to Google Drive

Using the Google Account that has Google Cloud Platform Free Trial activated, open the Google Drive and create a folder for your project. We will rename it to “Cloud” (you can rename it to whatever name you want).

In the “Cloud” (or the foldername you created) folder, create a new folder called “receipt”. This folder will be used to store all the input receipt images before it is processed.

In the “receipt” folder, upload the sample receipt images you have downloaded in Step 01 to this folder. Then, copy the Folder ID (the string of characters after /folder/ in the URL) of this folder and paste it in some place (eg. notepad) to be used later on.

You may also upload your other receipt images into this folder as well.

folder id

Step 03: Creating a new Google Sheets

Back to the “Cloud” folder (the folder that contains the “receipt” folder), create a new Google Sheet and set the name to be “Receipt_Data”.

Copy the Sheets ID (the string of characters between /spreadsheets/d/ and /edit#gid=0) of this Google Sheet and paste it with your Folder ID in Step 02 to be used later on.

Fill in the first row of the google sheets with the following horizontally.

Now, your Google Sheets should look like the image below.

gsheet

Then, select the first column (column for “Date”), then click “Format” > “Number” > “Date”.

gsheet config

Step 04: Creating Cloud Bucket

Type “Buckets” in the search tab of the Google Cloud Platform Console or navigate by “Cloud Storage” > “Buckets” from the navigation bar.

Click on the + CREATE button to create a cloud bucket, then make the configurations below. You will need to press the “Continue” button while creating the bucket for each section below.

Your bucket has been created.

Note
[Checkpoint] you should have 3 things copied and stored in a place:

Step 05: Enabling Relevant APIs

We will be enabling 3 APIs that will be used for this hands-on session.

One way is to do it manually. Navigate to “APIs & Services” and search for the following APIs and enable them individualy:

or alternatively, you could use the gcloud command in the Cloud Shell with the following command:

gcloud services enable drive.googleapis.com vision.googleapis.com sheets.googleapis.com

Step 06: Authorizing APIs

Note
This section can be skipped if you’ve already created user account authorization credentials.

Still under “APIs & Services”, navigate to “OAuth Consent Screen” and choose “External” for user type.

Next, we will be creating our OAuth Client ID. Under “APIs & Services”, navigate to “Credentials” and click + CREATE CREDENTIALS > “OAuth Client ID”.

In the “OAuth Client ID” page, set the “Application Type” to “Desktop app” and click “CREATE”.

After the creation is complete, a window will then pop up indicating the creation success. In the pop up window, click “Download JSON”.

If you closed the window already, you can click the download icon under “OAuth 2.0 Client IDs” to download the JSON file.

oauth

Rename the JSON file as client_secret.json.

Step 07: Coding on Cloud Shell Editor

Activate the GCP Cloud Shell then click “Open Editor”. This will create a “VSCode”-like cloud editor environment where we can use to execute our codes in the cloud.

editor

Click the 3 dots next to the “Open Terminal” button and click “Upload Files”. There are two files that you will need to upload to this Shell Editor:

Warning
Both files should not be uploaded into any folder.

After both files are uploaded, click on receipt-extractor.py to modify the code content in the following lines:

Then, we will be generating our storage.json file. Click “Terminal” > “New Terminal” and a terminal should appear at the bottom of the screen.

open terminal

In the terminal, paste the following command:

python3 receipt-extractor.py --noauth_local_webserver

The terminal will generate an output link that looks something like this:

Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?client_id= ...

Enter verification code: 

Click the link and a prompt should pop out asking your verification. Check all boxes for “Select what Receipt Extractor can access” then click “Continue”.

Next, under the “Authorization Code” section, copy the authorization code. Then, back to the Cloud Shell Editor Terminal, paste the authorization code right after the Enter verification code: prompt. Then, press Enter.

After the command executes successfully, the storage.json file should be generated.

Now, we will start to execute our program by using the following command in the terminal:

python3 receipt-extractor.py

This will load all receipt images in the Google Drive, process them through the Cloud Vision API and then produce the extracted receipt data into the Google Sheets.

Note
If you wish to execute more receipt images extraction in the future, upload them into the same Google Drive folder and then run the python3 receipt-extractor.py command again in the Cloud Shell Editor Terminal.

Step 08: Clean Up

If you do not need to use the receipt data extraction anymore, just disable all APIs that you have enabled in Step 05.

Complete!

You have successfully extracted your receipt data in batches!

More Readings