라벨 이미지

이미지 라벨링 작업을 시작합니다.

이 코드 샘플이 포함된 문서 페이지

코드 샘플

자바

이 샘플을 사용해 보기 전에 데이터 라벨링 서비스 빠른 시작: 클라이언트 라이브러리 사용의 자바 설정 안내를 따르세요. 자세한 내용은 데이터 라벨링 서비스 자바 API 참조 문서를 참조하세요.

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.datalabeling.v1beta1.AnnotatedDataset;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient;
import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings;
import com.google.cloud.datalabeling.v1beta1.HumanAnnotationConfig;
import com.google.cloud.datalabeling.v1beta1.ImageClassificationConfig;
import com.google.cloud.datalabeling.v1beta1.LabelImageRequest;
import com.google.cloud.datalabeling.v1beta1.LabelImageRequest.Feature;
import com.google.cloud.datalabeling.v1beta1.LabelOperationMetadata;
import com.google.cloud.datalabeling.v1beta1.StringAggregationType;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class LabelImage {

  // Start an Image Labeling Task
  static void labelImage(
      String formattedInstructionName,
      String formattedAnnotationSpecSetName,
      String formattedDatasetName)
      throws IOException {
    // String formattedInstructionName = DataLabelingServiceClient.formatInstructionName(
    //      "YOUR_PROJECT_ID", "YOUR_INSTRUCTION_UUID");
    // String formattedAnnotationSpecSetName =
    //     DataLabelingServiceClient.formatAnnotationSpecSetName(
    //         "YOUR_PROJECT_ID", "YOUR_ANNOTATION_SPEC_SET_UUID");
    // String formattedDatasetName = DataLabelingServiceClient.formatDatasetName(
    //      "YOUR_PROJECT_ID", "YOUR_DATASET_UUID");

    DataLabelingServiceSettings settings =
        DataLabelingServiceSettings.newBuilder()
            .build();
    try (DataLabelingServiceClient dataLabelingServiceClient =
        DataLabelingServiceClient.create(settings)) {
      HumanAnnotationConfig humanAnnotationConfig =
          HumanAnnotationConfig.newBuilder()
              .setAnnotatedDatasetDisplayName("annotated_displayname")
              .setAnnotatedDatasetDescription("annotated_description")
              .setInstruction(formattedInstructionName)
              .build();

      ImageClassificationConfig imageClassificationConfig =
          ImageClassificationConfig.newBuilder()
              .setAllowMultiLabel(true)
              .setAnswerAggregationType(StringAggregationType.MAJORITY_VOTE)
              .setAnnotationSpecSet(formattedAnnotationSpecSetName)
              .build();

      LabelImageRequest labelImageRequest =
          LabelImageRequest.newBuilder()
              .setParent(formattedDatasetName)
              .setBasicConfig(humanAnnotationConfig)
              .setImageClassificationConfig(imageClassificationConfig)
              .setFeature(Feature.CLASSIFICATION)
              .build();

      OperationFuture<AnnotatedDataset, LabelOperationMetadata> operation =
          dataLabelingServiceClient.labelImageAsync(labelImageRequest);

      // You'll want to save this for later to retrieve your completed operation.
      System.out.format("Operation Name: %s\n", operation.getName());

      // Cancel the operation to avoid charges when testing.
      dataLabelingServiceClient.getOperationsClient().cancelOperation(operation.getName());

    } catch (IOException | InterruptedException | ExecutionException e) {
      e.printStackTrace();
    }
  }
}

Python

이 샘플을 사용해 보기 전에 데이터 라벨링 서비스 빠른 시작: 클라이언트 라이브러리 사용의 Python 설정 안내를 따르세요. 자세한 내용은 데이터 라벨링 서비스 Python API 참조 문서를 확인하세요.

def label_image(
    dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name
):
    """Labels an image dataset."""
    from google.cloud import datalabeling_v1beta1 as datalabeling

    client = datalabeling.DataLabelingServiceClient()

    basic_config = datalabeling.HumanAnnotationConfig(
        instruction=instruction_resource_name,
        annotated_dataset_display_name="YOUR_ANNOTATED_DATASET_DISPLAY_NAME",
        label_group="YOUR_LABEL_GROUP",
        replica_count=1,
    )

    feature = datalabeling.LabelImageRequest.Feature.CLASSIFICATION

    # annotation_spec_set_resource_name needs to be created beforehand.
    # See the examples in the following:
    # https://cloud.google.com/ai-platform/data-labeling/docs/label-sets
    config = datalabeling.ImageClassificationConfig(
        annotation_spec_set=annotation_spec_set_resource_name,
        allow_multi_label=False,
        answer_aggregation_type=datalabeling.StringAggregationType.MAJORITY_VOTE,
    )

    response = client.label_image(
        request={
            "parent": dataset_resource_name,
            "basic_config": basic_config,
            "feature": feature,
            "image_classification_config": config,
        }
    )

    print("Label_image operation name: {}".format(response.operation.name))
    return response