Membuat kumpulan spesifikasi anotasi

Membuat kumpulan spesifikasi anotasi

Jelajahi lebih lanjut

Untuk dokumentasi mendetail yang menyertakan contoh kode ini, lihat artikel berikut:

Contoh kode

Java

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Layanan Pelabelan Data, lihat library klien Layanan Pelabelan Data. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Java Layanan Pelabelan Data.

Untuk mengautentikasi ke Data Labeling Service, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import com.google.cloud.datalabeling.v1beta1.AnnotationSpec;
import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet;
import com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings;
import com.google.cloud.datalabeling.v1beta1.ProjectName;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

class CreateAnnotationSpecSet {

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

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

    DataLabelingServiceSettings settings =
        DataLabelingServiceSettings.newBuilder()
            .build();
    try (DataLabelingServiceClient dataLabelingServiceClient =
        DataLabelingServiceClient.create(settings)) {
      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();
    }
  }
}

Python

Untuk mempelajari cara menginstal dan menggunakan library klien untuk Layanan Pelabelan Data, lihat library klien Layanan Pelabelan Data. Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Python Layanan Pelabelan Data.

Untuk mengautentikasi ke Data Labeling Service, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

def create_annotation_spec_set(project_id):
    """Creates a data labeling annotation spec set for the given
    Google Cloud project.
    """
    from google.cloud import datalabeling_v1beta1 as datalabeling

    client = datalabeling.DataLabelingServiceClient()

    project_path = f"projects/{project_id}"

    annotation_spec_1 = datalabeling.AnnotationSpec(
        display_name="label_1", description="label_description_1"
    )

    annotation_spec_2 = datalabeling.AnnotationSpec(
        display_name="label_2", description="label_description_2"
    )

    annotation_spec_set = datalabeling.AnnotationSpecSet(
        display_name="YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME",
        description="YOUR_DESCRIPTION",
        annotation_specs=[annotation_spec_1, annotation_spec_2],
    )

    response = client.create_annotation_spec_set(
        request={"parent": project_path, "annotation_spec_set": annotation_spec_set}
    )

    # The format of the resource name:
    # project_id/{project_id}/annotationSpecSets/{annotationSpecSets_id}
    print(f"The annotation_spec_set resource name: {response.name}")
    print(f"Display name: {response.display_name}")
    print(f"Description: {response.description}")
    print("Annotation specs:")
    for annotation_spec in response.annotation_specs:
        print(f"\tDisplay name: {annotation_spec.display_name}")
        print(f"\tDescription: {annotation_spec.description}\n")

    return response

Langkah selanjutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.