From b0c55e12a17d12e2f32203485fe5a17a149ea637 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 8 Jun 2020 14:43:09 -0700 Subject: [PATCH] chore: splitting sample into single files --- .../main/java/com/example/vision/Detect.java | 903 +----------------- .../vision/snippets/DetectCropHints.java | 76 ++ .../vision/snippets/DetectCropHintsGcs.java | 75 ++ .../vision/snippets/DetectLandmarks.java | 75 ++ .../vision/snippets/DetectLandmarksGcs.java | 75 ++ .../vision/snippets/DetectLandmarksUrl.java | 72 ++ .../example/vision/snippets/DetectLogos.java | 75 ++ .../vision/snippets/DetectLogosGcs.java | 73 ++ .../vision/snippets/DetectProperties.java | 81 ++ .../vision/snippets/DetectPropertiesGcs.java | 81 ++ .../vision/snippets/DetectSafeSearch.java | 79 ++ .../vision/snippets/DetectSafeSearchGcs.java | 80 ++ .../example/vision/snippets/DetectText.java | 75 ++ .../vision/snippets/DetectTextGcs.java | 74 ++ .../vision/snippets/DetectWebDetections.java | 105 ++ .../snippets/DetectWebDetectionsGcs.java | 99 ++ .../vision/snippets/DetectWebEntities.java | 75 ++ .../vision/snippets/DetectWebEntitiesGcs.java | 70 ++ .../DetectWebEntitiesIncludeGeoResults.java | 87 ++ ...DetectWebEntitiesIncludeGeoResultsGcs.java | 84 ++ .../vision/DetectCropHintsGcsTest.java | 63 ++ .../example/vision/DetectCropHintsTest.java | 60 ++ .../java/com/example/vision/DetectIT.java | 209 +--- .../vision/DetectLandmarksGcsTest.java | 73 ++ .../example/vision/DetectLandmarksTest.java | 57 ++ .../example/vision/DetectLogosGcsTest.java | 60 ++ .../com/example/vision/DetectLogosTest.java | 57 ++ .../vision/DetectPropertiesGcsTest.java | 63 ++ .../example/vision/DetectPropertiesTest.java | 60 ++ .../vision/DetectSafeSearchGcsTest.java | 61 ++ .../example/vision/DetectSafeSearchTest.java | 58 ++ .../com/example/vision/DetectTextGcsTest.java | 60 ++ .../com/example/vision/DetectTextTest.java | 57 ++ .../vision/DetectWebDetectionsGcsTest.java | 62 ++ .../vision/DetectWebDetectionsTest.java | 58 ++ .../vision/DetectWebEntitiesGcsTest.java | 59 ++ ...ctWebEntitiesIncludeGeoResultsGcsTest.java | 60 ++ ...etectWebEntitiesIncludeGeoResultsTest.java | 58 ++ .../example/vision/DetectWebEntitiesTest.java | 57 ++ 39 files changed, 2599 insertions(+), 1107 deletions(-) create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java create mode 100644 vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java create mode 100644 vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java 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 26d3f44d9ec..5beb7d143a2 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 @@ -31,928 +31,33 @@ import com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse; import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; import com.google.cloud.vision.v1.Block; -import com.google.cloud.vision.v1.ColorInfo; -import com.google.cloud.vision.v1.CropHint; -import com.google.cloud.vision.v1.CropHintsAnnotation; -import com.google.cloud.vision.v1.DominantColorsAnnotation; -import com.google.cloud.vision.v1.EntityAnnotation; import com.google.cloud.vision.v1.Feature; import com.google.cloud.vision.v1.Feature.Type; import com.google.cloud.vision.v1.GcsDestination; import com.google.cloud.vision.v1.GcsSource; import com.google.cloud.vision.v1.Image; import com.google.cloud.vision.v1.ImageAnnotatorClient; -import com.google.cloud.vision.v1.ImageContext; import com.google.cloud.vision.v1.ImageSource; import com.google.cloud.vision.v1.InputConfig; import com.google.cloud.vision.v1.LocalizedObjectAnnotation; -import com.google.cloud.vision.v1.LocationInfo; import com.google.cloud.vision.v1.OperationMetadata; import com.google.cloud.vision.v1.OutputConfig; import com.google.cloud.vision.v1.Page; import com.google.cloud.vision.v1.Paragraph; -import com.google.cloud.vision.v1.SafeSearchAnnotation; import com.google.cloud.vision.v1.Symbol; import com.google.cloud.vision.v1.TextAnnotation; -import com.google.cloud.vision.v1.WebDetection; -import com.google.cloud.vision.v1.WebDetection.WebEntity; -import com.google.cloud.vision.v1.WebDetection.WebImage; -import com.google.cloud.vision.v1.WebDetection.WebLabel; -import com.google.cloud.vision.v1.WebDetection.WebPage; -import com.google.cloud.vision.v1.WebDetectionParams; import com.google.cloud.vision.v1.Word; import com.google.protobuf.ByteString; import com.google.protobuf.util.JsonFormat; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Detect { - - /** - * Detects landmarks in the specified local image. - * - * @param filePath The path to the file to perform landmark detection on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_landmark_detection] - public static void detectLandmarks(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - // [END vision_landmark_detection] - - /** - * Detects landmarks in the specified URI. - * - * @param uri The path to the file to perform landmark detection on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectLandmarksUrl(String uri) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - - /** - * Detects landmarks in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark - * detection on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_landmark_detection_gcs] - public static void detectLandmarksGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { - LocationInfo info = annotation.getLocationsList().listIterator().next(); - System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); - } - } - } - } - // [END vision_landmark_detection_gcs] - - /** - * Detects logos in the specified local image. - * - * @param filePath The path to the local file to perform logo detection on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_logo_detection] - public static void detectLogos(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.LOGO_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 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.getLogoAnnotationsList()) { - System.out.println(annotation.getDescription()); - } - } - } - } - // [END vision_logo_detection] - - /** - * Detects logos in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to perform logo detection - * on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_logo_detection_gcs] - public static void detectLogosGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.LOGO_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 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.getLogoAnnotationsList()) { - System.out.println(annotation.getDescription()); - } - } - } - } - // [END vision_logo_detection_gcs] - - /** - * Detects text in the specified image. - * - * @param filePath The path to the file to detect text in. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_text_detection] - public static void detectText(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.TEXT_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 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.getTextAnnotationsList()) { - System.out.format("Text: %s%n", annotation.getDescription()); - System.out.format("Position : %s%n", annotation.getBoundingPoly()); - } - } - } - } - // [END vision_text_detection] - - /** - * Detects text in the specified remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect text in. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_text_detection_gcs] - public static void detectTextGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.TEXT_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 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.getTextAnnotationsList()) { - System.out.format("Text: %s%n", annotation.getDescription()); - System.out.format("Position : %s%n", annotation.getBoundingPoly()); - } - } - } - } - // [END vision_text_detection_gcs] - - /** - * Detects image properties such as color frequency from the specified local image. - * - * @param filePath The path to the file to detect properties. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_image_property_detection] - public static void detectProperties(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).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 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 - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - System.out.format( - "fraction: %f%nr: %f, g: %f, b: %f%n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); - } - } - } - } - // [END vision_image_property_detection] - - /** - * Detects image properties such as color frequency from the specified remote image on Google - * Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect properties on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_image_property_detection_gcs] - public static void detectPropertiesGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.IMAGE_PROPERTIES).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 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 - DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); - for (ColorInfo color : colors.getColorsList()) { - System.out.format( - "fraction: %f%nr: %f, g: %f, b: %f%n", - color.getPixelFraction(), - color.getColor().getRed(), - color.getColor().getGreen(), - color.getColor().getBlue()); - } - } - } - } - // [END vision_image_property_detection_gcs] - - /** - * Detects whether the specified image has features you would want to moderate. - * - * @param filePath The path to the local file used for safe search detection. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_safe_search_detection] - public static void detectSafeSearch(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_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 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 - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - System.out.format( - "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence(), - annotation.getRacy()); - } - } - } - // [END vision_safe_search_detection] - - /** - * Detects whether the specified image on Google Cloud Storage has features you would want to - * moderate. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_safe_search_detection_gcs] - public static void detectSafeSearchGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_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 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 - SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); - System.out.format( - "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", - annotation.getAdult(), - annotation.getMedical(), - annotation.getSpoof(), - annotation.getViolence(), - annotation.getRacy()); - } - } - } - // [END vision_safe_search_detection_gcs] - - // [START vision_web_detection] - /** - * Finds references to the specified image on the web. - * - * @param filePath The path to the local file used for web annotation detection. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebDetections(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.WEB_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 responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - System.out.format("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(); - System.out.println("Entity:Id:Score"); - System.out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - System.out.println( - entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); - } - for (WebLabel label : annotation.getBestGuessLabelsList()) { - System.out.format("%nBest guess label: %s", label.getLabel()); - } - System.out.println("%nPages with matching images: Score%n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - System.out.println(page.getUrl() + " : " + page.getScore()); - } - System.out.println("%nPages with partially matching images: Score%n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - System.out.println("%nPages with fully matching images: Score%n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - System.out.println("%nPages with visually similar images: Score%n=="); - for (WebImage image : annotation.getVisuallySimilarImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - } - } - } - // [END vision_web_detection] - - // [START vision_web_detection_gcs] - /** - * Detects whether the remote image on Google Cloud Storage has features you would want to - * moderate. - * - * @param gcsPath The path to the remote on Google Cloud Storage file to detect web annotations. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebDetectionsGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.WEB_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 responses = response.getResponsesList(); - - for (AnnotateImageResponse res : responses) { - if (res.hasError()) { - System.out.format("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(); - System.out.println("Entity:Id:Score"); - System.out.println("==============="); - for (WebEntity entity : annotation.getWebEntitiesList()) { - System.out.println( - entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); - } - for (WebLabel label : annotation.getBestGuessLabelsList()) { - System.out.format("%nBest guess label: %s", label.getLabel()); - } - System.out.println("%nPages with matching images: Score%n=="); - for (WebPage page : annotation.getPagesWithMatchingImagesList()) { - System.out.println(page.getUrl() + " : " + page.getScore()); - } - System.out.println("%nPages with partially matching images: Score%n=="); - for (WebImage image : annotation.getPartialMatchingImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - System.out.println("%nPages with fully matching images: Score%n=="); - for (WebImage image : annotation.getFullMatchingImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - System.out.println("%nPages with visually similar images: Score%n=="); - for (WebImage image : annotation.getVisuallySimilarImagesList()) { - System.out.println(image.getUrl() + " : " + image.getScore()); - } - } - } - } - // [END vision_web_detection_gcs] - - /** - * Find web entities given a local image. - * - * @param filePath The path of the image to detect. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntities(String filePath) throws Exception, IOException { - - // 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()) { - // Read in the local image - ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); - - // Build the image - Image image = Image.newBuilder().setContent(contents).build(); - - // Create the request with the image and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response.getResponsesList().stream() - .forEach( - r -> - r.getWebDetection().getWebEntitiesList().stream() - .forEach( - entity -> { - System.out.format("Description: %s%n", entity.getDescription()); - System.out.format("Score: %f%n", entity.getScore()); - })); - } - } - - /** - * Find web entities given the remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesGcs(String gcsPath) throws Exception, IOException { - - // 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()) { - // Set the image source to the given gs uri - ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - // Build the image - Image image = Image.newBuilder().setSource(imageSource).build(); - - // Create the request with the image and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response.getResponsesList().stream() - .forEach( - r -> - r.getWebDetection().getWebEntitiesList().stream() - .forEach( - entity -> { - System.out.format("Description: %s%n", entity.getDescription()); - System.out.format("Score: %f%n", entity.getScore()); - })); - } - } - - // [START vision_web_detection_include_geo] - /** - * Find web entities given a local image. - * - * @param filePath The path of the image to detect. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesIncludeGeoResults(String filePath) - throws Exception, IOException { - - // 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()) { - // Read in the local image - ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); - - // Build the image - Image image = Image.newBuilder().setContent(contents).build(); - - // Enable `IncludeGeoResults` - WebDetectionParams webDetectionParams = - WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); - - // Set the parameters for the image - ImageContext imageContext = - ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); - - // Create the request with the image, imageContext, and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .setImageContext(imageContext) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response.getResponsesList().stream() - .forEach( - r -> - r.getWebDetection().getWebEntitiesList().stream() - .forEach( - entity -> { - System.out.format("Description: %s%n", entity.getDescription()); - System.out.format("Score: %f%n", entity.getScore()); - })); - } - } - // [END vision_web_detection_include_geo] - - // [START vision_web_detection_include_geo_gcs] - /** - * Find web entities given the remote image on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities with - * geo results. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath) - throws Exception, IOException { - - // 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()) { - // Set the image source to the given gs uri - ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - // Build the image - Image image = Image.newBuilder().setSource(imageSource).build(); - - // Enable `IncludeGeoResults` - WebDetectionParams webDetectionParams = - WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); - - // Set the parameters for the image - ImageContext imageContext = - ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); - - // Create the request with the image, imageContext, and the specified feature: web detection - AnnotateImageRequest request = - AnnotateImageRequest.newBuilder() - .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) - .setImage(image) - .setImageContext(imageContext) - .build(); - - // Perform the request - BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); - - // Display the results - response.getResponsesList().stream() - .forEach( - r -> - r.getWebDetection().getWebEntitiesList().stream() - .forEach( - entity -> { - System.out.format("Description: %s%n", entity.getDescription()); - System.out.format("Score: %f%n", entity.getScore()); - })); - } - } - // [END vision_web_detection_include_geo_gcs] - - /** - * Suggests a region to crop to for a local file. - * - * @param filePath The path to the local file used for web annotation detection. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_crop_hint_detection] - public static void detectCropHints(String filePath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); - - Image img = Image.newBuilder().setContent(imgBytes).build(); - Feature feat = Feature.newBuilder().setType(Type.CROP_HINTS).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 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 - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - System.out.println(hint.getBoundingPoly()); - } - } - } - } - // [END vision_crop_hint_detection] - - /** - * Suggests a region to crop to for a remote file on Google Cloud Storage. - * - * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. - * @throws Exception on errors while closing the client. - * @throws IOException on Input/Output errors. - */ - // [START vision_crop_hint_detection_gcs] - public static void detectCropHintsGcs(String gcsPath) throws Exception, IOException { - List requests = new ArrayList<>(); - - ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); - Image img = Image.newBuilder().setSource(imgSource).build(); - Feature feat = Feature.newBuilder().setType(Type.CROP_HINTS).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 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 - CropHintsAnnotation annotation = res.getCropHintsAnnotation(); - for (CropHint hint : annotation.getCropHintsList()) { - System.out.println(hint.getBoundingPoly()); - } - } - } - } - // [END vision_crop_hint_detection_gcs] - /** * Performs document text detection on a local image file. * @@ -961,7 +66,7 @@ public static void detectCropHintsGcs(String gcsPath) throws Exception, IOExcept * @throws IOException on Input/Output errors. */ // [START vision_fulltext_detection] - public static void detectDocumentText(String filePath) throws Exception, IOException { + public static void detectDocumentText(String filePath) throws IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -1029,7 +134,7 @@ public static void detectDocumentText(String filePath) throws Exception, IOExcep * @throws IOException on Input/Output errors. */ // [START vision_fulltext_detection_gcs] - public static void detectDocumentTextGcs(String gcsPath) throws Exception, IOException { + public static void detectDocumentTextGcs(String gcsPath) throws IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); @@ -1216,7 +321,7 @@ public static void detectDocumentsGcs(String gcsSourcePath, String gcsDestinatio * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLocalizedObjects(String filePath) throws Exception, IOException { + public static void detectLocalizedObjects(String filePath) throws IOException { List requests = new ArrayList<>(); ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); @@ -1262,7 +367,7 @@ public static void detectLocalizedObjects(String filePath) throws Exception, IOE * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLocalizedObjectsGcs(String gcsPath) throws Exception, IOException { + public static void detectLocalizedObjectsGcs(String gcsPath) throws IOException { List requests = new ArrayList<>(); ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java new file mode 100644 index 00000000000..705a87ce0f8 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHints.java @@ -0,0 +1,76 @@ +/* + * 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_crop_hint_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.CropHint; +import com.google.cloud.vision.v1.CropHintsAnnotation; +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 DetectCropHints { + public static void detectCropHints() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectCropHints(filePath); + } + + // Suggests a region to crop to for a local file. + public static void detectCropHints(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).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 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 + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + System.out.println(hint.getBoundingPoly()); + } + } + } + } +} +// [END vision_crop_hint_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java new file mode 100644 index 00000000000..9448a4612c7 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectCropHintsGcs.java @@ -0,0 +1,75 @@ +/* + * 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_crop_hint_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.CropHint; +import com.google.cloud.vision.v1.CropHintsAnnotation; +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 DetectCropHintsGcs { + + public static void detectCropHintsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectCropHintsGcs(filePath); + } + + // Suggests a region to crop to for a remote file on Google Cloud Storage. + public static void detectCropHintsGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).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 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 + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + System.out.println(hint.getBoundingPoly()); + } + } + } + } +} +// [END vision_crop_hint_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java new file mode 100644 index 00000000000..5147e1e11dc --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarks.java @@ -0,0 +1,75 @@ +/* + * 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_landmark_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.cloud.vision.v1.LocationInfo; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarks { + public static void detectLandmarks() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectLandmarks(filePath); + } + + // Detects landmarks in the specified local image. + public static void detectLandmarks(String filePath) throws IOException { + List requests = new ArrayList<>(); + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} +// [END vision_landmark_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java new file mode 100644 index 00000000000..c5ff0c1e129 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksGcs.java @@ -0,0 +1,75 @@ +/* + * 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_landmark_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.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.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.LocationInfo; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarksGcs { + + public static void detectLandmarksGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLandmarksGcs(filePath); + } + + // Detects landmarks in the specified remote image on Google Cloud Storage. + public static void detectLandmarksGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} +// [END vision_landmark_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java new file mode 100644 index 00000000000..ff52931863e --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLandmarksUrl.java @@ -0,0 +1,72 @@ +/* + * 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; + +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.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.LocationInfo; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLandmarksUrl { + + public static void detectLandmarksUrl() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLandmarksUrl(filePath); + } + + // Detects landmarks in the specified URI. + public static void detectLandmarksUrl(String uri) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_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 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.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng()); + } + } + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java new file mode 100644 index 00000000000..22785a21dd0 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogos.java @@ -0,0 +1,75 @@ +/* + * 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_logo_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 DetectLogos { + public static void detectLogos() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectLogos(filePath); + } + + // Detects logos in the specified local image. + + public static void detectLogos(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LOGO_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 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.getLogoAnnotationsList()) { + System.out.println(annotation.getDescription()); + } + } + } + } +} +// [END vision_logo_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java new file mode 100644 index 00000000000..e7e0c2002bd --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectLogosGcs.java @@ -0,0 +1,73 @@ +/* + * 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_logo_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.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.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectLogosGcs { + + public static void detectLogosGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectLogosGcs(filePath); + } + + // Detects logos in the specified remote image on Google Cloud Storage. + public static void detectLogosGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.LOGO_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 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.getLogoAnnotationsList()) { + System.out.println(annotation.getDescription()); + } + } + } + } +} +// [END vision_logo_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java new file mode 100644 index 00000000000..9fd022b6469 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectProperties.java @@ -0,0 +1,81 @@ +/* + * 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_image_property_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.ColorInfo; +import com.google.cloud.vision.v1.DominantColorsAnnotation; +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 DetectProperties { + public static void detectProperties() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectProperties(filePath); + } + + // Detects image properties such as color frequency from the specified local image. + public static void detectProperties(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).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 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 + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + System.out.format( + "fraction: %f%nr: %f, g: %f, b: %f%n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } +} +// [END vision_image_property_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java new file mode 100644 index 00000000000..2a974c63296 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectPropertiesGcs.java @@ -0,0 +1,81 @@ +/* + * 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_image_property_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.ColorInfo; +import com.google.cloud.vision.v1.DominantColorsAnnotation; +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 DetectPropertiesGcs { + + public static void detectPropertiesGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectPropertiesGcs(filePath); + } + + // Detects image properties such as color frequency from the specified remote image on Google + // Cloud Storage. + public static void detectPropertiesGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.IMAGE_PROPERTIES).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 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 + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + System.out.format( + "fraction: %f%nr: %f, g: %f, b: %f%n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } +} +// [END vision_image_property_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java new file mode 100644 index 00000000000..351869f8172 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearch.java @@ -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_safe_search_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.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.SafeSearchAnnotation; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectSafeSearch { + public static void detectSafeSearch() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectSafeSearch(filePath); + } + + // Detects whether the specified image has features you would want to moderate. + public static void detectSafeSearch(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.SAFE_SEARCH_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 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 + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + System.out.format( + "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } +} +// [END vision_safe_search_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java new file mode 100644 index 00000000000..619404b6753 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectSafeSearchGcs.java @@ -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_safe_search_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.Feature; +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.ImageSource; +import com.google.cloud.vision.v1.SafeSearchAnnotation; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectSafeSearchGcs { + + public static void detectSafeSearchGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectSafeSearchGcs(filePath); + } + + // Detects whether the specified image on Google Cloud Storage has features you would want to + // moderate. + public static void detectSafeSearchGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Type.SAFE_SEARCH_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 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 + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + System.out.format( + "adult: %s%nmedical: %s%nspoofed: %s%nviolence: %s%nracy: %s%n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } +} +// [END vision_safe_search_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java new file mode 100644 index 00000000000..8cac7288a5e --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectText.java @@ -0,0 +1,75 @@ +/* + * 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_text_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 DetectText { + public static void detectText() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectText(filePath); + } + + // Detects text in the specified image. + public static void detectText(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_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 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.getTextAnnotationsList()) { + System.out.format("Text: %s%n", annotation.getDescription()); + System.out.format("Position : %s%n", annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_text_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java new file mode 100644 index 00000000000..18bedcdcf5c --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectTextGcs.java @@ -0,0 +1,74 @@ +/* + * 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_text_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.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.cloud.vision.v1.ImageSource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectTextGcs { + + public static void detectTextGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectTextGcs(filePath); + } + + // Detects text in the specified remote image on Google Cloud Storage. + public static void detectTextGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_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 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.getTextAnnotationsList()) { + System.out.format("Text: %s%n", annotation.getDescription()); + System.out.format("Position : %s%n", annotation.getBoundingPoly()); + } + } + } + } +} +// [END vision_text_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java new file mode 100644 index 00000000000..98a74f4d691 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetections.java @@ -0,0 +1,105 @@ +/* + * 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_web_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.Feature; +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.WebDetection; +import com.google.cloud.vision.v1.WebDetection.WebEntity; +import com.google.cloud.vision.v1.WebDetection.WebImage; +import com.google.cloud.vision.v1.WebDetection.WebLabel; +import com.google.cloud.vision.v1.WebDetection.WebPage; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectWebDetections { + + public static void detectWebDetections() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebDetections(filePath); + } + + // Finds references to the specified image on the web. + public static void detectWebDetections(String filePath) throws IOException { + List requests = new ArrayList<>(); + + ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath)); + + Image img = Image.newBuilder().setContent(imgBytes).build(); + Feature feat = Feature.newBuilder().setType(Type.WEB_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 responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("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(); + System.out.println("Entity:Id:Score"); + System.out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + System.out.println( + entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); + } + for (WebLabel label : annotation.getBestGuessLabelsList()) { + System.out.format("%nBest guess label: %s", label.getLabel()); + } + System.out.println("%nPages with matching images: Score%n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + System.out.println(page.getUrl() + " : " + page.getScore()); + } + System.out.println("%nPages with partially matching images: Score%n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with fully matching images: Score%n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with visually similar images: Score%n=="); + for (WebImage image : annotation.getVisuallySimilarImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } +} +// [END vision_web_detection] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java new file mode 100644 index 00000000000..484a6711cc6 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebDetectionsGcs.java @@ -0,0 +1,99 @@ +/* + * 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_web_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.Feature; +import com.google.cloud.vision.v1.Image; +import com.google.cloud.vision.v1.ImageAnnotatorClient; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.WebDetection; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class DetectWebDetectionsGcs { + + public static void detectWebDetectionsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebDetectionsGcs(filePath); + } + + // Detects whether the remote image on Google Cloud Storage has features you would want to + // moderate. + public static void detectWebDetectionsGcs(String gcsPath) throws IOException { + List requests = new ArrayList<>(); + + ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + Image img = Image.newBuilder().setSource(imgSource).build(); + Feature feat = Feature.newBuilder().setType(Feature.Type.WEB_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 responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + System.out.format("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(); + System.out.println("Entity:Id:Score"); + System.out.println("==============="); + for (WebDetection.WebEntity entity : annotation.getWebEntitiesList()) { + System.out.println( + entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore()); + } + for (WebDetection.WebLabel label : annotation.getBestGuessLabelsList()) { + System.out.format("%nBest guess label: %s", label.getLabel()); + } + System.out.println("%nPages with matching images: Score%n=="); + for (WebDetection.WebPage page : annotation.getPagesWithMatchingImagesList()) { + System.out.println(page.getUrl() + " : " + page.getScore()); + } + System.out.println("%nPages with partially matching images: Score%n=="); + for (WebDetection.WebImage image : annotation.getPartialMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with fully matching images: Score%n=="); + for (WebDetection.WebImage image : annotation.getFullMatchingImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + System.out.println("%nPages with visually similar images: Score%n=="); + for (WebDetection.WebImage image : annotation.getVisuallySimilarImagesList()) { + System.out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } +} +// [END vision_web_detection_gcs] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java new file mode 100644 index 00000000000..c8e99065754 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntities.java @@ -0,0 +1,75 @@ +/* + * 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_web_detection] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +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.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntities { + + public static void detectWebEntities() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebEntities(filePath); + } + + // Find web entities given a local image. + public static void detectWebEntities(String filePath) throws IOException { + + // 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()) { + // Read in the local image + ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); + + // Build the image + Image image = Image.newBuilder().setContent(contents).build(); + + // Create the request with the image and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) + .setImage(image) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java new file mode 100644 index 00000000000..5e4fab05bc9 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesGcs.java @@ -0,0 +1,70 @@ +/* + * 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; + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +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.Arrays; + +public class DetectWebEntitiesGcs { + + public static void detectWebEntitiesGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebEntitiesGcs(filePath); + } + + // Find web entities given the remote image on Google Cloud Storage. + public static void detectWebEntitiesGcs(String gcsPath) throws IOException { + + // 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()) { + // Set the image source to the given gs uri + ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + // Build the image + Image image = Image.newBuilder().setSource(imageSource).build(); + + // Create the request with the image and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)) + .setImage(image) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java new file mode 100644 index 00000000000..f1a0f58c969 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResults.java @@ -0,0 +1,87 @@ +/* + * 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_web_detection_include_geo] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1.Feature; +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.ImageContext; +import com.google.cloud.vision.v1.WebDetectionParams; +import com.google.protobuf.ByteString; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntitiesIncludeGeoResults { + + public static void detectWebEntitiesIncludeGeoResults() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "path/to/your/image/file.jpg"; + detectWebEntitiesIncludeGeoResults(filePath); + } + + // Find web entities given a local image. + public static void detectWebEntitiesIncludeGeoResults(String filePath) throws IOException { + + // 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()) { + // Read in the local image + ByteString contents = ByteString.readFrom(new FileInputStream(filePath)); + + // Build the image + Image image = Image.newBuilder().setContent(contents).build(); + + // Enable `IncludeGeoResults` + WebDetectionParams webDetectionParams = + WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); + + // Set the parameters for the image + ImageContext imageContext = + ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); + + // Create the request with the image, imageContext, and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) + .setImage(image) + .setImageContext(imageContext) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} +// [END vision_web_detection_include_geo] diff --git a/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java new file mode 100644 index 00000000000..d6dc1941ea3 --- /dev/null +++ b/vision/cloud-client/src/main/java/com/example/vision/snippets/DetectWebEntitiesIncludeGeoResultsGcs.java @@ -0,0 +1,84 @@ +/* + * 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_web_detection_include_geo_gcs] + +import com.google.cloud.vision.v1.AnnotateImageRequest; +import com.google.cloud.vision.v1.BatchAnnotateImagesResponse; +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.ImageContext; +import com.google.cloud.vision.v1.ImageSource; +import com.google.cloud.vision.v1.WebDetectionParams; +import java.io.IOException; +import java.util.Arrays; + +public class DetectWebEntitiesIncludeGeoResultsGcs { + + public static void detectWebEntitiesIncludeGeoResultsGcs() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg"; + detectWebEntitiesIncludeGeoResultsGcs(filePath); + } + + // Find web entities given the remote image on Google Cloud Storage. + public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath) throws IOException { + + // 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()) { + // Set the image source to the given gs uri + ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build(); + // Build the image + Image image = Image.newBuilder().setSource(imageSource).build(); + + // Enable `IncludeGeoResults` + WebDetectionParams webDetectionParams = + WebDetectionParams.newBuilder().setIncludeGeoResults(true).build(); + + // Set the parameters for the image + ImageContext imageContext = + ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build(); + + // Create the request with the image, imageContext, and the specified feature: web detection + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder() + .addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)) + .setImage(image) + .setImageContext(imageContext) + .build(); + + // Perform the request + BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); + + // Display the results + response.getResponsesList().stream() + .forEach( + r -> + r.getWebDetection().getWebEntitiesList().stream() + .forEach( + entity -> { + System.out.format("Description: %s%n", entity.getDescription()); + System.out.format("Score: %f%n", entity.getScore()); + })); + } + } +} +// [END vision_web_detection_include_geo_gcs] diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java new file mode 100644 index 00000000000..acbf5503d44 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsGcsTest.java @@ -0,0 +1,63 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectCropHintsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.regex.Pattern; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectCropHintsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCropHintsGcs() throws Exception { + // Act + DetectCropHintsGcs.detectCropHintsGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("vertices {"); + assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); + assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java new file mode 100644 index 00000000000..2a6b11c2b6d --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectCropHintsTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectCropHints; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.regex.Pattern; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectCropHintsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCropHints() throws Exception { + // Act + DetectCropHints.detectCropHints("./resources/wakeupcat.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("vertices {"); + assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); + assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); + } +} 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 0b4eb18ab40..6ccdcf7eb6b 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 @@ -26,7 +26,6 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.UUID; -import java.util.regex.Pattern; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,8 +38,8 @@ public class DetectIT { private ByteArrayOutputStream bout; private PrintStream out; - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String ASSET_BUCKET = "cloud-samples-data"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String OUTPUT_BUCKET = PROJECT_ID; private static final String OUTPUT_PREFIX = "OCR_PDF_TEST_OUTPUT_" + UUID.randomUUID().toString(); @@ -56,212 +55,6 @@ public void tearDown() { System.setOut(null); } - @Test - public void testLandmarks() throws Exception { - // Act - Detect.detectLandmarks("./resources/landmark.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLandmarksGcs() throws Exception { - // Act - Detect.detectLandmarksGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLandmarksUrl() throws Exception { - // Act - String uri = - "https://storage-download.googleapis.com/" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"; - Detect.detectLandmarksUrl(uri); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("palace of fine arts"); - } - - @Test - public void testLogos() throws Exception { - // Act - Detect.detectLogos("./resources/logos.png"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("google"); - } - - @Test - public void testLogosGcs() throws Exception { - // Act - Detect.detectLogosGcs("gs://" + ASSET_BUCKET + "/vision/logo/logo_google.png"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("google"); - } - - @Test - public void testText() throws Exception { - // Act - Detect.detectText("./resources/text.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("37%"); - } - - @Test - public void testTextGcs() throws Exception { - // Act - Detect.detectTextGcs("gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("37%"); - } - - @Test - public void testSafeSearch() throws Exception { - // Act - Detect.detectSafeSearch("./resources/wakeupcat.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("adult:"); - assertThat(got).contains("racy:"); - } - - @Test - public void testSafeSearchGcs() throws Exception { - // Act - Detect.detectSafeSearchGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("adult:"); - assertThat(got).contains("racy:"); - } - - @Test - public void testProperties() throws Exception { - // Act - Detect.detectProperties("./resources/landmark.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("fraction:"); - assertThat(got).contains("r:"); - assertThat(got).contains("g:"); - assertThat(got).contains("b:"); - } - - @Test - public void testPropertiesGcs() throws Exception { - // Act - Detect.detectPropertiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("fraction:"); - assertThat(got).contains("r:"); - assertThat(got).contains("g:"); - assertThat(got).contains("b:"); - } - - @Test - public void detectWebAnnotations() throws Exception { - // Act - Detect.detectWebDetections("./resources/landmark.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("entity:id:score"); - assertThat(got).contains("best guess label"); - } - - @Test - public void detectWebAnnotationsGcs() throws Exception { - // Act - Detect.detectWebDetectionsGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).contains("entity:id:score"); - assertThat(got).contains("best guess label"); - } - - @Test - public void testDetectWebEntities() throws Exception { - // Act - Detect.detectWebEntities("./resources/city.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - assertThat(got).doesNotContain("zepra"); - } - - @Test - public void testDetectWebEntitiesGcs() throws Exception { - // Act - Detect.detectWebEntitiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); - - String got = bout.toString().toLowerCase(); - assertThat(got).contains("description"); - } - - @Test - public void testDetectWebEntitiesIncludeGeoResults() throws Exception { - // Act - Detect.detectWebEntitiesIncludeGeoResults("./resources/city.jpg"); - - // Assert - String got = bout.toString().toLowerCase(); - // Note: entities and labels can change over time. - assertThat(got).doesNotContain("error"); - } - - @Test - public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { - // Act - Detect.detectWebEntitiesIncludeGeoResultsGcs( - "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); - - String got = bout.toString().toLowerCase(); - assertThat(got).contains("description"); - } - - @Test - public void testCropHints() throws Exception { - // Act - Detect.detectCropHints("./resources/wakeupcat.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("vertices {"); - assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); - assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); - } - - @Test - public void testCropHintsGcs() throws Exception { - // Act - Detect.detectCropHintsGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); - - // Assert - String got = bout.toString(); - assertThat(got).contains("vertices {"); - assertThat(got).containsMatch(Pattern.compile("x: 2\\d{2}")); - assertThat(got).containsMatch(Pattern.compile("y: 4\\d{2}")); - } - @Test public void testDocumentText() throws Exception { // Act diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java new file mode 100644 index 00000000000..ef9e4c5ca9e --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksGcsTest.java @@ -0,0 +1,73 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLandmarksGcs; +import com.example.vision.snippets.DetectLandmarksUrl; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLandmarksGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLandmarksUrl() throws Exception { + // Act + String uri = + "https://storage-download.googleapis.com/" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"; + DetectLandmarksUrl.detectLandmarksUrl(uri); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } + + @Test + public void testLandmarksGcs() throws Exception { + // Act + DetectLandmarksGcs.detectLandmarksGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java new file mode 100644 index 00000000000..d937a7f11c8 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLandmarksTest.java @@ -0,0 +1,57 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLandmarks; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLandmarksTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLandmarks() throws Exception { + // Act + DetectLandmarks.detectLandmarks("./resources/landmark.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("palace of fine arts"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java new file mode 100644 index 00000000000..1f6cd1c5201 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosGcsTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLogosGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLogosGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLogosGcs() throws Exception { + // Act + DetectLogosGcs.detectLogosGcs("gs://" + ASSET_BUCKET + "/vision/logo/logo_google.png"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("google"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java new file mode 100644 index 00000000000..ed3cf7f41ce --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectLogosTest.java @@ -0,0 +1,57 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectLogos; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectLogosTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testLogos() throws Exception { + // Act + DetectLogos.detectLogos("./resources/logos.png"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("google"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java new file mode 100644 index 00000000000..c0ff61caa05 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesGcsTest.java @@ -0,0 +1,63 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectPropertiesGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectPropertiesGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPropertiesGcs() throws Exception { + // Act + DetectPropertiesGcs.detectPropertiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("fraction:"); + assertThat(got).contains("r:"); + assertThat(got).contains("g:"); + assertThat(got).contains("b:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java new file mode 100644 index 00000000000..e591bf01128 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectPropertiesTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectProperties; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectPropertiesTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testProperties() throws Exception { + // Act + DetectProperties.detectProperties("./resources/landmark.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("fraction:"); + assertThat(got).contains("r:"); + assertThat(got).contains("g:"); + assertThat(got).contains("b:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java new file mode 100644 index 00000000000..6939b7a1c9b --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchGcsTest.java @@ -0,0 +1,61 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectSafeSearchGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectSafeSearchGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testSafeSearchGcs() throws Exception { + // Act + DetectSafeSearchGcs.detectSafeSearchGcs("gs://" + ASSET_BUCKET + "/vision/label/wakeupcat.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("adult:"); + assertThat(got).contains("racy:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java new file mode 100644 index 00000000000..abdd1d99bf7 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectSafeSearchTest.java @@ -0,0 +1,58 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectSafeSearch; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectSafeSearchTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testSafeSearch() throws Exception { + // Act + DetectSafeSearch.detectSafeSearch("./resources/wakeupcat.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("adult:"); + assertThat(got).contains("racy:"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java new file mode 100644 index 00000000000..7ec54a14fa8 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectTextGcsTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectTextGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectTextGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testTextGcs() throws Exception { + // Act + DetectTextGcs.detectTextGcs("gs://" + ASSET_BUCKET + "/vision/text/screen.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java new file mode 100644 index 00000000000..c55c0f4e24e --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectTextTest.java @@ -0,0 +1,57 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectText; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectTextTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testText() throws Exception { + // Act + DetectText.detectText("./resources/text.jpg"); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java new file mode 100644 index 00000000000..9829294257a --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsGcsTest.java @@ -0,0 +1,62 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebDetectionsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebDetectionsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebAnnotationsGcs() throws Exception { + // Act + DetectWebDetectionsGcs.detectWebDetectionsGcs( + "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("entity:id:score"); + assertThat(got).contains("best guess label"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java new file mode 100644 index 00000000000..d0cc10b53d7 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebDetectionsTest.java @@ -0,0 +1,58 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebDetections; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebDetectionsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void detectWebAnnotations() throws Exception { + // Act + DetectWebDetections.detectWebDetections("./resources/landmark.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).contains("entity:id:score"); + assertThat(got).contains("best guess label"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java new file mode 100644 index 00000000000..596451bf79a --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesGcsTest.java @@ -0,0 +1,59 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesGcs() throws Exception { + // Act + DetectWebEntitiesGcs.detectWebEntitiesGcs("gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + String got = bout.toString().toLowerCase(); + assertThat(got).contains("description"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java new file mode 100644 index 00000000000..bedd5bde23f --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsGcsTest.java @@ -0,0 +1,60 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesIncludeGeoResultsGcs; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesIncludeGeoResultsGcsTest { + + private static final String ASSET_BUCKET = "cloud-samples-data"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { + // Act + DetectWebEntitiesIncludeGeoResultsGcs.detectWebEntitiesIncludeGeoResultsGcs( + "gs://" + ASSET_BUCKET + "/vision/landmark/pofa.jpg"); + + String got = bout.toString().toLowerCase(); + assertThat(got).contains("description"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java new file mode 100644 index 00000000000..b455c761d96 --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesIncludeGeoResultsTest.java @@ -0,0 +1,58 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntitiesIncludeGeoResults; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesIncludeGeoResultsTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResults() throws Exception { + // Act + DetectWebEntitiesIncludeGeoResults.detectWebEntitiesIncludeGeoResults("./resources/city.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + // Note: entities and labels can change over time. + assertThat(got).doesNotContain("error"); + } +} diff --git a/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java new file mode 100644 index 00000000000..f2a37676bdc --- /dev/null +++ b/vision/cloud-client/src/test/java/com/example/vision/DetectWebEntitiesTest.java @@ -0,0 +1,57 @@ +/* + * 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.example.vision.snippets.DetectWebEntities; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectWebEntitiesTest { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDetectWebEntities() throws Exception { + // Act + DetectWebEntities.detectWebEntities("./resources/city.jpg"); + + // Assert + String got = bout.toString().toLowerCase(); + assertThat(got).doesNotContain("zepra"); + } +}