장면 변화 감지

장면 변화 분석은 동영상의 장면 변화를 감지합니다.

이 섹션에서는 동영상의 장면 변화를 분석하는 몇 가지 방법을 보여줍니다.

다음은 Cloud Storage에 있는 파일에서 동영상 장면 변화 분석을 수행하는 예시입니다.

자세한 정보를 알고 싶다면 자세한 Python 가이드를 참조하세요.

REST

동영상 주석 요청 전송

다음은 videos:annotate 메서드에 POST 요청을 보내는 방법을 보여줍니다. 이 예시에서는 Google Cloud CLI를 사용하여 액세스 토큰을 만듭니다. gcloud CLI 설치에 대한 안내는 Video Intelligence API 빠른 시작을 참조하세요.

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • INPUT_URI: 파일 이름을 포함하여 주석을 추가하고자 하는 파일을 포함한 Cloud Storage 버킷입니다. gs://로 시작해야 합니다.
  • PROJECT_NUMBER: Google Cloud 프로젝트의 숫자 식별자

HTTP 메서드 및 URL:

POST https://videointelligence.googleapis.com/v1/videos:annotate

JSON 요청 본문:

{
    "inputUri": "INPUT_URI",
    "features": ["SHOT_CHANGE_DETECTION"]
}

요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

다음과 비슷한 JSON 응답이 표시됩니다.

응답이 성공하면 Video Intelligence API가 작업의 name을 반환합니다. 위 항목은 이러한 응답의 예시를 보여줍니다. 여기서 project-name는 프로젝트의 이름이고 operation-id는 요청에 대해 생성된 장기 실행 작업의 ID입니다.

  • PROJECT_NUMBER: 프로젝트 수입니다.
  • LOCATION_ID: 주석이 있어야 할 클라우드 리전입니다. 지원되는 클라우드 리전은 us-east1, us-west1, europe-west1, asia-east1입니다. 리전을 지정하지 않으면 동영상 파일 위치를 기준으로 리전이 결정됩니다.
  • OPERATION_ID: 작업을 시작할 때 요청으로 생성되고 응답으로 제공된 장기 실행 작업의 ID로 예를 들면 12345....입니다.

주석 결과 가져오기

작업 결과를 검색하려면 다음 예시에 표시된 것처럼 videos:annotate 호출로부터 반환된 작업 이름을 사용하여 GET 요청을 수행합니다.

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • OPERATION_NAME: Video Intelligence API로 반환되는 작업의 이름입니다. 작업 이름은 projects/PROJECT_NUMBER/locations/LOCATION_ID/operations/OPERATION_ID 형식입니다.
  • PROJECT_NUMBER: Google Cloud 프로젝트의 숫자 식별자

HTTP 메서드 및 URL:

GET https://videointelligence.googleapis.com/v1/OPERATION_NAME

요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

다음과 비슷한 JSON 응답이 표시됩니다.

장면 감지 주석은 shotAnnotations 목록으로 반환됩니다. 참고: done 필드는 값이 True일 경우에만 반환됩니다. 작업이 완료되지 않은 경우에는 응답에 이 필드가 포함되지 않습니다.

주석 결과 다운로드

소스에서 대상 버킷으로 주석을 복사합니다(파일 및 객체 복사 참조).

gsutil cp gcs_uri gs://my-bucket

참고: 사용자가 출력 gcs uri를 제공하면 주석이 해당 gcs uri에 저장됩니다.

Go


func shotChangeURI(w io.Writer, file string) error {
	ctx := context.Background()
	client, err := video.NewClient(ctx)
	if err != nil {
		return err
	}
	defer client.Close()

	op, err := client.AnnotateVideo(ctx, &videopb.AnnotateVideoRequest{
		Features: []videopb.Feature{
			videopb.Feature_SHOT_CHANGE_DETECTION,
		},
		InputUri: file,
	})
	if err != nil {
		return err
	}
	resp, err := op.Wait(ctx)
	if err != nil {
		return err
	}

	// A single video was processed. Get the first result.
	result := resp.AnnotationResults[0].ShotAnnotations

	for _, shot := range result {
		start, _ := ptypes.Duration(shot.StartTimeOffset)
		end, _ := ptypes.Duration(shot.EndTimeOffset)

		fmt.Fprintf(w, "Shot: %s to %s\n", start, end)
	}

	return nil
}

Java

Video Intelligence에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

// Instantiate a com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient
try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
  // Provide path to file hosted on GCS as "gs://bucket-name/..."
  AnnotateVideoRequest request =
      AnnotateVideoRequest.newBuilder()
          .setInputUri(gcsUri)
          .addFeatures(Feature.SHOT_CHANGE_DETECTION)
          .build();

  // Create an operation that will contain the response when the operation completes.
  OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response =
      client.annotateVideoAsync(request);

  System.out.println("Waiting for operation to complete...");
  // Print detected shot changes and their location ranges in the analyzed video.
  for (VideoAnnotationResults result : response.get().getAnnotationResultsList()) {
    if (result.getShotAnnotationsCount() > 0) {
      System.out.println("Shots: ");
      for (VideoSegment segment : result.getShotAnnotationsList()) {
        double startTime =
            segment.getStartTimeOffset().getSeconds()
                + segment.getStartTimeOffset().getNanos() / 1e9;
        double endTime =
            segment.getEndTimeOffset().getSeconds()
                + segment.getEndTimeOffset().getNanos() / 1e9;
        System.out.printf("Location: %.3f:%.3f\n", startTime, endTime);
      }
    } else {
      System.out.println("No shot changes detected in " + gcsUri);
    }
  }
}

Node.js

Video Intelligence에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

// Imports the Google Cloud Video Intelligence library
const video = require('@google-cloud/video-intelligence').v1;

// Creates a client
const client = new video.VideoIntelligenceServiceClient();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const gcsUri = 'GCS URI of file to analyze, e.g. gs://my-bucket/my-video.mp4';

const request = {
  inputUri: gcsUri,
  features: ['SHOT_CHANGE_DETECTION'],
};

// Detects camera shot changes
const [operation] = await client.annotateVideo(request);
console.log('Waiting for operation to complete...');
const [operationResult] = await operation.promise();
// Gets shot changes
const shotChanges = operationResult.annotationResults[0].shotAnnotations;
console.log('Shot changes:');

if (shotChanges.length === 1) {
  console.log('The entire video is one shot.');
} else {
  shotChanges.forEach((shot, shotIdx) => {
    console.log(`Scene ${shotIdx} occurs from:`);
    if (shot.startTimeOffset === undefined) {
      shot.startTimeOffset = {};
    }
    if (shot.endTimeOffset === undefined) {
      shot.endTimeOffset = {};
    }
    if (shot.startTimeOffset.seconds === undefined) {
      shot.startTimeOffset.seconds = 0;
    }
    if (shot.startTimeOffset.nanos === undefined) {
      shot.startTimeOffset.nanos = 0;
    }
    if (shot.endTimeOffset.seconds === undefined) {
      shot.endTimeOffset.seconds = 0;
    }
    if (shot.endTimeOffset.nanos === undefined) {
      shot.endTimeOffset.nanos = 0;
    }
    console.log(
      `\tStart: ${shot.startTimeOffset.seconds}` +
        `.${(shot.startTimeOffset.nanos / 1e6).toFixed(0)}s`
    );
    console.log(
      `\tEnd: ${shot.endTimeOffset.seconds}.` +
        `${(shot.endTimeOffset.nanos / 1e6).toFixed(0)}s`
    );
  });
}

Python

Python용 Video Intelligence API 클라이언트 라이브러리 설치 및 사용에 대한 자세한 내용은 Video Intelligence API 클라이언트 라이브러리를 참조하세요.
"""Detects camera shot changes."""
video_client = videointelligence.VideoIntelligenceServiceClient()
features = [videointelligence.Feature.SHOT_CHANGE_DETECTION]
operation = video_client.annotate_video(
    request={"features": features, "input_uri": path}
)
print("\nProcessing video for shot change annotations:")

result = operation.result(timeout=90)
print("\nFinished processing.")

# first result is retrieved because a single video was processed
for i, shot in enumerate(result.annotation_results[0].shot_annotations):
    start_time = (
        shot.start_time_offset.seconds + shot.start_time_offset.microseconds / 1e6
    )
    end_time = (
        shot.end_time_offset.seconds + shot.end_time_offset.microseconds / 1e6
    )
    print("\tShot {}: {} to {}".format(i, start_time, end_time))

추가 언어

C#: 클라이언트 라이브러리 페이지의 C# 설정 안내를 따른 후 .NET용 Video Intelligence 참조 문서를 참조하세요.

PHP: 클라이언트 라이브러리 페이지의 PHP 설정 안내를 따른 다음 PHP용 Video Intelligence 참조 문서를 참조하세요.

Ruby: 클라이언트 라이브러리 페이지의 Ruby 설정 안내를 따른 다음 Ruby용 Video Intelligence 참조 문서를 참조하세요.