Skip to content

Commit

Permalink
chore: added comment on try/catch block (#2980)
Browse files Browse the repository at this point in the history
* chore: added comment on try/catch block

* started breaking down detect file

* nits

* formatted new files
  • Loading branch information
munkhuushmgl authored May 28, 2020
1 parent 86cea4d commit 93d7891
Show file tree
Hide file tree
Showing 20 changed files with 816 additions and 611 deletions.
2 changes: 1 addition & 1 deletion vision/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
</execution>
</executions>
<configuration>
<mainClass>com.example.vision.QuickstartSample</mainClass>
<mainClass>com.example.vision.quickstart.QuickstartSample</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
Expand Down
689 changes: 208 additions & 481 deletions vision/cloud-client/src/main/java/com/example/vision/Detect.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.example.vision;
package com.example.vision.quickstart;

// [START vision_quickstart]
// Imports the Google Cloud client library
Expand All @@ -36,7 +36,9 @@

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

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

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.printf("Error: %s\n", res.getError().getMessage());
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}

for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation
.getAllFields()
.forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString()));
.forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.example.vision;
package com.example.vision.snippets;

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

// The output is written to GCS with the provided output_uri as prefix
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
System.out.printf("Output written to GCS with prefix: %s\n", gcsOutputUri);
System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.example.vision;
package com.example.vision.snippets;

// [START vision_batch_annotate_files]
import com.google.cloud.vision.v1.AnnotateFileRequest;
Expand Down Expand Up @@ -90,17 +90,17 @@ public static void batchAnnotateFiles(String filePath) throws IOException {
// sample.
for (AnnotateImageResponse imageResponse :
response.getResponsesList().get(0).getResponsesList()) {
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
for (Block block : page.getBlocksList()) {
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
System.out.format("%nBlock confidence: %s%n", block.getConfidence());
for (Paragraph par : block.getParagraphsList()) {
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
for (Word word : par.getWordsList()) {
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
for (Symbol symbol : word.getSymbolsList()) {
System.out.printf(
"\t\t\tSymbol: %s, (confidence: %s)\n",
System.out.format(
"\t\t\tSymbol: %s, (confidence: %s)%n",
symbol.getText(), symbol.getConfidence());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.example.vision;
package com.example.vision.snippets;

// [START vision_batch_annotate_files_gcs]
import com.google.cloud.vision.v1.AnnotateFileRequest;
Expand Down Expand Up @@ -85,17 +85,17 @@ public static void batchAnnotateFilesGcs(String gcsUri) throws IOException {
// sample.
for (AnnotateImageResponse imageResponse :
response.getResponsesList().get(0).getResponsesList()) {
System.out.printf("Full text: %s\n", imageResponse.getFullTextAnnotation().getText());
System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
for (Block block : page.getBlocksList()) {
System.out.printf("\nBlock confidence: %s\n", block.getConfidence());
System.out.format("%nBlock confidence: %s%n", block.getConfidence());
for (Paragraph par : block.getParagraphsList()) {
System.out.printf("\tParagraph confidence: %s\n", par.getConfidence());
System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
for (Word word : par.getWordsList()) {
System.out.printf("\t\tWord confidence: %s\n", word.getConfidence());
System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
for (Symbol symbol : word.getSymbolsList()) {
System.out.printf(
"\t\t\tSymbol: %s, (confidence: %s)\n",
System.out.format(
"\t\t\tSymbol: %s, (confidence: %s)%n",
symbol.getText(), symbol.getConfidence());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.vision.snippets;

// [START vision_face_detection]

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.FaceAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DetectFaces {

public static void detectFaces() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "path/to/your/image/file.jpg";
detectFaces(filePath);
}

// Detects faces in the specified local image.
public static void detectFaces(String filePath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();

ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}

// For full list of available annotations, see http://g.co/cloud/vision/docs
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
System.out.format(
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
annotation.getAngerLikelihood(),
annotation.getJoyLikelihood(),
annotation.getSurpriseLikelihood(),
annotation.getBoundingPoly());
}
}
}
}
}
// [END vision_face_detection]
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.vision.snippets;

// [START vision_face_detection_gcs]

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.FaceAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.cloud.vision.v1.ImageSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DetectFacesGcs {

public static void detectFacesGcs() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
detectFacesGcs(filePath);
}

// Detects faces in the specified remote image on Google Cloud Storage.
public static void detectFacesGcs(String gcsPath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();

ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.FACE_DETECTION).build();

AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}

// For full list of available annotations, see http://g.co/cloud/vision/docs
for (FaceAnnotation annotation : res.getFaceAnnotationsList()) {
System.out.format(
"anger: %s%njoy: %s%nsurprise: %s%nposition: %s",
annotation.getAngerLikelihood(),
annotation.getJoyLikelihood(),
annotation.getSurpriseLikelihood(),
annotation.getBoundingPoly());
}
}
}
}
}
// [END vision_face_detection_gcs]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.vision.snippets;

// [START vision_label_detection]

import com.google.cloud.vision.v1.AnnotateImageRequest;
import com.google.cloud.vision.v1.AnnotateImageResponse;
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
import com.google.cloud.vision.v1.EntityAnnotation;
import com.google.cloud.vision.v1.Feature;
import com.google.cloud.vision.v1.Image;
import com.google.cloud.vision.v1.ImageAnnotatorClient;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class DetectLabels {

public static void detectLabels() throws IOException {
// TODO(developer): Replace these variables before running the sample.
String filePath = "path/to/your/image/file.jpg";
detectLabels(filePath);
}

// Detects labels in the specified local image.
public static void detectLabels(String filePath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();

ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));

Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
AnnotateImageRequest request =
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
requests.add(request);

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();

for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
System.out.format("Error: %s%n", res.getError().getMessage());
return;
}

// For full list of available annotations, see http://g.co/cloud/vision/docs
for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
annotation
.getAllFields()
.forEach((k, v) -> System.out.format("%s : %s%n", k, v.toString()));
}
}
}
}
}
// [END vision_label_detection]
Loading

0 comments on commit 93d7891

Please sign in to comment.