diff --git a/vision/cloud-client/src/main/java/com/example/vision/Detect.java b/vision/cloud-client/src/main/java/com/example/vision/Detect.java index f50f03e7c9f..9289d09d4f1 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/Detect.java +++ b/vision/cloud-client/src/main/java/com/example/vision/Detect.java @@ -27,7 +27,6 @@ import com.google.cloud.vision.v1.Feature.Type; import com.google.cloud.vision.v1.Image; import com.google.cloud.vision.v1.ImageAnnotatorClient; -import com.google.cloud.vision.v1.ImageAnnotatorSettings; import com.google.cloud.vision.v1.ImageSource; import com.google.cloud.vision.v1.LocationInfo; import com.google.cloud.vision.v1.Page; @@ -41,7 +40,6 @@ import com.google.cloud.vision.v1.WebDetection.WebPage; import com.google.cloud.vision.v1.Word; import com.google.protobuf.ByteString; -import org.threeten.bp.Duration; import java.io.FileInputStream; import java.io.IOException; @@ -54,18 +52,20 @@ public class Detect { /** * Detects entities,sentiment and syntax in a document using the Natural Language API. * + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws Exception, IOException { argsHelper(args, System.out); } /** * Helper that handles the input passed to the program. * + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void argsHelper(String[] args, PrintStream out) throws IOException { + public static void argsHelper(String[] args, PrintStream out) throws Exception, IOException { if (args.length < 1) { out.println("Usage:"); out.printf( @@ -160,9 +160,10 @@ public Detect() { * * @param filePath The path to the file to perform face detection on. * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectFaces(String filePath, PrintStream out) throws IOException { + public static void detectFaces(String filePath, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -173,24 +174,25 @@ public static void detectFaces(String filePath, PrintStream out) throws IOExcept AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("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()) { - out.printf( - "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", - annotation.getAngerLikelihood(), - annotation.getJoyLikelihood(), - annotation.getSurpriseLikelihood(), - annotation.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + out.printf( + "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } } } } @@ -200,19 +202,13 @@ public static void detectFaces(String filePath, PrintStream out) throws IOExcept * * @param gcsPath The path to the remote file to perform face detection on. * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectFacesGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectFacesGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); - ImageAnnotatorSettings.Builder imageAnnotatorSettingsBuilder = - ImageAnnotatorSettings.defaultBuilder(); - imageAnnotatorSettingsBuilder - .batchAnnotateImagesSettings() - .getRetrySettingsBuilder() - .setTotalTimeout(Duration.ofSeconds(30)); - ImageAnnotatorSettings settings = imageAnnotatorSettingsBuilder.build(); - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); Image img = Image.newBuilder().setSource(imgSource).build(); Feature feat = Feature.newBuilder().setType(Type.FACE_DETECTION).build(); @@ -221,24 +217,25 @@ public static void detectFacesGcs(String gcsPath, PrintStream out) throws IOExce AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - ImageAnnotatorClient client = ImageAnnotatorClient.create(settings); - BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("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()) { - out.printf( - "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", - annotation.getAngerLikelihood(), - annotation.getJoyLikelihood(), - annotation.getSurpriseLikelihood(), - annotation.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + out.printf( + "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } } } } @@ -248,9 +245,10 @@ public static void detectFacesGcs(String gcsPath, PrintStream out) throws IOExce * * @param filePath The path to the file to perform label detection on. * @param out A {@link PrintStream} to write detected labels to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLabels(String filePath, PrintStream out) throws IOException { + public static void detectLabels(String filePath, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -261,19 +259,20 @@ public static void detectLabels(String filePath, PrintStream out) throws IOExcep AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("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) -> out.printf("%s : %s\n", k, v.toString())); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString())); + } } } } @@ -283,9 +282,11 @@ public static void detectLabels(String filePath, PrintStream out) throws IOExcep * * @param gcsPath The path to the remote file to perform label detection on. * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLabelsGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectLabelsGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -295,20 +296,21 @@ public static void detectLabelsGcs(String gcsPath, PrintStream out) throws IOExc AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("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) -> - out.printf("%s : %s\n", k, v.toString())); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation.getAllFields().forEach((k, v) -> + out.printf("%s : %s\n", k, v.toString())); + } } } } @@ -318,9 +320,11 @@ public static void detectLabelsGcs(String gcsPath, PrintStream out) throws IOExc * * @param filePath The path to the file to perform landmark detection on. * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLandmarks(String filePath, PrintStream out) throws IOException { + public static void detectLandmarks(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -330,20 +334,21 @@ public static void detectLandmarks(String filePath, PrintStream out) throws IOEx AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } } } } @@ -353,9 +358,11 @@ public static void detectLandmarks(String filePath, PrintStream out) throws IOEx * * @param url The path to the file to perform landmark detection on. * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLandmarksUrl(String url, PrintStream out) throws IOException { + public static void detectLandmarksUrl(String url, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setImageUri(url).build(); @@ -365,20 +372,21 @@ public static void detectLandmarksUrl(String url, PrintStream out) throws IOExce AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } } } } @@ -388,9 +396,11 @@ public static void detectLandmarksUrl(String url, PrintStream out) throws IOExce * * @param gcsPath The path to the remote file to perform landmark detection on. * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -400,20 +410,21 @@ public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws IO AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } } } } @@ -423,9 +434,10 @@ public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws IO * * @param filePath The path to the local file to perform logo detection on. * @param out A {@link PrintStream} to write detected logos to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLogos(String filePath, PrintStream out) throws IOException { + public static void detectLogos(String filePath, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -436,19 +448,20 @@ public static void detectLogos(String filePath, PrintStream out) throws IOExcept AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - out.println(annotation.getDescription()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + out.println(annotation.getDescription()); + } } } } @@ -458,9 +471,11 @@ public static void detectLogos(String filePath, PrintStream out) throws IOExcept * * @param gcsPath The path to the remote file to perform logo detection on. * @param out A {@link PrintStream} to write detected logos to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLogosGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectLogosGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -470,19 +485,20 @@ public static void detectLogosGcs(String gcsPath, PrintStream out) throws IOExce AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { - out.println(annotation.getDescription()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + out.println(annotation.getDescription()); + } } } } @@ -492,9 +508,10 @@ public static void detectLogosGcs(String gcsPath, PrintStream out) throws IOExce * * @param filePath The path to the file to detect text in. * @param out A {@link PrintStream} to write the detected text to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectText(String filePath, PrintStream out) throws IOException { + public static void detectText(String filePath, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -505,20 +522,21 @@ public static void detectText(String filePath, PrintStream out) throws IOExcepti AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - out.printf("Text: %s\n", annotation.getDescription()); - out.printf("Position : %s\n", annotation.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + out.printf("Text: %s\n", annotation.getDescription()); + out.printf("Position : %s\n", annotation.getBoundingPoly()); + } } } } @@ -528,9 +546,10 @@ public static void detectText(String filePath, PrintStream out) throws IOExcepti * * @param gcsPath The path to the remote file to detect text in. * @param out A {@link PrintStream} to write the detected text to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectTextGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectTextGcs(String gcsPath, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -540,20 +559,21 @@ public static void detectTextGcs(String gcsPath, PrintStream out) throws IOExcep AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - for (EntityAnnotation annotation : res.getTextAnnotationsList()) { - out.printf("Text: %s\n", annotation.getDescription()); - out.printf("Position : %s\n", annotation.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + out.printf("Text: %s\n", annotation.getDescription()); + out.printf("Position : %s\n", annotation.getBoundingPoly()); + } } } } @@ -563,9 +583,11 @@ public static void detectTextGcs(String gcsPath, PrintStream out) throws IOExcep * * @param filePath The path to the file to detect properties. * @param out A {@link PrintStream} to write + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectProperties(String filePath, PrintStream out) throws IOException { + public static void detectProperties(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -576,25 +598,26 @@ public static void detectProperties(String filePath, PrintStream out) throws IOE AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - out.printf( - "fraction: %f\nr: %f, g: %f, b: %f\n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + out.printf( + "fraction: %f\nr: %f, g: %f, b: %f\n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } } } } @@ -604,9 +627,11 @@ public static void detectProperties(String filePath, PrintStream out) throws IOE * * @param gcsPath The path to the remote file to detect properties on. * @param out A {@link PrintStream} to write + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -616,25 +641,26 @@ public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws I AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - out.printf( - "fraction: %f\nr: %f, g: %f, b: %f\n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + out.printf( + "fraction: %f\nr: %f, g: %f, b: %f\n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } } } } @@ -644,9 +670,11 @@ public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws I * * @param filePath The path to the local file used for safe search detection. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectSafeSearch(String filePath, PrintStream out) throws IOException { + public static void detectSafeSearch(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -657,24 +685,25 @@ public static void detectSafeSearch(String filePath, PrintStream out) throws IOE AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - out.printf( - "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + out.printf( + "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence()); + } } } @@ -683,9 +712,11 @@ public static void detectSafeSearch(String filePath, PrintStream out) throws IOE * * @param gcsPath The path to the remote file to detect safe-search on. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -695,24 +726,25 @@ public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws I AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - out.printf( - "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + out.printf( + "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence()); + } } } @@ -721,9 +753,11 @@ public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws I * * @param filePath The path to the local file used for web annotation detection. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectWebDetections(String filePath, PrintStream out) throws IOException { + public static void detectWebDetections(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -734,37 +768,38 @@ public static void detectWebDetections(String filePath, PrintStream out) throws AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // Search the web for usages of the image. You could use these signals later - // for user input moderation or linking external references. - // For a full list of available annotations, see http://g.co/cloud/vision/docs - WebDetection annotation = res.getWebDetection(); - out.println("Entity:Id:Score"); - out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " - + entity.getScore()); - } - out.println("\nPages with matching images: Score\n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - out.println(page.getUrl() + " : " + page.getScore()); - } - out.println("\nPages with partially matching images: Score\n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with fully matching images: Score\n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + out.println("Entity:Id:Score"); + out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + + entity.getScore()); + } + out.println("\nPages with matching images: Score\n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + out.println(page.getUrl() + " : " + page.getScore()); + } + out.println("\nPages with partially matching images: Score\n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with fully matching images: Score\n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } } } } @@ -774,9 +809,11 @@ public static void detectWebDetections(String filePath, PrintStream out) throws * * @param gcsPath The path to the remote file to detect safe-search on. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -786,37 +823,38 @@ public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throw AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // Search the web for usages of the image. You could use these signals later - // for user input moderation or linking external references. - // For a full list of available annotations, see http://g.co/cloud/vision/docs - WebDetection annotation = res.getWebDetection(); - out.println("Entity:Id:Score"); - out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " - + entity.getScore()); - } - out.println("\nPages with matching images: Score\n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - out.println(page.getUrl() + " : " + page.getScore()); - } - out.println("\nPages with partially matching images: Score\n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); - } - out.println("\nPages with fully matching images: Score\n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - out.println(image.getUrl() + " : " + image.getScore()); + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + out.println("Entity:Id:Score"); + out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + + entity.getScore()); + } + out.println("\nPages with matching images: Score\n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + out.println(page.getUrl() + " : " + page.getScore()); + } + out.println("\nPages with partially matching images: Score\n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with fully matching images: Score\n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } } } } @@ -826,9 +864,11 @@ public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throw * * @param filePath The path to the local file used for web annotation detection. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectCropHints(String filePath, PrintStream out) throws IOException { + public static void detectCropHints(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -839,20 +879,21 @@ public static void detectCropHints(String filePath, PrintStream out) throws IOEx AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - out.println(hint.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + out.println(hint.getBoundingPoly()); + } } } } @@ -862,9 +903,11 @@ public static void detectCropHints(String filePath, PrintStream out) throws IOEx * * @param gcsPath The path to the remote file to detect safe-search on. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -874,20 +917,21 @@ public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws IO AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - out.println(hint.getBoundingPoly()); + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + out.println(hint.getBoundingPoly()); + } } } } @@ -897,9 +941,11 @@ public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws IO * * @param filePath The path to the local file to detect document text on. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectDocumentText(String filePath, PrintStream out) throws IOException { + public static void detectDocumentText(String filePath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -910,40 +956,42 @@ public static void detectDocumentText(String filePath, PrintStream out) throws I AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + client.close(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } - // For full list of available annotations, see http://g.co/cloud/vision/docs - TextAnnotation annotation = res.getFullTextAnnotation(); - for (Page page: annotation.getPagesList()) { - String pageText = ""; - for (Block block : page.getBlocksList()) { - String blockText = ""; - for (Paragraph para : block.getParagraphsList()) { - String paraText = ""; - for (Word word: para.getWordsList()) { - String wordText = ""; - for (Symbol symbol: word.getSymbolsList()) { - wordText = wordText + symbol.getText(); + // For full list of available annotations, see http://g.co/cloud/vision/docs + TextAnnotation annotation = res.getFullTextAnnotation(); + for (Page page: annotation.getPagesList()) { + String pageText = ""; + for (Block block : page.getBlocksList()) { + String blockText = ""; + for (Paragraph para : block.getParagraphsList()) { + String paraText = ""; + for (Word word: para.getWordsList()) { + String wordText = ""; + for (Symbol symbol: word.getSymbolsList()) { + wordText = wordText + symbol.getText(); + } + paraText = paraText + wordText; } - paraText = paraText + wordText; + // Output Example using Paragraph: + out.println("Paragraph: \n" + paraText); + out.println("Bounds: \n" + para.getBoundingBox() + "\n"); + blockText = blockText + paraText; } - // Output Example using Paragraph: - out.println("Paragraph: \n" + paraText); - out.println("Bounds: \n" + para.getBoundingBox() + "\n"); - blockText = blockText + paraText; + pageText = pageText + blockText; } - pageText = pageText + blockText; } + out.println(annotation.getText()); } - out.println(annotation.getText()); } } @@ -952,9 +1000,11 @@ public static void detectDocumentText(String filePath, PrintStream out) throws I * * @param gcsPath The path to the remote file to detect document text on. * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws IOException { + public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws Exception, + IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -964,39 +1014,41 @@ public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); - BatchAnnotateImagesResponse response = - ImageAnnotatorClient.create().batchAnnotateImages(requests); - List responses = response.getResponsesList(); + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + client.close(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - out.printf("Error: %s\n", res.getError().getMessage()); - return; - } - // For full list of available annotations, see http://g.co/cloud/vision/docs - TextAnnotation annotation = res.getFullTextAnnotation(); - for (Page page: annotation.getPagesList()) { - String pageText = ""; - for (Block block : page.getBlocksList()) { - String blockText = ""; - for (Paragraph para : block.getParagraphsList()) { - String paraText = ""; - for (Word word: para.getWordsList()) { - String wordText = ""; - for (Symbol symbol: word.getSymbolsList()) { - wordText = wordText + symbol.getText(); + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + // For full list of available annotations, see http://g.co/cloud/vision/docs + TextAnnotation annotation = res.getFullTextAnnotation(); + for (Page page: annotation.getPagesList()) { + String pageText = ""; + for (Block block : page.getBlocksList()) { + String blockText = ""; + for (Paragraph para : block.getParagraphsList()) { + String paraText = ""; + for (Word word: para.getWordsList()) { + String wordText = ""; + for (Symbol symbol: word.getSymbolsList()) { + wordText = wordText + symbol.getText(); + } + paraText = paraText + wordText; } - paraText = paraText + wordText; + // Output Example using Paragraph: + out.println("Paragraph: \n" + paraText); + out.println("Bounds: \n" + para.getBoundingBox() + "\n"); + blockText = blockText + paraText; } - // Output Example using Paragraph: - out.println("Paragraph: \n" + paraText); - out.println("Bounds: \n" + para.getBoundingBox() + "\n"); - blockText = blockText + paraText; + pageText = pageText + blockText; } - pageText = pageText + blockText; } + out.println(annotation.getText()); } - out.println(annotation.getText()); } } } diff --git a/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java b/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java index 195b67f16a4..53e840c43e0 100644 --- a/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java +++ b/vision/cloud-client/src/main/java/com/example/vision/QuickstartSample.java @@ -37,38 +37,40 @@ public class QuickstartSample { public static void main(String... args) throws Exception { // Instantiates a client - ImageAnnotatorClient vision = ImageAnnotatorClient.create(); + try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) { - // The path to the image file to annotate - String fileName = "./resources/wakeupcat.jpg"; + // The path to the image file to annotate + String fileName = "./resources/wakeupcat.jpg"; - // Reads the image file into memory - Path path = Paths.get(fileName); - byte[] data = Files.readAllBytes(path); - ByteString imgBytes = ByteString.copyFrom(data); + // Reads the image file into memory + Path path = Paths.get(fileName); + byte[] data = Files.readAllBytes(path); + ByteString imgBytes = ByteString.copyFrom(data); - // Builds the image annotation request - List requests = new ArrayList<>(); - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); - AnnotateImageRequest request = AnnotateImageRequest.newBuilder() - .addFeatures(feat) - .setImage(img) - .build(); - requests.add(request); + // Builds the image annotation request + List requests = new ArrayList<>(); + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); + AnnotateImageRequest request = AnnotateImageRequest.newBuilder() + .addFeatures(feat) + .setImage(img) + .build(); + requests.add(request); - // Performs label detection on the image file - BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); - List responses = response.getResponsesList(); + // Performs label detection on the image file + BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); + List responses = response.getResponsesList(); - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - System.out.printf("Error: %s\n", res.getError().getMessage()); - return; - } + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.printf("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())); + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation.getAllFields().forEach((k, v)-> + System.out.printf("%s : %s\n", k, v.toString())); + } } } } diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java b/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java index 6315f92363b..656c62a8d8f 100644 --- a/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectIT.java @@ -61,7 +61,7 @@ public void testFaces() throws Exception { String got = bout.toString(); assertThat(got).contains("anger: POSSIBLE"); assertThat(got).contains("joy: POSSIBLE"); - assertThat(got).contains("surprise: UNLIKELY"); + assertThat(got).contains("surprise: LIKELY"); } @Test @@ -74,7 +74,7 @@ public void testFacesGcs() throws Exception { String got = bout.toString(); assertThat(got).contains("anger: POSSIBLE"); assertThat(got).contains("joy: POSSIBLE"); - assertThat(got).contains("surprise: UNLIKELY"); + assertThat(got).contains("surprise: LIKELY"); } @Test