Annotare un video utilizzando la riga di comando

Questa pagina mostra come effettuare una richiesta all'API Video Intelligence con curl per i progetti di tua proprietà.

Puoi seguire i passaggi riportati in questa pagina o provare questa guida rapida come lab di formazioneGoogle Cloud .

Provare con un lab

Prima di iniziare

  1. Accedi al tuo account Google Cloud . Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei carichi di lavoro.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Video Intelligence API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Install the Google Cloud CLI.

  7. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Video Intelligence API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Install the Google Cloud CLI.

  14. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  15. To initialize the gcloud CLI, run the following command:

    gcloud init

Ruoli obbligatori

Per ottenere le autorizzazioni necessarie per annotare un video, chiedi all'amministratore di concederti i seguenti ruoli IAM nel progetto:

Per saperne di più sulla concessione dei ruoli, consulta Gestisci l'accesso a progetti, cartelle e organizzazioni.

Potresti anche riuscire a ottenere le autorizzazioni richieste tramite i ruoli personalizzati o altri ruoli predefiniti.

Crea un bucket

Nel tuo ambiente di sviluppo, utilizza il comando gcloud storage buckets create per creare un bucket.

Prima di eseguire il comando, sostituisci i seguenti valori:

  • BUCKET_LOCATION: la posizione geografica in cui vuoi archiviare i dati nel bucket. Ad esempio, US.
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION

Se la richiesta ha esito positivo, il comando restituisce il seguente messaggio:

Creating gs://videointelligence-quickstart-bucket/...

Carica un video nel bucket

Nel tuo ambiente di sviluppo, utilizza il comando gcloud storage cp per caricare un video nel bucket che hai creato.

Prima di eseguire il comando, sostituisci i seguenti valori:

  • OBJECT_LOCATION: il percorso locale dell'oggetto. Ad esempio, Desktop/dog.mp4.
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket

Se l'operazione ha esito positivo, la risposta è simile al seguente esempio:

Completed files 1/1 | 164.3kiB/164.3kiB

Annotare il video

gcloud

Utilizza gcloud CLI per chiamare il comando detect-labels sul percorso del video da analizzare.

Prima di eseguire il comando, sostituisci i seguenti valori:

  • YOUR_OBJECT: il nome del file del video che hai caricato. Ad esempio, dog.mp4.
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT

Riga di comando

Utilizza curl per inviare una richiesta POST al metodo videos:annotate.

Prima di eseguire il comando, sostituisci i seguenti valori:

  • YOUR_OBJECT: il nome del file del video che hai caricato. Ad esempio, dog.mp4.
curl -X POST \
  -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
  -H "Content-Type: application/json; charset=utf-8" \
  --data '{"inputUri":"gs://videointelligence-quickstart-bucket/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\
  "https://videointelligence.googleapis.com/v1/videos:annotate"

L'API Video Intelligence crea un'operazione per elaborare la richiesta. La risposta include il nome dell'operazione:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID"
}

Puoi richiedere informazioni sull'operazione chiamando l'endpoint v1.operations e sostituendo OPERATION_NAME nell'esempio seguente con il nome restituito nel passaggio precedente:

  curl -X GET \
    -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
    https://videointelligence.googleapis.com/v1/OPERATION_NAME

Questo comando restituisce informazioni relative all'operazione. Se l'operazione è stata completata, verrà incluso un campo done, che verrà impostato su true:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
  "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  ...
}

Dopo circa un minuto, la richiesta restituisce i risultati dell'annotazione. I risultati dovrebbero essere simili ai seguenti:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "videointelligence-quickstart-bucket/YOUR_OBJECT",
        "progressPercent": 100,
        "startTime": "2020-04-01T22:13:17.978847Z",
        "updateTime": "2020-04-01T22:13:29.576004Z"
      }
    ]
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoResponse",
    "annotationResults": [
      {
        "inputUri": "/videointelligence-quickstart-bucket/YOUR_OBJECT",
        "segmentLabelAnnotations": [
          {
            "entity": {
              "entityId": "/m/07bsy",
              "description": "transport",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.81231534
              }
            ]
          },
         {
          "entity": {
              "entityId": "/m/01n32",
              "description": "city",
              "languageCode": "en-US"
            },
            "categoryEntities": [
              {
                "entityId": "/m/043rvww",
                "description": "geographical feature",
                "languageCode": "en-US"
              }
            ],
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.3942462
              }
            ]
          },
          ...
          {
            "entity": {
              "entityId": "/m/06gfj",
              "description": "road",
              "languageCode": "en-US"
            },
            "segments": [
              {
                "segment": {
                  "startTimeOffset": "0s",
                  "endTimeOffset": "38.757872s"
                },
                "confidence": 0.86698604
              }
            ]
          }
        ]
      }
    ]
  }
}
   

Complimenti! Hai inviato la tua prima richiesta all'API Video Intelligence.

Esegui la pulizia

Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questa pagina, esegui una delle seguenti operazioni:

  • Se vuoi continuare a utilizzare il progetto che hai creato, utilizza il comando gcloud storage rm con il flag --recursive per eliminare il bucket e tutto ciò che contiene al suo interno:

    gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
  • Se non hai bisogno del progetto che hai creato per questa guida rapida, eliminalo.

    1. Nella console Google Cloud , vai alla pagina Gestisci risorse.

      Vai a Gestisci risorse

    2. Nell'elenco dei progetti, seleziona il progetto che vuoi eliminare, quindi fai clic su Elimina.
    3. Nella finestra di dialogo, digita l'ID progetto, quindi fai clic su Chiudi per eliminare il progetto.

Passaggi successivi