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