Skip to content

Commit 3a4cd0c

Browse files
munkhuushmglchingor13
authored andcommitted
samples: chore: added comment on try/catch block (#2980)
* chore: added comment on try/catch block * started breaking down detect file * nits * formatted new files
1 parent 1f8b0aa commit 3a4cd0c

19 files changed

+815
-610
lines changed

vision/snippets/src/main/java/com/example/vision/Detect.java

+208-481
Large diffs are not rendered by default.

vision/snippets/src/main/java/com/example/vision/QuickstartSample.java renamed to vision/snippets/src/main/java/com/example/vision/quickstart/QuickstartSample.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.vision;
17+
package com.example.vision.quickstart;
1818

1919
// [START vision_quickstart]
2020
// Imports the Google Cloud client library
@@ -36,7 +36,9 @@
3636

3737
public class QuickstartSample {
3838
public static void main(String... args) throws Exception {
39-
// Instantiates a client
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
4042
try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
4143

4244
// The path to the image file to annotate
@@ -61,14 +63,14 @@ public static void main(String... args) throws Exception {
6163

6264
for (AnnotateImageResponse res : responses) {
6365
if (res.hasError()) {
64-
System.out.printf("Error: %s\n", res.getError().getMessage());
66+
System.out.format("Error: %s%n", res.getError().getMessage());
6567
return;
6668
}
6769

6870
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
6971
annotation
7072
.getAllFields()
71-
.forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString()));
73+
.forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
7274
}
7375
}
7476
}

vision/snippets/src/main/java/com/example/vision/AsyncBatchAnnotateImages.java renamed to vision/snippets/src/main/java/com/example/vision/snippets/AsyncBatchAnnotateImages.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.vision;
17+
package com.example.vision.snippets;
1818

1919
// [START vision_async_batch_annotate_images]
2020
import com.google.cloud.vision.v1.AnnotateImageRequest;
@@ -82,7 +82,7 @@ public static void asyncBatchAnnotateImages(String inputImageUri, String outputU
8282

8383
// The output is written to GCS with the provided output_uri as prefix
8484
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
85-
System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri);
85+
System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri);
8686
}
8787
}
8888
}

vision/snippets/src/main/java/com/example/vision/BatchAnnotateFiles.java renamed to vision/snippets/src/main/java/com/example/vision/snippets/BatchAnnotateFiles.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.vision;
17+
package com.example.vision.snippets;
1818

1919
// [START vision_batch_annotate_files]
2020
import com.google.cloud.vision.v1.AnnotateFileRequest;
@@ -90,17 +90,17 @@ public static void batchAnnotateFiles(String filePath) throws IOException {
9090
// sample.
9191
for (AnnotateImageResponse imageResponse :
9292
response.getResponsesList().get(0).getResponsesList()) {
93-
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
93+
System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
9494
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
9595
for (Block block : page.getBlocksList()) {
96-
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
96+
System.out.format("%nBlock confidence: %s%n", block.getConfidence());
9797
for (Paragraph par : block.getParagraphsList()) {
98-
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
98+
System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
9999
for (Word word : par.getWordsList()) {
100-
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
100+
System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
101101
for (Symbol symbol : word.getSymbolsList()) {
102-
System.out.printf(
103-
"\t\t\tSymbol: %s, (confidence: %s)\n",
102+
System.out.format(
103+
"\t\t\tSymbol: %s, (confidence: %s)%n",
104104
symbol.getText(), symbol.getConfidence());
105105
}
106106
}

vision/snippets/src/main/java/com/example/vision/BatchAnnotateFilesGcs.java renamed to vision/snippets/src/main/java/com/example/vision/snippets/BatchAnnotateFilesGcs.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.vision;
17+
package com.example.vision.snippets;
1818

1919
// [START vision_batch_annotate_files_gcs]
2020
import com.google.cloud.vision.v1.AnnotateFileRequest;
@@ -85,17 +85,17 @@ public static void batchAnnotateFilesGcs(String gcsUri) throws IOException {
8585
// sample.
8686
for (AnnotateImageResponse imageResponse :
8787
response.getResponsesList().get(0).getResponsesList()) {
88-
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
88+
System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
8989
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
9090
for (Block block : page.getBlocksList()) {
91-
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
91+
System.out.format("%nBlock confidence: %s%n", block.getConfidence());
9292
for (Paragraph par : block.getParagraphsList()) {
93-
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
93+
System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
9494
for (Word word : par.getWordsList()) {
95-
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
95+
System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
9696
for (Symbol symbol : word.getSymbolsList()) {
97-
System.out.printf(
98-
"\t\t\tSymbol: %s, (confidence: %s)\n",
97+
System.out.format(
98+
"\t\t\tSymbol: %s, (confidence: %s)%n",
9999
symbol.getText(), symbol.getConfidence());
100100
}
101101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.snippets;
18+
19+
// [START vision_face_detection]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.FaceAnnotation;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.Image;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.protobuf.ByteString;
29+
import java.io.FileInputStream;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
public class DetectFaces {
35+
36+
public static void detectFaces() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "path/to/your/image/file.jpg";
39+
detectFaces(filePath);
40+
}
41+
42+
// Detects faces in the specified local image.
43+
public static void detectFaces(String filePath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
46+
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
47+
48+
Image img = Image.newBuilder().setContent(imgBytes).build();
49+
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
50+
AnnotateImageRequest request =
51+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
52+
requests.add(request);
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
58+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
59+
List<AnnotateImageResponse> responses = response.getResponsesList();
60+
61+
for (AnnotateImageResponse res : responses) {
62+
if (res.hasError()) {
63+
System.out.format("Error: %s%n", res.getError().getMessage());
64+
return;
65+
}
66+
67+
// For full list of available annotations, see http://g.co/cloud/vision/docs
68+
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
69+
System.out.format(
70+
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
71+
annotation.getAngerLikelihood(),
72+
annotation.getJoyLikelihood(),
73+
annotation.getSurpriseLikelihood(),
74+
annotation.getBoundingPoly());
75+
}
76+
}
77+
}
78+
}
79+
}
80+
// [END vision_face_detection]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.snippets;
18+
19+
// [START vision_face_detection_gcs]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.FaceAnnotation;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.Image;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.cloud.vision.v1.ImageSource;
29+
import java.io.IOException;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
public class DetectFacesGcs {
34+
35+
public static void detectFacesGcs() throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
38+
detectFacesGcs(filePath);
39+
}
40+
41+
// Detects faces in the specified remote image on Google Cloud Storage.
42+
public static void detectFacesGcs(String gcsPath) throws IOException {
43+
List<AnnotateImageRequest> requests = new ArrayList<>();
44+
45+
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
46+
Image img = Image.newBuilder().setSource(imgSource).build();
47+
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
48+
49+
AnnotateImageRequest request =
50+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
51+
requests.add(request);
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
57+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
58+
List<AnnotateImageResponse> responses = response.getResponsesList();
59+
60+
for (AnnotateImageResponse res : responses) {
61+
if (res.hasError()) {
62+
System.out.format("Error: %s%n", res.getError().getMessage());
63+
return;
64+
}
65+
66+
// For full list of available annotations, see http://g.co/cloud/vision/docs
67+
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
68+
System.out.format(
69+
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
70+
annotation.getAngerLikelihood(),
71+
annotation.getJoyLikelihood(),
72+
annotation.getSurpriseLikelihood(),
73+
annotation.getBoundingPoly());
74+
}
75+
}
76+
}
77+
}
78+
}
79+
// [END vision_face_detection_gcs]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.snippets;
18+
19+
// [START vision_label_detection]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.EntityAnnotation;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.Image;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.protobuf.ByteString;
29+
import java.io.FileInputStream;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
public class DetectLabels {
35+
36+
public static void detectLabels() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "path/to/your/image/file.jpg";
39+
detectLabels(filePath);
40+
}
41+
42+
// Detects labels in the specified local image.
43+
public static void detectLabels(String filePath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
46+
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
47+
48+
Image img = Image.newBuilder().setContent(imgBytes).build();
49+
Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
50+
AnnotateImageRequest request =
51+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
52+
requests.add(request);
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
58+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
59+
List<AnnotateImageResponse> responses = response.getResponsesList();
60+
61+
for (AnnotateImageResponse res : responses) {
62+
if (res.hasError()) {
63+
System.out.format("Error: %s%n", res.getError().getMessage());
64+
return;
65+
}
66+
67+
// For full list of available annotations, see http://g.co/cloud/vision/docs
68+
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
69+
annotation
70+
.getAllFields()
71+
.forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
72+
}
73+
}
74+
}
75+
}
76+
}
77+
// [END vision_label_detection]

0 commit comments

Comments
 (0)