Skip to content

Datalabeling beta samples #1365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions datalabeling/beta/cloud-client/README.md
Original file line number Diff line number Diff line change
@@ -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
```
56 changes: 56 additions & 0 deletions datalabeling/beta/cloud-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.datalabeling</groupId>
<artifactId>datalabeling-samples</artifactId>
<packaging>jar</packaging>

<!--
The parent pom defines common style checks and testing strategies for our samples.
Removing or replacing it should not affect the execution of the samples in anyway.
-->
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.10</version>
</parent>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datalabeling</artifactId>
<version>0.86.0-alpha</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.42</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datalabeling;

// [START datalabeling_create_annotation_spec_set_beta]
import com.google.cloud.datalabeling.v1beta1.AnnotationSpec;
import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet;
import com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.ProjectName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class CreateAnnotationSpecSet {

// Create an annotation spec set.
static void createAnnotationSpecSet(String projectId) {
// String projectId = "YOUR_PROJECT_ID";

Map<String, String> annotationLabels = new HashMap<>();
annotationLabels.put("label_1", "label_1_description");
annotationLabels.put("label_2", "label_2_description");

try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {

ProjectName projectName = ProjectName.of(projectId);

List<AnnotationSpec> annotationSpecs = new ArrayList<>();
for (Entry<String, String> entry : annotationLabels.entrySet()) {
AnnotationSpec annotationSpec = AnnotationSpec.newBuilder()
.setDisplayName(entry.getKey())
.setDescription(entry.getValue())
.build();
annotationSpecs.add(annotationSpec);
}

AnnotationSpecSet annotationSpecSet = AnnotationSpecSet.newBuilder()
.setDisplayName("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")
.setDescription("YOUR_DESCRIPTION")
.addAllAnnotationSpecs(annotationSpecs)
.build();

CreateAnnotationSpecSetRequest request = CreateAnnotationSpecSetRequest.newBuilder()
.setAnnotationSpecSet(annotationSpecSet)
.setParent(projectName.toString())
.build();

AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request);

System.out.format("Name: %s\n", result.getName());
System.out.format("DisplayName: %s\n", result.getDisplayName());
System.out.format("Description: %s\n", result.getDescription());
System.out.format("Annotation Count: %d\n", result.getAnnotationSpecsCount());

for (AnnotationSpec annotationSpec : result.getAnnotationSpecsList()) {
System.out.format("\tDisplayName: %s\n", annotationSpec.getDisplayName());
System.out.format("\tDescription: %s\n\n", annotationSpec.getDescription());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// [END datalabeling_create_annotation_spec_set_beta]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datalabeling;

// [START datalabeling_create_dataset_beta]
import com.google.cloud.datalabeling.v1beta1.CreateDatasetRequest;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.Dataset;
import com.google.cloud.datalabeling.v1beta1.ProjectName;
import java.io.IOException;

class CreateDataset {

// Create a dataset that is initially empty.
static void createDataset(String projectId, String datasetName) {
// String projectId = "YOUR_PROJECT_ID";
// String datasetName = "YOUR_DATASET_DISPLAY_NAME";

try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {
ProjectName projectName = ProjectName.of(projectId);

Dataset dataset = Dataset.newBuilder()
.setDisplayName(datasetName)
.setDescription("YOUR_DESCRIPTION")
.build();

CreateDatasetRequest createDatasetRequest = CreateDatasetRequest.newBuilder()
.setParent(projectName.toString())
.setDataset(dataset)
.build();

Dataset createdDataset = dataLabelingServiceClient.createDataset(createDatasetRequest);

System.out.format("Name: %s\n", createdDataset.getName());
System.out.format("DisplayName: %s\n", createdDataset.getDisplayName());
System.out.format("Description: %s\n", createdDataset.getDescription());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// [END datalabeling_create_dataset_beta]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.datalabeling;

// [START datalabeling_create_instruction_beta]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.datalabeling.v1beta1.CreateInstructionMetadata;
import com.google.cloud.datalabeling.v1beta1.CreateInstructionRequest;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.DataType;
import com.google.cloud.datalabeling.v1beta1.Instruction;
import com.google.cloud.datalabeling.v1beta1.PdfInstruction;
import com.google.cloud.datalabeling.v1beta1.ProjectName;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class CreateInstruction {

// Create a instruction for a dataset.
static void createInstruction(String projectId, String pdfUri) {
// String projectId = "YOUR_PROJECT_ID";
// String pdfUri = "gs://YOUR_BUCKET_ID/path_to_pdf_or_csv";

try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {

ProjectName projectName = ProjectName.of(projectId);

// There are two types of instructions: CSV (CsvInstruction) or PDF (PdfInstruction)
PdfInstruction pdfInstruction = PdfInstruction.newBuilder()
.setGcsFileUri(pdfUri)
.build();

Instruction instruction = Instruction.newBuilder()
.setDisplayName("YOUR_INSTRUCTION_DISPLAY_NAME")
.setDescription("YOUR_DESCRIPTION")
.setDataType(DataType.IMAGE) // DataTypes: AUDIO, IMAGE, VIDEO, TEXT
.setPdfInstruction(pdfInstruction) // .setCsvInstruction() or .setPdfInstruction()
.build();

CreateInstructionRequest createInstructionRequest = CreateInstructionRequest.newBuilder()
.setInstruction(instruction)
.setParent(projectName.toString())
.build();

OperationFuture<Instruction, CreateInstructionMetadata> 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]
Original file line number Diff line number Diff line change
@@ -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<ExportDataOperationResponse, ExportDataOperationMetadata> operation =
dataLabelingServiceClient.exportDataAsync(exportDataRequest);

ExportDataOperationResponse response = operation.get();

System.out.format("Exported item count: %d\n", response.getExportCount());
LabelStats labelStats = response.getLabelStats();
Set<Entry<String, Long>> entries = labelStats.getExampleCountMap().entrySet();
for (Entry<String, Long> entry : entries) {
System.out.format("\tLabel: %s\n", entry.getKey());
System.out.format("\tCount: %d\n\n", entry.getValue());
}
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END datalabeling_export_data_beta]
Loading