diff --git a/datalabeling/beta/cloud-client/README.md b/datalabeling/beta/cloud-client/README.md new file mode 100644 index 00000000000..4ac927c6100 --- /dev/null +++ b/datalabeling/beta/cloud-client/README.md @@ -0,0 +1,41 @@ +# DataLabeling API Java examples + +These samples demonstrate the use of the [DataLabeling API][https://cloud.google.com/datalabeling/]. + +These samples show how to perform the following actions: +* create / import a dataset and annotation spec sheet. +* create instructions for the labelers. +* start a labeling task for audio, images, text and video. +* export an annotated dataset. + + +## Prerequisites + +This sample requires you to have java [setup](https://cloud.google.com/java/docs/setup). + + +## Setup + +* Create a project with the [Google Cloud Console][cloud-console], and enable + the [DataLabeling API][datalabeling-api]. +* [Set up][auth] authentication. For + example, from the Cloud Console, create a service account, + download its json credentials file, then set the appropriate environment + variable: + + ```bash + export GOOGLE_CLOUD_PROJECT=PROJECT_ID + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json + ``` + +[cloud-console]: https://console.cloud.google.com +[datalabeling-api]: https://console.cloud.google.com/apis/library/datalabeling.googleapis.com +[auth]: https://cloud.google.com/docs/authentication/getting-started + +## Run the Tests + +To verify the API's are enabled, run the unit tests via + +```bash +mvn clean verify +``` \ No newline at end of file diff --git a/datalabeling/beta/cloud-client/pom.xml b/datalabeling/beta/cloud-client/pom.xml new file mode 100644 index 00000000000..129f38a01ea --- /dev/null +++ b/datalabeling/beta/cloud-client/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + com.example.datalabeling + datalabeling-samples + jar + + + + com.google.cloud.samples + shared-configuration + 1.0.10 + + + + 1.8 + 1.8 + UTF-8 + + + + + com.google.cloud + google-cloud-datalabeling + 0.86.0-alpha + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.42 + test + + + diff --git a/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java new file mode 100644 index 00000000000..28626466ef8 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java @@ -0,0 +1,82 @@ +/* + * 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.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) { + // String projectId = "YOUR_PROJECT_ID"; + + Map annotationLabels = new HashMap<>(); + annotationLabels.put("label_1", "label_1_description"); + annotationLabels.put("label_2", "label_2_description"); + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + + 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/beta/cloud-client/src/main/java/com/example/datalabeling/CreateDataset.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateDataset.java new file mode 100644 index 00000000000..b8eceb820e1 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateDataset.java @@ -0,0 +1,56 @@ +/* + * 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.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) { + // String projectId = "YOUR_PROJECT_ID"; + // String datasetName = "YOUR_DATASET_DISPLAY_NAME"; + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + 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/beta/cloud-client/src/main/java/com/example/datalabeling/CreateInstruction.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateInstruction.java new file mode 100644 index 00000000000..ab56b3eeed0 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateInstruction.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_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.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) { + // String projectId = "YOUR_PROJECT_ID"; + // String pdfUri = "gs://YOUR_BUCKET_ID/path_to_pdf_or_csv"; + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + + 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/beta/cloud-client/src/main/java/com/example/datalabeling/ExportData.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ExportData.java new file mode 100644 index 00000000000..9b73085cc0e --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ExportData.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_export_data_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; +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) { + // 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"; + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + 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] \ No newline at end of file diff --git a/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ImportData.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ImportData.java new file mode 100644 index 00000000000..f0e8c46dc13 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ImportData.java @@ -0,0 +1,66 @@ +/* + * 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.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) { + // String datasetName = DataLabelingServiceClient.formatDatasetName( + // "YOUR_PROJECT_ID", "YOUR_DATASETS_UUID"); + // String gcsSourceUri = "gs://YOUR_BUCKET_ID/path_to_data"; + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + 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/beta/cloud-client/src/main/java/com/example/datalabeling/LabelImage.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelImage.java new file mode 100644 index 00000000000..35f2503d1d8 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelImage.java @@ -0,0 +1,85 @@ +/* + * 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.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) { + // 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"); + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + + 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/beta/cloud-client/src/main/java/com/example/datalabeling/LabelText.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelText.java new file mode 100644 index 00000000000..2ff411f0dd8 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelText.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; + +// [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.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) { + // 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"); + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + + 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/beta/cloud-client/src/main/java/com/example/datalabeling/LabelVideo.java b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelVideo.java new file mode 100644 index 00000000000..b5705cf32fb --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelVideo.java @@ -0,0 +1,83 @@ +/* + * 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.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) { + // 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"); + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + + 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] \ No newline at end of file diff --git a/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java new file mode 100644 index 00000000000..3ee1103d960 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java @@ -0,0 +1,89 @@ +/* + * 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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link CreateAnnotationSpecSet}. + */ +@RunWith(JUnit4.class) +@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() { + 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/beta/cloud-client/src/test/java/com/example/datalabeling/CreateDatasetIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateDatasetIT.java new file mode 100644 index 00000000000..b8b57a54394 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateDatasetIT.java @@ -0,0 +1,89 @@ +/* + * 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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link CreateDataset}. + */ +@RunWith(JUnit4.class) +@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() { + 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/beta/cloud-client/src/test/java/com/example/datalabeling/CreateInstructionIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateInstructionIT.java new file mode 100644 index 00000000000..d80230d4c83 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateInstructionIT.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; + +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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link CreateInstruction}. + */ +@RunWith(JUnit4.class) +@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() { + 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/beta/cloud-client/src/test/java/com/example/datalabeling/ImportDataIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/ImportDataIT.java new file mode 100644 index 00000000000..bf7fd2396b2 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/ImportDataIT.java @@ -0,0 +1,112 @@ +/* + * 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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link ImportData}. + */ +@RunWith(JUnit4.class) +@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() { + 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() { + ImportData.importData(dataset.getName(), GCS_SOURCE_URI); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString("Imported items: 3")); + } +} diff --git a/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelImageIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelImageIT.java new file mode 100644 index 00000000000..718aaa32390 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelImageIT.java @@ -0,0 +1,134 @@ +/* + * 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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link LabelImage}. + */ +@RunWith(JUnit4.class) +@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() { + // Start the labeling task + LabelImage.labelImage(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +} diff --git a/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelTextIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelTextIT.java new file mode 100644 index 00000000000..f6379ed1e04 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelTextIT.java @@ -0,0 +1,152 @@ +/* + * 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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link LabelText}. */ +@RunWith(JUnit4.class) +@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() { + // Start the labeling task + LabelText.labelText(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +} diff --git a/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelVideoIT.java b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelVideoIT.java new file mode 100644 index 00000000000..409df270bbf --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelVideoIT.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.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Integration (system) tests for {@link LabelVideo}. */ +@RunWith(JUnit4.class) +@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() { + // Start the labeling task + LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); + } +} diff --git a/pom.xml b/pom.xml index dc3dc38fd9b..d3b48f1e88a 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,8 @@ dataflow/spanner-io dataflow/transforms + datalabeling/beta/cloud-client + datastore datastore/cloud-client