Search in sources :

Example 1 with ImageContext

use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by spring-cloud.

the class CloudVisionTemplateTests method testAddImageContext_analyzeImage.

@Test
public void testAddImageContext_analyzeImage() throws IOException {
    when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class))).thenReturn(DEFAULT_API_RESPONSE);
    ImageContext imageContext = Mockito.mock(ImageContext.class);
    this.cloudVisionTemplate.analyzeImage(FAKE_IMAGE, imageContext, Type.FACE_DETECTION);
    BatchAnnotateImagesRequest expectedRequest = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.FACE_DETECTION)).setImageContext(imageContext).setImage(Image.newBuilder().setContent(ByteString.readFrom(FAKE_IMAGE.getInputStream())).build())).build();
    verify(this.imageAnnotatorClient, times(1)).batchAnnotateImages(expectedRequest);
}
Also used : BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) ImageContext(com.google.cloud.vision.v1.ImageContext) Test(org.junit.Test)

Example 2 with ImageContext

use of com.google.cloud.vision.v1p3beta1.ImageContext in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectWebEntitiesIncludeGeoResults.

// [START vision_web_entities_include_geo_results]
/**
 * Find web entities given a local image.
 * @param filePath The path of the image to detect.
 * @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 detectWebEntitiesIncludeGeoResults(String filePath, PrintStream out) throws Exception, IOException {
    // Instantiates a client
    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();
        // 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) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) FileInputStream(java.io.FileInputStream) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 3 with ImageContext

use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by GoogleCloudPlatform.

the class CloudVisionTemplateTests method testAddImageContext_extractText.

@Test
public void testAddImageContext_extractText() throws IOException {
    when(this.imageAnnotatorClient.batchAnnotateImages(any(BatchAnnotateImagesRequest.class))).thenReturn(DEFAULT_API_RESPONSE);
    ImageContext imageContext = Mockito.mock(ImageContext.class);
    this.cloudVisionTemplate.extractTextFromImage(FAKE_IMAGE, imageContext);
    BatchAnnotateImagesRequest expectedRequest = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.TEXT_DETECTION)).setImageContext(imageContext).setImage(Image.newBuilder().setContent(ByteString.readFrom(FAKE_IMAGE.getInputStream())).build())).build();
    verify(this.imageAnnotatorClient, times(1)).batchAnnotateImages(expectedRequest);
}
Also used : BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) ImageContext(com.google.cloud.vision.v1.ImageContext) Test(org.junit.Test)

Example 4 with ImageContext

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

the class DetectWebEntitiesIncludeGeoResultsGcs method detectWebEntitiesIncludeGeoResultsGcs.

// Find web entities given the remote image on Google Cloud Storage.
public static void detectWebEntitiesIncludeGeoResultsGcs(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();
        // 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(Feature.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 -> {
            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) WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) 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) ImageContext(com.google.cloud.vision.v1.ImageContext) 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) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 5 with ImageContext

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

the class DetectBeta method detectHandwrittenOcr.

// [END vision_localize_objects_gcs_beta]
// [START vision_handwritten_ocr_beta]
/**
 * Performs handwritten text detection on a local image file.
 *
 * @param filePath The path to the local file to detect handwritten text 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 detectHandwrittenOcr(String filePath, PrintStream out) throws Exception {
    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.DOCUMENT_TEXT_DETECTION).build();
    // Set the Language Hint codes for handwritten OCR
    ImageContext imageContext = ImageContext.newBuilder().addLanguageHints("en-t-i0-handwrit").build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).setImageContext(imageContext).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        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
            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();
                                out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), symbol.getConfidence());
                            }
                            out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        out.println("\nParagraph: \n" + paraText);
                        out.format("Paragraph Confidence: %f\n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            out.println("\nComplete annotation:");
            out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1p3beta1.Word) ByteString(com.google.protobuf.ByteString) Symbol(com.google.cloud.vision.v1p3beta1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1p3beta1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) Page(com.google.cloud.vision.v1p3beta1.Page) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1p3beta1.Image) Feature(com.google.cloud.vision.v1p3beta1.Feature) FileInputStream(java.io.FileInputStream) Paragraph(com.google.cloud.vision.v1p3beta1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1p3beta1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1p3beta1.AnnotateImageResponse) Block(com.google.cloud.vision.v1p3beta1.Block) TextAnnotation(com.google.cloud.vision.v1p3beta1.TextAnnotation) ImageContext(com.google.cloud.vision.v1p3beta1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1p3beta1.BatchAnnotateImagesResponse)

Aggregations

ImageContext (com.google.cloud.vision.v1.ImageContext)14 ByteString (com.google.protobuf.ByteString)11 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)10 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)10 Feature (com.google.cloud.vision.v1.Feature)10 Image (com.google.cloud.vision.v1.Image)10 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)8 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)6 BatchAnnotateImagesRequest (com.google.cloud.vision.v1.BatchAnnotateImagesRequest)6 WebDetectionParams (com.google.cloud.vision.v1.WebDetectionParams)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Arrays (java.util.Arrays)6 Test (org.junit.Test)6 Type (com.google.cloud.vision.v1.Feature.Type)5 ImageSource (com.google.cloud.vision.v1.ImageSource)5 FileInputStream (java.io.FileInputStream)5 WebDetection (com.google.cloud.vision.v1.WebDetection)4 List (java.util.List)3 Block (com.google.cloud.vision.v1.Block)2