use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project java-vision by googleapis.
the class DetectCropHints method detectCropHints.
// Suggests a region to crop to for a local file.
public static void detectCropHints(String filePath) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
Image img = Image.newBuilder().setContent(imgBytes).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).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;
}
// For full list of available annotations, see http://g.co/cloud/vision/docs
CropHintsAnnotation annotation = res.getCropHintsAnnotation();
for (CropHint hint : annotation.getCropHintsList()) {
System.out.println(hint.getBoundingPoly());
}
}
}
}
use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project java-vision by googleapis.
the class ITSystemTest method detectWebEntitiesGcsTest.
@Test
public void detectWebEntitiesGcsTest() throws IOException {
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + "landmark/pofa.jpg").build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
List<AnnotateImageResponse> responses = response.getResponsesList();
List<String> actual = new ArrayList<>();
for (AnnotateImageResponse imgResponse : responses) {
for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
actual.add(entity.getDescription());
}
}
assertThat(actual).contains("The Palace Of Fine Arts");
}
use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest in project java-vision by googleapis.
the class ITSystemTest method getResponsesList.
private static List<AnnotateImageResponse> getResponsesList(String image, Type type, boolean isGcs) throws IOException {
ImageSource imgSource;
Image img;
if (isGcs) {
imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + image).build();
img = Image.newBuilder().setSource(imgSource).build();
} else {
ByteString imgBytes = ByteString.readFrom(new FileInputStream(RESOURCES + image));
img = Image.newBuilder().setContent(imgBytes).build();
}
Feature feat = Feature.newBuilder().setType(type).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
return response.getResponsesList();
}
use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest 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());
}));
}
}
use of com.google.cloud.vision.v1p4beta1.AnnotateImageRequest 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()));
}
}
}
}
Aggregations