Skip to content

Commit 6056b61

Browse files
nnegreychingor13
authored andcommitted
samples: vision: move samples out of branch (#2298)
1 parent a53314e commit 6056b61

File tree

5 files changed

+338
-0
lines changed

5 files changed

+338
-0
lines changed

vision/snippets/resources/kafka.pdf

85.2 KB
Binary file not shown.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2020 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+
// [START vision_batch_annotate_files]
20+
import com.google.cloud.vision.v1.AnnotateFileRequest;
21+
import com.google.cloud.vision.v1.AnnotateImageResponse;
22+
import com.google.cloud.vision.v1.BatchAnnotateFilesRequest;
23+
import com.google.cloud.vision.v1.BatchAnnotateFilesResponse;
24+
import com.google.cloud.vision.v1.Block;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
27+
import com.google.cloud.vision.v1.InputConfig;
28+
import com.google.cloud.vision.v1.Page;
29+
import com.google.cloud.vision.v1.Paragraph;
30+
import com.google.cloud.vision.v1.Symbol;
31+
import com.google.cloud.vision.v1.Word;
32+
import com.google.protobuf.ByteString;
33+
34+
import java.io.IOException;
35+
import java.nio.file.Files;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
38+
39+
public class BatchAnnotateFiles {
40+
41+
public static void batchAnnotateFiles() throws IOException {
42+
String filePath = "path/to/your/file.pdf";
43+
batchAnnotateFiles(filePath);
44+
}
45+
46+
public static void batchAnnotateFiles(String filePath) throws IOException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
51+
// You can send multiple files to be annotated, this sample demonstrates how to do this with
52+
// one file. If you want to use multiple files, you have to create a `AnnotateImageRequest`
53+
// object for each file that you want annotated.
54+
// First read the files contents
55+
Path path = Paths.get(filePath);
56+
byte[] data = Files.readAllBytes(path);
57+
ByteString content = ByteString.copyFrom(data);
58+
59+
// Specify the input config with the file's contents and its type.
60+
// Supported mime_type: application/pdf, image/tiff, image/gif
61+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
62+
InputConfig inputConfig =
63+
InputConfig.newBuilder().setMimeType("application/pdf").setContent(content).build();
64+
65+
// Set the type of annotation you want to perform on the file
66+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
67+
Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
68+
69+
// Build the request object for that one file. Note: for additional file you have to create
70+
// additional `AnnotateFileRequest` objects and store them in a list to be used below.
71+
// Since we are sending a file of type `application/pdf`, we can use the `pages` field to
72+
// specify which pages to process. The service can process up to 5 pages per document file.
73+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
74+
AnnotateFileRequest fileRequest =
75+
AnnotateFileRequest.newBuilder()
76+
.setInputConfig(inputConfig)
77+
.addFeatures(feature)
78+
.addPages(1) // Process the first page
79+
.addPages(2) // Process the second page
80+
.addPages(-1) // Process the last page
81+
.build();
82+
83+
// Add each `AnnotateFileRequest` object to the batch request.
84+
BatchAnnotateFilesRequest request =
85+
BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build();
86+
87+
// Make the synchronous batch request.
88+
BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);
89+
90+
// Process the results, just get the first result, since only one file was sent in this
91+
// sample.
92+
for (AnnotateImageResponse imageResponse :
93+
response.getResponsesList().get(0).getResponsesList()) {
94+
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
95+
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
96+
for (Block block : page.getBlocksList()) {
97+
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
98+
for (Paragraph par : block.getParagraphsList()) {
99+
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
100+
for (Word word : par.getWordsList()) {
101+
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
102+
for (Symbol symbol : word.getSymbolsList()) {
103+
System.out.printf(
104+
"\t\t\tSymbol: %s, (confidence: %s)\n",
105+
symbol.getText(), symbol.getConfidence());
106+
}
107+
}
108+
}
109+
}
110+
}
111+
}
112+
}
113+
}
114+
}
115+
// [END vision_batch_annotate_files]
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2020 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+
// [START vision_batch_annotate_files]
20+
import com.google.cloud.vision.v1.AnnotateFileRequest;
21+
import com.google.cloud.vision.v1.AnnotateImageResponse;
22+
import com.google.cloud.vision.v1.BatchAnnotateFilesRequest;
23+
import com.google.cloud.vision.v1.BatchAnnotateFilesResponse;
24+
import com.google.cloud.vision.v1.Block;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.GcsSource;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.cloud.vision.v1.InputConfig;
29+
import com.google.cloud.vision.v1.Page;
30+
import com.google.cloud.vision.v1.Paragraph;
31+
import com.google.cloud.vision.v1.Symbol;
32+
import com.google.cloud.vision.v1.Word;
33+
34+
import java.io.IOException;
35+
36+
public class BatchAnnotateFilesGcs {
37+
38+
public static void batchAnnotateFilesGcs() throws IOException {
39+
String gcsUri = "gs://cloud-samples-data/vision/document_understanding/kafka.pdf";
40+
batchAnnotateFilesGcs(gcsUri);
41+
}
42+
43+
public static void batchAnnotateFilesGcs(String gcsUri) throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests. After completing all of your requests, call
46+
// the "close" method on the client to safely clean up any remaining background resources.
47+
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
48+
// You can send multiple files to be annotated, this sample demonstrates how to do this with
49+
// one file. If you want to use multiple files, you have to create a `AnnotateImageRequest`
50+
// object for each file that you want annotated.
51+
// First specify where the vision api can find the image
52+
GcsSource gcsSource = GcsSource.newBuilder().setUri(gcsUri).build();
53+
54+
// Specify the input config with the file's uri and its type.
55+
// Supported mime_type: application/pdf, image/tiff, image/gif
56+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
57+
InputConfig inputConfig =
58+
InputConfig.newBuilder().setMimeType("application/pdf").setGcsSource(gcsSource).build();
59+
60+
// Set the type of annotation you want to perform on the file
61+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
62+
Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
63+
64+
// Build the request object for that one file. Note: for additional file you have to create
65+
// additional `AnnotateFileRequest` objects and store them in a list to be used below.
66+
// Since we are sending a file of type `application/pdf`, we can use the `pages` field to
67+
// specify which pages to process. The service can process up to 5 pages per document file.
68+
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
69+
AnnotateFileRequest fileRequest =
70+
AnnotateFileRequest.newBuilder()
71+
.setInputConfig(inputConfig)
72+
.addFeatures(feature)
73+
.addPages(1) // Process the first page
74+
.addPages(2) // Process the second page
75+
.addPages(-1) // Process the last page
76+
.build();
77+
78+
// Add each `AnnotateFileRequest` object to the batch request.
79+
BatchAnnotateFilesRequest request =
80+
BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build();
81+
82+
// Make the synchronous batch request.
83+
BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);
84+
85+
// Process the results, just get the first result, since only one file was sent in this
86+
// sample.
87+
for (AnnotateImageResponse imageResponse :
88+
response.getResponsesList().get(0).getResponsesList()) {
89+
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
90+
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
91+
for (Block block : page.getBlocksList()) {
92+
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
93+
for (Paragraph par : block.getParagraphsList()) {
94+
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
95+
for (Word word : par.getWordsList()) {
96+
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
97+
for (Symbol symbol : word.getSymbolsList()) {
98+
System.out.printf(
99+
"\t\t\tSymbol: %s, (confidence: %s)\n",
100+
symbol.getText(), symbol.getConfidence());
101+
}
102+
}
103+
}
104+
}
105+
}
106+
}
107+
}
108+
}
109+
}
110+
// [END vision_batch_annotate_files]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 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+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class BatchAnnotateFilesGcsTest {
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
}
48+
49+
@Test
50+
public void testSetEndpoint() throws IOException {
51+
BatchAnnotateFilesGcs.batchAnnotateFilesGcs(
52+
"gs://cloud-samples-data/vision/document_understanding/kafka.pdf");
53+
54+
String got = bout.toString();
55+
assertThat(got).contains("Word confidence");
56+
}
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 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+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class BatchAnnotateFilesTest {
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
}
48+
49+
@Test
50+
public void testSetEndpoint() throws IOException {
51+
BatchAnnotateFiles.batchAnnotateFiles("resources/kafka.pdf");
52+
53+
String got = bout.toString();
54+
assertThat(got).contains("Word confidence");
55+
}
56+
}

0 commit comments

Comments
 (0)