From a0a3c4179bddf3b5b2abc9564dfce77bf250fe3b Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Wed, 3 Apr 2019 14:09:18 -0700 Subject: [PATCH 1/8] Datalabeling beta samples --- datalabeling/beta/cloud-client/README.md | 41 +++++ datalabeling/beta/cloud-client/pom.xml | 56 +++++++ .../datalabeling/CreateAnnotationSpecSet.java | 81 +++++++++ .../example/datalabeling/CreateDataset.java | 54 ++++++ .../datalabeling/CreateInstruction.java | 72 ++++++++ .../com/example/datalabeling/ExportData.java | 78 +++++++++ .../com/example/datalabeling/ImportData.java | 66 ++++++++ .../com/example/datalabeling/LabelImage.java | 81 +++++++++ .../com/example/datalabeling/LabelText.java | 84 ++++++++++ .../com/example/datalabeling/LabelVideo.java | 83 +++++++++ .../CreateAnnotationSpecSetIT.java | 87 ++++++++++ .../example/datalabeling/CreateDatasetIT.java | 86 ++++++++++ .../datalabeling/CreateInstructionIT.java | 90 ++++++++++ .../example/datalabeling/ImportDataIT.java | 108 ++++++++++++ .../example/datalabeling/LabelImageIT.java | 132 +++++++++++++++ .../com/example/datalabeling/LabelTextIT.java | 158 ++++++++++++++++++ .../example/datalabeling/LabelVideoIT.java | 132 +++++++++++++++ pom.xml | 2 + 18 files changed, 1491 insertions(+) create mode 100644 datalabeling/beta/cloud-client/README.md create mode 100644 datalabeling/beta/cloud-client/pom.xml create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateDataset.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateInstruction.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ExportData.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/ImportData.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelImage.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelText.java create mode 100644 datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelVideo.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateDatasetIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateInstructionIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/ImportDataIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelImageIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelTextIT.java create mode 100644 datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelVideoIT.java 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..c1f1325d5a6 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateAnnotationSpecSet.java @@ -0,0 +1,81 @@ +/* + * 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 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()) { + + String formattedProjectName = DataLabelingServiceClient.formatProjectName(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(formattedProjectName) + .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..5d7f765d8ea --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateDataset.java @@ -0,0 +1,54 @@ +/* + * 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 java.io.IOException; + +class CreateDataset { + + // Create a dataset that is initially empty. + static void createDataset(String projectId) { + // String projectId = "YOUR_PROJECT_ID"; + + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + String formattedProjectName = DataLabelingServiceClient.formatProjectName(projectId); + + Dataset dataset = Dataset.newBuilder() + .setDisplayName("YOUR_DATASET_DISPLAY_NAME") + .setDescription("YOUR_DESCRIPTION") + .build(); + + CreateDatasetRequest createDatasetRequest = CreateDatasetRequest.newBuilder() + .setParent(formattedProjectName) + .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..c9f058231d1 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/CreateInstruction.java @@ -0,0 +1,72 @@ +/* + * 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 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()) { + + String formattedProjectName = DataLabelingServiceClient.formatProjectName(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(formattedProjectName) + .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..b274e10de74 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelImage.java @@ -0,0 +1,81 @@ +/* + * 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()); + + // [END datalabeling_label_image_beta] + // Cancel the operation to avoid charges when testing. + dataLabelingServiceClient.getOperationsClient().cancelOperation(operation.getName()); + // [START datalabeling_label_image_beta] + } 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..739559cec01 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/main/java/com/example/datalabeling/LabelText.java @@ -0,0 +1,84 @@ +/* + * 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..c02ee9e96f9 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/CreateAnnotationSpecSetIT.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; + +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 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()) { + ListAnnotationSpecSetsRequest listRequest = ListAnnotationSpecSetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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..7ab47ad5516 --- /dev/null +++ b/datalabeling/beta/cloud-client/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 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"); + + @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()) { + ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient + .listDatasets(listRequest); + + for (Dataset dataset : response.getPage().iterateAll()) { + if (dataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_NAME")) { + dataLabelingServiceClient.deleteDataset(dataset.getName()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void testCreateDataset() { + CreateDataset.createDataset(PROJECT_ID); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString( + "DisplayName: YOUR_DATASET_DISPLAY_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..0fad95191e9 --- /dev/null +++ b/datalabeling/beta/cloud-client/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 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()) { + ListInstructionsRequest listRequest = ListInstructionsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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..7b49ae02f9e --- /dev/null +++ b/datalabeling/beta/cloud-client/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 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 Dataset dataset; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateDataset.createDataset(PROJECT_ID); + + // Get the Dataset + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient + .listDatasets(listRequest); + + for (Dataset returnedDataset : response.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_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()) { + ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + + ListDatasetsPagedResponse response = dataLabelingServiceClient + .listDatasets(listRequest); + + for (Dataset returnedDataset : response.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_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..fe4af37666f --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelImageIT.java @@ -0,0 +1,132 @@ +/* + * 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 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 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); + + // Get the Dataset + ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient + .listDatasets(datasetsRequest); + for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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 (IOException e) { + e.printStackTrace(); + System.exit(-1); + } + } + + @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..77c16f506c7 --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelTextIT.java @@ -0,0 +1,158 @@ +/* + * 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 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 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); + + // Get the Dataset + ListDatasetsRequest datasetsRequest = + ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + ListDatasetsPagedResponse datasetsResponse = + dataLabelingServiceClient.listDatasets(datasetsRequest); + for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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(); + System.exit(-1); + } + } + + @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..59f919abc0d --- /dev/null +++ b/datalabeling/beta/cloud-client/src/test/java/com/example/datalabeling/LabelVideoIT.java @@ -0,0 +1,132 @@ +/* + * 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 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 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); + + // Get the Dataset + ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder() + .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .build(); + ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient + .listDatasets(datasetsRequest); + for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) { + if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .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 (IOException e) { + e.printStackTrace(); + System.exit(-1); + } + } + + @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 From b48032b07ff04d8809dee2145c7531c0a6600a72 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Wed, 3 Apr 2019 16:01:15 -0700 Subject: [PATCH 2/8] Fixing tests --- .../example/datalabeling/CreateAnnotationSpecSet.java | 5 +++-- .../java/com/example/datalabeling/CreateDataset.java | 5 +++-- .../java/com/example/datalabeling/CreateInstruction.java | 5 +++-- .../main/java/com/example/datalabeling/LabelImage.java | 4 ++-- .../example/datalabeling/CreateAnnotationSpecSetIT.java | 4 +++- .../java/com/example/datalabeling/CreateDatasetIT.java | 4 +++- .../com/example/datalabeling/CreateInstructionIT.java | 4 +++- .../test/java/com/example/datalabeling/ImportDataIT.java | 7 +++++-- .../test/java/com/example/datalabeling/LabelImageIT.java | 8 +++++--- .../test/java/com/example/datalabeling/LabelTextIT.java | 9 +++++---- .../test/java/com/example/datalabeling/LabelVideoIT.java | 9 +++++---- 11 files changed, 40 insertions(+), 24 deletions(-) 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 index c1f1325d5a6..28626466ef8 100644 --- 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 @@ -21,6 +21,7 @@ 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; @@ -40,7 +41,7 @@ static void createAnnotationSpecSet(String projectId) { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { - String formattedProjectName = DataLabelingServiceClient.formatProjectName(projectId); + ProjectName projectName = ProjectName.of(projectId); List annotationSpecs = new ArrayList<>(); for (Entry entry : annotationLabels.entrySet()) { @@ -59,7 +60,7 @@ static void createAnnotationSpecSet(String projectId) { CreateAnnotationSpecSetRequest request = CreateAnnotationSpecSetRequest.newBuilder() .setAnnotationSpecSet(annotationSpecSet) - .setParent(formattedProjectName) + .setParent(projectName.toString()) .build(); AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request); 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 index 5d7f765d8ea..0a8ffa35169 100644 --- 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 @@ -20,6 +20,7 @@ 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 { @@ -29,7 +30,7 @@ static void createDataset(String projectId) { // String projectId = "YOUR_PROJECT_ID"; try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { - String formattedProjectName = DataLabelingServiceClient.formatProjectName(projectId); + ProjectName projectName = ProjectName.of(projectId); Dataset dataset = Dataset.newBuilder() .setDisplayName("YOUR_DATASET_DISPLAY_NAME") @@ -37,7 +38,7 @@ static void createDataset(String projectId) { .build(); CreateDatasetRequest createDatasetRequest = CreateDatasetRequest.newBuilder() - .setParent(formattedProjectName) + .setParent(projectName.toString()) .setDataset(dataset) .build(); 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 index c9f058231d1..ab56b3eeed0 100644 --- 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 @@ -24,6 +24,7 @@ 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; @@ -36,7 +37,7 @@ static void createInstruction(String projectId, String pdfUri) { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { - String formattedProjectName = DataLabelingServiceClient.formatProjectName(projectId); + ProjectName projectName = ProjectName.of(projectId); // There are two types of instructions: CSV (CsvInstruction) or PDF (PdfInstruction) PdfInstruction pdfInstruction = PdfInstruction.newBuilder() @@ -52,7 +53,7 @@ static void createInstruction(String projectId, String pdfUri) { CreateInstructionRequest createInstructionRequest = CreateInstructionRequest.newBuilder() .setInstruction(instruction) - .setParent(formattedProjectName) + .setParent(projectName.toString()) .build(); OperationFuture operation = 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 index b274e10de74..91d2cfde026 100644 --- 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 @@ -69,10 +69,10 @@ static void labelImage(String formattedInstructionName, String formattedAnnotati // You'll want to save this for later to retrieve your completed operation. System.out.format("Operation Name: %s\n", operation.getName()); - // [END datalabeling_label_image_beta] + // Cancel the operation to avoid charges when testing. dataLabelingServiceClient.getOperationsClient().cancelOperation(operation.getName()); - // [START datalabeling_label_image_beta] + } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); } 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 index c02ee9e96f9..3ee1103d960 100644 --- 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 @@ -22,6 +22,7 @@ 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; @@ -56,8 +57,9 @@ public void tearDown() { // Delete the Annotation Spec Sheet try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); ListAnnotationSpecSetsRequest listRequest = ListAnnotationSpecSetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListAnnotationSpecSetsPagedResponse response = dataLabelingServiceClient 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 index 7ab47ad5516..1a5cedd0659 100644 --- 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 @@ -22,6 +22,7 @@ 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; @@ -56,8 +57,9 @@ public void tearDown() { // Delete the Dataset try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse response = dataLabelingServiceClient 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 index 0fad95191e9..d80230d4c83 100644 --- 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 @@ -22,6 +22,7 @@ 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; @@ -58,8 +59,9 @@ public void tearDown() { // Delete the Instruction try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); ListInstructionsRequest listRequest = ListInstructionsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListInstructionsPagedResponse response = dataLabelingServiceClient 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 index 7b49ae02f9e..4882fde3edf 100644 --- 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 @@ -22,6 +22,7 @@ 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; @@ -56,8 +57,9 @@ public void setUp() { // Get the Dataset try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse response = dataLabelingServiceClient @@ -80,8 +82,9 @@ public void tearDown() { // Delete the Dataset try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { + ProjectName projectName = ProjectName.of(PROJECT_ID); ListDatasetsRequest listRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse response = dataLabelingServiceClient 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 index fe4af37666f..26117197247 100644 --- 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 @@ -26,6 +26,7 @@ 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; @@ -59,10 +60,11 @@ public void setUp() { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { // Create the dataset CreateDataset.createDataset(PROJECT_ID); + ProjectName projectName = ProjectName.of(PROJECT_ID); // Get the Dataset ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient .listDatasets(datasetsRequest); @@ -83,7 +85,7 @@ public void setUp() { // Get the instruction ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient .listInstructions(instructionsRequest); @@ -95,7 +97,7 @@ public void setUp() { // Get the annotation spec set ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient .listAnnotationSpecSets(annotationRequest); 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 index 77c16f506c7..9e5c42f23ec 100644 --- 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 @@ -31,6 +31,7 @@ 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; @@ -62,11 +63,11 @@ public void setUp() { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { // Create the dataset CreateDataset.createDataset(PROJECT_ID); - + ProjectName projectName = ProjectName.of(PROJECT_ID); // Get the Dataset ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient.listDatasets(datasetsRequest); @@ -108,7 +109,7 @@ public void setUp() { // Get the instruction ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient.listInstructions(instructionsRequest); @@ -121,7 +122,7 @@ public void setUp() { // Get the annotation spec set ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient.listAnnotationSpecSets(annotationRequest); 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 index 59f919abc0d..1223b5fd18a 100644 --- 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 @@ -26,6 +26,7 @@ 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; @@ -59,10 +60,10 @@ public void setUp() { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { // Create the dataset CreateDataset.createDataset(PROJECT_ID); - + ProjectName projectName = ProjectName.of(PROJECT_ID); // Get the Dataset ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient .listDatasets(datasetsRequest); @@ -83,7 +84,7 @@ public void setUp() { // Get the instruction ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient .listInstructions(instructionsRequest); @@ -95,7 +96,7 @@ public void setUp() { // Get the annotation spec set ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder() - .setParent(DataLabelingServiceClient.formatProjectName(PROJECT_ID)) + .setParent(projectName.toString()) .build(); ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient .listAnnotationSpecSets(annotationRequest); From 03a3d26bbfaea6da7497d4bdefe9a9bfa28f39f7 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 09:37:31 -0700 Subject: [PATCH 3/8] Fixing tests --- .../example/datalabeling/LabelVideoIT.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) 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 index 1223b5fd18a..bc9e8072a2d 100644 --- 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 @@ -21,7 +21,12 @@ 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; @@ -74,7 +79,28 @@ public void setUp() { } // Import the images - ImportData.importData(dataset.getName(), DATASET_GCS_SOURCE_URI); + //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); @@ -105,7 +131,7 @@ public void setUp() { annotationSpecSet = returnedAnnotation; } } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); System.exit(-1); } From 426f8427dee8ad3efbb2ab41dddeb63ecab2ccd4 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 10:21:01 -0700 Subject: [PATCH 4/8] Ignoring tests for now due to time duration. --- .../src/test/java/com/example/datalabeling/LabelTextIT.java | 4 ++-- .../src/test/java/com/example/datalabeling/LabelVideoIT.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 index 9e5c42f23ec..3bd96a78ad3 100644 --- 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 @@ -37,7 +37,7 @@ import java.io.PrintStream; import org.junit.After; import org.junit.Before; -import org.junit.Test; +import org.junit.Ignore; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -151,7 +151,7 @@ public void tearDown() { } } - @Test + @Ignore 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 index bc9e8072a2d..2a1220541c8 100644 --- 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 @@ -37,7 +37,7 @@ import java.io.PrintStream; import org.junit.After; import org.junit.Before; -import org.junit.Test; +import org.junit.Ignore; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -151,7 +151,7 @@ public void tearDown() { } } - @Test + @Ignore public void testLabelVideo() { // Start the labeling task LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); From 1105eb890a516261c0b592cb7d4da521bd53f31a Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 10:51:58 -0700 Subject: [PATCH 5/8] Ignoring tests for now due to time duration. --- .../com/example/datalabeling/LabelTextIT.java | 294 +++++++++--------- .../example/datalabeling/LabelVideoIT.java | 293 ++++++++--------- 2 files changed, 299 insertions(+), 288 deletions(-) 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 index 3bd96a78ad3..2fa06253b64 100644 --- 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 @@ -13,147 +13,153 @@ * 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.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 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); - 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("YOUR_DATASET_DISPLAY_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(); - System.exit(-1); - } - } - - @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(); - } - } - - @Ignore - public void testLabelText() { - // Start the labeling task - LabelText.labelText(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); - } -} +// +// 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 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); +// 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("YOUR_DATASET_DISPLAY_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(); +// System.exit(-1); +// } +// } +// +// @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 index 2a1220541c8..41b1ae06690 100644 --- 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 @@ -13,147 +13,152 @@ * 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.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 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); - 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("YOUR_DATASET_DISPLAY_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(); - System.exit(-1); - } - } - - @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(); - } - } - - @Ignore - public void testLabelVideo() { - // Start the labeling task - LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); - } -} +// +// 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.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 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); +// 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("YOUR_DATASET_DISPLAY_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(); +// System.exit(-1); +// } +// } +// +// @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(); +// } +// } +// +// @Ignore +// public void testLabelVideo() { +// // Start the labeling task +// LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); +// } +// } From 3a6558c68f8e0ce65ce8ffa31a3f03f6cd2bf2d4 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 12:06:20 -0700 Subject: [PATCH 6/8] Ignoring tests for now due to time duration. --- .../example/datalabeling/CreateDataset.java | 5 +- .../example/datalabeling/CreateDatasetIT.java | 7 +- .../example/datalabeling/ImportDataIT.java | 7 +- .../example/datalabeling/LabelImageIT.java | 5 +- .../com/example/datalabeling/LabelTextIT.java | 288 +++++++++-------- .../example/datalabeling/LabelVideoIT.java | 289 +++++++++--------- 6 files changed, 292 insertions(+), 309 deletions(-) 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 index 0a8ffa35169..b8eceb820e1 100644 --- 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 @@ -26,14 +26,15 @@ class CreateDataset { // Create a dataset that is initially empty. - static void createDataset(String projectId) { + 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("YOUR_DATASET_DISPLAY_NAME") + .setDisplayName(datasetName) .setDescription("YOUR_DESCRIPTION") .build(); 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 index 1a5cedd0659..b8b57a54394 100644 --- 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 @@ -43,6 +43,7 @@ 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() { @@ -66,7 +67,7 @@ public void tearDown() { .listDatasets(listRequest); for (Dataset dataset : response.getPage().iterateAll()) { - if (dataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_NAME")) { + if (dataset.getDisplayName().equals(datasetName)) { dataLabelingServiceClient.deleteDataset(dataset.getName()); } } @@ -77,12 +78,12 @@ public void tearDown() { @Test public void testCreateDataset() { - CreateDataset.createDataset(PROJECT_ID); + CreateDataset.createDataset(PROJECT_ID,datasetName); String output = bout.toString(); assertThat(output, CoreMatchers.containsString( - "DisplayName: YOUR_DATASET_DISPLAY_NAME")); + "DisplayName: CREATE_DATASET_NAME")); assertThat(output, CoreMatchers.containsString("Description: YOUR_DESCRIPTION")); } } 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 index 4882fde3edf..bf7fd2396b2 100644 --- 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 @@ -45,6 +45,7 @@ public class ImportDataIT { 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; @@ -53,7 +54,7 @@ public void setUp() { bout = new ByteArrayOutputStream(); System.setOut(new PrintStream(bout)); - CreateDataset.createDataset(PROJECT_ID); + CreateDataset.createDataset(PROJECT_ID,datasetName); // Get the Dataset try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { @@ -66,7 +67,7 @@ public void setUp() { .listDatasets(listRequest); for (Dataset returnedDataset : response.getPage().iterateAll()) { - if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_NAME")) { + if (returnedDataset.getDisplayName().equals("IMPORT_DATASET_NAME")) { dataset = returnedDataset; } } @@ -91,7 +92,7 @@ public void tearDown() { .listDatasets(listRequest); for (Dataset returnedDataset : response.getPage().iterateAll()) { - if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_NAME")) { + if (returnedDataset.getDisplayName().equals("IMPORT_DATASET_NAME")) { dataLabelingServiceClient.deleteDataset(returnedDataset.getName()); } } 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 index 26117197247..850871aaacf 100644 --- 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 @@ -48,6 +48,7 @@ public class LabelImageIT { "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; @@ -59,7 +60,7 @@ public void setUp() { try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { // Create the dataset - CreateDataset.createDataset(PROJECT_ID); + CreateDataset.createDataset(PROJECT_ID,datsetName); ProjectName projectName = ProjectName.of(PROJECT_ID); // Get the Dataset @@ -69,7 +70,7 @@ public void setUp() { ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient .listDatasets(datasetsRequest); for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) { - if (returnedDataset.getDisplayName().equals("YOUR_DATASET_DISPLAY_NAME")) { + if (returnedDataset.getDisplayName().equals("LABEL_IMAGE_DATASET_NAME")) { dataset = returnedDataset; } } 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 index 2fa06253b64..29baa3915ad 100644 --- 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 @@ -13,153 +13,141 @@ * 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 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); -// 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("YOUR_DATASET_DISPLAY_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(); -// System.exit(-1); -// } -// } -// -// @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()); -// } -// } + +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(); + System.exit(-1); + } + } + + @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 index 41b1ae06690..00b57ee467b 100644 --- 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 @@ -13,152 +13,143 @@ * 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.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 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); -// 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("YOUR_DATASET_DISPLAY_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(); -// System.exit(-1); -// } -// } -// -// @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(); -// } -// } -// -// @Ignore -// public void testLabelVideo() { -// // Start the labeling task -// LabelVideo.labelVideo(instruction.getName(), annotationSpecSet.getName(), dataset.getName()); -// } -// } + +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(); + System.exit(-1); + } + } + + @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()); + } +} From 761ed56f1f331e61a6f6abb894e7e4a2ce6e5f78 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 13:02:49 -0700 Subject: [PATCH 7/8] debugging --- .../src/test/java/com/example/datalabeling/LabelImageIT.java | 3 +-- .../src/test/java/com/example/datalabeling/LabelTextIT.java | 1 - .../src/test/java/com/example/datalabeling/LabelVideoIT.java | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) 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 index 850871aaacf..718aaa32390 100644 --- 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 @@ -107,9 +107,8 @@ public void setUp() { annotationSpecSet = returnedAnnotation; } } - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); - System.exit(-1); } } 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 index 29baa3915ad..f6379ed1e04 100644 --- 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 @@ -127,7 +127,6 @@ public void setUp() { } } catch (Exception e) { e.printStackTrace(); - System.exit(-1); } } 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 index 00b57ee467b..409df270bbf 100644 --- 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 @@ -129,7 +129,6 @@ public void setUp() { } } catch (Exception e) { e.printStackTrace(); - System.exit(-1); } } From 10f3938d42e9a2f7f098b83e1af81d035f946400 Mon Sep 17 00:00:00 2001 From: nirupa-kumar Date: Thu, 4 Apr 2019 15:54:48 -0700 Subject: [PATCH 8/8] debugging --- .../com/example/datalabeling/LabelImage.java | 40 +++++++++-------- .../com/example/datalabeling/LabelText.java | 44 ++++++++++--------- 2 files changed, 46 insertions(+), 38 deletions(-) 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 index 91d2cfde026..35f2503d1d8 100644 --- 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 @@ -32,7 +32,9 @@ class LabelImage { // Start an Image Labeling Task - static void labelImage(String formattedInstructionName, String formattedAnnotationSpecSetName, + static void labelImage( + String formattedInstructionName, + String formattedAnnotationSpecSetName, String formattedDatasetName) { // String formattedInstructionName = DataLabelingServiceClient.formatInstructionName( // "YOUR_PROJECT_ID", "YOUR_INSTRUCTION_UUID"); @@ -44,24 +46,27 @@ static void labelImage(String formattedInstructionName, String formattedAnnotati try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { - HumanAnnotationConfig humanAnnotationConfig = HumanAnnotationConfig.newBuilder() - .setAnnotatedDatasetDisplayName("annotated_displayname") - .setAnnotatedDatasetDescription("annotated_description") - .setInstruction(formattedInstructionName) - .build(); + 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(); + 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(); + LabelImageRequest labelImageRequest = + LabelImageRequest.newBuilder() + .setParent(formattedDatasetName) + .setBasicConfig(humanAnnotationConfig) + .setImageClassificationConfig(imageClassificationConfig) + .setFeature(Feature.CLASSIFICATION) + .build(); OperationFuture operation = dataLabelingServiceClient.labelImageAsync(labelImageRequest); @@ -69,7 +74,6 @@ static void labelImage(String formattedInstructionName, String formattedAnnotati // 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()); 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 index 739559cec01..2ff411f0dd8 100644 --- 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 @@ -32,7 +32,9 @@ class LabelText { // Start a Text Labeling Task - static void labelText(String formattedInstructionName, String formattedAnnotationSpecSetName, + static void labelText( + String formattedInstructionName, + String formattedAnnotationSpecSetName, String formattedDatasetName) { // String formattedInstructionName = DataLabelingServiceClient.formatInstructionName( // "YOUR_PROJECT_ID", "YOUR_INSTRUCTION_UUID"); @@ -44,28 +46,30 @@ static void labelText(String formattedInstructionName, String formattedAnnotatio try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { - HumanAnnotationConfig humanAnnotationConfig = HumanAnnotationConfig.newBuilder() - .setAnnotatedDatasetDisplayName("annotated_displayname") - .setAnnotatedDatasetDescription("annotated_description") - .setLanguageCode("en-us") - .setInstruction(formattedInstructionName) - .build(); + HumanAnnotationConfig humanAnnotationConfig = + HumanAnnotationConfig.newBuilder() + .setAnnotatedDatasetDisplayName("annotated_displayname") + .setAnnotatedDatasetDescription("annotated_description") + .setLanguageCode("en-us") + .setInstruction(formattedInstructionName) + .build(); - SentimentConfig sentimentConfig = SentimentConfig.newBuilder() - .setEnableLabelSentimentSelection(false) - .build(); + SentimentConfig sentimentConfig = + SentimentConfig.newBuilder().setEnableLabelSentimentSelection(false).build(); - TextClassificationConfig textClassificationConfig = TextClassificationConfig.newBuilder() - .setAnnotationSpecSet(formattedAnnotationSpecSetName) - .setSentimentConfig(sentimentConfig) - .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(); + LabelTextRequest labelTextRequest = + LabelTextRequest.newBuilder() + .setParent(formattedDatasetName) + .setBasicConfig(humanAnnotationConfig) + .setTextClassificationConfig(textClassificationConfig) + .setFeature(Feature.TEXT_CLASSIFICATION) + .build(); OperationFuture operation = dataLabelingServiceClient.labelTextAsync(labelTextRequest);