public static void main(String... args) throws Exception { ImageAnnotatorClient vision = ImageAnnotatorClient.create(); Image img = Image.newBuilder().setContent(imgBytes).build(); Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); if (res.hasError()) { System.out.printf("Error: %s\n", res.getError().getMessage()); return; for (EntityAnnotation annotation : res.getLabelAnnotationsList()) { for (Map.Entry<FieldDescriptor, Object> entry : annotation.getAllFields().entrySet()) { System.out.printf("%s : %s\n", entry.getKey(), entry.getValue());
/** * Run asynchronous image detection and annotation for a list of generic files, such as PDF files, * which may contain multiple pages and multiple images per page. Progress and results can be * retrieved through the `google.longrunning.Operations` interface. `Operation.metadata` contains * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` * (results). * * <p>Sample code: * * <pre><code> * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { * List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); * AsyncBatchAnnotateFilesResponse response = imageAnnotatorClient.asyncBatchAnnotateFilesAsync(requests).get(); * } * </code></pre> * * @param requests Individual async file annotation requests for this batch. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @BetaApi( "The surface for long-running operations is not stable yet and may change in the future.") public final OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> asyncBatchAnnotateFilesAsync(List<AsyncAnnotateFileRequest> requests) { AsyncBatchAnnotateFilesRequest request = AsyncBatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); return asyncBatchAnnotateFilesAsync(request); }
@After public void tearDown() throws Exception { client.close(); }
public static void executeNoCatch() throws Exception { try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { String gcsImageUri = "gs://gapic-toolkit/President_Barack_Obama.jpg"; ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsImageUri).build(); Image image = Image.newBuilder().setSource(source).build(); Feature.Type type = Feature.Type.FACE_DETECTION; Feature featuresElement = Feature.newBuilder().setType(type).build(); List<Feature> features = Arrays.asList(featuresElement); AnnotateImageRequest requestsElement = AnnotateImageRequest.newBuilder().setImage(image).addAllFeatures(features).build(); List<AnnotateImageRequest> requests = Arrays.asList(requestsElement); BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); } }
Image img = Image.newBuilder().setContent(imgBytes).build(); Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); client.close(); if (res.hasError()) { out.printf("Error: %s\n", res.getError().getMessage()); return; TextAnnotation annotation = res.getFullTextAnnotation(); for (Page page: annotation.getPagesList()) { String pageText = "";
IOException { try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { Image image = Image.newBuilder().setContent(contents).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder() .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) .setImage(image) .build(); BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); response.getResponsesList().stream() .forEach(r -> r.getWebDetection().getWebEntitiesList().stream() .forEach(entity -> { out.format("Description: %s\n", entity.getDescription());
Image img = Image.newBuilder().setSource(imgSource).build(); AnnotateImageRequest.newBuilder() .addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)) .setImage(img) .build(); requests.add(request); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); client.close(); for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { out.format("Object name: %s\n", entity.getName()); out.format("Confidence: %s\n", entity.getScore());
Image image = Image.newBuilder().setContent(imgBytes).build(); .map((featureType) -> Feature.newBuilder().setType(featureType).build()) .collect(Collectors.toList()); AnnotateImageRequest.newBuilder() .addAllFeatures(featureList) .setImage(image)) .build(); BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request); List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build(); client.asyncBatchAnnotateFilesAsync(requests); System.out.format("\nText: %s\n", annotateImageResponse.getFullTextAnnotation().getText()); } else { System.out.println("No MATCH");
/** Constructs an instance of ImageAnnotatorClient with default settings. */ public static final ImageAnnotatorClient create() throws IOException { return create(ImageAnnotatorSettings.newBuilder().build()); }
/** * Run image detection and annotation for a batch of images. * * <p>Sample code: * * <pre><code> * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { * List<AnnotateImageRequest> requests = new ArrayList<>(); * BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests); * } * </code></pre> * * @param requests Individual image annotation requests for this batch. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final BatchAnnotateImagesResponse batchAnnotateImages( List<AnnotateImageRequest> requests) { BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addAllRequests(requests).build(); return batchAnnotateImages(request); }
@Test @SuppressWarnings("all") public void batchAnnotateImagesTest() { BatchAnnotateImagesResponse expectedResponse = BatchAnnotateImagesResponse.newBuilder().build(); mockImageAnnotator.addResponse(expectedResponse); List<AnnotateImageRequest> requests = new ArrayList<>(); BatchAnnotateImagesResponse actualResponse = client.batchAnnotateImages(requests); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockImageAnnotator.getRequests(); Assert.assertEquals(1, actualRequests.size()); BatchAnnotateImagesRequest actualRequest = (BatchAnnotateImagesRequest) actualRequests.get(0); Assert.assertEquals(requests, actualRequest.getRequestsList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * Constructs an instance of ImageAnnotatorClient, using the given settings. The channels are * created based on the settings passed in, or defaults for any settings that are not set. */ public static final ImageAnnotatorClient create(ImageAnnotatorSettings settings) throws IOException { return new ImageAnnotatorClient(settings); }
/** * Run asynchronous image detection and annotation for a list of generic files, such as PDF files, * which may contain multiple pages and multiple images per page. Progress and results can be * retrieved through the `google.longrunning.Operations` interface. `Operation.metadata` contains * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` * (results). * * <p>Sample code: * * <pre><code> * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { * List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); * AsyncBatchAnnotateFilesRequest request = AsyncBatchAnnotateFilesRequest.newBuilder() * .addAllRequests(requests) * .build(); * AsyncBatchAnnotateFilesResponse response = imageAnnotatorClient.asyncBatchAnnotateFilesAsync(request).get(); * } * </code></pre> * * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ @BetaApi( "The surface for long-running operations is not stable yet and may change in the future.") public final OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> asyncBatchAnnotateFilesAsync(AsyncBatchAnnotateFilesRequest request) { return asyncBatchAnnotateFilesOperationCallable().futureCall(request); }
Image img = Image.newBuilder().setSource(imgSource).build(); Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build(); requests.add(request); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); client.close(); if (res.hasError()) { out.printf("Error: %s\n", res.getError().getMessage()); return; TextAnnotation annotation = res.getFullTextAnnotation(); for (Page page: annotation.getPagesList()) { String pageText = "";
IOException { try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { .build(); Image image = Image.newBuilder().setSource(imageSource).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder() .addFeatures(Feature.newBuilder().setType(Type.WEB_DETECTION)) .setImage(image) .build(); BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request)); response.getResponsesList().stream() .forEach(r -> r.getWebDetection().getWebEntitiesList().stream() .forEach(entity -> { System.out.format("Description: %s\n", entity.getDescription());
Image img = Image.newBuilder().setContent(imgBytes).build(); AnnotateImageRequest request = AnnotateImageRequest.newBuilder() .addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)) .setImage(img) .build(); requests.add(request); try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) { BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests); List<AnnotateImageResponse> responses = response.getResponsesList(); for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) { out.format("Object name: %s\n", entity.getName()); out.format("Confidence: %s\n", entity.getScore());
Image image = Image.newBuilder().setContent(imgBytes).build(); .map((featureType) -> Feature.newBuilder().setType(featureType).build()) .collect(Collectors.toList()); AnnotateImageRequest.newBuilder() .addAllFeatures(featureList) .setImage(image)) .build(); BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request); List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
@Before public void setUp() throws IOException { serviceHelper.reset(); channelProvider = serviceHelper.createChannelProvider(); ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder() .setTransportChannelProvider(channelProvider) .setCredentialsProvider(NoCredentialsProvider.create()) .build(); client = ImageAnnotatorClient.create(settings); }
@Test @SuppressWarnings("all") public void batchAnnotateImagesExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockImageAnnotator.addException(exception); try { List<AnnotateImageRequest> requests = new ArrayList<>(); client.batchAnnotateImages(requests); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }