Search in sources :

Example 16 with ImageSource

use of com.google.cloud.vision.v1p4beta1.ImageSource in project java-vision by googleapis.

the class DetectLandmarksUrl method detectLandmarksUrl.

// Detects landmarks in the specified URI.
public static void detectLandmarksUrl(String uri) throws 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(Feature.Type.LANDMARK_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.format("Error: %s%n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
                LocationInfo info = annotation.getLocationsList().listIterator().next();
                System.out.format("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) 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)

Example 17 with ImageSource

use of com.google.cloud.vision.v1p4beta1.ImageSource in project java-vision by googleapis.

the class DetectBeta method detectLocalizedObjectsGcs.

// [END vision_localize_objects_beta]
// [START vision_localize_objects_gcs_beta]
/**
 * Detects localized objects in a remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
 *     on.
 * @param out A {@link PrintStream} to write detected objects to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjectsGcs(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();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).setImage(img).build();
    requests.add(request);
    // Perform the request
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                out.format("Object name: %s\n", entity.getName());
                out.format("Confidence: %s\n", entity.getScore());
                out.format("Normalized Vertices:\n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1p3beta1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1p3beta1.AnnotateImageResponse) ArrayList(java.util.ArrayList) LocalizedObjectAnnotation(com.google.cloud.vision.v1p3beta1.LocalizedObjectAnnotation) ImageSource(com.google.cloud.vision.v1p3beta1.ImageSource) Image(com.google.cloud.vision.v1p3beta1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1p3beta1.BatchAnnotateImagesResponse)

Example 18 with ImageSource

use of com.google.cloud.vision.v1p4beta1.ImageSource in project java-vision by googleapis.

the class ProductSearch method getSimilarProductsGcs.

// [END vision_product_search_get_similar_products]
// [START vision_product_search_get_similar_products_gcs]
/**
 * Search similar products to image in Google Cloud Storage.
 *
 * @param projectId - Id of the project.
 * @param computeRegion - Region name.
 * @param productSetId - Id of the product set.
 * @param productCategory - Category of the product.
 * @param gcsUri - GCS file path of the image to be searched
 * @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
 *     color = blue) AND style = kids It will search on all products with the following labels:
 *     color:red AND style:kids color:blue AND style:kids
 * @throws Exception - on errors.
 */
public static void getSimilarProductsGcs(String projectId, String computeRegion, String productSetId, String productCategory, String gcsUri, String filter) throws Exception {
    try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
        // Get the full path of the product set.
        String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();
        // Get the image from Google Cloud Storage
        ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();
        // Create annotate image request along with product search feature.
        Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
        Image image = Image.newBuilder().setSource(source).build();
        ImageContext imageContext = ImageContext.newBuilder().setProductSearchParams(ProductSearchParams.newBuilder().setProductSet(productSetPath).addProductCategories(productCategory).setFilter(filter)).build();
        AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().addFeatures(featuresElement).setImage(image).setImageContext(imageContext).build();
        List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);
        // Search products similar to the image.
        BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);
        List<Result> similarProducts = response.getResponses(0).getProductSearchResults().getResultsList();
        System.out.println("Similar Products: ");
        for (Result product : similarProducts) {
            System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
            System.out.println(String.format("Product display name: %s", product.getProduct().getDisplayName()));
            System.out.println(String.format("Product description: %s", product.getProduct().getDescription()));
            System.out.println(String.format("Score(Confidence): %s", product.getScore()));
            System.out.println(String.format("Image name: %s", product.getImage()));
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ByteString(com.google.protobuf.ByteString) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Result(com.google.cloud.vision.v1.ProductSearchResults.Result)

Example 19 with ImageSource

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

the class Detect method detectWebDetectionsGcs.

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

Example 20 with ImageSource

use of com.google.cloud.vision.v1p4beta1.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)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)37 Image (com.google.cloud.vision.v1.Image)37 ImageSource (com.google.cloud.vision.v1.ImageSource)37 Feature (com.google.cloud.vision.v1.Feature)36 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)34 ArrayList (java.util.ArrayList)34 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)33 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)31 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)13 ByteString (com.google.protobuf.ByteString)11 IOException (java.io.IOException)7 LocationInfo (com.google.cloud.vision.v1.LocationInfo)6 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)6 WebDetection (com.google.cloud.vision.v1.WebDetection)6 CropHint (com.google.cloud.vision.v1.CropHint)5 ImageContext (com.google.cloud.vision.v1.ImageContext)5 Block (com.google.cloud.vision.v1.Block)4 ColorInfo (com.google.cloud.vision.v1.ColorInfo)4 CropHintsAnnotation (com.google.cloud.vision.v1.CropHintsAnnotation)4