Files
spring-cloud-static/Greenwich.SR1/multi/multi__google_cloud_vision.html
2019-03-06 10:23:45 -05:00

27 lines
9.5 KiB
HTML

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>162.&nbsp;Google Cloud Vision</title><link rel="stylesheet" type="text/css" href="css/manual-multipage.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="multi_spring-cloud.html" title="Spring Cloud"><link rel="up" href="multi_spring-cloud-gcp-reference.html" title="Part&nbsp;XVIII.&nbsp;Spring Cloud GCP"><link rel="prev" href="multi__cloud_identity_aware_proxy_iap_authentication.html" title="161.&nbsp;Cloud Identity-Aware Proxy (IAP) Authentication"><link rel="next" href="multi__cloud_foundry.html" title="163.&nbsp;Cloud Foundry"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">162.&nbsp;Google Cloud Vision</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="multi__cloud_identity_aware_proxy_iap_authentication.html">Prev</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;XVIII.&nbsp;Spring Cloud GCP</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="multi__cloud_foundry.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="_google_cloud_vision" href="#_google_cloud_vision"></a>162.&nbsp;Google Cloud Vision</h2></div></div></div><p>The <a class="link" href="https://cloud.google.com/vision/" target="_top">Google Cloud Vision API</a> allows users to leverage machine learning algorithms for processing images including: image classification, face detection, text extraction, and others.</p><p>Spring Cloud GCP provides:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">A convenience starter which automatically configures authentication settings and client objects needed to begin using the <a class="link" href="https://cloud.google.com/vision/" target="_top">Google Cloud Vision API</a>.</li><li class="listitem"><p class="simpara">A Cloud Vision Template which simplifies interactions with the Cloud Vision API.</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem">Allows you to easily send images to the API as Spring Resources.</li><li class="listitem">Offers convenience methods for common operations, such as extracting the text from an image.</li></ul></div></li></ul></div><p>Maven coordinates, using Spring Cloud GCP BOM:</p><pre class="programlisting"><span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;dependency&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;groupId&gt;</span>org.springframework.cloud<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/groupId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;artifactId&gt;</span>spring-cloud-gcp-starter-vision<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/artifactId&gt;</span>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-tag">&lt;/dependency&gt;</span></pre><p>Gradle coordinates:</p><pre class="screen">dependencies {
compile group: 'org.springframework.cloud', name: 'spring-cloud-gcp-starter-vision'
}</pre><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_cloud_vision_template" href="#_cloud_vision_template"></a>162.1&nbsp;Cloud Vision Template</h2></div></div></div><p>The <code class="literal">CloudVisionTemplate</code> offers a simple way to use the Cloud Vision APIs with Spring Resources.</p><p>After you add the <code class="literal">spring-cloud-gcp-starter-vision</code> dependency to your project, you may <code class="literal">@Autowire</code> an instance of <code class="literal">CloudVisionTemplate</code> to use in your code.</p><p>The <code class="literal">CloudVisionTemplate</code> offers the following method for interfacing with Cloud Vision:</p><p><code class="literal">public AnnotateImageResponse analyzeImage(Resource imageResource, Feature.Type&#8230;&#8203; featureTypes)</code></p><p><span class="strong"><strong>Parameters:</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">Resource imageResource</code> refers to the Spring Resource of the image object you wish to analyze.
The Google Cloud Vision documentation provides a <a class="link" href="https://cloud.google.com/vision/docs/supported-files" target="_top">list of the image types that they support</a>.</li><li class="listitem"><code class="literal">Feature.Type&#8230;&#8203; featureTypes</code> refers to a var-arg array of Cloud Vision Features to extract from the image.
A feature refers to a kind of image analysis one wishes to perform on an image, such as label detection, OCR recognition, facial detection, etc.
One may specify multiple features to analyze within one request.
A full list of Cloud Vision Features is provided in the <a class="link" href="https://cloud.google.com/vision/docs/features" target="_top">Cloud Vision Feature docs</a>.</li></ul></div><p><span class="strong"><strong>Returns:</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p class="simpara"><a class="link" href="https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateImageResponse" target="_top"><code class="literal">AnnotateImageResponse</code></a> contains the results of all the feature analyses that were specified in the request.
For each feature type that you provide in the request, <code class="literal">AnnotateImageResponse</code> provides a getter method to get the result of that feature analysis.
For example, if you analyzed an image using the <code class="literal">LABEL_DETECTION</code> feature, you would retrieve the results from the response using <code class="literal">annotateImageResponse.getLabelAnnotationsList()</code>.</p><p class="simpara"><code class="literal">AnnotateImageResponse</code> is provided by the Google Cloud Vision libraries; please consult the <a class="link" href="https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateImageResponse" target="_top">RPC reference</a> or <a class="link" href="http://googleapis.github.io/googleapis/java/all/latest/apidocs/com/google/cloud/vision/v1/AnnotateImageResponse.html" target="_top">Javadoc</a> for more details.
Additionally, you may consult the <a class="link" href="https://cloud.google.com/vision/docs/" target="_top">Cloud Vision docs</a> to familiarize yourself with the concepts and features of the API.</p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_detect_image_labels_example" href="#_detect_image_labels_example"></a>162.2&nbsp;Detect Image Labels Example</h2></div></div></div><p><a class="link" href="https://cloud.google.com/vision/docs/detecting-labels" target="_top">Image labeling</a> refers to producing labels that describe the contents of an image.
Below is a code sample of how this is done using the Cloud Vision Spring Template.</p><pre class="programlisting"><em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> ResourceLoader resourceLoader;
<em><span class="hl-annotation" style="color: gray">@Autowired</span></em>
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">private</span> CloudVisionTemplate cloudVisionTemplate;
<span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">public</span> <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">void</span> processImage() {
Resource imageResource = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.resourceLoader.getResource(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"my_image.jpg"</span>);
AnnotateImageResponse response = <span xmlns:d="http://docbook.org/ns/docbook" class="hl-keyword">this</span>.cloudVisionTemplate.analyzeImage(
imageResource, Type.LABEL_DETECTION);
System.out.println(<span xmlns:d="http://docbook.org/ns/docbook" class="hl-string">"Image Classification results: "</span> + response.getLabelAnnotationsList());
}</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_sample_12" href="#_sample_12"></a>162.3&nbsp;Sample</h2></div></div></div><p>A <a class="link" href="https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-vision-api-sample" target="_top">Sample Spring Boot Application</a> is provided to show how to use the Cloud Vision starter and template.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="multi__cloud_identity_aware_proxy_iap_authentication.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="multi_spring-cloud-gcp-reference.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="multi__cloud_foundry.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">161.&nbsp;Cloud Identity-Aware Proxy (IAP) Authentication&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="multi_spring-cloud.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;163.&nbsp;Cloud Foundry</td></tr></table></div></body></html>