Skip to content

Commit 856b906

Browse files
authored
Add vision ocr set endpoint snippets (#1748)
1 parent 29f70f5 commit 856b906

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
import com.google.cloud.vision.v1.AnnotateImageRequest;
20+
import com.google.cloud.vision.v1.AnnotateImageResponse;
21+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
22+
import com.google.cloud.vision.v1.EntityAnnotation;
23+
import com.google.cloud.vision.v1.Feature;
24+
import com.google.cloud.vision.v1.Image;
25+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
26+
import com.google.cloud.vision.v1.ImageAnnotatorSettings;
27+
import com.google.cloud.vision.v1.ImageSource;
28+
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
class SetEndpoint {
34+
35+
// Change your endpoint
36+
static void setEndpoint() throws IOException {
37+
// [START vision_set_endpoint]
38+
ImageAnnotatorSettings settings =
39+
ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build();
40+
41+
// Initialize client that will be used to send requests. This client only needs to be created
42+
// once, and can be reused for multiple requests. After completing all of your requests, call
43+
// the "close" method on the client to safely clean up any remaining background resources.
44+
ImageAnnotatorClient client = ImageAnnotatorClient.create(settings);
45+
// [END vision_set_endpoint]
46+
47+
ImageSource imgSource =
48+
ImageSource.newBuilder()
49+
.setGcsImageUri("gs://cloud-samples-data/vision/text/screen.jpg")
50+
.build();
51+
Image image = Image.newBuilder().setSource(imgSource).build();
52+
Feature feature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
53+
AnnotateImageRequest request =
54+
AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(image).build();
55+
List<AnnotateImageRequest> requests = new ArrayList<>();
56+
requests.add(request);
57+
58+
BatchAnnotateImagesResponse batchResponse = client.batchAnnotateImages(requests);
59+
60+
for (AnnotateImageResponse response : batchResponse.getResponsesList()) {
61+
for (EntityAnnotation annotation : response.getTextAnnotationsList()) {
62+
System.out.printf("Text: %s\n", annotation.getDescription());
63+
System.out.println("Position:");
64+
System.out.printf("%s\n", annotation.getBoundingPoly());
65+
}
66+
}
67+
client.close();
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.vision;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Tests for Vision Set Endpoint */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class SetEndpointIT {
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testSetEndpoint() throws IOException {
53+
// Act
54+
SetEndpoint.setEndpoint();
55+
56+
// Assert
57+
String got = bout.toString();
58+
assertThat(got).contains("System Software Update");
59+
assertThat(got).contains("x:");
60+
assertThat(got).contains("y:");
61+
}
62+
}

0 commit comments

Comments
 (0)