Ajouter une étiquette à la vidéo

Démarrez une tâche d'étiquetage de vidéos.

En savoir plus

Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :

Exemple de code

Java

Pour savoir comment installer et utiliser la bibliothèque cliente pour le service d'étiquetage de données, consultez la page Bibliothèques clientes du service d'étiquetage de données. Pour en savoir plus, consultez la documentation de référence de l'API Java du service d'étiquetage de données.

Pour vous authentifier auprès du service d'étiquetage de données, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

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.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)
      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();

      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<AnnotatedDataset, LabelOperationMetadata> 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();
    }
  }
}

Python

Pour savoir comment installer et utiliser la bibliothèque cliente pour le service d'étiquetage de données, consultez la page Bibliothèques clientes du service d'étiquetage de données. Pour en savoir plus, consultez la documentation de référence de l'API Python du service d'étiquetage de données.

Pour vous authentifier auprès du service d'étiquetage de données, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

def label_video(
    dataset_resource_name, instruction_resource_name, annotation_spec_set_resource_name
):
    """Labels a video 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.LabelVideoRequest.Feature.OBJECT_TRACKING

    config = datalabeling.ObjectTrackingConfig(
        annotation_spec_set=annotation_spec_set_resource_name
    )

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

    print(f"Label_video operation name: {response.operation.name}")
    return response

Étapes suivantes

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'exemple de navigateur Google Cloud.