Annota un video utilizzando la riga di comando

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

Puoi seguire i passaggi in questa pagina o provare questa guida rapida come lab Formazione Google Cloud.

Provalo 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 gratuiti 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.

    Go to project selector

  3. Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.

  4. Attiva l'API richiesta.

    Abilita l'API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. Imposta la variabile di ambiente GOOGLE_APPLICATION_CREDENTIALS sul percorso del file JSON che contiene le tue credenziali. Questa variabile si applica solo alla sessione di shell attuale. Pertanto, se apri una nuova sessione, imposta di nuovo la variabile.

  8. Installa Google Cloud CLI.
  9. Per initialize gcloud CLI, esegui questo comando:

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

    Go to project selector

  11. Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.

  12. Attiva l'API richiesta.

    Abilita l'API

  13. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  14. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  15. Imposta la variabile di ambiente GOOGLE_APPLICATION_CREDENTIALS sul percorso del file JSON che contiene le tue credenziali. Questa variabile si applica solo alla sessione di shell attuale. Pertanto, se apri una nuova sessione, imposta di nuovo la variabile.

  16. Installa Google Cloud CLI.
  17. Per initialize gcloud CLI, esegui questo comando:

    gcloud init

Effettua una richiesta di annotazione video

gcloud

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

gcloud ml video detect-labels gs://YOUR_BUCKET/YOUR_OBJECT

Riga di comando

  1. Utilizza curl per effettuare una richiesta POST al metodo videos:annotate, utilizzando il comando gcloud auth application-default print-access-token per incollare un token di accesso nel tuo account di servizio:

    curl -X POST \
      -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
      -H "Content-Type: application/json; charset=utf-8" \
      --data '{"inputUri":"gs://YOUR_BUCKET/YOUR_OBJECT","features":["LABEL_DETECTION"]}'\
      "https://videointelligence.googleapis.com/v1/videos:annotate"
    
  2. 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"
    }
    
  3. Puoi richiedere informazioni sull'operazione chiamando l'endpoint v1.operations, 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
    
  4. Vedrai le informazioni relative all'operazione. Se l'operazione è stata completata, viene incluso un campo done e 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": "/YOUR_BUCKET/YOUR_OBJECT",
            "progressPercent": 100,
            "startTime": "2020-04-01T22:13:17.978847Z",
            "updateTime": "2020-04-01T22:13:29.576004Z"
          }
        ]
      },
      "done": true,
     ...
    }
    

Dopo aver concesso alla richiesta un po' di tempo (in genere circa un minuto), la stessa richiesta restituisce i risultati dell'annotazione:

{
  "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "YOUR_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": "/YOUR_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 addebiti Google Cloud non necessari, utilizza la console Google Cloud per eliminare il progetto se non ti serve.

Passaggi successivi