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