명령줄을 사용하여 동영상에 주석 추가

이 페이지에서는 소유한 프로젝트에서 curl을 사용해서 Video Intelligence API 요청을 수행하는 방법을 보여줍니다.

이 페이지의 단계를 따르거나 교육 실습으로 이 빠른 시작을 사용할 수 있습니다.Google Cloud

실습에 사용해보기

시작하기 전에

  1. 계정에 로그인합니다. Google Cloud 를 처음 사용하는 경우 Google Cloud, 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  6. Google Cloud CLI를 설치합니다.

  7. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  8. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    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.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  13. Google Cloud CLI를 설치합니다.

  14. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  15. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init

필요한 역할

동영상에 주석을 추가하는 데 필요한 권한을 얻으려면 관리자에게 프로젝트에 대해 다음 IAM 역할을 부여해 달라고 요청하세요.

역할 부여에 대한 자세한 내용은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참조하세요.

커스텀 역할이나 다른 사전 정의된 역할을 통해 필요한 권한을 얻을 수도 있습니다.

버킷 만들기

개발 환경에서 gcloud storage buckets create 명령어를 사용하여 버킷을 만듭니다.

명령어를 실행하기 전에 다음 값을 바꿉니다.

  • BUCKET_LOCATION: 버킷에 데이터를 저장할 지리적 위치입니다. 예를 들면 US입니다.
gcloud storage buckets create gs://videointelligence-quickstart-bucket --location=BUCKET_LOCATION

요청이 성공하면 명령어에서 다음과 같은 메시지를 반환합니다.

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

버킷에 동영상 업로드

개발 환경에서 gcloud storage cp 명령어를 사용하여 만든 버킷에 동영상을 업로드합니다.

명령어를 실행하기 전에 다음 값을 바꿉니다.

  • OBJECT_LOCATION: 객체의 로컬 경로입니다. 예를 들면 Desktop/dog.mp4입니다.
gcloud storage cp OBJECT_LOCATION gs://videointelligence-quickstart-bucket

성공하면 응답은 다음 예시와 같습니다.

Completed files 1/1 | 164.3kiB/164.3kiB

동영상에 주석 추가

gcloud

gcloud CLI를 사용하여 분석할 동영상 경로에서 detect-labels 명령어를 호출합니다.

명령어를 실행하기 전에 다음 값을 바꿉니다.

  • YOUR_OBJECT: 업로드한 동영상의 파일 이름입니다. 예를 들면 dog.mp4입니다.
gcloud ml video detect-labels gs://videointelligence-quickstart-bucket/YOUR_OBJECT

명령줄

curl을 사용하여 videos:annotate 메서드에 POST 요청을 수행합니다.

명령어를 실행하기 전에 다음 값을 바꿉니다.

  • YOUR_OBJECT: 업로드한 동영상의 파일 이름입니다. 예를 들면 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"

Video Intelligence API가 요청을 처리하는 작업 을 만듭니다. 응답에 작업 이름이 포함됩니다.

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

v1.operations 엔드포인트를 호출하고, 아래 예시의 OPERATION_NAME을 이전 단계에서 반환된 이름으로 바꿔서 작업에 대한 정보를 요청할 수 있습니다.

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

이 명령어는 작업과 관련된 정보를 반환합니다. 작업이 완료되면 done 필드가 포함되고 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,
  ...
}

약 1분 후 요청이 주석 결과를 반환합니다. 결과는 다음과 비슷하게 표시됩니다.

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

수고하셨습니다. 첫 번째 요청을 Video Intelligence API에 보냈습니다.

삭제

이 페이지에서 사용된 리소스에 대한 비용이 Google Cloud 계정에 청구되지 않게 하려면 다음 중 하나를 수행합니다.

  • 만든 프로젝트를 계속 사용하려면 --recursive 플래그와 함께 gcloud storage rm 명령어를 사용하여 버킷과 버킷 내부의 모든 항목을 삭제합니다.

    gcloud storage rm gs://videointelligence-quickstart-bucket --recursive
  • 이 빠른 시작을 위해 만든 프로젝트가 필요하지 않으면 프로젝트를 삭제합니다.

    1. 콘솔에서 리소스 관리 페이지로 이동합니다. Google Cloud

      리소스 관리로 이동

    2. 프로젝트 목록에서 삭제할 프로젝트를 선택하고 삭제를 클릭합니다.
    3. 대화상자에서 프로젝트 ID를 입력한 후 종료 를 클릭하여 프로젝트를 삭제합니다.

다음 단계