Integrate Google Cloud Vision with Spring Boot

Hamdi Bouallague
3 min readMar 20, 2021

--

1. Before you begin :

Create a Google Cloud Platform project with billing enabled.

Enable the Google Cloud Vision API from the “APIs & Services” menu of the Google Cloud Console.

Follow these steps from this link:

2. Create Spring boot application :

https://start.spring.io/

Dependencies: Spring Web, and Spring Boot DevTools.

After downloading and unzipping the project adds these dependencies :

spring-cloud-gcp-starter-vision and spring-cloud-gcp-dependencies

See pom.xml below :

3. Create a service account :

Follow these steps :

Then download the secret Json file.

4. Encode the secret to base-64 encoded :

Open the Json file and copy his content.

Navigate to https://www.base64encode.org/

Then click encode and copy the encoded secret .

Now inside application.properties add this code :

server.port=8888 # this to change the default port to 8888spring.cloud.gcp.credentials.encoded-key=paste the encoded secret here

Next, we add a VisionController where we inject the CloudVisionTemplate which we will use to access the Cloud Vision API and ResourceLoader to load images as a resource.

5. Detect Labels:

The Vision API can detect and extract information about entities in an image across a broad group of categories.

We will use the image of this lion to do so :

add the getLabelDetection method to VisionController:

Next navigate to http://localhost:8888/api/getLabelDetection

[
, mid: "/m/096mb"
description: "Lion"
score: 0.8928216
topicality: 0.8928216
, mid: "/m/0bm9sv0"
description: "Masai lion"
score: 0.88758737
............
]

The mid parameter is a unique machine-generated identifier corresponding to the entity’s Google Knowledge Graph entry.

6. Detect landmarks :

Landmark Detection detects popular natural and human-made structures within an image.

We will use the Eiffel Tower :

Next navigate to http://localhost:8888/api/getLandmarkDetection

[
mid: "/m/02j81"
description: "Eiffel Tower"
score: 0.65436697
bounding_poly {
vertices {
x: 201
y: 53
}
vertices {
x: 359
y: 53
}
vertices {
x: 359
y: 315
}
vertices {
x: 201
y: 315
}
}
locations {
lat_lng {
latitude: 48.858461
longitude: 2.294351
}
}
...........
]

7. Optical Character Recognition (OCR):

The Vision API can detect and extract text from images.

Next navigate to http://localhost:8888/api/extractTextFromImage

WAITING?
PLEASE
TURN OFE
YOUR
ENGINE

8. Last word:

You can find the code from GitHub :

Useful links :

https://cloud.google.com/vision/docs/before-you-begin

https://cloud.google.com/docs/authentication/getting-started

If you enjoyed this article, please clap it up 👏 and share it so that others can find it! Follow me to get more of me 😄.

--

--