Search in sources :

Example 1 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource 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);
}
Also used : GcsSource(com.google.cloud.vision.v1.GcsSource) OutputConfig(com.google.cloud.vision.v1.OutputConfig) AsyncBatchAnnotateFilesResponse(com.google.cloud.vision.v1.AsyncBatchAnnotateFilesResponse) InputConfig(com.google.cloud.vision.v1.InputConfig) AsyncAnnotateFileRequest(com.google.cloud.vision.v1.AsyncAnnotateFileRequest) GcsDestination(com.google.cloud.vision.v1.GcsDestination) OperationMetadata(com.google.cloud.vision.v1.OperationMetadata)

Example 2 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-datalabeling by googleapis.

the class LabelVideoIT method setUp.

@Before
public void setUp() {
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {
        // Create the dataset
        CreateDataset.createDataset(PROJECT_ID, datasetName);
        ProjectName projectName = ProjectName.of(PROJECT_ID);
        // Get the Dataset
        ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient.listDatasets(datasetsRequest);
        for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) {
            if (returnedDataset.getDisplayName().equals("LABEL_VIDEO_DATASET_NAME")) {
                dataset = returnedDataset;
            }
        }
        // Import the images
        // ImportData.importData(dataset.getName(), DATASET_GCS_SOURCE_URI);
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(DATASET_GCS_SOURCE_URI).setMimeType("text/csv").build();
        InputConfig inputConfig = InputConfig.newBuilder().setDataType(// DataTypes: AUDIO, IMAGE, VIDEO, TEXT
        DataType.VIDEO).setGcsSource(gcsSource).build();
        ImportDataRequest importDataRequest = ImportDataRequest.newBuilder().setName(dataset.getName()).setInputConfig(inputConfig).build();
        ImportDataOperationResponse response = dataLabelingServiceClient.importDataAsync(importDataRequest).get();
        System.out.format("Imported items: %d\n", response.getImportCount());
        // Create the instruction
        CreateInstruction.createInstruction(PROJECT_ID, INSTRUCTION_GCS_SOURCE_URI);
        // Create the annotation spec set
        CreateAnnotationSpecSet.createAnnotationSpecSet(PROJECT_ID);
        // Get the instruction
        ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder().setParent(projectName.toString()).build();
        ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient.listInstructions(instructionsRequest);
        for (Instruction returnedInstruction : instructionsResponse.getPage().iterateAll()) {
            if (returnedInstruction.getDisplayName().equals("YOUR_INSTRUCTION_DISPLAY_NAME")) {
                instruction = returnedInstruction;
            }
        }
        // Get the annotation spec set
        ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient.listAnnotationSpecSets(annotationRequest);
        for (AnnotationSpecSet returnedAnnotation : annotationsResponse.getPage().iterateAll()) {
            if (returnedAnnotation.getDisplayName().equals("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")) {
                annotationSpecSet = returnedAnnotation;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ListAnnotationSpecSetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse) PrintStream(java.io.PrintStream) DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ListDatasetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse) GcsSource(com.google.cloud.datalabeling.v1beta1.GcsSource) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) ListAnnotationSpecSetsRequest(com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest) Dataset(com.google.cloud.datalabeling.v1beta1.Dataset) AnnotationSpecSet(com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Instruction(com.google.cloud.datalabeling.v1beta1.Instruction) IOException(java.io.IOException) ListDatasetsRequest(com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest) ListInstructionsRequest(com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest) ListInstructionsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse) InputConfig(com.google.cloud.datalabeling.v1beta1.InputConfig) ImportDataRequest(com.google.cloud.datalabeling.v1beta1.ImportDataRequest) ImportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse) Before(org.junit.Before)

Example 3 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-datalabeling by googleapis.

the class ImportData method importData.

// Import data to an existing dataset.
static void importData(String datasetName, String gcsSourceUri) throws IOException {
    // String datasetName = DataLabelingServiceClient.formatDatasetName(
    // "YOUR_PROJECT_ID", "YOUR_DATASETS_UUID");
    // String gcsSourceUri = "gs://YOUR_BUCKET_ID/path_to_data";
    // [END datalabeling_import_data_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_import_data_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(gcsSourceUri).setMimeType("text/csv").build();
        InputConfig inputConfig = InputConfig.newBuilder().setDataType(// DataTypes: AUDIO, IMAGE, VIDEO, TEXT
        DataType.IMAGE).setGcsSource(gcsSource).build();
        ImportDataRequest importDataRequest = ImportDataRequest.newBuilder().setName(datasetName).setInputConfig(inputConfig).build();
        OperationFuture<ImportDataOperationResponse, ImportDataOperationMetadata> operation = dataLabelingServiceClient.importDataAsync(importDataRequest);
        ImportDataOperationResponse response = operation.get();
        System.out.format("Imported items: %d\n", response.getImportCount());
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) GcsSource(com.google.cloud.datalabeling.v1beta1.GcsSource) IOException(java.io.IOException) InputConfig(com.google.cloud.datalabeling.v1beta1.InputConfig) ImportDataRequest(com.google.cloud.datalabeling.v1beta1.ImportDataRequest) ImportDataOperationMetadata(com.google.cloud.datalabeling.v1beta1.ImportDataOperationMetadata) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) ExecutionException(java.util.concurrent.ExecutionException) ImportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse)

Example 4 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-aiplatform by googleapis.

the class ImportDataImageObjectDetectionSample method importDataImageObjectDetectionSample.

static void importDataImageObjectDetectionSample(String project, String datasetId, String gcsSourceUri) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    DatasetServiceSettings datasetServiceSettings = DatasetServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DatasetServiceClient datasetServiceClient = DatasetServiceClient.create(datasetServiceSettings)) {
        String location = "us-central1";
        String importSchemaUri = "gs://google-cloud-aiplatform/schema/dataset/ioformat/" + "image_bounding_box_io_format_1.0.0.yaml";
        GcsSource.Builder gcsSource = GcsSource.newBuilder();
        gcsSource.addUris(gcsSourceUri);
        DatasetName datasetName = DatasetName.of(project, location, datasetId);
        List<ImportDataConfig> importDataConfigList = Collections.singletonList(ImportDataConfig.newBuilder().setGcsSource(gcsSource).setImportSchemaUri(importSchemaUri).build());
        OperationFuture<ImportDataResponse, ImportDataOperationMetadata> importDataResponseFuture = datasetServiceClient.importDataAsync(datasetName, importDataConfigList);
        System.out.format("Operation name: %s\n", importDataResponseFuture.getInitialFuture().get().getName());
        System.out.println("Waiting for operation to finish...");
        ImportDataResponse importDataResponse = importDataResponseFuture.get(300, TimeUnit.SECONDS);
        System.out.format("Import Data Image Object Detection Response: %s\n", importDataResponse.toString());
    }
}
Also used : ImportDataResponse(com.google.cloud.aiplatform.v1.ImportDataResponse) GcsSource(com.google.cloud.aiplatform.v1.GcsSource) ImportDataConfig(com.google.cloud.aiplatform.v1.ImportDataConfig) DatasetServiceSettings(com.google.cloud.aiplatform.v1.DatasetServiceSettings) DatasetName(com.google.cloud.aiplatform.v1.DatasetName) DatasetServiceClient(com.google.cloud.aiplatform.v1.DatasetServiceClient) ImportDataOperationMetadata(com.google.cloud.aiplatform.v1.ImportDataOperationMetadata)

Example 5 with GcsSource

use of com.google.cloud.aiplatform.v1.GcsSource in project java-aiplatform by googleapis.

the class ImportDataTextClassificationSingleLabelSample method importDataTextClassificationSingleLabelSample.

static void importDataTextClassificationSingleLabelSample(String project, String datasetId, String gcsSourceUri) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    DatasetServiceSettings datasetServiceSettings = DatasetServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DatasetServiceClient datasetServiceClient = DatasetServiceClient.create(datasetServiceSettings)) {
        String location = "us-central1";
        String importSchemaUri = "gs://google-cloud-aiplatform/schema/dataset/ioformat/" + "text_classification_single_label_io_format_1.0.0.yaml";
        GcsSource.Builder gcsSource = GcsSource.newBuilder();
        gcsSource.addUris(gcsSourceUri);
        DatasetName datasetName = DatasetName.of(project, location, datasetId);
        List<ImportDataConfig> importDataConfigList = Collections.singletonList(ImportDataConfig.newBuilder().setGcsSource(gcsSource).setImportSchemaUri(importSchemaUri).build());
        OperationFuture<ImportDataResponse, ImportDataOperationMetadata> importDataResponseFuture = datasetServiceClient.importDataAsync(datasetName, importDataConfigList);
        System.out.format("Operation name: %s\n", importDataResponseFuture.getInitialFuture().get().getName());
        System.out.println("Waiting for operation to finish...");
        ImportDataResponse importDataResponse = importDataResponseFuture.get(300, TimeUnit.SECONDS);
        System.out.format("Import Data Text Classification Response: %s\n", importDataResponse.toString());
    }
}
Also used : ImportDataResponse(com.google.cloud.aiplatform.v1.ImportDataResponse) GcsSource(com.google.cloud.aiplatform.v1.GcsSource) ImportDataConfig(com.google.cloud.aiplatform.v1.ImportDataConfig) DatasetServiceSettings(com.google.cloud.aiplatform.v1.DatasetServiceSettings) DatasetName(com.google.cloud.aiplatform.v1.DatasetName) DatasetServiceClient(com.google.cloud.aiplatform.v1.DatasetServiceClient) ImportDataOperationMetadata(com.google.cloud.aiplatform.v1.ImportDataOperationMetadata)

Aggregations

GcsSource (com.google.cloud.aiplatform.v1.GcsSource)16 BatchPredictionJob (com.google.cloud.aiplatform.v1.BatchPredictionJob)8 DatasetName (com.google.cloud.aiplatform.v1.DatasetName)8 DatasetServiceClient (com.google.cloud.aiplatform.v1.DatasetServiceClient)8 DatasetServiceSettings (com.google.cloud.aiplatform.v1.DatasetServiceSettings)8 GcsDestination (com.google.cloud.aiplatform.v1.GcsDestination)8 ImportDataConfig (com.google.cloud.aiplatform.v1.ImportDataConfig)8 ImportDataOperationMetadata (com.google.cloud.aiplatform.v1.ImportDataOperationMetadata)8 ImportDataResponse (com.google.cloud.aiplatform.v1.ImportDataResponse)8 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)8 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)8 LocationName (com.google.cloud.aiplatform.v1.LocationName)7 Document (com.google.cloud.documentai.v1beta2.Document)5 DocumentUnderstandingServiceClient (com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient)5 GcsSource (com.google.cloud.documentai.v1beta2.GcsSource)5 InputConfig (com.google.cloud.documentai.v1beta2.InputConfig)5 ProcessDocumentRequest (com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)5 GcsSource (com.google.cloud.translate.v3.GcsSource)5 LocationName (com.google.cloud.translate.v3.LocationName)5 TranslationServiceClient (com.google.cloud.translate.v3.TranslationServiceClient)5