use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by GoogleCloudPlatform.
the class CloudVisionTemplateTests method testAddImageContext_extractText.
@Test
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);
}
use of com.google.cloud.vision.v1p3beta1.ImageContext in project java-docs-samples by GoogleCloudPlatform.
the class Detect method detectWebEntitiesIncludeGeoResultsGcs.
// [END vision_web_entities_include_geo_results]
// [START vision_web_entities_include_geo_results_uri]
/**
* Find web entities given the remote image on Google Cloud Storage.
* @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities with
* geo results.
* @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 detectWebEntitiesIncludeGeoResultsGcs(String gcsPath, PrintStream out) throws Exception, IOException {
// Instantiates a client
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(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());
}));
}
}
use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by spring-cloud.
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);
}
use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by spring-cloud.
the class CloudVisionTemplate method analyzeImage.
/**
* Analyze an image and extract the features of the image specified by
* {@code featureTypes}.
* <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an
* image, such as text detection, image labelling, facial detection, etc. A full list of
* feature types can be found in {@link Feature.Type}.
* @param imageResource the image one wishes to analyze. The Cloud Vision APIs support
* image formats described here: https://cloud.google.com/vision/docs/supported-files
* @param imageContext the image context used to customize the Vision API request
* @param featureTypes the types of image analysis to perform on the image
* @return the results of image analyses
* @throws CloudVisionException if the image could not be read or if a malformed response
* is received from the Cloud Vision APIs
*/
public AnnotateImageResponse analyzeImage(Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
ByteString imgBytes;
try {
imgBytes = ByteString.readFrom(imageResource.getInputStream());
} catch (IOException ex) {
throw new CloudVisionException("Failed to read image bytes from provided resource.", ex);
}
Image image = Image.newBuilder().setContent(imgBytes).build();
List<Feature> featureList = Arrays.stream(featureTypes).map((featureType) -> Feature.newBuilder().setType(featureType).build()).collect(Collectors.toList());
BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addAllFeatures(featureList).setImageContext(imageContext).setImage(image)).build();
BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
if (!annotateImageResponses.isEmpty()) {
return annotateImageResponses.get(0);
} else {
throw new CloudVisionException("Failed to receive valid response Vision APIs; empty response received.");
}
}
use of com.google.cloud.vision.v1p3beta1.ImageContext in project spring-cloud-gcp by GoogleCloudPlatform.
the class CloudVisionTemplate method analyzeImage.
/**
* Analyze an image and extract the features of the image specified by {@code featureTypes}.
*
* <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an image,
* such as text detection, image labelling, facial detection, etc. A full list of feature types
* can be found in {@link Feature.Type}.
*
* @param imageResource the image one wishes to analyze. The Cloud Vision APIs support image
* formats described here: https://cloud.google.com/vision/docs/supported-files
* @param imageContext the image context used to customize the Vision API request
* @param featureTypes the types of image analysis to perform on the image
* @return the results of image analyses
* @throws CloudVisionException if the image could not be read or if a malformed response is
* received from the Cloud Vision APIs
*/
public AnnotateImageResponse analyzeImage(Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
ByteString imgBytes;
try {
imgBytes = ByteString.readFrom(imageResource.getInputStream());
} catch (IOException ex) {
throw new CloudVisionException(READ_BYTES_ERROR_MESSAGE, ex);
}
Image image = Image.newBuilder().setContent(imgBytes).build();
List<Feature> featureList = Arrays.stream(featureTypes).map(featureType -> Feature.newBuilder().setType(featureType).build()).collect(Collectors.toList());
BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addAllFeatures(featureList).setImageContext(imageContext).setImage(image)).build();
BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
if (!annotateImageResponses.isEmpty()) {
return annotateImageResponses.get(0);
} else {
throw new CloudVisionException(EMPTY_RESPONSE_ERROR_MESSAGE);
}
}
Aggregations