Search in sources :

Example 31 with AnnotateImageResponse

use of com.google.cloud.vision.v1p3beta1.AnnotateImageResponse in project java-vision by googleapis.

the class DetectWebDetectionsGcs method detectWebDetectionsGcs.

// Detects whether the remote image on Google Cloud Storage has features you would want to
// moderate.
public static void detectWebDetectionsGcs(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.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 (WebDetection.WebEntity entity : annotation.getWebEntitiesList()) {
                System.out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore());
            }
            for (WebDetection.WebLabel label : annotation.getBestGuessLabelsList()) {
                System.out.format("%nBest guess label: %s", label.getLabel());
            }
            System.out.println("%nPages with matching images: Score%n==");
            for (WebDetection.WebPage page : annotation.getPagesWithMatchingImagesList()) {
                System.out.println(page.getUrl() + " : " + page.getScore());
            }
            System.out.println("%nPages with partially matching images: Score%n==");
            for (WebDetection.WebImage image : annotation.getPartialMatchingImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
            System.out.println("%nPages with fully matching images: Score%n==");
            for (WebDetection.WebImage image : annotation.getFullMatchingImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
            System.out.println("%nPages with visually similar images: Score%n==");
            for (WebDetection.WebImage image : annotation.getVisuallySimilarImagesList()) {
                System.out.println(image.getUrl() + " : " + image.getScore());
            }
        }
    }
}
Also used : WebDetection(com.google.cloud.vision.v1.WebDetection) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ImageSource(com.google.cloud.vision.v1.ImageSource) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 32 with AnnotateImageResponse

use of com.google.cloud.vision.v1p3beta1.AnnotateImageResponse in project java-vision by googleapis.

the class Detect method detectDocumentTextGcs.

// [END vision_fulltext_detection]
/**
 * 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.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
// [START vision_fulltext_detection_gcs]
public static void detectDocumentTextGcs(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(Type.DOCUMENT_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();
        client.close();
        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
            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();
                                System.out.format("Symbol text: %s (confidence: %f)%n", symbol.getText(), symbol.getConfidence());
                            }
                            System.out.format("Word text: %s (confidence: %f)%n%n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        System.out.println("%nParagraph: %n" + paraText);
                        System.out.format("Paragraph Confidence: %f%n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            System.out.println("%nComplete annotation:");
            System.out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1.Word) Symbol(com.google.cloud.vision.v1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) Paragraph(com.google.cloud.vision.v1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Block(com.google.cloud.vision.v1.Block) ImageSource(com.google.cloud.vision.v1.ImageSource) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 33 with AnnotateImageResponse

use of com.google.cloud.vision.v1p3beta1.AnnotateImageResponse in project java-vision by googleapis.

the class Detect method detectLocalizedObjectsGcs.

// [END vision_localize_objects]
// [START vision_localize_objects_gcs]
/**
 * 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.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjectsGcs(String gcsPath) throws 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);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                System.out.format("Object name: %s%n", entity.getName());
                System.out.format("Confidence: %s%n", entity.getScore());
                System.out.format("Normalized Vertices:%n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
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) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 34 with AnnotateImageResponse

use of com.google.cloud.vision.v1p3beta1.AnnotateImageResponse in project java-vision by googleapis.

the class Detect method detectLocalizedObjects.

// [END vision_text_detection_pdf_gcs]
// [START vision_localize_objects]
/**
 * Detects localized objects in the specified local image.
 *
 * @param filePath The path to the file to perform localized object detection on.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectLocalizedObjects(String filePath) throws IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).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()) {
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        // Display the results
        for (AnnotateImageResponse res : responses) {
            for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
                System.out.format("Object name: %s%n", entity.getName());
                System.out.format("Confidence: %s%n", entity.getScore());
                System.out.format("Normalized Vertices:%n");
                entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> System.out.format("- (%s, %s)%n", vertex.getX(), vertex.getY()));
            }
        }
    }
}
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) LocalizedObjectAnnotation(com.google.cloud.vision.v1.LocalizedObjectAnnotation) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 35 with AnnotateImageResponse

use of com.google.cloud.vision.v1p3beta1.AnnotateImageResponse in project java-vision by googleapis.

the class VisionController method extractLabels.

// [END spring_vision_autowire]
/**
 * This method downloads an image from a URL and sends its contents to the Vision API for label
 * detection.
 *
 * @param imageUrl the URL of the image
 * @param map the model map to use
 * @return a string with the list of labels and percentage of certainty
 */
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
    // [START spring_vision_image_labelling]
    AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);
    Map<String, Float> imageLabels = response.getLabelAnnotationsList().stream().collect(Collectors.toMap(EntityAnnotation::getDescription, EntityAnnotation::getScore, (u, v) -> {
        throw new IllegalStateException(String.format("Duplicate key %s", u));
    }, LinkedHashMap::new));
    // [END spring_vision_image_labelling]
    map.addAttribute("annotations", imageLabels);
    map.addAttribute("imageUrl", imageUrl);
    return new ModelAndView("result", map);
}
Also used : ResourceLoader(org.springframework.core.io.ResourceLoader) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Type(com.google.cloud.vision.v1.Feature.Type) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModelMap(org.springframework.ui.ModelMap) LinkedHashMap(java.util.LinkedHashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) CloudVisionTemplate(org.springframework.cloud.gcp.vision.CloudVisionTemplate) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ModelAndView(org.springframework.web.servlet.ModelAndView) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)91 ArrayList (java.util.ArrayList)73 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)63 Feature (com.google.cloud.vision.v1.Feature)62 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)61 Image (com.google.cloud.vision.v1.Image)61 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)60 ByteString (com.google.protobuf.ByteString)53 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)34 ImageSource (com.google.cloud.vision.v1.ImageSource)31 FileInputStream (java.io.FileInputStream)25 Test (org.junit.Test)23 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)22 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)9 WebDetection (com.google.cloud.vision.v1.WebDetection)8 IOException (java.io.IOException)8 Block (com.google.cloud.vision.v1.Block)6 ColorInfo (com.google.cloud.vision.v1.ColorInfo)6 CropHint (com.google.cloud.vision.v1.CropHint)6 CropHintsAnnotation (com.google.cloud.vision.v1.CropHintsAnnotation)6