Search in sources :

Example 91 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-vision by googleapis.

the class DetectLogosGcs method detectLogosGcs.

// Detects logos in the specified remote image on Google Cloud Storage.
public static void detectLogosGcs(String gcsPath) throws 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(Feature.Type.LOGO_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.getLogoAnnotationsList()) {
                System.out.println(annotation.getDescription());
            }
        }
    }
}
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)

Example 92 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-vision by googleapis.

the class DetectText method detectText.

// Detects text in the specified image.
public static void detectText(String filePath) throws IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    // 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.getTextAnnotationsList()) {
                System.out.format("Text: %s%n", annotation.getDescription());
                System.out.format("Position : %s%n", annotation.getBoundingPoly());
            }
        }
    }
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) Image(com.google.cloud.vision.v1.Image) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 93 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-vision by googleapis.

the class DetectWebDetections method detectWebDetections.

// Finds references to the specified image on the web.
public static void detectWebDetections(String filePath) throws IOException {
    List<AnnotateImageRequest> 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);
    // 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;
            }
            // Search the web for usages of the image. You could use these signals later
            // for user input moderation or linking external references.
            // For a full list of available annotations, see http://g.co/cloud/vision/docs
            WebDetection annotation = res.getWebDetection();
            System.out.println("Entity:Id:Score");
            System.out.println("===============");
            for (WebEntity entity : annotation.getWebEntitiesList()) {
                System.out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore());
            }
            for (WebLabel label : annotation.getBestGuessLabelsList()) {
                System.out.format("%nBest guess label: %s", label.getLabel());
            }
            System.out.println("%nPages with matching images: Score%n==");
            for (WebPage page : annotation.getPagesWithMatchingImagesList()) {
                System.out.println(page.getUrl() + " : " + page.getScore());
            }
            System.out.println("%nPages with partially matching images: Score%n==");
            for (WebImage image : annotation.getPartialMatchingImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
            System.out.println("%nPages with fully matching images: Score%n==");
            for (WebImage image : annotation.getFullMatchingImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
            System.out.println("%nPages with visually similar images: Score%n==");
            for (WebImage image : annotation.getVisuallySimilarImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
        }
    }
}
Also used : WebDetection(com.google.cloud.vision.v1.WebDetection) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) ByteString(com.google.protobuf.ByteString) 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) Image(com.google.cloud.vision.v1.Image) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) WebLabel(com.google.cloud.vision.v1.WebDetection.WebLabel) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 94 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-vision by googleapis.

the class DetectWebEntities method detectWebEntities.

// Find web entities given a local image.
public static void detectWebEntities(String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Read in the local image
        ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
        // Build the image
        Image image = Image.newBuilder().setContent(contents).build();
        // Create the request with the image and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)).setImage(image).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : ByteString(com.google.protobuf.ByteString) Arrays(java.util.Arrays) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Type(com.google.cloud.vision.v1.Feature.Type) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 95 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-vision by googleapis.

the class DetectWebEntitiesGcs method detectWebEntitiesGcs.

// Find web entities given the remote image on Google Cloud Storage.
public static void detectWebEntitiesGcs(String gcsPath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Set the image source to the given gs uri
        ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
        // Build the image
        Image image = Image.newBuilder().setSource(imageSource).build();
        // Create the request with the image and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)).setImage(image).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : Arrays(java.util.Arrays) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)73 Image (com.google.cloud.vision.v1.Image)73 Feature (com.google.cloud.vision.v1.Feature)71 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)70 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)67 ArrayList (java.util.ArrayList)65 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)64 ByteString (com.google.protobuf.ByteString)51 ImageSource (com.google.cloud.vision.v1.ImageSource)40 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)27 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)20 ImageContext (com.google.cloud.vision.v1.ImageContext)14 WebDetection (com.google.cloud.vision.v1.WebDetection)11 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)10 Arrays (java.util.Arrays)10 CropHint (com.google.cloud.vision.v1.CropHint)9 Type (com.google.cloud.vision.v1.Feature.Type)9