Spring Cloud GCP and Cloud Vision: A New Record Chain

Reading Time: 3 minutes

Introduction

In this blog post, we will explore Spring Cloud GCP’s support for Spring Boot and Cloud Vision.

Spring Cloud GCP

Spring Cloud GCP is a suite of libraries that help you build and manage applications on the Google Cloud Platform. Applications built using Spring Cloud GCP have the following characteristics:

  • They are resilient, with a robust design and fault tolerance features like Circuit Breaker and Hystrix;
  • They provide better scalability by enabling auto-scaling based on metrics collected by Prometheus;
  • They allow developers to focus on their business logic by taking care of things like load balancing, service discovery (using Eureka), and client-side SSL termination.

Cloud Vision

It is a service that allows you to analyze images and recognize objects, faces, and landmarks in them. Cloud Vision provides OCR, face, landmark, and image analysis capabilities.

Cloud Vision can recognize text in images and provide results in the form of a list of possible matches. The following example shows how to use the Cloud Vision API (https://cloud.google.com/vision/docs/) on GCP to perform real-time OCR on an image captured by your camera app:

Dependency Setup

To use Spring Cloud GCP, add the following dependencies to your project:

To use Google Cloud Vision, you’ll need to add these dependencies:

Configuration

In this section, we will learn how to configure your application, service, controller, and template.

  • Application Configuration
  • Service Configuration
  • Controller Configuration
  • Template Configuration
  • Model Configuration

Cloud Vision OCR Dependencies

Use the following dependencies to enable GCP Cloud Vision OCR.

  • `com.google.cloud`: Version 1.4 or higher
  • `io.springcloud.*`: This dependency is already included in Spring Cloud GCP samples, so you don’t have to add it manually if you’re using those samples as a reference.
  • `org.springframework.*` (Java 8 or later): You’ll need Spring Framework if you’re generating images from text content and want the images’ metadata attached with them (for example, filename and time stamp).

Image Analysis

Image analysis is the process of detecting and extracting useful information from images. This typically involves using image processing algorithms, but can also be done manually by people. Moreover, image analysis is used in many fields, including astronomy, biology, and medicine.

File Analysis

File Analysis is an API that can detect the type of file and its properties. It can detect the type of file and its properties. Moreover, file Analysis is helpful to detect the type of file and its properties.

Document OCR Template

To create a document OCR template, you must set up a Spring Cloud GCP project and define your model in the model format of your choice. You then need to create an image file with the same name as the model filename, but with a different extension. For example, if you have a file named “document_ocr_template.xml”, it’s like saved as “document_ocr_template.png”. The image will be for training your model before use in production environments.

To use this template for detection, firstly, you need to convert your images into byte arrays (byte[]) by calling ImageIO’s getBytes() method on it or reading from an InputStream bitmap = new FileInputStream(“.\images\\image1”). This will provide a copy of an image file’s pixels that can then be fed into other tools such as Tesseract . In addition, to detect text from these bytes using Tesseract , add them after calling TesseractEngine::newInstance() : tesseractEngine->setInput(“image-file”); tesseractEngine->setOcrLanguage (“eng”); tesseractEngine->process(); tesseractResults = tesseractEngine->getResults(); Your results should look like:

How to use Spring Cloud GCP and Cloud Vision

To use Spring Cloud GCP you must add the following dependency to your pom.xml file:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-vision</artifactId>
</dependency>

And for Gradle

dependencies {
  implementation("org.springframework.cloud:spring-cloud-gcp-starter-vision")
}

For instance, consider the below sample code for the image labeling:

@Autowired
private ResourceLoader resourceLoader;

@Autowired
private CloudVisionTemplate cloudVisionTemplate;

public void processImage() {
  Resource imageResource = this.resourceLoader.getResource("my_image.jpg");
  AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(
      imageResource, Type.LABEL_DETECTION);
  System.out.println("Image Classification results: " + response.getLabelAnnotationsList());
}

Conclusion

  • Follow the steps below to get started with Cloud Vision, our vision library that allows you to detect objects in photos and images.
  • In a new terminal window, run the “cloud-vision” command and provide it with your vision library location (the one we installed earlier) as well as a sample image.
  • Run “cloud-vision” again (without the quotes) and this time provides it with an input image of your choice:
  • A successful analysis will produce an output JSON file, which can be read using any JSON reader such as this one from JSONView: https://www.jsonvieweronline.com/tools/jsonviewer/
  • You now have everything you need to get started detecting objects in images!

For More Details on Spring Cloud GCP Please visit Our Blogs.
And For Detailed Reference on Spring Cloud GCP please visit below Google Documents.
https://docs.spring.io/spring-cloud-gcp/docs/current/reference/html/index.html#cloud-vision

Leave a Reply