diff --git a/pom.xml b/pom.xml index 4ad9de0c0ed..0f8154e45e7 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,7 @@ unittests video/cloud-client + vision/beta/cloud-client vision/cloud-client vision/face-detection vision/label diff --git a/vision/beta/cloud-client/README.md b/vision/beta/cloud-client/README.md new file mode 100644 index 00000000000..6c91ddbc61b --- /dev/null +++ b/vision/beta/cloud-client/README.md @@ -0,0 +1,87 @@ +# Image Feature Detection Sample + +[Google Cloud Vision API][vision] provides feature detection for images. +This API is part of the larger collection of Cloud Machine Learning APIs. + +This sample Java application demonstrates how to access the Cloud Vision API +using the [Google Cloud Client Library for Java][google-cloud-java]. + +[vision]: https://cloud.google.com/vision/docs/ +[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java + +## Build the sample + +Install [Maven](http://maven.apache.org/). + +Build your project with: + +``` +mvn clean package +``` + +You can then run `Detect` via: + +``` +mvn exec:java -DDetect -Dexec.args="arg1 'arg 2' arg3" +``` + +### Analyze an image + +``` +export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json +``` + +#### Faces +``` +mvn exec:java -DDetect -Dexec.args="faces ./resources/face_no_surprise.jpg" +``` + +#### Labels +``` +mvn exec:java -DDetect -Dexec.args="labels ./resources/wakeupcat.jpg" +``` + +#### Landmarks +``` +mvn exec:java -DDetect -Dexec.args="landmarks ./resources/landmark.jpg" +``` + +#### Logos +``` +mvn exec:java -DDetect -Dexec.args="logos ./resources/logos.png" +``` + +#### Text +``` +mvn exec:java -DDetect -Dexec.args="text ./resources/text.jpg" +``` + +#### Safe Search +``` +mvn exec:java -DDetect -Dexec.args="safe-search ./resources/wakeupcat.jpg" +``` + +#### Properties +``` +mvn exec:java -DDetect -Dexec.args="properties ./resources/city.jpg" +``` + +#### Web +``` +mvn exec:java -DDetect -Dexec.args="web ./resources/landmark.jpg" +``` + +#### Web Entities +``` +mvn exec:java -DDetect -Dexec.args="web-entities ./resources/landmark.jpg" +``` + +#### Web Entities Include Geo +``` +mvn exec:java -DDetect -Dexec.args="web-entities-include-geo ./resources/landmark.jpg" +``` + +#### Crop +``` +mvn exec:java -DDetect -Dexec.args="crop ./resources/landmark.jpg" +``` diff --git a/vision/beta/cloud-client/pom.xml b/vision/beta/cloud-client/pom.xml new file mode 100644 index 00000000000..ad4723f2d1d --- /dev/null +++ b/vision/beta/cloud-client/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + com.example.vision + vision-detect-beta + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../../.. + + + + 1.8 + 1.8 + UTF-8 + + + + + + com.google.cloud + google-cloud-vision + 1.14.0 + + + + + + junit + junit + 4.12 + test + + + + com.google.truth + truth + 0.36 + test + + + + + + Detect + + + Detect + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.Detect + false + + + + + + + diff --git a/vision/beta/cloud-client/resources/city.jpg b/vision/beta/cloud-client/resources/city.jpg new file mode 100644 index 00000000000..b14282e7539 Binary files /dev/null and b/vision/beta/cloud-client/resources/city.jpg differ diff --git a/vision/beta/cloud-client/resources/face_no_surprise.jpg b/vision/beta/cloud-client/resources/face_no_surprise.jpg new file mode 100644 index 00000000000..0e2894adb83 Binary files /dev/null and b/vision/beta/cloud-client/resources/face_no_surprise.jpg differ diff --git a/vision/beta/cloud-client/resources/landmark.jpg b/vision/beta/cloud-client/resources/landmark.jpg new file mode 100644 index 00000000000..41c3d0fc935 Binary files /dev/null and b/vision/beta/cloud-client/resources/landmark.jpg differ diff --git a/vision/beta/cloud-client/resources/logos.png b/vision/beta/cloud-client/resources/logos.png new file mode 100644 index 00000000000..dcfb4ac955f Binary files /dev/null and b/vision/beta/cloud-client/resources/logos.png differ diff --git a/vision/beta/cloud-client/resources/text.jpg b/vision/beta/cloud-client/resources/text.jpg new file mode 100644 index 00000000000..3b17d55de0e Binary files /dev/null and b/vision/beta/cloud-client/resources/text.jpg differ diff --git a/vision/beta/cloud-client/resources/wakeupcat.jpg b/vision/beta/cloud-client/resources/wakeupcat.jpg new file mode 100644 index 00000000000..139cf461eca Binary files /dev/null and b/vision/beta/cloud-client/resources/wakeupcat.jpg differ diff --git a/vision/beta/cloud-client/src/main/java/com/example/vision/Detect.java b/vision/beta/cloud-client/src/main/java/com/example/vision/Detect.java new file mode 100644 index 00000000000..8e53680d1f5 --- /dev/null +++ b/vision/beta/cloud-client/src/main/java/com/example/vision/Detect.java @@ -0,0 +1,1280 @@ +/* + 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 com.google.cloud.vision.v1p1beta1.AnnotateImageRequest; +import com.google.cloud.vision.v1p1beta1.AnnotateImageResponse; +import com.google.cloud.vision.v1p1beta1.BatchAnnotateImagesResponse; +import com.google.cloud.vision.v1p1beta1.Block; +import com.google.cloud.vision.v1p1beta1.ColorInfo; +import com.google.cloud.vision.v1p1beta1.CropHint; +import com.google.cloud.vision.v1p1beta1.CropHintsAnnotation; +import com.google.cloud.vision.v1p1beta1.DominantColorsAnnotation; +import com.google.cloud.vision.v1p1beta1.EntityAnnotation; +import com.google.cloud.vision.v1p1beta1.FaceAnnotation; +import com.google.cloud.vision.v1p1beta1.Feature; +import com.google.cloud.vision.v1p1beta1.Feature.Type; +import com.google.cloud.vision.v1p1beta1.Image; +import com.google.cloud.vision.v1p1beta1.ImageAnnotatorClient; +import com.google.cloud.vision.v1p1beta1.ImageContext; +import com.google.cloud.vision.v1p1beta1.ImageSource; +import com.google.cloud.vision.v1p1beta1.LocationInfo; +import com.google.cloud.vision.v1p1beta1.Page; +import com.google.cloud.vision.v1p1beta1.Paragraph; +import com.google.cloud.vision.v1p1beta1.SafeSearchAnnotation; +import com.google.cloud.vision.v1p1beta1.Symbol; +import com.google.cloud.vision.v1p1beta1.TextAnnotation; +import com.google.cloud.vision.v1p1beta1.WebDetection; +import com.google.cloud.vision.v1p1beta1.WebDetection.WebEntity; +import com.google.cloud.vision.v1p1beta1.WebDetection.WebImage; +import com.google.cloud.vision.v1p1beta1.WebDetection.WebLabel; +import com.google.cloud.vision.v1p1beta1.WebDetection.WebPage; +import com.google.cloud.vision.v1p1beta1.WebDetectionParams; +import com.google.cloud.vision.v1p1beta1.Word; + +import com.google.protobuf.ByteString; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Detect { + + /** + * Detects entities, sentiment, and syntax in a document using the Vision API. + * + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void main(String[] args) throws Exception, IOException { + argsHelper(args, System.out); + } + + /** + * Helper that handles the input passed to the program. + * + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void argsHelper(String[] args, PrintStream out) throws Exception, IOException { + if (args.length < 1) { + out.println("Usage:"); + out.printf( + "\tmvn exec:java -DDetect -Dexec.args=\" \"\n" + + "Commands:\n" + + "\tfaces | labels | landmarks | logos | text | safe-search | properties" + + "| web | web-entities | web-entities-include-geo | crop \n" + + "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage " + + "resource (gs://...)\n"); + return; + } + String command = args[0]; + String path = args.length > 1 ? args[1] : ""; + + if (command.equals("faces")) { + if (path.startsWith("gs://")) { + detectFacesGcs(path, out); + } else { + detectFaces(path, out); + } + } else if (command.equals("labels")) { + if (path.startsWith("gs://")) { + detectLabelsGcs(path, out); + } else { + detectLabels(path, out); + } + } else if (command.equals("landmarks")) { + if (path.startsWith("http")) { + detectLandmarksUrl(path, out); + } else if (path.startsWith("gs://")) { + detectLandmarksGcs(path, out); + } else { + detectLandmarks(path, out); + } + } else if (command.equals("logos")) { + if (path.startsWith("gs://")) { + detectLogosGcs(path, out); + } else { + detectLogos(path, out); + } + } else if (command.equals("text")) { + if (path.startsWith("gs://")) { + detectTextGcs(path, out); + } else { + detectText(path, out); + } + } else if (command.equals("properties")) { + if (path.startsWith("gs://")) { + detectPropertiesGcs(path, out); + } else { + detectProperties(path, out); + } + } else if (command.equals("safe-search")) { + if (path.startsWith("gs://")) { + detectSafeSearchGcs(path, out); + } else { + detectSafeSearch(path, out); + } + } else if (command.equals("web")) { + if (path.startsWith("gs://")) { + detectWebDetectionsGcs(path, out); + } else { + detectWebDetections(path, out); + } + } else if (command.equals("web-entities")) { + if (path.startsWith("gs://")) { + detectWebEntitiesGcs(path, out); + } else { + detectWebEntities(path, out); + } + } else if (command.equals("web-entities-include-geo")) { + if (path.startsWith("gs://")) { + detectWebEntitiesIncludeGeoResultsGcs(path, out); + } else { + detectWebEntitiesIncludeGeoResults(path, out); + } + } else if (command.equals("crop")) { + if (path.startsWith("gs://")) { + detectCropHintsGcs(path, out); + } else { + detectCropHints(path, out); + } + } else if (command.equals("fulltext")) { + if (path.startsWith("gs://")) { + detectDocumentTextGcs(path, out); + } else { + detectDocumentText(path, out); + } + } + } + + /** + * Detects faces in the specified local image. + * + * @param filePath The path to the file to perform face detection on. + * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectFaces(String filePath, PrintStream out) throws 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.FACE_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + out.printf( + "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } + } + } + } + + /** + * Detects faces in the specified remote image on Google Cloud Storage. + * + * @param gcsPath The path to the remote file on Google Cloud Storage to perform face detection + * on. + * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectFacesGcs(String gcsPath, PrintStream out) throws 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.FACE_DETECTION).build(); + + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (FaceAnnotation annotation : res.getFaceAnnotationsList()) { + out.printf( + "anger: %s\njoy: %s\nsurprise: %s\nposition: %s", + annotation.getAngerLikelihood(), + annotation.getJoyLikelihood(), + annotation.getSurpriseLikelihood(), + annotation.getBoundingPoly()); + } + } + } + } + + /** + * Detects labels in the specified local image. + * + * @param filePath The path to the file to perform label detection on. + * @param out A {@link PrintStream} to write detected labels to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLabels(String filePath, PrintStream out) throws 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.LABEL_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation.getAllFields().forEach((k, v) -> out.printf("%s : %s\n", k, v.toString())); + } + } + } + } + + /** + * Detects labels in the specified remote image on Google Cloud Storage. + * + * @param gcsPath The path to the remote file on Google Cloud Storage to perform label detection + * on. + * @param out A {@link PrintStream} to write detected features to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLabelsGcs(String gcsPath, PrintStream out) throws 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.LABEL_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { + annotation.getAllFields().forEach((k, v) -> + out.printf("%s : %s\n", k, v.toString())); + } + } + } + } + + /** + * Detects landmarks in the specified local image. + * + * @param filePath The path to the file to perform landmark detection on. + * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLandmarks(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } + } + } + } + + /** + * Detects landmarks in the specified URI. + * + * @param uri The path to the file to perform landmark detection on. + * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLandmarksUrl(String uri, PrintStream out) 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } + } + } + } + + /** + * Detects landmarks in the specified remote imageon Google Cloud Storage. + * + * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark + * detection on. + * @param out A {@link PrintStream} to write detected landmarks to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLandmarksGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) { + LocationInfo info = annotation.getLocationsList().listIterator().next(); + out.printf("Landmark: %s\n %s\n", annotation.getDescription(), info.getLatLng()); + } + } + } + } + + /** + * Detects logos in the specified local image. + * + * @param filePath The path to the local file to perform logo detection on. + * @param out A {@link PrintStream} to write detected logos to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLogos(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + out.println(annotation.getDescription()); + } + } + } + } + + /** + * 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. + * @param out A {@link PrintStream} to write detected logos to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectLogosGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getLogoAnnotationsList()) { + out.println(annotation.getDescription()); + } + } + } + } + + /** + * Detects text in the specified image. + * + * @param filePath The path to the file to detect text in. + * @param out A {@link PrintStream} to write the detected text to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectText(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + out.printf("Text: %s\n", annotation.getDescription()); + out.printf("Position : %s\n", annotation.getBoundingPoly()); + } + } + } + } + + /** + * 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. + * @param out A {@link PrintStream} to write the detected text to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectTextGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + for (EntityAnnotation annotation : res.getTextAnnotationsList()) { + out.printf("Text: %s\n", annotation.getDescription()); + out.printf("Position : %s\n", annotation.getBoundingPoly()); + } + } + } + } + + /** + * Detects image properties such as color frequency from the specified local image. + * + * @param filePath The path to the file to detect properties. + * @param out A {@link PrintStream} to write + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectProperties(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + out.printf( + "fraction: %f\nr: %f, g: %f, b: %f\n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } + + /** + * 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. + * @param out A {@link PrintStream} to write + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectPropertiesGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + DominantColorsAnnotation colors = res.getImagePropertiesAnnotation().getDominantColors(); + for (ColorInfo color : colors.getColorsList()) { + out.printf( + "fraction: %f\nr: %f, g: %f, b: %f\n", + color.getPixelFraction(), + color.getColor().getRed(), + color.getColor().getGreen(), + color.getColor().getBlue()); + } + } + } + } + + // [START vision_detect_safe_search] + /** + * 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectSafeSearch(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + out.printf( + "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } + // [END vision_detect_safe_search] + + // [START vision_detect_safe_search_uri] + /** + * 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectSafeSearchGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + SafeSearchAnnotation annotation = res.getSafeSearchAnnotation(); + out.printf( + "adult: %s\nmedical: %s\nspoofed: %s\nviolence: %s\nracy: %s\n", + annotation.getAdult(), + annotation.getMedical(), + annotation.getSpoof(), + annotation.getViolence(), + annotation.getRacy()); + } + } + } + // [END vision_detect_safe_search_uri] + + // [START vision_detect_web] + /** + * Finds references to the specified image on the web. + * + * @param filePath The path to the local file used for web annotation detection. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebDetections(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + out.println("Entity:Id:Score"); + out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + + entity.getScore()); + } + for (WebLabel label : annotation.getBestGuessLabelsList()) { + out.format("\nBest guess label: %s", label.getLabel()); + } + out.println("\nPages with matching images: Score\n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + out.println(page.getUrl() + " : " + page.getScore()); + } + out.println("\nPages with partially matching images: Score\n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with fully matching images: Score\n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with visually similar images: Score\n=="); + for (WebImage image : annotation.getVisuallySimilarImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } + // [END vision_detect_web] + + // [START vision_detect_web_uri] + /** + * 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebDetectionsGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // Search the web for usages of the image. You could use these signals later + // for user input moderation or linking external references. + // For a full list of available annotations, see http://g.co/cloud/vision/docs + WebDetection annotation = res.getWebDetection(); + out.println("Entity:Id:Score"); + out.println("==============="); + for (WebEntity entity : annotation.getWebEntitiesList()) { + out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + + entity.getScore()); + } + for (WebLabel label : annotation.getBestGuessLabelsList()) { + out.format("\nBest guess label: %s", label.getLabel()); + } + out.println("\nPages with matching images: Score\n=="); + for (WebPage page : annotation.getPagesWithMatchingImagesList()) { + out.println(page.getUrl() + " : " + page.getScore()); + } + out.println("\nPages with partially matching images: Score\n=="); + for (WebImage image : annotation.getPartialMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with fully matching images: Score\n=="); + for (WebImage image : annotation.getFullMatchingImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + out.println("\nPages with visually similar images: Score\n=="); + for (WebImage image : annotation.getVisuallySimilarImagesList()) { + out.println(image.getUrl() + " : " + image.getScore()); + } + } + } + } + // [END vision_detect_web_uri] + + /** + * Find web entities given a local image. + * @param filePath The path of the image to detect. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebEntities(String filePath, PrintStream out) throws Exception, + IOException { + // Instantiates a client + 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 -> { + out.format("Description: %s\n", entity.getDescription()); + 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebEntitiesGcs(String gcsPath, PrintStream out) throws Exception, + IOException { + // Instantiates a client + 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_entities_include_geo_results] + /** + * Find web entities given a local image. + * @param filePath The path of the image to detect. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebEntitiesIncludeGeoResults(String filePath, PrintStream out) throws + Exception, IOException { + // Instantiates a client + 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 -> { + out.format("Description: %s\n", entity.getDescription()); + out.format("Score: %f\n", entity.getScore()); + })); + } + } + // [END vision_web_entities_include_geo_results] + + // [START vision_web_entities_include_geo_results_uri] + /** + * 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath, PrintStream out) throws + Exception, IOException { + // Instantiates a client + 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 -> { + out.format("Description: %s\n", entity.getDescription()); + out.format("Score: %f\n", entity.getScore()); + })); + } + } + // [END vision_web_entities_include_geo_results_uri] + + /** + * Suggests a region to crop to for a local file. + * + * @param filePath The path to the local file used for web annotation detection. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectCropHints(String filePath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + out.println(hint.getBoundingPoly()); + } + } + } + } + + /** + * 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. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectCropHintsGcs(String gcsPath, PrintStream out) throws 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); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + CropHintsAnnotation annotation = res.getCropHintsAnnotation(); + for (CropHint hint : annotation.getCropHintsList()) { + out.println(hint.getBoundingPoly()); + } + } + } + } + + // [START vision_detect_document] + /** + * Performs document text detection on a local image file. + * + * @param filePath The path to the local file to detect document text on. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectDocumentText(String filePath, PrintStream out) throws 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.DOCUMENT_TEXT_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + client.close(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + + // For full list of available annotations, see http://g.co/cloud/vision/docs + TextAnnotation annotation = res.getFullTextAnnotation(); + for (Page page: annotation.getPagesList()) { + String pageText = ""; + for (Block block : page.getBlocksList()) { + String blockText = ""; + for (Paragraph para : block.getParagraphsList()) { + String paraText = ""; + for (Word word: para.getWordsList()) { + String wordText = ""; + for (Symbol symbol: word.getSymbolsList()) { + wordText = wordText + symbol.getText(); + out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), + symbol.getConfidence()); + } + out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence()); + paraText = String.format("%s %s", paraText, wordText); + } + // Output Example using Paragraph: + out.println("\nParagraph: \n" + paraText); + out.format("Paragraph Confidence: %f\n", para.getConfidence()); + blockText = blockText + paraText; + } + pageText = pageText + blockText; + } + } + out.println("\nComplete annotation:"); + out.println(annotation.getText()); + } + } + } + // [END vision_detect_document] + + // [START vision_detect_document_uri] + /** + * Performs document text detection on a remote image on Google Cloud Storage. + * + * @param gcsPath The path to the remote file on Google Cloud Storage to detect document text on. + * @param out A {@link PrintStream} to write the results to. + * @throws Exception on errors while closing the client. + * @throws IOException on Input/Output errors. + */ + public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws 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.DOCUMENT_TEXT_DETECTION).build(); + AnnotateImageRequest request = + AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); + requests.add(request); + + try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { + BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); + List responses = response.getResponsesList(); + client.close(); + + for (AnnotateImageResponse res : responses) { + if (res.hasError()) { + out.printf("Error: %s\n", res.getError().getMessage()); + return; + } + // For full list of available annotations, see http://g.co/cloud/vision/docs + TextAnnotation annotation = res.getFullTextAnnotation(); + for (Page page: annotation.getPagesList()) { + String pageText = ""; + for (Block block : page.getBlocksList()) { + String blockText = ""; + for (Paragraph para : block.getParagraphsList()) { + String paraText = ""; + for (Word word: para.getWordsList()) { + String wordText = ""; + for (Symbol symbol: word.getSymbolsList()) { + wordText = wordText + symbol.getText(); + out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), + symbol.getConfidence()); + } + out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence()); + paraText = String.format("%s %s", paraText, wordText); + } + // Output Example using Paragraph: + out.println("\nParagraph: \n" + paraText); + out.format("Paragraph Confidence: %f\n", para.getConfidence()); + blockText = blockText + paraText; + } + pageText = pageText + blockText; + } + } + out.println("\nComplete annotation:"); + out.println(annotation.getText()); + } + } + } + // [END vision_detect_document_uri] +} diff --git a/vision/beta/cloud-client/src/test/java/com/example/vision/DetectIT.java b/vision/beta/cloud-client/src/test/java/com/example/vision/DetectIT.java new file mode 100644 index 00000000000..d0833c58a86 --- /dev/null +++ b/vision/beta/cloud-client/src/test/java/com/example/vision/DetectIT.java @@ -0,0 +1,350 @@ +/* + 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 java.io.ByteArrayOutputStream; +import java.io.IOException; +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; + +/** Tests for vision "Detect" sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DetectIT { + private ByteArrayOutputStream bout; + private PrintStream out; + private Detect app; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET = PROJECT_ID; + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + app = new Detect(); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testFaces() throws Exception { + // Act + String[] args = {"faces", "./resources/face_no_surprise.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("anger: POSSIBLE"); + assertThat(got).contains("joy: POSSIBLE"); + assertThat(got).contains("surprise: LIKELY"); + } + + @Test + public void testFacesGcs() throws Exception { + // Act + String[] args = {"faces", "gs://" + BUCKET + "/vision/face_no_surprise.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("anger: POSSIBLE"); + assertThat(got).contains("joy: POSSIBLE"); + assertThat(got).contains("surprise: LIKELY"); + } + + @Test + public void testLabels() throws Exception { + // Act + String[] args = {"labels", "./resources/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("whiskers"); + } + + @Test + public void testLabelsGcs() throws Exception { + // Act + String[] args = {"labels", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("whiskers"); + } + + @Test + public void testLandmarks() throws Exception { + // Act + String[] args = {"landmarks", "./resources/landmark.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Palace of Fine Arts"); + } + + @Test + public void testLandmarksGcs() throws Exception { + // Act + String[] args = {"landmarks", "gs://" + BUCKET + "/vision/landmark.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Palace of Fine Arts"); + } + + @Test + public void testLandmarksUrl() throws Exception { + // Act + String uri = "https://storage-download.googleapis.com/" + + BUCKET + "/vision/landmark.jpg"; + String[] args = {"landmarks", uri}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Palace of Fine Arts"); + } + + @Test + public void testLogos() throws Exception { + // Act + String[] args = {"logos", "./resources/logos.png"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Google"); + } + + @Test + public void testLogosGcs() throws Exception { + // Act + String[] args = {"logos", "gs://" + BUCKET + "/vision/logos.png"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Google"); + } + + @Test + public void testText() throws Exception { + // Act + String[] args = {"text", "./resources/text.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } + + @Test + public void testTextGcs() throws Exception { + // Act + String[] args = {"text", "gs://" + BUCKET + "/vision/text.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("37%"); + } + + @Test + public void testSafeSearch() throws Exception { + // Act + String[] args = {"safe-search", "./resources/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("adult: VERY_UNLIKELY"); + assertThat(got).contains("racy: UNLIKELY"); + } + + @Test + public void testSafeSearchGcs() throws Exception { + // Act + String[] args = {"safe-search", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("adult: VERY_UNLIKELY"); + assertThat(got).contains("racy: UNLIKELY"); + } + + @Test + public void testProperties() throws Exception { + // Act + String[] args = {"properties", "./resources/landmark.jpg"}; + Detect.argsHelper(args, out); + + // 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 + String[] args = {"properties", "gs://" + BUCKET + "/vision/landmark.jpg"}; + Detect.argsHelper(args, out); + + // 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 + String[] args = {"web", "./resources/landmark.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Palace of Fine Arts Theatre"); + assertThat(got).contains("Best guess label: palace of fine arts"); + } + + @Test + public void detectWebAnnotationsGcs() throws Exception { + // Act + String[] args = {"web", "gs://" + BUCKET + "/vision/landmark.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Palace of Fine Arts Theatre"); + assertThat(got).contains("Best guess label: palace of fine arts"); + } + + @Test + public void testDetectWebEntities() throws Exception { + // Act + String[] args = {"web-entities", "./resources/city.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).doesNotContain("Zepra"); + } + + @Test + public void testDetectWebEntitiesGcs() throws Exception { + // Act + String[] args = {"web-entities", "gs://" + BUCKET + "/vision/landmark.jpg"}; + Detect.argsHelper(args, out); + + String got = bout.toString(); + assertThat(got).contains("Description: Palace of Fine Arts Theatre"); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResults() throws Exception { + // Act + String[] args = {"web-entities-include-geo", "./resources/city.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Zepra"); + } + + @Test + public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { + // Act + String[] args = {"web-entities-include-geo", "gs://" + BUCKET + "/vision/landmark.jpg"}; + Detect.argsHelper(args, out); + + String got = bout.toString(); + assertThat(got).contains("Description: Palace of Fine Arts Theatre"); + } + + @Test + public void testCropHints() throws Exception { + // Act + String[] args = {"crop", "./resources/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("vertices {"); + assertThat(got).contains("x: 599"); + assertThat(got).contains("y: 475"); + } + + @Test + public void testCropHintsGcs() throws Exception { + // Act + String[] args = {"crop", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("vertices {"); + assertThat(got).contains("x: 599"); + assertThat(got).contains("y: 475"); + } + + @Test + public void testDocumentText() throws Exception { + // Act + String[] args = {"fulltext", "./resources/text.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("After preparation is complete, the "); + assertThat(got).contains("37%"); + assertThat(got).contains("Word text: class (confidence:"); + } + + @Test + public void testDocumentTextGcs() throws Exception { + // Act + String[] args = {"fulltext", "gs://" + BUCKET + "/vision/text.jpg"}; + Detect.argsHelper(args, out); + + // Assert + String got = bout.toString(); + assertThat(got).contains("After preparation is complete, the "); + assertThat(got).contains("37%"); + assertThat(got).contains("Word text: class (confidence:"); + } +} diff --git a/vision/cloud-client/README.md b/vision/cloud-client/README.md index 06c8ec1d706..c8cdefa55a2 100644 --- a/vision/cloud-client/README.md +++ b/vision/cloud-client/README.md @@ -16,15 +16,13 @@ Install [Maven](http://maven.apache.org/). Build your project with: ``` -mvn clean compile assembly:single +mvn clean package ``` You can then run a given `ClassName` via: ``` -mvn exec:java -Dexec.mainClass=com.example.vision.ClassName \ - -DpropertyName=propertyValue \ - -Dexec.args="arg1 'arg 2' arg3" +mvn exec:java -DClassName -Dexec.args="arg1 'arg 2' arg3" ``` ### Analyze an image @@ -33,8 +31,53 @@ mvn exec:java -Dexec.mainClass=com.example.vision.ClassName \ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json ``` +#### Quickstart ``` -java -cp target/vision-google-cloud-samples-1.0.0-jar-with-dependencies.jar \ - com.example.vision.Detect \ - logos "./resources/logos.png" +mvn exec:java -DQuickstartSample ``` + +#### Faces +``` +mvn exec:java -DDetect -Dexec.args="faces ./resources/face_no_surprise.jpg" +``` + +#### Labels +``` +mvn exec:java -DDetect -Dexec.args="labels ./resources/wakeupcat.jpg" +``` + +#### Landmarks +``` +mvn exec:java -DDetect -Dexec.args="landmarks ./resources/landmark.jpg" +``` + +#### Logos +``` +mvn exec:java -DDetect -Dexec.args="logos ./resources/logos.png" +``` + +#### Text +``` +mvn exec:java -DDetect -Dexec.args="text ./resources/text.jpg" +``` + +#### Safe Search +``` +mvn exec:java -DDetect -Dexec.args="safe-search ./resources/wakeupcat.jpg" +``` + +#### Properties +``` +mvn exec:java -DDetect -Dexec.args="properties ./resources/city.jpg" +``` + +#### Web +``` +mvn exec:java -DDetect -Dexec.args="web ./resources/landmark.jpg" +``` + +#### Crop +``` +mvn exec:java -DDetect -Dexec.args="crop ./resources/landmark.jpg" +``` + diff --git a/vision/cloud-client/pom.xml b/vision/cloud-client/pom.xml index 2d28db353d8..87a8149d0db 100644 --- a/vision/cloud-client/pom.xml +++ b/vision/cloud-client/pom.xml @@ -38,20 +38,10 @@ com.google.cloud google-cloud-vision - 1.12.0 + 1.14.0 - - com.google.auth - google-auth-library-oauth2-http - 0.9.0 - - - com.google.guava - guava - 23.0 - junit @@ -67,21 +57,63 @@ test - - - - maven-assembly-plugin - - - + + + Detect + + + Detect + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + com.example.vision.Detect - - - - jar-with-dependencies - - - - - + false + + + + + + + + QuickstartSample + + + QuickstartSample + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + + java + + + + + com.example.vision.QuickstartSample + false + + + + + + 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 9289d09d4f1..e25c6b29353 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 @@ -1,16 +1,19 @@ -/** - * 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. - */ +/* + 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 com.google.cloud.vision.v1.AnnotateImageRequest; @@ -50,7 +53,7 @@ public class Detect { /** - * Detects entities,sentiment and syntax in a document using the Natural Language API. + * Detects entities,sentiment and syntax in a document using the Vision API. * * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -69,13 +72,12 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception, if (args.length < 1) { out.println("Usage:"); out.printf( - "\tjava %s \"\" \"\"\n" + "\tmvn exec:java -DDetect -Dexec.args=\" \"\n" + "Commands:\n" + "\tfaces | labels | landmarks | logos | text | safe-search | properties" + "| web | crop \n" + "Path:\n\tA file path (ex: ./resources/wakeupcat.jpg) or a URI for a Cloud Storage " - + "resource (gs://...)\n", - Detect.class.getCanonicalName()); + + "resource (gs://...)\n"); return; } String command = args[0]; @@ -147,14 +149,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception, } } - /** - * Constructs a {@link Detect} which connects to the Cloud Vision API. - * - * @param client The Vision API client. - */ - public Detect() { - } - /** * Detects faces in the specified local image. * @@ -198,9 +192,10 @@ public static void detectFaces(String filePath, PrintStream out) throws Exceptio } /** - * Detects faces in the specified remote image. + * Detects faces in the specified remote image on Google Cloud Storage. * - * @param gcsPath The path to the remote file to perform face detection on. + * @param gcsPath The path to the remote file on Google Cloud Storage to perform face detection + * on. * @param out A {@link PrintStream} to write detected features to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -278,9 +273,10 @@ public static void detectLabels(String filePath, PrintStream out) throws Excepti } /** - * Detects labels in the specified remote image. + * Detects labels in the specified remote image on Google Cloud Storage. * - * @param gcsPath The path to the remote file to perform label detection on. + * @param gcsPath The path to the remote file on Google Cloud Storage to perform label detection + * on. * @param out A {@link PrintStream} to write detected features to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -356,16 +352,16 @@ public static void detectLandmarks(String filePath, PrintStream out) throws Exce /** * Detects landmarks in the specified URI. * - * @param url The path to the file to perform landmark detection on. + * @param uri The path to the file to perform landmark detection on. * @param out A {@link PrintStream} to write detected landmarks to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. */ - public static void detectLandmarksUrl(String url, PrintStream out) throws Exception, + public static void detectLandmarksUrl(String uri, PrintStream out) throws Exception, IOException { List requests = new ArrayList<>(); - ImageSource imgSource = ImageSource.newBuilder().setImageUri(url).build(); + 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 = @@ -392,9 +388,10 @@ public static void detectLandmarksUrl(String url, PrintStream out) throws Except } /** - * Detects landmarks in the specified remote image. + * Detects landmarks in the specified remote image on Google Cloud Storage. * - * @param gcsPath The path to the remote file to perform landmark detection on. + * @param gcsPath The path to the remote file on Google Cloud Storage to perform landmark + * detection on. * @param out A {@link PrintStream} to write detected landmarks to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -467,9 +464,10 @@ public static void detectLogos(String filePath, PrintStream out) throws Exceptio } /** - * Detects logos in the specified remote image. + * Detects logos in the specified remote image on Google Cloud Storage. * - * @param gcsPath The path to the remote file to perform logo detection on. + * @param gcsPath The path to the remote file on Google Cloud Storage to perform logo detection + * on. * @param out A {@link PrintStream} to write detected logos to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -542,9 +540,9 @@ public static void detectText(String filePath, PrintStream out) throws Exception } /** - * Detects text in the specified remote image. + * Detects text in the specified remote image on Google Cloud Storage. * - * @param gcsPath The path to the remote file to detect text in. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect text in. * @param out A {@link PrintStream} to write the detected text to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -623,9 +621,10 @@ public static void detectProperties(String filePath, PrintStream out) throws Exc } /** - * Detects image properties such as color frequency from the specified remote image. + * Detects image properties such as color frequency from the specified remote image on Google + * Cloud Storage. * - * @param gcsPath The path to the remote file to detect properties on. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect properties on. * @param out A {@link PrintStream} to write * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -708,9 +707,10 @@ public static void detectSafeSearch(String filePath, PrintStream out) throws Exc } /** - * Detects whether the specified remote image has features you would want to moderate. + * Detects whether the specified remote image on Google Cloud Storage has features you would want + * to moderate. * - * @param gcsPath The path to the remote file to detect safe-search on. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -805,9 +805,9 @@ public static void detectWebDetections(String filePath, PrintStream out) throws } /** - * Detects whether the specified remote image has features you would want to moderate. + * Detects whether the specified remote image on Google Cloud Storage has features you would want to moderate. * - * @param gcsPath The path to the remote file to detect safe-search on. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -899,9 +899,9 @@ public static void detectCropHints(String filePath, PrintStream out) throws Exce } /** - * Suggests a region to crop to for a remote file. + * Suggests a region to crop to for a remote file on Google Cloud Storage. * - * @param gcsPath The path to the remote file to detect safe-search on. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect safe-search on. * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. @@ -996,9 +996,9 @@ public static void detectDocumentText(String filePath, PrintStream out) throws E } /** - * Performs document text detection on a local image file. + * Performs document text detection on a local image file on Google Cloud Storage. * - * @param gcsPath The path to the remote file to detect document text on. + * @param gcsPath The path to the remote file on Google Cloud Storage to detect document text on. * @param out A {@link PrintStream} to write the results to. * @throws Exception on errors while closing the client. * @throws IOException on Input/Output errors. 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 44fce53835a..6d4ee5767b3 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 @@ -1,5 +1,5 @@ /* - Copyright 2016, Google, Inc. + 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.