Search in sources :

Example 1 with Status

use of com.google.api.services.vision.v1.model.Status in project java-docs-samples by GoogleCloudPlatform.

the class TextApp method detectText.

/**
 * Gets up to {@code maxResults} text annotations for images stored at {@code paths}.
 */
public ImmutableList<ImageText> detectText(List<Path> paths) {
    ImmutableList.Builder<AnnotateImageRequest> requests = ImmutableList.builder();
    try {
        for (Path path : paths) {
            byte[] data;
            data = Files.readAllBytes(path);
            requests.add(new AnnotateImageRequest().setImage(new Image().encodeContent(data)).setFeatures(ImmutableList.of(new Feature().setType("TEXT_DETECTION").setMaxResults(MAX_RESULTS))));
        }
        Vision.Images.Annotate annotate = vision.images().annotate(new BatchAnnotateImagesRequest().setRequests(requests.build()));
        // Due to a bug: requests to Vision API containing large images fail when GZipped.
        annotate.setDisableGZipContent(true);
        BatchAnnotateImagesResponse batchResponse = annotate.execute();
        assert batchResponse.getResponses().size() == paths.size();
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (int i = 0; i < paths.size(); i++) {
            Path path = paths.get(i);
            AnnotateImageResponse response = batchResponse.getResponses().get(i);
            output.add(ImageText.builder().path(path).textAnnotations(MoreObjects.firstNonNull(response.getTextAnnotations(), ImmutableList.<EntityAnnotation>of())).error(response.getError()).build());
        }
        return output.build();
    } catch (IOException ex) {
        // Got an exception, which means the whole batch had an error.
        ImmutableList.Builder<ImageText> output = ImmutableList.builder();
        for (Path path : paths) {
            output.add(ImageText.builder().path(path).textAnnotations(ImmutableList.<EntityAnnotation>of()).error(new Status().setMessage(ex.getMessage())).build());
        }
        return output.build();
    }
}
Also used : Path(java.nio.file.Path) Status(com.google.api.services.vision.v1.model.Status) BatchAnnotateImagesRequest(com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) Image(com.google.api.services.vision.v1.model.Image) Feature(com.google.api.services.vision.v1.model.Feature) AnnotateImageRequest(com.google.api.services.vision.v1.model.AnnotateImageRequest) AnnotateImageResponse(com.google.api.services.vision.v1.model.AnnotateImageResponse) BatchAnnotateImagesResponse(com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)

Example 2 with Status

use of com.google.api.services.vision.v1.model.Status in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminService method createApplication.

@Override
public Application createApplication(@NotNull String locationId, @NotNull final String projectId, @NotNull final Credential credential) throws IOException, GoogleApiException {
    Application arg = new Application();
    arg.setId(projectId);
    arg.setLocationId(locationId);
    Apps.Create createRequest = GoogleApiClientFactory.getInstance().getAppEngineApiClient(credential).apps().create(arg);
    Operation operation;
    try {
        // make the initial request to create the application
        operation = createRequest.execute();
        // poll for updates while the application is being created
        boolean done = false;
        while (!done) {
            try {
                Thread.sleep(CREATE_APPLICATION_POLLING_INTERVAL_MS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            operation = getOperation(projectId, operation.getName(), credential);
            if (operation.getDone() != null) {
                done = operation.getDone();
            }
        }
    } catch (GoogleJsonResponseException e) {
        throw GoogleApiException.from(e);
    }
    if (operation.getError() != null) {
        Status status = operation.getError();
        throw new GoogleApiException(status.getMessage(), status.getCode());
    } else {
        Application result = new Application();
        result.putAll(operation.getResponse());
        return result;
    }
}
Also used : Status(com.google.api.services.appengine.v1.model.Status) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.appengine.v1.model.Operation) Application(com.google.api.services.appengine.v1.model.Application) Apps(com.google.api.services.appengine.v1.Appengine.Apps)

Example 3 with Status

use of com.google.api.services.vision.v1.model.Status in project google-cloud-intellij by GoogleCloudPlatform.

the class GoogleApiClientAppEngineAdminServiceTest method testCreateApplication_operationFailed.

@Test
public void testCreateApplication_operationFailed() throws IOException, GoogleApiException {
    String operationName = "apps/-/operations/12345";
    Operation inProgressOperation = buildInProgressOperation(operationName);
    when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
    String errorMessage = "The operation failed.";
    int errorCode = 400;
    Status status = new Status();
    status.setMessage(errorMessage);
    status.setCode(errorCode);
    Operation failedOperation = new Operation();
    failedOperation.setError(status);
    failedOperation.setDone(true);
    failedOperation.setName(operationName);
    when(appengineClientMock.getAppsCreateQuery().execute()).thenReturn(inProgressOperation);
    when(appengineClientMock.getAppsOperationsGetQuery().execute()).thenReturn(failedOperation);
    try {
        service.createApplication("us-east", "my-project-id", mock(Credential.class));
    } catch (GoogleApiException expected) {
        assertEquals(errorCode, expected.getStatusCode());
        assertEquals(errorMessage, expected.getMessage());
        return;
    }
    fail();
}
Also used : Status(com.google.api.services.appengine.v1.model.Status) Credential(com.google.api.client.auth.oauth2.Credential) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Operation(com.google.api.services.appengine.v1.model.Operation) Test(org.junit.Test)

Aggregations

Operation (com.google.api.services.appengine.v1.model.Operation)2 Status (com.google.api.services.appengine.v1.model.Status)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Apps (com.google.api.services.appengine.v1.Appengine.Apps)1 Application (com.google.api.services.appengine.v1.model.Application)1 AnnotateImageRequest (com.google.api.services.vision.v1.model.AnnotateImageRequest)1 AnnotateImageResponse (com.google.api.services.vision.v1.model.AnnotateImageResponse)1 BatchAnnotateImagesRequest (com.google.api.services.vision.v1.model.BatchAnnotateImagesRequest)1 BatchAnnotateImagesResponse (com.google.api.services.vision.v1.model.BatchAnnotateImagesResponse)1 Feature (com.google.api.services.vision.v1.model.Feature)1 Image (com.google.api.services.vision.v1.model.Image)1 Status (com.google.api.services.vision.v1.model.Status)1 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1