diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java new file mode 100644 index 00000000000..4d9a3823212 --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java @@ -0,0 +1,99 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_create_annotation_spec_set_beta] +import com.google.cloud.datalabeling.v1beta1.AnnotationSpec; +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; +import com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +class CreateAnnotationSpecSet { + + // Create an annotation spec set. + static void createAnnotationSpecSet(String projectId) throws IOException { + // String projectId = "YOUR_PROJECT_ID"; + + Map annotationLabels = new HashMap<>(); + annotationLabels.put("label_1", "label_1_description"); + annotationLabels.put("label_2", "label_2_description"); + + // [END datalabeling_create_annotation_spec_set_beta] + String endpoint = System.getenv("DATALABELING_ENDPOINT"); + if (endpoint == null) { + endpoint = DataLabelingServiceSettings.getDefaultEndpoint(); + } + // [START datalabeling_create_annotation_spec_set_beta] + + DataLabelingServiceSettings settings = + DataLabelingServiceSettings.newBuilder() + // [END datalabeling_create_annotation_spec_set_beta] + .setEndpoint(endpoint) + // [START datalabeling_create_annotation_spec_set_beta] + .build(); + try (DataLabelingServiceClient dataLabelingServiceClient = + DataLabelingServiceClient.create(settings)) { + ProjectName projectName = ProjectName.of(projectId); + + List annotationSpecs = new ArrayList<>(); + for (Entry entry : annotationLabels.entrySet()) { + AnnotationSpec annotationSpec = + AnnotationSpec.newBuilder() + .setDisplayName(entry.getKey()) + .setDescription(entry.getValue()) + .build(); + annotationSpecs.add(annotationSpec); + } + + AnnotationSpecSet annotationSpecSet = + AnnotationSpecSet.newBuilder() + .setDisplayName("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME") + .setDescription("YOUR_DESCRIPTION") + .addAllAnnotationSpecs(annotationSpecs) + .build(); + + CreateAnnotationSpecSetRequest request = + CreateAnnotationSpecSetRequest.newBuilder() + .setAnnotationSpecSet(annotationSpecSet) + .setParent(projectName.toString()) + .build(); + + AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request); + + System.out.format("Name: %s\n", result.getName()); + System.out.format("DisplayName: %s\n", result.getDisplayName()); + System.out.format("Description: %s\n", result.getDescription()); + System.out.format("Annotation Count: %d\n", result.getAnnotationSpecsCount()); + + for (AnnotationSpec annotationSpec : result.getAnnotationSpecsList()) { + System.out.format("\tDisplayName: %s\n", annotationSpec.getDisplayName()); + System.out.format("\tDescription: %s\n\n", annotationSpec.getDescription()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END datalabeling_create_annotation_spec_set_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateDataset.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateDataset.java new file mode 100644 index 00000000000..9f4fa8dfec8 --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateDataset.java @@ -0,0 +1,73 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_create_dataset_beta] +import com.google.cloud.datalabeling.v1beta1.CreateDatasetRequest; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.IOException; + +class 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() + // [END datalabeling_create_dataset_beta] + .setEndpoint(endpoint) + // [START datalabeling_create_dataset_beta] + .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(); + } + } +} +// [END datalabeling_create_dataset_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateInstruction.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateInstruction.java new file mode 100644 index 00000000000..7562189993b --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/CreateInstruction.java @@ -0,0 +1,87 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_create_instruction_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.CreateInstructionMetadata; +import com.google.cloud.datalabeling.v1beta1.CreateInstructionRequest; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.DataType; +import com.google.cloud.datalabeling.v1beta1.Instruction; +import com.google.cloud.datalabeling.v1beta1.PdfInstruction; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class CreateInstruction { + + // Create a instruction for a dataset. + static void createInstruction(String projectId, String pdfUri) throws IOException { + // String projectId = "YOUR_PROJECT_ID"; + // String pdfUri = "gs://YOUR_BUCKET_ID/path_to_pdf_or_csv"; + + // [END datalabeling_create_instruction_beta] + String endpoint = System.getenv("DATALABELING_ENDPOINT"); + if (endpoint == null) { + endpoint = DataLabelingServiceSettings.getDefaultEndpoint(); + } + // [START datalabeling_create_instruction_beta] + + DataLabelingServiceSettings settings = + DataLabelingServiceSettings.newBuilder() + // [END datalabeling_create_instruction_beta] + .setEndpoint(endpoint) + // [START datalabeling_create_instruction_beta] + .build(); + try (DataLabelingServiceClient dataLabelingServiceClient = + DataLabelingServiceClient.create(settings)) { + ProjectName projectName = ProjectName.of(projectId); + + // There are two types of instructions: CSV (CsvInstruction) or PDF (PdfInstruction) + PdfInstruction pdfInstruction = PdfInstruction.newBuilder().setGcsFileUri(pdfUri).build(); + + Instruction instruction = + Instruction.newBuilder() + .setDisplayName("YOUR_INSTRUCTION_DISPLAY_NAME") + .setDescription("YOUR_DESCRIPTION") + .setDataType(DataType.IMAGE) // DataTypes: AUDIO, IMAGE, VIDEO, TEXT + .setPdfInstruction(pdfInstruction) // .setCsvInstruction() or .setPdfInstruction() + .build(); + + CreateInstructionRequest createInstructionRequest = + CreateInstructionRequest.newBuilder() + .setInstruction(instruction) + .setParent(projectName.toString()) + .build(); + + OperationFuture operation = + dataLabelingServiceClient.createInstructionAsync(createInstructionRequest); + + Instruction result = operation.get(); + + System.out.format("Name: %s\n", result.getName()); + System.out.format("DisplayName: %s\n", result.getDisplayName()); + System.out.format("Description: %s\n", result.getDescription()); + System.out.format("GCS SOURCE URI: %s\n", result.getPdfInstruction().getGcsFileUri()); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END datalabeling_create_instruction_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/ExportData.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/ExportData.java new file mode 100644 index 00000000000..235fc635b6a --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/ExportData.java @@ -0,0 +1,92 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_export_data_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.ExportDataOperationMetadata; +import com.google.cloud.datalabeling.v1beta1.ExportDataOperationResponse; +import com.google.cloud.datalabeling.v1beta1.ExportDataRequest; +import com.google.cloud.datalabeling.v1beta1.GcsDestination; +import com.google.cloud.datalabeling.v1beta1.LabelStats; +import com.google.cloud.datalabeling.v1beta1.OutputConfig; +import java.io.IOException; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.ExecutionException; + +class 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() + // [END datalabeling_export_data_beta] + .setEndpoint(endpoint) + // [START datalabeling_export_data_beta] + .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 operation = + dataLabelingServiceClient.exportDataAsync(exportDataRequest); + + ExportDataOperationResponse response = operation.get(); + + System.out.format("Exported item count: %d\n", response.getExportCount()); + LabelStats labelStats = response.getLabelStats(); + Set> entries = labelStats.getExampleCountMap().entrySet(); + for (Entry 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(); + } + } +} +// [END datalabeling_export_data_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/ImportData.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/ImportData.java new file mode 100644 index 00000000000..e9d861e0db5 --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/ImportData.java @@ -0,0 +1,78 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_import_data_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.DataType; +import com.google.cloud.datalabeling.v1beta1.GcsSource; +import com.google.cloud.datalabeling.v1beta1.ImportDataOperationMetadata; +import com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse; +import com.google.cloud.datalabeling.v1beta1.ImportDataRequest; +import com.google.cloud.datalabeling.v1beta1.InputConfig; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class 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() + // [END datalabeling_import_data_beta] + .setEndpoint(endpoint) + // [START datalabeling_import_data_beta] + .build(); + try (DataLabelingServiceClient dataLabelingServiceClient = + DataLabelingServiceClient.create(settings)) { + GcsSource gcsSource = + GcsSource.newBuilder().setInputUri(gcsSourceUri).setMimeType("text/csv").build(); + + InputConfig inputConfig = + InputConfig.newBuilder() + .setDataType(DataType.IMAGE) // DataTypes: AUDIO, IMAGE, VIDEO, TEXT + .setGcsSource(gcsSource) + .build(); + + ImportDataRequest importDataRequest = + ImportDataRequest.newBuilder().setName(datasetName).setInputConfig(inputConfig).build(); + + OperationFuture operation = + dataLabelingServiceClient.importDataAsync(importDataRequest); + + ImportDataOperationResponse response = operation.get(); + + System.out.format("Imported items: %d\n", response.getImportCount()); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END datalabeling_import_data_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelImage.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelImage.java new file mode 100644 index 00000000000..1e4f1a8b200 --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelImage.java @@ -0,0 +1,100 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_label_image_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.AnnotatedDataset; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig; +import com.google.cloud.datalabeling.v1beta1.ImageClassificationConfig; +import com.google.cloud.datalabeling.v1beta1.LabelImageRequest; +import com.google.cloud.datalabeling.v1beta1.LabelImageRequest.Feature; +import com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata; +import com.google.cloud.datalabeling.v1beta1.StringAggregationType; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class 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() + // [END datalabeling_label_image_beta] + .setEndpoint(endpoint) + // [START datalabeling_label_image_beta] + .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 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(); + } + } +} +// [END datalabeling_label_image_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelText.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelText.java new file mode 100644 index 00000000000..92ded226126 --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelText.java @@ -0,0 +1,103 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_label_text_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.AnnotatedDataset; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig; +import com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata; +import com.google.cloud.datalabeling.v1beta1.LabelTextRequest; +import com.google.cloud.datalabeling.v1beta1.LabelTextRequest.Feature; +import com.google.cloud.datalabeling.v1beta1.SentimentConfig; +import com.google.cloud.datalabeling.v1beta1.TextClassificationConfig; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class 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() + // [END datalabeling_label_text_beta] + .setEndpoint(endpoint) + // [START datalabeling_label_text_beta] + .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 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(); + } + } +} +// [END datalabeling_label_text_beta] diff --git a/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelVideo.java b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelVideo.java new file mode 100644 index 00000000000..6dc71080a6d --- /dev/null +++ b/datalabeling/snippets/src/main/java/com/example/datalabeling/LabelVideo.java @@ -0,0 +1,104 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +// [START datalabeling_label_video_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.AnnotatedDataset; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings; +import com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig; +import com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata; +import com.google.cloud.datalabeling.v1beta1.LabelVideoRequest; +import com.google.cloud.datalabeling.v1beta1.LabelVideoRequest.Feature; +import com.google.cloud.datalabeling.v1beta1.VideoClassificationConfig; +import com.google.cloud.datalabeling.v1beta1.VideoClassificationConfig.AnnotationSpecSetConfig; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LabelVideo { + + // Start a Video Labeling Task + static void labelVideo( + 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_video_beta] + String endpoint = System.getenv("DATALABELING_ENDPOINT"); + if (endpoint == null) { + endpoint = DataLabelingServiceSettings.getDefaultEndpoint(); + } + // [START datalabeling_label_video_beta] + + DataLabelingServiceSettings settings = + DataLabelingServiceSettings.newBuilder() + // [END datalabeling_label_video_beta] + .setEndpoint(endpoint) + // [START datalabeling_label_video_beta] + .build(); + try (DataLabelingServiceClient dataLabelingServiceClient = + DataLabelingServiceClient.create(settings)) { + HumanAnnotationConfig humanAnnotationConfig = + HumanAnnotationConfig.newBuilder() + .setAnnotatedDatasetDisplayName("annotated_displayname") + .setAnnotatedDatasetDescription("annotated_description") + .setInstruction(formattedInstructionName) + .build(); + + AnnotationSpecSetConfig annotationSpecSetConfig = + AnnotationSpecSetConfig.newBuilder() + .setAnnotationSpecSet(formattedAnnotationSpecSetName) + .setAllowMultiLabel(true) + .build(); + + VideoClassificationConfig videoClassificationConfig = + VideoClassificationConfig.newBuilder() + .setApplyShotDetection(true) + .addAnnotationSpecSetConfigs(annotationSpecSetConfig) + .build(); + + LabelVideoRequest labelVideoRequest = + LabelVideoRequest.newBuilder() + .setParent(formattedDatasetName) + .setBasicConfig(humanAnnotationConfig) + .setVideoClassificationConfig(videoClassificationConfig) + .setFeature(Feature.CLASSIFICATION) + .build(); + + OperationFuture operation = + dataLabelingServiceClient.labelVideoAsync(labelVideoRequest); + + // 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(); + } + } +} +// [END datalabeling_label_video_beta] diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java new file mode 100644 index 00000000000..90ffb73fb13 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java @@ -0,0 +1,88 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link CreateAnnotationSpecSet}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateAnnotationSpecSetIT { + + private ByteArrayOutputStream bout; + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + // Delete the Annotation Spec Sheet + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); + ListAnnotationSpecSetsRequest listRequest = + ListAnnotationSpecSetsRequest.newBuilder().setParent(projectName.toString()).build(); + + ListAnnotationSpecSetsPagedResponse response = + dataLabelingServiceClient.listAnnotationSpecSets(listRequest); + + for (AnnotationSpecSet annotationSpecSet : response.getPage().iterateAll()) { + if (annotationSpecSet.getDisplayName().equals("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")) { + dataLabelingServiceClient.deleteAnnotationSpecSet(annotationSpecSet.getName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateAnnotationSpecSet() throws IOException { + CreateAnnotationSpecSet.createAnnotationSpecSet(PROJECT_ID); + + String output = bout.toString(); + + assertThat( + output, CoreMatchers.containsString("DisplayName: YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")); + assertThat(output, CoreMatchers.containsString("Description: YOUR_DESCRIPTION")); + assertThat(output, CoreMatchers.containsString("Annotation Count: 2")); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateDatasetIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateDatasetIT.java new file mode 100644 index 00000000000..eaeb6102b0d --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateDatasetIT.java @@ -0,0 +1,86 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link CreateDataset}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateDatasetIT { + + private ByteArrayOutputStream bout; + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String datasetName = "CREATE_DATASET_NAME"; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + // Delete the Dataset + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); + ListDatasetsRequest listRequest = + ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient.listDatasets(listRequest); + + for (Dataset dataset : response.getPage().iterateAll()) { + if (dataset.getDisplayName().equals(datasetName)) { + dataLabelingServiceClient.deleteDataset(dataset.getName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateDataset() throws IOException { + CreateDataset.createDataset(PROJECT_ID, datasetName); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString("DisplayName: CREATE_DATASET_NAME")); + assertThat(output, CoreMatchers.containsString("Description: YOUR_DESCRIPTION")); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateInstructionIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateInstructionIT.java new file mode 100644 index 00000000000..420a0796f41 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/CreateInstructionIT.java @@ -0,0 +1,90 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.Instruction; +import com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link CreateInstruction}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class CreateInstructionIT { + + private ByteArrayOutputStream bout; + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/instruction/test.pdf"; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + // Delete the Instruction + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); + ListInstructionsRequest listRequest = + ListInstructionsRequest.newBuilder().setParent(projectName.toString()).build(); + + ListInstructionsPagedResponse response = + dataLabelingServiceClient.listInstructions(listRequest); + + for (Instruction instruction : response.getPage().iterateAll()) { + if (instruction.getDisplayName().equals("YOUR_INSTRUCTION_DISPLAY_NAME")) { + dataLabelingServiceClient.deleteInstruction(instruction.getName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateInstruction() throws IOException { + CreateInstruction.createInstruction(PROJECT_ID, GCS_SOURCE_URI); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString("DisplayName: YOUR_INSTRUCTION_DISPLAY_NAME")); + assertThat(output, CoreMatchers.containsString("Description: YOUR_DESCRIPTION")); + assertThat( + output, CoreMatchers.containsString(String.format("GCS SOURCE URI: %s", GCS_SOURCE_URI))); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/ImportDataIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/ImportDataIT.java new file mode 100644 index 00000000000..4852d6c94a7 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/ImportDataIT.java @@ -0,0 +1,108 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import static org.junit.Assert.assertThat; + +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link ImportData}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ImportDataIT { + + private ByteArrayOutputStream bout; + + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + private static String GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/image/image_dataset.csv"; + private static String datasetName = "IMPORT_DATASET_NAME"; + + private static Dataset dataset; + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateDataset.createDataset(PROJECT_ID, datasetName); + + // Get the Dataset + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); + ListDatasetsRequest listRequest = + ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient.listDatasets(listRequest); + + for (Dataset returnedDataset : response.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("IMPORT_DATASET_NAME")) { + dataset = returnedDataset; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @After + public void tearDown() { + System.setOut(null); + bout.reset(); + + // Delete the Dataset + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); + ListDatasetsRequest listRequest = + ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient.listDatasets(listRequest); + + for (Dataset returnedDataset : response.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("IMPORT_DATASET_NAME")) { + dataLabelingServiceClient.deleteDataset(returnedDataset.getName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testImportDataset() throws IOException { + ImportData.importData(dataset.getName(), GCS_SOURCE_URI); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString("Imported items: 3")); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelImageIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelImageIT.java new file mode 100644 index 00000000000..eac1c2087f3 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelImageIT.java @@ -0,0 +1,131 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.Instruction; +import com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link LabelImage}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LabelImageIT { + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + + private static String DATASET_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/image/image_dataset.csv"; + private static String INSTRUCTION_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/instruction/test.pdf"; + private static String datsetName = "LABEL_IMAGE_DATASET_NAME"; + + private Dataset dataset; + private Instruction instruction; + private AnnotationSpecSet annotationSpecSet; + + @Before + public void setUp() { + System.setOut(new PrintStream(new ByteArrayOutputStream())); + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + // Create the dataset + CreateDataset.createDataset(PROJECT_ID, datsetName); + 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_IMAGE_DATASET_NAME")) { + dataset = returnedDataset; + } + } + + // Import the images + ImportData.importData(dataset.getName(), DATASET_GCS_SOURCE_URI); + + // 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(); + } + } + + @After + public void tearDown() { + System.setOut(null); + + // Delete the created dataset. + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + dataLabelingServiceClient.deleteDataset(dataset.getName()); + dataLabelingServiceClient.deleteInstruction(instruction.getName()); + dataLabelingServiceClient.deleteAnnotationSpecSet(annotationSpecSet.getName()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testLabelImage() throws IOException { + // Start the labeling task + LabelImage.labelImage(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelTextIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelTextIT.java new file mode 100644 index 00000000000..a80cac1a608 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelTextIT.java @@ -0,0 +1,154 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataType; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.GcsSource; +import com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse; +import com.google.cloud.datalabeling.v1beta1.ImportDataRequest; +import com.google.cloud.datalabeling.v1beta1.InputConfig; +import com.google.cloud.datalabeling.v1beta1.Instruction; +import com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link LabelText}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LabelTextIT { + private static String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + + private static String DATASET_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/text/text_dataset.csv"; + private static String INSTRUCTION_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/instruction/test.pdf"; + private static String datasetName = "LABEL_TEXT_DATASET_NAME"; + private Dataset dataset; + private Instruction instruction; + private AnnotationSpecSet annotationSpecSet; + + @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_TEXT_DATASET_NAME")) { + dataset = returnedDataset; + } + } + + // Import the texts + GcsSource gcsSource = + GcsSource.newBuilder() + .setInputUri(DATASET_GCS_SOURCE_URI) + .setMimeType("text/csv") + .build(); + + InputConfig inputConfig = + InputConfig.newBuilder() + .setDataType(DataType.TEXT) // DataTypes: AUDIO, IMAGE, VIDEO, TEXT + .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(); + } + } + + @After + public void tearDown() { + System.setOut(null); + + // Delete the created dataset. + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + dataLabelingServiceClient.deleteDataset(dataset.getName()); + dataLabelingServiceClient.deleteInstruction(instruction.getName()); + dataLabelingServiceClient.deleteAnnotationSpecSet(annotationSpecSet.getName()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testLabelText() throws IOException { + // Start the labeling task + LabelText.labelText(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +} diff --git a/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelVideoIT.java b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelVideoIT.java new file mode 100644 index 00000000000..473abad2c70 --- /dev/null +++ b/datalabeling/snippets/src/test/java/com/example/datalabeling/LabelVideoIT.java @@ -0,0 +1,156 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.datalabeling; + +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse; +import com.google.cloud.datalabeling.v1beta1.DataType; +import com.google.cloud.datalabeling.v1beta1.Dataset; +import com.google.cloud.datalabeling.v1beta1.GcsSource; +import com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse; +import com.google.cloud.datalabeling.v1beta1.ImportDataRequest; +import com.google.cloud.datalabeling.v1beta1.InputConfig; +import com.google.cloud.datalabeling.v1beta1.Instruction; +import com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest; +import com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest; +import com.google.cloud.datalabeling.v1beta1.ProjectName; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link LabelVideo}. */ +@RunWith(JUnit4.class) +@Ignore("b/146064330") +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LabelVideoIT { + private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); + + private static String DATASET_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/videos/video_dataset.csv"; + private static String INSTRUCTION_GCS_SOURCE_URI = + "gs://cloud-samples-data/datalabeling/instruction/test.pdf"; + private static String datasetName = "LABEL_VIDEO_DATASET_NAME"; + + private Dataset dataset; + private Instruction instruction; + private AnnotationSpecSet annotationSpecSet; + + @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(DataType.VIDEO) // DataTypes: AUDIO, IMAGE, VIDEO, TEXT + .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(); + } + } + + @After + public void tearDown() { + System.setOut(null); + + // Delete the created dataset. + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + dataLabelingServiceClient.deleteDataset(dataset.getName()); + dataLabelingServiceClient.deleteInstruction(instruction.getName()); + dataLabelingServiceClient.deleteAnnotationSpecSet(annotationSpecSet.getName()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testLabelVideo() throws IOException { + // Start the labeling task + LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +}