Search in sources :

Example 1 with DataLabelingServiceSettings

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings in project java-datalabeling by googleapis.

the class CreateDataset method createDataset.

// Create a dataset that is initially empty.
static void createDataset(String projectId, String datasetName) throws IOException {
    // String projectId = "YOUR_PROJECT_ID";
    // String datasetName = "YOUR_DATASET_DISPLAY_NAME";
    // [END datalabeling_create_dataset_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_create_dataset_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        ProjectName projectName = ProjectName.of(projectId);
        Dataset dataset = Dataset.newBuilder().setDisplayName(datasetName).setDescription("YOUR_DESCRIPTION").build();
        CreateDatasetRequest createDatasetRequest = CreateDatasetRequest.newBuilder().setParent(projectName.toString()).setDataset(dataset).build();
        Dataset createdDataset = dataLabelingServiceClient.createDataset(createDatasetRequest);
        System.out.format("Name: %s\n", createdDataset.getName());
        System.out.format("DisplayName: %s\n", createdDataset.getDisplayName());
        System.out.format("Description: %s\n", createdDataset.getDescription());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : CreateDatasetRequest(com.google.cloud.datalabeling.v1beta1.CreateDatasetRequest) DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) Dataset(com.google.cloud.datalabeling.v1beta1.Dataset) IOException(java.io.IOException) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings)

Example 2 with DataLabelingServiceSettings

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings in project java-datalabeling by googleapis.

the class ExportData method exportData.

// Export data from an annotated dataset.
static void exportData(String datasetName, String annotatedDatasetName, String gcsOutputUri) throws IOException {
    // String datasetName = DataLabelingServiceClient.formatDatasetName(
    // "YOUR_PROJECT_ID", "YOUR_DATASETS_UUID");
    // String annotatedDatasetName = DataLabelingServiceClient.formatAnnotatedDatasetName(
    // "YOUR_PROJECT_ID",
    // "YOUR_DATASET_UUID",
    // "YOUR_ANNOTATED_DATASET_UUID");
    // String gcsOutputUri = "gs://YOUR_BUCKET_ID/export_path";
    // [END datalabeling_export_data_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_export_data_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUri(gcsOutputUri).setMimeType("text/csv").build();
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        ExportDataRequest exportDataRequest = ExportDataRequest.newBuilder().setName(datasetName).setOutputConfig(outputConfig).setAnnotatedDataset(annotatedDatasetName).build();
        OperationFuture<ExportDataOperationResponse, ExportDataOperationMetadata> operation = dataLabelingServiceClient.exportDataAsync(exportDataRequest);
        ExportDataOperationResponse response = operation.get();
        System.out.format("Exported item count: %d\n", response.getExportCount());
        LabelStats labelStats = response.getLabelStats();
        Set<Entry<String, Long>> entries = labelStats.getExampleCountMap().entrySet();
        for (Entry<String, Long> entry : entries) {
            System.out.format("\tLabel: %s\n", entry.getKey());
            System.out.format("\tCount: %d\n\n", entry.getValue());
        }
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ExportDataRequest(com.google.cloud.datalabeling.v1beta1.ExportDataRequest) IOException(java.io.IOException) ExportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ExportDataOperationResponse) Entry(java.util.Map.Entry) OutputConfig(com.google.cloud.datalabeling.v1beta1.OutputConfig) ExportDataOperationMetadata(com.google.cloud.datalabeling.v1beta1.ExportDataOperationMetadata) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) GcsDestination(com.google.cloud.datalabeling.v1beta1.GcsDestination) LabelStats(com.google.cloud.datalabeling.v1beta1.LabelStats) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with DataLabelingServiceSettings

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings 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 DataLabelingServiceSettings

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings in project java-datalabeling by googleapis.

the class LabelText method labelText.

// Start a Text Labeling Task
static void labelText(String formattedInstructionName, String formattedAnnotationSpecSetName, String formattedDatasetName) throws IOException {
    // String formattedInstructionName = DataLabelingServiceClient.formatInstructionName(
    // "YOUR_PROJECT_ID", "YOUR_INSTRUCTION_UUID");
    // String formattedAnnotationSpecSetName =
    // DataLabelingServiceClient.formatAnnotationSpecSetName(
    // "YOUR_PROJECT_ID", "YOUR_ANNOTATION_SPEC_SET_UUID");
    // String formattedDatasetName = DataLabelingServiceClient.formatDatasetName(
    // "YOUR_PROJECT_ID", "YOUR_DATASET_UUID");
    // [END datalabeling_label_text_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_label_text_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        HumanAnnotationConfig humanAnnotationConfig = HumanAnnotationConfig.newBuilder().setAnnotatedDatasetDisplayName("annotated_displayname").setAnnotatedDatasetDescription("annotated_description").setLanguageCode("en-us").setInstruction(formattedInstructionName).build();
        SentimentConfig sentimentConfig = SentimentConfig.newBuilder().setEnableLabelSentimentSelection(false).build();
        TextClassificationConfig textClassificationConfig = TextClassificationConfig.newBuilder().setAnnotationSpecSet(formattedAnnotationSpecSetName).setSentimentConfig(sentimentConfig).build();
        LabelTextRequest labelTextRequest = LabelTextRequest.newBuilder().setParent(formattedDatasetName).setBasicConfig(humanAnnotationConfig).setTextClassificationConfig(textClassificationConfig).setFeature(Feature.TEXT_CLASSIFICATION).build();
        OperationFuture<AnnotatedDataset, LabelOperationMetadata> operation = dataLabelingServiceClient.labelTextAsync(labelTextRequest);
        // You'll want to save this for later to retrieve your completed operation.
        // System.out.format("Operation Name: %s\n", operation.getName());
        // Cancel the operation to avoid charges when testing.
        dataLabelingServiceClient.getOperationsClient().cancelOperation(operation.getName());
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) LabelOperationMetadata(com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata) SentimentConfig(com.google.cloud.datalabeling.v1beta1.SentimentConfig) IOException(java.io.IOException) LabelTextRequest(com.google.cloud.datalabeling.v1beta1.LabelTextRequest) AnnotatedDataset(com.google.cloud.datalabeling.v1beta1.AnnotatedDataset) HumanAnnotationConfig(com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig) TextClassificationConfig(com.google.cloud.datalabeling.v1beta1.TextClassificationConfig) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with DataLabelingServiceSettings

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings in project java-datalabeling by googleapis.

the class LabelImage method labelImage.

// Start an Image Labeling Task
static void labelImage(String formattedInstructionName, String formattedAnnotationSpecSetName, String formattedDatasetName) throws IOException {
    // String formattedInstructionName = DataLabelingServiceClient.formatInstructionName(
    // "YOUR_PROJECT_ID", "YOUR_INSTRUCTION_UUID");
    // String formattedAnnotationSpecSetName =
    // DataLabelingServiceClient.formatAnnotationSpecSetName(
    // "YOUR_PROJECT_ID", "YOUR_ANNOTATION_SPEC_SET_UUID");
    // String formattedDatasetName = DataLabelingServiceClient.formatDatasetName(
    // "YOUR_PROJECT_ID", "YOUR_DATASET_UUID");
    // [END datalabeling_label_image_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_label_image_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        HumanAnnotationConfig humanAnnotationConfig = HumanAnnotationConfig.newBuilder().setAnnotatedDatasetDisplayName("annotated_displayname").setAnnotatedDatasetDescription("annotated_description").setInstruction(formattedInstructionName).build();
        ImageClassificationConfig imageClassificationConfig = ImageClassificationConfig.newBuilder().setAllowMultiLabel(true).setAnswerAggregationType(StringAggregationType.MAJORITY_VOTE).setAnnotationSpecSet(formattedAnnotationSpecSetName).build();
        LabelImageRequest labelImageRequest = LabelImageRequest.newBuilder().setParent(formattedDatasetName).setBasicConfig(humanAnnotationConfig).setImageClassificationConfig(imageClassificationConfig).setFeature(Feature.CLASSIFICATION).build();
        OperationFuture<AnnotatedDataset, LabelOperationMetadata> operation = dataLabelingServiceClient.labelImageAsync(labelImageRequest);
        // You'll want to save this for later to retrieve your completed operation.
        System.out.format("Operation Name: %s\n", operation.getName());
        // Cancel the operation to avoid charges when testing.
        dataLabelingServiceClient.getOperationsClient().cancelOperation(operation.getName());
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) LabelOperationMetadata(com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata) IOException(java.io.IOException) AnnotatedDataset(com.google.cloud.datalabeling.v1beta1.AnnotatedDataset) HumanAnnotationConfig(com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig) LabelImageRequest(com.google.cloud.datalabeling.v1beta1.LabelImageRequest) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) ExecutionException(java.util.concurrent.ExecutionException) ImageClassificationConfig(com.google.cloud.datalabeling.v1beta1.ImageClassificationConfig)

Aggregations

DataLabelingServiceClient (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient)8 DataLabelingServiceSettings (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings)8 IOException (java.io.IOException)8 ExecutionException (java.util.concurrent.ExecutionException)6 AnnotatedDataset (com.google.cloud.datalabeling.v1beta1.AnnotatedDataset)3 HumanAnnotationConfig (com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig)3 LabelOperationMetadata (com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata)3 ProjectName (com.google.cloud.datalabeling.v1beta1.ProjectName)3 AnnotationSpec (com.google.cloud.datalabeling.v1beta1.AnnotationSpec)1 AnnotationSpecSet (com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet)1 CreateAnnotationSpecSetRequest (com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest)1 CreateDatasetRequest (com.google.cloud.datalabeling.v1beta1.CreateDatasetRequest)1 CreateInstructionMetadata (com.google.cloud.datalabeling.v1beta1.CreateInstructionMetadata)1 CreateInstructionRequest (com.google.cloud.datalabeling.v1beta1.CreateInstructionRequest)1 Dataset (com.google.cloud.datalabeling.v1beta1.Dataset)1 ExportDataOperationMetadata (com.google.cloud.datalabeling.v1beta1.ExportDataOperationMetadata)1 ExportDataOperationResponse (com.google.cloud.datalabeling.v1beta1.ExportDataOperationResponse)1 ExportDataRequest (com.google.cloud.datalabeling.v1beta1.ExportDataRequest)1 GcsDestination (com.google.cloud.datalabeling.v1beta1.GcsDestination)1 GcsSource (com.google.cloud.datalabeling.v1beta1.GcsSource)1