Search in sources :

Example 11 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by spring-cloud.

the class VisionController method extractLabels.

/**
 * 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
 * @throws org.springframework.cloud.gcp.vision.CloudVisionException if the Vision API call
 *    produces an error
 */
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
    AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);
    // This gets the annotations of the image from the response object.
    List<EntityAnnotation> annotations = response.getLabelAnnotationsList();
    map.addAttribute("annotations", annotations);
    map.addAttribute("imageUrl", imageUrl);
    return new ModelAndView("result", map);
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ModelAndView(org.springframework.web.servlet.ModelAndView) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 12 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by spring-cloud.

the class DocumentOcrTemplate method extractOcrResultFuture.

private ListenableFuture<DocumentOcrResultSet> extractOcrResultFuture(OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> grpcFuture) {
    SettableListenableFuture<DocumentOcrResultSet> result = new SettableListenableFuture<>();
    ApiFutures.addCallback(grpcFuture, new ApiFutureCallback<AsyncBatchAnnotateFilesResponse>() {

        @Override
        public void onFailure(Throwable throwable) {
            result.setException(throwable);
        }

        @Override
        public void onSuccess(AsyncBatchAnnotateFilesResponse asyncBatchAnnotateFilesResponse) {
            String outputLocationUri = asyncBatchAnnotateFilesResponse.getResponsesList().get(0).getOutputConfig().getGcsDestination().getUri();
            GoogleStorageLocation outputFolderLocation = new GoogleStorageLocation(outputLocationUri);
            result.set(readOcrOutputFileSet(outputFolderLocation));
        }
    }, this.executor);
    return result;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) GoogleStorageLocation(org.springframework.cloud.gcp.storage.GoogleStorageLocation)

Example 13 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.

the class VisionController method extractLabels.

/**
 * 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
 * @throws com.google.cloud.spring.vision.CloudVisionException if the Vision API call produces an
 *     error
 */
@GetMapping("/extractLabels")
public ModelAndView extractLabels(String imageUrl, ModelMap map) {
    AnnotateImageResponse response = this.cloudVisionTemplate.analyzeImage(this.resourceLoader.getResource(imageUrl), Type.LABEL_DETECTION);
    // This gets the annotations of the image from the response object.
    List<EntityAnnotation> annotations = response.getLabelAnnotationsList();
    map.addAttribute("annotations", annotations);
    map.addAttribute("imageUrl", imageUrl);
    return new ModelAndView("result", map);
}
Also used : AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ModelAndView(org.springframework.web.servlet.ModelAndView) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 14 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.

the class VisionApiSampleApplicationIntegrationTests method testClassifyImageLabels.

@Test
void testClassifyImageLabels() throws Exception {
    this.mockMvc.perform(get(LABEL_IMAGE_URL)).andDo(response -> {
        ModelAndView result = response.getModelAndView();
        List<EntityAnnotation> annotations = (List<EntityAnnotation>) result.getModelMap().get("annotations");
        List<String> annotationNames = annotations.stream().map(annotation -> annotation.getDescription().toLowerCase().trim()).collect(Collectors.toList());
        assertThat(annotationNames).contains("dog");
    });
}
Also used : SpringExtension(org.springframework.test.context.junit.jupiter.SpringExtension) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) MockMvc(org.springframework.test.web.servlet.MockMvc) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) EnabledIfSystemProperty(org.junit.jupiter.api.condition.EnabledIfSystemProperty) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) ModelAndView(org.springframework.web.servlet.ModelAndView) List(java.util.List) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 15 with Result

use of com.google.cloud.vision.v1.ProductSearchResults.Result in project spring-cloud-gcp by GoogleCloudPlatform.

the class DocumentOcrResultSet method getAllPages.

/**
 * Returns an {@link Iterator} over all the OCR pages of the document.
 *
 * @return iterator of {@link TextAnnotation} describing OCR content of each page in the document.
 */
public Iterator<TextAnnotation> getAllPages() {
    return new Iterator<TextAnnotation>() {

        private final Iterator<OcrPageRange> pageRangeIterator = ocrPageRanges.values().iterator();

        private int offset = 0;

        private List<TextAnnotation> currentPageRange = Collections.emptyList();

        @Override
        public boolean hasNext() {
            return pageRangeIterator.hasNext() || offset < currentPageRange.size();
        }

        @Override
        public TextAnnotation next() {
            if (!hasNext()) {
                throw new NoSuchElementException("No more pages left in DocumentOcrResultSet.");
            }
            if (offset >= currentPageRange.size()) {
                OcrPageRange pageRange = pageRangeIterator.next();
                offset = 0;
                try {
                    currentPageRange = pageRange.getPages();
                } catch (InvalidProtocolBufferException e) {
                    throw new RuntimeException("Failed to parse OCR output from JSON output file " + pageRange.getBlob().getName(), e);
                }
            }
            TextAnnotation result = currentPageRange.get(offset);
            offset++;
            return result;
        }
    };
}
Also used : Iterator(java.util.Iterator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)8 ByteString (com.google.protobuf.ByteString)6 List (java.util.List)6 AsyncBatchAnnotateFilesResponse (com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse)5 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)5 Feature (com.google.cloud.vision.v1.Feature)5 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)5 Collectors (java.util.stream.Collectors)5 ModelAndView (org.springframework.web.servlet.ModelAndView)5 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)4 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)4 Image (com.google.cloud.vision.v1.Image)4 InputConfig (com.google.cloud.vision.v1.InputConfig)4 TextAnnotation (com.google.cloud.vision.v1.TextAnnotation)4 AsyncAnnotateFileRequest (com.google.cloud.vision.v1.AsyncAnnotateFileRequest)3 GcsDestination (com.google.cloud.vision.v1.GcsDestination)3 GcsSource (com.google.cloud.vision.v1.GcsSource)3 ImageContext (com.google.cloud.vision.v1.ImageContext)3 OperationMetadata (com.google.cloud.vision.v1.OperationMetadata)3 ArrayList (java.util.ArrayList)3