Search in sources :

Example 6 with ImageSource

use of com.google.cloud.vision.v1.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectWebEntitiesGcs.

/**
 * 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());
        }));
    }
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) Arrays(java.util.Arrays) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Paragraph(com.google.cloud.vision.v1.Paragraph) ArrayList(java.util.ArrayList) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) WebLabel(com.google.cloud.vision.v1.WebDetection.WebLabel) CropHint(com.google.cloud.vision.v1.CropHint) PrintStream(java.io.PrintStream) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) WebDetection(com.google.cloud.vision.v1.WebDetection) Type(com.google.cloud.vision.v1.Feature.Type) LocationInfo(com.google.cloud.vision.v1.LocationInfo) FaceAnnotation(com.google.cloud.vision.v1.FaceAnnotation) Block(com.google.cloud.vision.v1.Block) CropHintsAnnotation(com.google.cloud.vision.v1.CropHintsAnnotation) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) Symbol(com.google.cloud.vision.v1.Symbol) ColorInfo(com.google.cloud.vision.v1.ColorInfo) ByteString(com.google.protobuf.ByteString) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) Image(com.google.cloud.vision.v1.Image) WebEntity(com.google.cloud.vision.v1.WebDetection.WebEntity) ImageContext(com.google.cloud.vision.v1.ImageContext) SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) Page(com.google.cloud.vision.v1.Page) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) Word(com.google.cloud.vision.v1.Word) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 7 with ImageSource

use of com.google.cloud.vision.v1.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectCropHintsGcs.

/**
 * 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<AnnotateImageRequest> 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<AnnotateImageResponse> 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());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) CropHintsAnnotation(com.google.cloud.vision.v1.CropHintsAnnotation) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) CropHint(com.google.cloud.vision.v1.CropHint) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 8 with ImageSource

use of com.google.cloud.vision.v1.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectTextGcs.

/**
 * 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<AnnotateImageRequest> 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<AnnotateImageResponse> 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());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 9 with ImageSource

use of com.google.cloud.vision.v1.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectWebEntitiesIncludeGeoResultsGcs.

// [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());
        }));
    }
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) Arrays(java.util.Arrays) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Paragraph(com.google.cloud.vision.v1.Paragraph) ArrayList(java.util.ArrayList) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) WebLabel(com.google.cloud.vision.v1.WebDetection.WebLabel) CropHint(com.google.cloud.vision.v1.CropHint) PrintStream(java.io.PrintStream) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) WebDetection(com.google.cloud.vision.v1.WebDetection) Type(com.google.cloud.vision.v1.Feature.Type) LocationInfo(com.google.cloud.vision.v1.LocationInfo) FaceAnnotation(com.google.cloud.vision.v1.FaceAnnotation) Block(com.google.cloud.vision.v1.Block) CropHintsAnnotation(com.google.cloud.vision.v1.CropHintsAnnotation) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) Symbol(com.google.cloud.vision.v1.Symbol) ColorInfo(com.google.cloud.vision.v1.ColorInfo) ByteString(com.google.protobuf.ByteString) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) Image(com.google.cloud.vision.v1.Image) WebEntity(com.google.cloud.vision.v1.WebDetection.WebEntity) ImageContext(com.google.cloud.vision.v1.ImageContext) SafeSearchAnnotation(com.google.cloud.vision.v1.SafeSearchAnnotation) Page(com.google.cloud.vision.v1.Page) DominantColorsAnnotation(com.google.cloud.vision.v1.DominantColorsAnnotation) Word(com.google.cloud.vision.v1.Word) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 10 with ImageSource

use of com.google.cloud.vision.v1.ImageSource in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectLandmarksUrl.

/**
 * 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<AnnotateImageRequest> 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<AnnotateImageResponse> 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());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) LocationInfo(com.google.cloud.vision.v1.LocationInfo)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)13 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)13 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)13 Feature (com.google.cloud.vision.v1.Feature)13 Image (com.google.cloud.vision.v1.Image)13 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)13 ImageSource (com.google.cloud.vision.v1.ImageSource)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)13 ArrayList (java.util.ArrayList)13 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)7 LocationInfo (com.google.cloud.vision.v1.LocationInfo)4 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)4 Block (com.google.cloud.vision.v1.Block)3 ColorInfo (com.google.cloud.vision.v1.ColorInfo)3 CropHint (com.google.cloud.vision.v1.CropHint)3 CropHintsAnnotation (com.google.cloud.vision.v1.CropHintsAnnotation)3 DominantColorsAnnotation (com.google.cloud.vision.v1.DominantColorsAnnotation)3 FaceAnnotation (com.google.cloud.vision.v1.FaceAnnotation)3 Page (com.google.cloud.vision.v1.Page)3 Paragraph (com.google.cloud.vision.v1.Paragraph)3