In this blog post we will see how we can use Google Cloud Platform’s Vision API to extract text from images in different languages. This will be implemented in a container based AWS Lambda.
We will walk through setting up a GCP project, service account and build, deploy and test the container lambda. To implement these services you should have an AWS user account with permissions to — read/ write to S3, upload to ECR and create Lambda functions, for GCP you can use the free trial period but it will need billing information to be added to use its services.
There are multiple ways to extract text from images but what makes GCP Vision API special? The use case here is for images that can be in different languages and we need to extract the raw text to identify the language.
Some of the popular tools include –
Google’s Cloud Vision API comes to the rescue here. It supports >50 languages that are actively maintained and providing language hints to the service is not required. All the supported languages can be found here.
Set up your GCP project and authentication
Search for Cloud Vision API under APIs & Services and click on ENABLE. We need the Vision API to extract text from image
Once the service account is created, go to Manage Keys and select Add Key -> Create New Key -> Json (key type)
This downloads the private key as a json file. We need this key to initialize the GCP Client in Lambda. There are multiple ways to do that-
from google.oauth2.service_account import Credentials
Credentials.from_service_account_file(<PATH_TO_SERVICE_ACCOUNT_JSON>)
To do so we need to add the file to the lambda container image.
Credentials.from_service_account_info(<JSON_ACCOUNT_INFO>)
We will implement this second method in our example.
Add private key to AWS Secret Manager
We use the AWS CLI to add the key value pair to AWS Secret Manager using create-secret. Replace PRIVATE_KEY_JSON_CONTENT with contents of the downloaded key json as secret-string
aws secretsmanager create-secret –name “”service-account-key”” –secret-string ‘<PRIVATE_KEY_JSON_CONTENT>’
We are going to create a container based lambda function that uses the GCP Vision package to extract text from an image stored in an AWS S3 bucket. We have set up all the permissions needed in GCP and added the service account key to AWS Secret Manager, this key is all we need now to make the GCP Vision API call.
mkdir gcp-in-lambda
Before getting started with the Lambda function, there are some env vars that we need to set for deploying the function. There are 2 ways we can do this-
export AWS_REGION=<YOUR_AWS_REGION>
export AWS_ACCOUNT_ID=<YOUR_AWS_ACCOUNT_ID>
or
Now let’s look at the main piece of code. Here,
echo google-cloud-vision==2.7.1 > requirements.txt
This script creates a new AWS ECR repository named container-lambda-for-gcp-vision, builds the docker image and pushes it to this repo.
Now that the final Lambda container image is in AWS ECR, let’s create a container Lambda with reference to our image URI. To get started let’s create a basic execution role for Lambda and then add some additional permissions needed from AWS Console.
After running these 2 commands, go to the AWS Lambda Console and you should see the image-text-extraction Lambda created.
Select Add permissions -> attach policies
Here, search for policy AmazonS3ReadOnlyAccess, SecretsManagerReadWrite and CloudWatchFullAccess and attach these to the role. We need S3 permissions when reading the image from s3, Secret Manager permissions to access the GCP service account key and CloudWatch access to write Lambda logs.
Let’s also bump the Lambda timeout from the default 3 sec to 5 min giving it time to make the text extraction api call.
Once the files are added to the S3 bucket, lets invoke the lambda function
You can see the logs in AWS CloudWatch, look for the lambda under log groups here –
In this blog we demonstrate how to use GCP Vision service in AWS Lambda. A similar approach can be used to extract text from images in all languages supported by GCP Vision API and to implement more GCP services in Lambda.
To read more about GCP services click here.
To understand how to use container based AWS Lambda check out this blog.