use of com.google.cloud.automl.v1beta1.OperationMetadata in project spring-cloud-gcp by spring-cloud.
the class DocumentOcrTemplate method runOcrForDocument.
/**
* Runs OCR processing for a specified {@code document} and generates OCR output files
* under the path specified by {@code outputFilePathPrefix}.
*
* <p>
* For example, if you specify an {@code outputFilePathPrefix} of
* "gs://bucket_name/ocr_results/myDoc_", all the output files of OCR processing will be
* saved under prefix, such as:
*
* <ul>
* <li>gs://bucket_name/ocr_results/myDoc_output-1-to-5.json
* <li>gs://bucket_name/ocr_results/myDoc_output-6-to-10.json
* <li>gs://bucket_name/ocr_results/myDoc_output-11-to-15.json
* </ul>
*
* <p>
* Note: OCR processing operations may take several minutes to complete, so it may not be
* advisable to block on the completion of the operation. One may use the returned
* {@link ListenableFuture} to register callbacks or track the status of the operation.
*
* @param document The {@link GoogleStorageLocation} of the document to run OCR processing
* @param outputFilePathPrefix The {@link GoogleStorageLocation} of a file, folder, or a
* bucket describing the path for which all output files shall be saved under
*
* @return A {@link ListenableFuture} allowing you to register callbacks or wait for the
* completion of the operation.
*/
public ListenableFuture<DocumentOcrResultSet> runOcrForDocument(GoogleStorageLocation document, GoogleStorageLocation outputFilePathPrefix) {
Assert.isTrue(document.isFile(), "Provided document location is not a valid file location: " + document);
GcsSource gcsSource = GcsSource.newBuilder().setUri(document.uriString()).build();
String contentType = extractContentType(document);
InputConfig inputConfig = InputConfig.newBuilder().setMimeType(contentType).setGcsSource(gcsSource).build();
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputFilePathPrefix.uriString()).build();
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(this.jsonOutputBatchSize).build();
AsyncAnnotateFileRequest request = AsyncAnnotateFileRequest.newBuilder().addFeatures(DOCUMENT_OCR_FEATURE).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> result = imageAnnotatorClient.asyncBatchAnnotateFilesAsync(Collections.singletonList(request));
return extractOcrResultFuture(result);
}
use of com.google.cloud.automl.v1beta1.OperationMetadata in project java-automl by googleapis.
the class BatchPredict method batchPredict.
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
// Configure where to store the output in a GCS bucket
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
// Build the request that will be sent to the API
BatchPredictRequest request = BatchPredictRequest.newBuilder().setName(name.toString()).setInputConfig(inputConfig).setOutputConfig(outputConfig).build();
// Start an asynchronous request
OperationFuture<BatchPredictResult, OperationMetadata> future = client.batchPredictAsync(request);
System.out.println("Waiting for operation to complete...");
BatchPredictResult response = future.get();
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
}
}
use of com.google.cloud.automl.v1beta1.OperationMetadata in project java-automl by googleapis.
the class VisionObjectDetectionCreateModel method createModel.
// Create a model
static void createModel(String projectId, String datasetId, String displayName) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
// Set model metadata.
ImageObjectDetectionModelMetadata metadata = ImageObjectDetectionModelMetadata.newBuilder().build();
Model model = Model.newBuilder().setDisplayName(displayName).setDatasetId(datasetId).setImageObjectDetectionModelMetadata(metadata).build();
// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> future = client.createModelAsync(projectLocation, model);
// OperationFuture.get() will block until the model is created, which may take several hours.
// You can use OperationFuture.getInitialFuture to get a future representing the initial
// response to the request, which contains information while the operation is in progress.
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
System.out.println("Training started...");
}
}
use of com.google.cloud.automl.v1beta1.OperationMetadata in project java-automl by googleapis.
the class ModelApi method createModel.
// [START automl_vision_create_model]
/**
* Demonstrates using the AutoML client to create a model.
*
* @param projectId the Id of the project.
* @param computeRegion the Region name.
* @param dataSetId the Id of the dataset to which model is created.
* @param modelName the Name of the model.
* @param trainBudget the Budget for training the model.
*/
static void createModel(String projectId, String computeRegion, String dataSetId, String modelName, String trainBudget) {
// Instantiates a client
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);
// Set model metadata.
ImageClassificationModelMetadata imageClassificationModelMetadata = Long.valueOf(trainBudget) == 0 ? ImageClassificationModelMetadata.newBuilder().build() : ImageClassificationModelMetadata.newBuilder().setTrainBudget(Long.valueOf(trainBudget)).build();
// Set model name and model metadata for the image dataset.
Model myModel = Model.newBuilder().setDisplayName(modelName).setDatasetId(dataSetId).setImageClassificationModelMetadata(imageClassificationModelMetadata).build();
// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> response = client.createModelAsync(projectLocation, myModel);
System.out.println(String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
System.out.println("Training started...");
} catch (IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
use of com.google.cloud.automl.v1beta1.OperationMetadata in project java-automl by googleapis.
the class DeployModel method deployModel.
// Deploy a model for prediction
static void deployModel(String projectId, String modelId) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
DeployModelRequest request = DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
future.get();
System.out.println("Model deployment finished");
}
}
Aggregations