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);
}
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;
}
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);
}
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");
});
}
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;
}
};
}
Aggregations