Building Google Lens Functionality with Spring Boot, Cloud Vision, and GCP

Nitin Kalra
4 min readJan 17, 2020

In this project, we will learn how to build a spring boot app with which we can do a lot of things with images, like performing OCR (optical character recognition), detecting faces, adding labels based on image content, detecting landmarks, detecting company logos and many other things.

If you are interested to know what other things you can do with Google Cloud Vision, check out the link below :

Sound’s Fun, Let's Build it.

Before starting, if you are not familiar with Spring Boot and Google Cloud Platform, I would recommend taking this code lab.

In this code lab, you will learn how to deploy a simple spring boot app on Google App Engine.

With the Spring Boot App, the image given by the user will be uploaded to Google Cloud Storage Bucket and then we will use Cloud Vision API.

Currently, the app has a minimal UI and looks like this, Just a simple image chooser and an upload button.

You can choose a file and click on the post and then you will get the results, For example I uploaded an image of a car, and got these results.

uploaded image

{Land vehicle=0.9961627, Vehicle=0.99030316, Car=0.98701257, Hatchback=0.96138614, Hyundai=0.90618163, Hyundai i20=0.83815885, Mid-size car=0.8270783, City car=0.7624699, Compact car=0.730952, Automotive design=0.72283965}

Let's understand the results, This is a Land vehicle with a .996 score and a car with .987 and Hyundai i20 with .838 score.

Earlier this API returned confidence — The accuracy of the entity detection in an image, but that is deprecated now and the score is returned now.

The results are pretty accurate.

As Linus Torvalds says:

Code is uploaded at

Let's check some important portions of the code.

// After file is written to GCS, let's analyze the image.            AnnotateImageResponse response =  analyzeImage(bucket + "/" + filename);
Call the cloud vision API to detect the label
Filter the result to show only relevant portions

If you want to try some other feature of Cloud Vision API, you will have to change the feature and run the code.

Feature feature = Feature.newBuilder()
.setType(Feature.Type.LABEL_DETECTION).build();

Some other options for features are :

LOGO_DETECTION
LANDMARK_DETECTION
TEXT_DETECTION
WEB_DETECTION

Complete list available at — https://cloud.google.com/vision/pricing

Some commands that you will need to build, run and deploy the app.

gcloud auth login --- To login gcloud config set project <project id> -- To set the project gcloud app create --- To create a new app engine instance gcloud services enable vision.googleapis.com -- To enable cloud vision api#To create a servcie account ----- start export PROJECT_ID=$(gcloud config list --format 'value(core.project)')gcloud iam service-accounts create <project-name>gcloud projects add-iam-policy-binding ${PROJECT_ID} \   --member serviceAccount:<project-name>@${PROJECT_ID}.iam.gserviceaccount.com \   --role roles/editorgcloud iam service-accounts keys create \     ~/service-account.json \     --iam-account <project-name>@${PROJECT_ID}.iam.gserviceaccount.com#To create a servcie account -----end./mvnw package appengine:deploy -- To deploy the app ./mvnw spring-boot:run \-   Dspring.cloud.gcp.credentials.location=file:////Users/<username>/Downloads/service-account.json  --- To run the app locally with credentialsexport GOOGLE_APPLICATION_CREDENTIALS="/Users/<username>/Downloads/service-account.json"./mvnw spring-boot:run -- To run the app locally

Treat the service-account.json file as your own username/password. Do not share this information

As using this API incurs charges and only 1000 requests per month are free, I will be switching off this project after some time.

The app is deployed at :

If you found this tutorial useful, you know what to do now. Hit that clap button and star the GitHub repo follow me to get more articles and tutorials on your feed.

If you want to understand how Complex systems work at scale, i recommend reading this book
Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems

https://amzn.to/3vdVy4q

--

--