Anote um vídeo através da linha de comandos

Esta página mostra como fazer um pedido da Video Intelligence API com curl para projetos que lhe pertencem.

Pode seguir os passos nesta página ou experimentar este início rápido como um laboratório de formação do Google Cloud.

Experimente com um laboratório

Antes de começar

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  4. Enable the required API.

    Enable the 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. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  8. Install the Google Cloud CLI.

  9. Se estiver a usar um fornecedor de identidade (IdP) externo, primeiro, tem de iniciar sessão na CLI gcloud com a sua identidade federada.

  10. Para inicializar a CLI gcloud, execute o seguinte comando:

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

    Go to project selector

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

  13. Enable the required API.

    Enable the API

  14. 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.

  15. 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.
  16. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

  17. Install the Google Cloud CLI.

  18. Se estiver a usar um fornecedor de identidade (IdP) externo, primeiro, tem de iniciar sessão na CLI gcloud com a sua identidade federada.

  19. Para inicializar a CLI gcloud, execute o seguinte comando:

    gcloud init
  20. Faça um pedido de anotação de vídeo

    gcloud

    Use a CLI gcloud para chamar o comando detect-labels no caminho do vídeo a ser analisado.

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

    Linha de comandos

    1. Use curl para fazer um pedido POST ao método videos:annotate, usando o comando gcloud auth application-default print-access-token para colar uma chave de acesso na sua conta de serviço:

      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. A Video Intelligence API cria uma operação para processar o seu pedido. A resposta inclui o nome da operação:

      {
        "name": "projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID"
      }
    3. Pode pedir informações sobre a operação chamando o ponto final v1.operations e substituindo OPERATION_NAME no exemplo abaixo pelo nome devolvido no passo anterior:

       curl -X GET \
         -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         https://videointelligence.googleapis.com/v1/OPERATION_NAME
    4. São apresentadas informações relacionadas com a sua operação. Se a operação tiver sido concluída, é incluído um campo done e definido como 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,
       ...
      }

    Após dar algum tempo ao pedido (normalmente, cerca de um minuto), o mesmo pedido devolve resultados de anotação:

    {
      "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
                  }
                ]
              }
            ]
          }
        ]
      }
    }
       

    Parabéns! Enviou o seu primeiro pedido à API Video Intelligence.

    Limpar

    Para evitar Google Cloud cobranças desnecessárias, use o Google Cloud console para eliminar o seu projeto se não precisar dele.

    O que se segue?