Search in sources :

Example 1 with WebLabel

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

the class Detect method detectWebDetections.

// [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<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);
    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) 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) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) 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 2 with WebLabel

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

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)2 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)2 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)2 Feature (com.google.cloud.vision.v1.Feature)2 Image (com.google.cloud.vision.v1.Image)2 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)2 WebDetection (com.google.cloud.vision.v1.WebDetection)2 WebEntity (com.google.cloud.vision.v1.WebDetection.WebEntity)2 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)2 WebLabel (com.google.cloud.vision.v1.WebDetection.WebLabel)2 WebPage (com.google.cloud.vision.v1.WebDetection.WebPage)2 ArrayList (java.util.ArrayList)2 ImageSource (com.google.cloud.vision.v1.ImageSource)1 ByteString (com.google.protobuf.ByteString)1 FileInputStream (java.io.FileInputStream)1