ロゴの認識

Video Intelligence API は、動画コンテンツ内の 100,000 を超えるブランドやロゴの存在を検出、追跡、認識します。

このページでは、Video Intelligence API を使用して動画内のロゴを認識する方法について説明します。

Cloud Storage の動画にアノテーションを付ける

次のコードサンプルは、Cloud Storage で動画内のロゴを検出する方法を示しています。

REST

プロセス リクエストを送信する

ローカル動画ファイルに対してアノテーションを付けるには、動画ファイルの内容を Base64 形式でエンコードします。リクエストの inputContent フィールドに Base64 形式でエンコードされたコンテンツを格納します。動画ファイルのコンテンツを Base64 形式でエンコードする方法については、Base64 エンコードをご覧ください。

POST リクエストを videos:annotate メソッドに送信する方法を以下に示します。この例では、Google Cloud CLI を使用するプロジェクト用に設定されたサービス アカウントのアクセス トークンを使用します。Google Cloud CLI のインストール、サービス アカウントでのプロジェクトの設定、アクセス トークンの取得を行う手順については、Video Intelligence のクイックスタートをご覧ください。

リクエストのデータを使用する前に、次のように置き換えます。

  • INPUT_URI: アノテーションを付けるファイルを含む Cloud Storage バケット(ファイル名を含む)。gs:// で始まる必要があります。
    次に例を示します。
    "inputUri": "gs://cloud-videointelligence-demo/assistant.mp4",
  • PROJECT_NUMBER: Google Cloud プロジェクトの数値識別子。

HTTP メソッドと URL:

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

リクエストの本文(JSON):

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

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

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

レスポンスが成功すると、Video Intelligence API はオペレーションの name を返します。上記はこのようなレスポンスの例です。project-number はプロジェクトの番号、operation-id はリクエストに対して作成された長時間実行オペレーションの ID です。

  • PROJECT_NUMBER: プロジェクトの数
  • LOCATION_ID: アノテーションを実行する Cloud リージョン。サポート対象のクラウド リージョンは us-east1us-west1europe-west1asia-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 レスポンスが返されます。

アノテーションの結果をダウンロードする

アノテーションを、送信元バケットから送信先バケットにコピーします(ファイルとオブジェクトのコピーをご覧ください)。

gsutil cp gcs_uri gs://my-bucket

注: 出力 GCS URI がユーザーによって指定された場合、アノテーションはその GCS URI に格納されます。

Go

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

import (
	"context"
	"fmt"
	"io"
	"time"

	video "cloud.google.com/go/videointelligence/apiv1"
	videopb "cloud.google.com/go/videointelligence/apiv1/videointelligencepb"
	"github.com/golang/protobuf/ptypes"
)

// logoDetectionGCS analyzes a video and extracts logos with their bounding boxes.
func logoDetectionGCS(w io.Writer, gcsURI string) error {
	// gcsURI := "gs://cloud-samples-data/video/googlework_tiny.mp4"

	ctx := context.Background()

	// Creates a client.
	client, err := video.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("video.NewClient: %w", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*180)
	defer cancel()

	op, err := client.AnnotateVideo(ctx, &videopb.AnnotateVideoRequest{
		InputUri: gcsURI,
		Features: []videopb.Feature{
			videopb.Feature_LOGO_RECOGNITION,
		},
	})
	if err != nil {
		return fmt.Errorf("AnnotateVideo: %w", err)
	}

	resp, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %w", err)
	}

	// Only one video was processed, so get the first result.
	result := resp.GetAnnotationResults()[0]

	// Annotations for list of logos detected, tracked and recognized in video.
	for _, annotation := range result.LogoRecognitionAnnotations {
		fmt.Fprintf(w, "Description: %q\n", annotation.Entity.GetDescription())
		// Opaque entity ID. Some IDs may be available in Google Knowledge
		// Graph Search API (https://developers.google.com/knowledge-graph/).
		if len(annotation.Entity.EntityId) > 0 {
			fmt.Fprintf(w, "\tEntity ID: %q\n", annotation.Entity.GetEntityId())
		}

		// All logo tracks where the recognized logo appears. Each track
		// corresponds to one logo instance appearing in consecutive frames.
		for _, track := range annotation.Tracks {
			// Video segment of a track.
			segment := track.GetSegment()
			start, _ := ptypes.Duration(segment.GetStartTimeOffset())
			end, _ := ptypes.Duration(segment.GetEndTimeOffset())
			fmt.Fprintf(w, "\tSegment: %v to %v\n", start, end)
			fmt.Fprintf(w, "\tConfidence: %f\n", track.GetConfidence())

			// The object with timestamp and attributes per frame in the track.
			for _, timestampedObject := range track.TimestampedObjects {
				// Normalized Bounding box in a frame, where the object is
				// located.
				box := timestampedObject.GetNormalizedBoundingBox()
				fmt.Fprintf(w, "\tBounding box position:\n")
				fmt.Fprintf(w, "\t\tleft  : %f\n", box.GetLeft())
				fmt.Fprintf(w, "\t\ttop   : %f\n", box.GetTop())
				fmt.Fprintf(w, "\t\tright : %f\n", box.GetRight())
				fmt.Fprintf(w, "\t\tbottom: %f\n", box.GetBottom())

				// Optional. The attributes of the object in the bounding box.
				for _, attribute := range timestampedObject.Attributes {
					fmt.Fprintf(w, "\t\t\tName: %q\n", attribute.GetName())
					fmt.Fprintf(w, "\t\t\tConfidence: %f\n", attribute.GetConfidence())
					fmt.Fprintf(w, "\t\t\tValue: %q\n", attribute.GetValue())
				}
			}

			// Optional. Attributes in the track level.
			for _, trackAttribute := range track.Attributes {
				fmt.Fprintf(w, "\t\tName: %q\n", trackAttribute.GetName())
				fmt.Fprintf(w, "\t\tConfidence: %f\n", trackAttribute.GetConfidence())
				fmt.Fprintf(w, "\t\tValue: %q\n", trackAttribute.GetValue())
			}
		}

		// All video segments where the recognized logo appears. There might be
		// multiple instances of the same logo class appearing in one VideoSegment.
		for _, segment := range annotation.Segments {
			start, _ := ptypes.Duration(segment.GetStartTimeOffset())
			end, _ := ptypes.Duration(segment.GetEndTimeOffset())
			fmt.Fprintf(w, "\tSegment: %v to %v\n", start, end)
		}
	}

	return nil
}

Java

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.videointelligence.v1.AnnotateVideoProgress;
import com.google.cloud.videointelligence.v1.AnnotateVideoRequest;
import com.google.cloud.videointelligence.v1.AnnotateVideoResponse;
import com.google.cloud.videointelligence.v1.DetectedAttribute;
import com.google.cloud.videointelligence.v1.Entity;
import com.google.cloud.videointelligence.v1.Feature;
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation;
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox;
import com.google.cloud.videointelligence.v1.TimestampedObject;
import com.google.cloud.videointelligence.v1.Track;
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient;
import com.google.cloud.videointelligence.v1.VideoSegment;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class LogoDetectionGcs {

  public static void detectLogoGcs() throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String gcsUri = "gs://YOUR_BUCKET_ID/path/to/your/video.mp4";
    detectLogoGcs(gcsUri);
  }

  public static void detectLogoGcs(String inputUri)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
      // Create the request
      AnnotateVideoRequest request =
          AnnotateVideoRequest.newBuilder()
              .setInputUri(inputUri)
              .addFeatures(Feature.LOGO_RECOGNITION)
              .build();

      // asynchronously perform object tracking on videos
      OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future =
          client.annotateVideoAsync(request);

      System.out.println("Waiting for operation to complete...");
      // The first result is retrieved because a single video was processed.
      AnnotateVideoResponse response = future.get(600, TimeUnit.SECONDS);
      VideoAnnotationResults annotationResult = response.getAnnotationResults(0);

      // Annotations for list of logos detected, tracked and recognized in video.
      for (LogoRecognitionAnnotation logoRecognitionAnnotation :
          annotationResult.getLogoRecognitionAnnotationsList()) {
        Entity entity = logoRecognitionAnnotation.getEntity();
        // Opaque entity ID. Some IDs may be available in
        // [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
        System.out.printf("Entity Id : %s\n", entity.getEntityId());
        System.out.printf("Description : %s\n", entity.getDescription());
        // All logo tracks where the recognized logo appears. Each track corresponds to one logo
        // instance appearing in consecutive frames.
        for (Track track : logoRecognitionAnnotation.getTracksList()) {

          // Video segment of a track.
          Duration startTimeOffset = track.getSegment().getStartTimeOffset();
          System.out.printf(
              "\n\tStart Time Offset: %s.%s\n",
              startTimeOffset.getSeconds(), startTimeOffset.getNanos());
          Duration endTimeOffset = track.getSegment().getEndTimeOffset();
          System.out.printf(
              "\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.getNanos());
          System.out.printf("\tConfidence: %s\n", track.getConfidence());

          // The object with timestamp and attributes per frame in the track.
          for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {

            // Normalized Bounding box in a frame, where the object is located.
            NormalizedBoundingBox normalizedBoundingBox =
                timestampedObject.getNormalizedBoundingBox();
            System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
            System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
            System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
            System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());

            // Optional. The attributes of the object in the bounding box.
            for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
              System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
              System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
              System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
            }
          }

          // Optional. Attributes in the track level.
          for (DetectedAttribute trackAttribute : track.getAttributesList()) {
            System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
            System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
            System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
          }
        }

        // All video segments where the recognized logo appears. There might be multiple instances
        // of the same logo class appearing in one VideoSegment.
        for (VideoSegment segment : logoRecognitionAnnotation.getSegmentsList()) {
          System.out.printf(
              "\n\tStart Time Offset : %s.%s\n",
              segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos());
          System.out.printf(
              "\tEnd Time Offset : %s.%s\n",
              segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos());
        }
      }
    }
  }
}

Node.js

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const inputUri = 'gs://cloud-samples-data/video/googlework_short.mp4';

// Imports the Google Cloud client libraries
const Video = require('@google-cloud/video-intelligence');

// Instantiates a client
const client = new Video.VideoIntelligenceServiceClient();

// Performs asynchronous video annotation for logo recognition on a file hosted in GCS.
async function detectLogoGcs() {
  // Build the request with the input uri and logo recognition feature.
  const request = {
    inputUri: inputUri,
    features: ['LOGO_RECOGNITION'],
  };

  // Make the asynchronous request
  const [operation] = await client.annotateVideo(request);

  // Wait for the results
  const [response] = await operation.promise();

  // Get the first response, since we sent only one video.
  const annotationResult = response.annotationResults[0];
  for (const logoRecognitionAnnotation of annotationResult.logoRecognitionAnnotations) {
    const entity = logoRecognitionAnnotation.entity;
    // Opaque entity ID. Some IDs may be available in
    // [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
    console.log(`Entity Id: ${entity.entityId}`);
    console.log(`Description: ${entity.description}`);

    // All logo tracks where the recognized logo appears.
    // Each track corresponds to one logo instance appearing in consecutive frames.
    for (const track of logoRecognitionAnnotation.tracks) {
      console.log(
        `\n\tStart Time Offset: ${track.segment.startTimeOffset.seconds}.${track.segment.startTimeOffset.nanos}`
      );
      console.log(
        `\tEnd Time Offset: ${track.segment.endTimeOffset.seconds}.${track.segment.endTimeOffset.nanos}`
      );
      console.log(`\tConfidence: ${track.confidence}`);

      // The object with timestamp and attributes per frame in the track.
      for (const timestampedObject of track.timestampedObjects) {
        // Normalized Bounding box in a frame, where the object is located.
        const normalizedBoundingBox = timestampedObject.normalizedBoundingBox;
        console.log(`\n\t\tLeft: ${normalizedBoundingBox.left}`);
        console.log(`\t\tTop: ${normalizedBoundingBox.top}`);
        console.log(`\t\tRight: ${normalizedBoundingBox.right}`);
        console.log(`\t\tBottom: ${normalizedBoundingBox.bottom}`);
        // Optional. The attributes of the object in the bounding box.
        for (const attribute of timestampedObject.attributes) {
          console.log(`\n\t\t\tName: ${attribute.name}`);
          console.log(`\t\t\tConfidence: ${attribute.confidence}`);
          console.log(`\t\t\tValue: ${attribute.value}`);
        }
      }

      // Optional. Attributes in the track level.
      for (const trackAttribute of track.attributes) {
        console.log(`\n\t\tName: ${trackAttribute.name}`);
        console.log(`\t\tConfidence: ${trackAttribute.confidence}`);
        console.log(`\t\tValue: ${trackAttribute.value}`);
      }
    }

    // All video segments where the recognized logo appears.
    // There might be multiple instances of the same logo class appearing in one VideoSegment.
    for (const segment of logoRecognitionAnnotation.segments) {
      console.log(
        `\n\tStart Time Offset: ${segment.startTimeOffset.seconds}.${segment.startTimeOffset.nanos}`
      );
      console.log(
        `\tEnd Time Offset: ${segment.endTimeOffset.seconds}.${segment.endTimeOffset.nanos}`
      );
    }
  }
}

detectLogoGcs();

Python

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


from google.cloud import videointelligence

def detect_logo_gcs(input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.mp4"):
    client = videointelligence.VideoIntelligenceServiceClient()

    features = [videointelligence.Feature.LOGO_RECOGNITION]

    operation = client.annotate_video(
        request={"features": features, "input_uri": input_uri}
    )

    print("Waiting for operation to complete...")
    response = operation.result()

    # Get the first response, since we sent only one video.
    annotation_result = response.annotation_results[0]

    # Annotations for list of logos detected, tracked and recognized in video.
    for logo_recognition_annotation in annotation_result.logo_recognition_annotations:
        entity = logo_recognition_annotation.entity

        # Opaque entity ID. Some IDs may be available in [Google Knowledge Graph
        # Search API](https://developers.google.com/knowledge-graph/).
        print("Entity Id : {}".format(entity.entity_id))

        print("Description : {}".format(entity.description))

        # All logo tracks where the recognized logo appears. Each track corresponds
        # to one logo instance appearing in consecutive frames.
        for track in logo_recognition_annotation.tracks:
            # Video segment of a track.
            print(
                "\n\tStart Time Offset : {}.{}".format(
                    track.segment.start_time_offset.seconds,
                    track.segment.start_time_offset.microseconds * 1000,
                )
            )
            print(
                "\tEnd Time Offset : {}.{}".format(
                    track.segment.end_time_offset.seconds,
                    track.segment.end_time_offset.microseconds * 1000,
                )
            )
            print("\tConfidence : {}".format(track.confidence))

            # The object with timestamp and attributes per frame in the track.
            for timestamped_object in track.timestamped_objects:
                # Normalized Bounding box in a frame, where the object is located.
                normalized_bounding_box = timestamped_object.normalized_bounding_box
                print("\n\t\tLeft : {}".format(normalized_bounding_box.left))
                print("\t\tTop : {}".format(normalized_bounding_box.top))
                print("\t\tRight : {}".format(normalized_bounding_box.right))
                print("\t\tBottom : {}".format(normalized_bounding_box.bottom))

                # Optional. The attributes of the object in the bounding box.
                for attribute in timestamped_object.attributes:
                    print("\n\t\t\tName : {}".format(attribute.name))
                    print("\t\t\tConfidence : {}".format(attribute.confidence))
                    print("\t\t\tValue : {}".format(attribute.value))

            # Optional. Attributes in the track level.
            for track_attribute in track.attributes:
                print("\n\t\tName : {}".format(track_attribute.name))
                print("\t\tConfidence : {}".format(track_attribute.confidence))
                print("\t\tValue : {}".format(track_attribute.value))

        # All video segments where the recognized logo appears. There might be
        # multiple instances of the same logo class appearing in one VideoSegment.
        for segment in logo_recognition_annotation.segments:
            print(
                "\n\tStart Time Offset : {}.{}".format(
                    segment.start_time_offset.seconds,
                    segment.start_time_offset.microseconds * 1000,
                )
            )
            print(
                "\tEnd Time Offset : {}.{}".format(
                    segment.end_time_offset.seconds,
                    segment.end_time_offset.microseconds * 1000,
                )
            )

その他の言語

C#: クライアント ライブラリ ページの C# の設定手順を実行してから、.NET の Video Intelligence のリファレンス ドキュメントをご覧ください。

PHP: クライアント ライブラリ ページのPHP の設定手順を実行してから、PHP の Video Intelligence のリファレンス ドキュメントをご覧ください。

Ruby: クライアント ライブラリ ページの Ruby の設定手順を実行してから、Ruby の Video Intelligence のリファレンス ドキュメントをご覧ください。

ローカル動画にアノテーションを付ける

次のコードサンプルは、ローカル動画ファイル内のロゴを検出する方法を示しています。

REST

動画アノテーションリクエストを送信する

ローカル動画ファイルにアノテーションを付けるには、動画ファイルの内容を Base64 形式でエンコードします。 リクエストの inputContent フィールドに Base64 形式でエンコードされたコンテンツを格納します。 動画ファイルのコンテンツを Base64 形式でエンコードする方法については、Base64 エンコードをご覧ください。

POST リクエストをvideos:annotate メソッドに送信する方法を以下に示します。この例では、Google Cloud CLI を使用するプロジェクト用に設定されたサービス アカウントのアクセス トークンを使用します。Google Cloud CLI のインストール、サービス アカウントでのプロジェクトの設定、アクセス トークンの取得を行う手順については、Video Intelligence API のクイックスタートをご覧ください。

リクエストのデータを使用する前に、次のように置き換えます。

  • "inputContent": BASE64_ENCODED_CONTENT
    以下に例を示します。
    "UklGRg41AwBBVkkgTElTVAwBAABoZHJsYXZpaDgAAAA1ggAAxPMBAAAAAAAQCAA..."
  • LANGUAGE_CODE: [オプション] サポートされている言語をご覧ください。
  • PROJECT_NUMBER: Google Cloud プロジェクトの数値識別子。

HTTP メソッドと URL:

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

リクエストの本文(JSON):

{
  "inputContent": "BASE64_ENCODED_CONTENT",
  "features": ["LOGO_RECOGNITION"],
  "videoContext": {
  }
}

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

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

レスポンスが成功すると、Video Intelligence API はオペレーションの name を返します。上記はこのようなレスポンスの例です。project-number はプロジェクトの名前、operation-id はリクエストに対して作成された長時間実行オペレーションの ID です。

  • OPERATION_ID: オペレーションの開始時にレスポンスで提供されます(例: 12345...)。

アノテーション結果を取得する

オペレーションの結果を取得するには、次の例のように、動画アノテーションの呼び出しから返されたオペレーション名を使用して GET リクエストを行います。

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_NUMBER: Google Cloud プロジェクトの数値識別子。

HTTP メソッドと URL:

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

リクエストを送信するには、次のいずれかのオプションを展開します。

次のような JSON レスポンスが返されます。

テキスト検出アノテーションは、textAnnotations リストとして返されます。注: done フィールドは、値が True の場合にのみ返されます。オペレーションが完了していない場合、レスポンスには含まれません。

Go

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"time"

	video "cloud.google.com/go/videointelligence/apiv1"
	videopb "cloud.google.com/go/videointelligence/apiv1/videointelligencepb"
	"github.com/golang/protobuf/ptypes"
)

// logoDetection analyzes a video and extracts logos with their bounding boxes.
func logoDetection(w io.Writer, filename string) error {
	// filename := "../testdata/googlework_short.mp4"

	ctx := context.Background()

	// Creates a client.
	client, err := video.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("video.NewClient: %w", err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(ctx, time.Second*180)
	defer cancel()

	fileBytes, err := ioutil.ReadFile(filename)
	if err != nil {
		return fmt.Errorf("ioutil.ReadFile: %w", err)
	}

	op, err := client.AnnotateVideo(ctx, &videopb.AnnotateVideoRequest{
		InputContent: fileBytes,
		Features: []videopb.Feature{
			videopb.Feature_LOGO_RECOGNITION,
		},
	})
	if err != nil {
		return fmt.Errorf("AnnotateVideo: %w", err)
	}

	resp, err := op.Wait(ctx)
	if err != nil {
		return fmt.Errorf("Wait: %w", err)
	}

	// Only one video was processed, so get the first result.
	result := resp.GetAnnotationResults()[0]

	// Annotations for list of logos detected, tracked and recognized in video.
	for _, annotation := range result.LogoRecognitionAnnotations {
		fmt.Fprintf(w, "Description: %q\n", annotation.Entity.GetDescription())
		// Opaque entity ID. Some IDs may be available in Google Knowledge
		// Graph Search API (https://developers.google.com/knowledge-graph/).
		if len(annotation.Entity.EntityId) > 0 {
			fmt.Fprintf(w, "\tEntity ID: %q\n", annotation.Entity.GetEntityId())
		}

		// All logo tracks where the recognized logo appears. Each track
		// corresponds to one logo instance appearing in consecutive frames.
		for _, track := range annotation.Tracks {
			// Video segment of a track.
			segment := track.GetSegment()
			start, _ := ptypes.Duration(segment.GetStartTimeOffset())
			end, _ := ptypes.Duration(segment.GetEndTimeOffset())
			fmt.Fprintf(w, "\tSegment: %v to %v\n", start, end)
			fmt.Fprintf(w, "\tConfidence: %f\n", track.GetConfidence())

			// The object with timestamp and attributes per frame in the track.
			for _, timestampedObject := range track.TimestampedObjects {
				// Normalized Bounding box in a frame, where the object is
				// located.
				box := timestampedObject.GetNormalizedBoundingBox()
				fmt.Fprintf(w, "\tBounding box position:\n")
				fmt.Fprintf(w, "\t\tleft  : %f\n", box.GetLeft())
				fmt.Fprintf(w, "\t\ttop   : %f\n", box.GetTop())
				fmt.Fprintf(w, "\t\tright : %f\n", box.GetRight())
				fmt.Fprintf(w, "\t\tbottom: %f\n", box.GetBottom())

				// Optional. The attributes of the object in the bounding box.
				for _, attribute := range timestampedObject.Attributes {
					fmt.Fprintf(w, "\t\t\tName: %q\n", attribute.GetName())
					fmt.Fprintf(w, "\t\t\tConfidence: %f\n", attribute.GetConfidence())
					fmt.Fprintf(w, "\t\t\tValue: %q\n", attribute.GetValue())
				}
			}

			// Optional. Attributes in the track level.
			for _, trackAttribute := range track.Attributes {
				fmt.Fprintf(w, "\t\tName: %q\n", trackAttribute.GetName())
				fmt.Fprintf(w, "\t\tConfidence: %f\n", trackAttribute.GetConfidence())
				fmt.Fprintf(w, "\t\tValue: %q\n", trackAttribute.GetValue())
			}
		}

		// All video segments where the recognized logo appears. There might be
		// multiple instances of the same logo class appearing in one VideoSegment.
		for _, segment := range annotation.Segments {
			start, _ := ptypes.Duration(segment.GetStartTimeOffset())
			end, _ := ptypes.Duration(segment.GetEndTimeOffset())
			fmt.Fprintf(w, "\tSegment: %v to %v\n", start, end)
		}
	}

	return nil
}

Java

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.videointelligence.v1.AnnotateVideoProgress;
import com.google.cloud.videointelligence.v1.AnnotateVideoRequest;
import com.google.cloud.videointelligence.v1.AnnotateVideoResponse;
import com.google.cloud.videointelligence.v1.DetectedAttribute;
import com.google.cloud.videointelligence.v1.Entity;
import com.google.cloud.videointelligence.v1.Feature;
import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation;
import com.google.cloud.videointelligence.v1.NormalizedBoundingBox;
import com.google.cloud.videointelligence.v1.TimestampedObject;
import com.google.cloud.videointelligence.v1.Track;
import com.google.cloud.videointelligence.v1.VideoAnnotationResults;
import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient;
import com.google.cloud.videointelligence.v1.VideoSegment;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class LogoDetection {

  public static void detectLogo() throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String localFilePath = "path/to/your/video.mp4";
    detectLogo(localFilePath);
  }

  public static void detectLogo(String filePath)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) {
      // Read file
      Path path = Paths.get(filePath);
      byte[] data = Files.readAllBytes(path);
      // Create the request
      AnnotateVideoRequest request =
          AnnotateVideoRequest.newBuilder()
              .setInputContent(ByteString.copyFrom(data))
              .addFeatures(Feature.LOGO_RECOGNITION)
              .build();

      // asynchronously perform object tracking on videos
      OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future =
          client.annotateVideoAsync(request);

      System.out.println("Waiting for operation to complete...");
      // The first result is retrieved because a single video was processed.
      AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS);
      VideoAnnotationResults annotationResult = response.getAnnotationResults(0);

      // Annotations for list of logos detected, tracked and recognized in video.
      for (LogoRecognitionAnnotation logoRecognitionAnnotation :
          annotationResult.getLogoRecognitionAnnotationsList()) {
        Entity entity = logoRecognitionAnnotation.getEntity();
        // Opaque entity ID. Some IDs may be available in
        // [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
        System.out.printf("Entity Id : %s\n", entity.getEntityId());
        System.out.printf("Description : %s\n", entity.getDescription());
        // All logo tracks where the recognized logo appears. Each track corresponds to one logo
        // instance appearing in consecutive frames.
        for (Track track : logoRecognitionAnnotation.getTracksList()) {

          // Video segment of a track.
          Duration startTimeOffset = track.getSegment().getStartTimeOffset();
          System.out.printf(
              "\n\tStart Time Offset: %s.%s\n",
              startTimeOffset.getSeconds(), startTimeOffset.getNanos());
          Duration endTimeOffset = track.getSegment().getEndTimeOffset();
          System.out.printf(
              "\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.getNanos());
          System.out.printf("\tConfidence: %s\n", track.getConfidence());

          // The object with timestamp and attributes per frame in the track.
          for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) {

            // Normalized Bounding box in a frame, where the object is located.
            NormalizedBoundingBox normalizedBoundingBox =
                timestampedObject.getNormalizedBoundingBox();
            System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft());
            System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop());
            System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight());
            System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom());

            // Optional. The attributes of the object in the bounding box.
            for (DetectedAttribute attribute : timestampedObject.getAttributesList()) {
              System.out.printf("\n\t\t\tName: %s\n", attribute.getName());
              System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence());
              System.out.printf("\t\t\tValue: %s\n", attribute.getValue());
            }
          }

          // Optional. Attributes in the track level.
          for (DetectedAttribute trackAttribute : track.getAttributesList()) {
            System.out.printf("\n\t\tName : %s\n", trackAttribute.getName());
            System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence());
            System.out.printf("\t\tValue : %s\n", trackAttribute.getValue());
          }
        }

        // All video segments where the recognized logo appears. There might be multiple instances
        // of the same logo class appearing in one VideoSegment.
        for (VideoSegment segment : logoRecognitionAnnotation.getSegmentsList()) {
          System.out.printf(
              "\n\tStart Time Offset : %s.%s\n",
              segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos());
          System.out.printf(
              "\tEnd Time Offset : %s.%s\n",
              segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos());
        }
      }
    }
  }
}

Node.js

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const localFilePath = 'path/to/your/video.mp4'

// Imports the Google Cloud client libraries
const Video = require('@google-cloud/video-intelligence');
const fs = require('fs');

// Instantiates a client
const client = new Video.VideoIntelligenceServiceClient();

// Performs asynchronous video annotation for logo recognition on a file.
async function detectLogo() {
  const inputContent = fs.readFileSync(localFilePath).toString('base64');

  // Build the request with the input content and logo recognition feature.
  const request = {
    inputContent: inputContent,
    features: ['LOGO_RECOGNITION'],
  };

  // Make the asynchronous request
  const [operation] = await client.annotateVideo(request);

  // Wait for the results
  const [response] = await operation.promise();

  // Get the first response, since we sent only one video.
  const annotationResult = response.annotationResults[0];
  for (const logoRecognitionAnnotation of annotationResult.logoRecognitionAnnotations) {
    const entity = logoRecognitionAnnotation.entity;
    // Opaque entity ID. Some IDs may be available in
    // [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/).
    console.log(`Entity Id: ${entity.entityId}`);
    console.log(`Description: ${entity.description}`);

    // All logo tracks where the recognized logo appears.
    // Each track corresponds to one logo instance appearing in consecutive frames.
    for (const track of logoRecognitionAnnotation.tracks) {
      console.log(
        `\n\tStart Time Offset: ${track.segment.startTimeOffset.seconds}.${track.segment.startTimeOffset.nanos}`
      );
      console.log(
        `\tEnd Time Offset: ${track.segment.endTimeOffset.seconds}.${track.segment.endTimeOffset.nanos}`
      );
      console.log(`\tConfidence: ${track.confidence}`);

      // The object with timestamp and attributes per frame in the track.
      for (const timestampedObject of track.timestampedObjects) {
        // Normalized Bounding box in a frame, where the object is located.
        const normalizedBoundingBox = timestampedObject.normalizedBoundingBox;
        console.log(`\n\t\tLeft: ${normalizedBoundingBox.left}`);
        console.log(`\t\tTop: ${normalizedBoundingBox.top}`);
        console.log(`\t\tRight: ${normalizedBoundingBox.right}`);
        console.log(`\t\tBottom: ${normalizedBoundingBox.bottom}`);
        // Optional. The attributes of the object in the bounding box.
        for (const attribute of timestampedObject.attributes) {
          console.log(`\n\t\t\tName: ${attribute.name}`);
          console.log(`\t\t\tConfidence: ${attribute.confidence}`);
          console.log(`\t\t\tValue: ${attribute.value}`);
        }
      }

      // Optional. Attributes in the track level.
      for (const trackAttribute of track.attributes) {
        console.log(`\n\t\tName: ${trackAttribute.name}`);
        console.log(`\t\tConfidence: ${trackAttribute.confidence}`);
        console.log(`\t\tValue: ${trackAttribute.value}`);
      }
    }

    // All video segments where the recognized logo appears.
    // There might be multiple instances of the same logo class appearing in one VideoSegment.
    for (const segment of logoRecognitionAnnotation.segments) {
      console.log(
        `\n\tStart Time Offset: ${segment.startTimeOffset.seconds}.${segment.startTimeOffset.nanos}`
      );
      console.log(
        `\tEnd Time Offset: ${segment.endTimeOffset.seconds}.${segment.endTimeOffset.nanos}`
      );
    }
  }
}

detectLogo();

Python

Video Intelligence への認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

import io

from google.cloud import videointelligence

def detect_logo(local_file_path="path/to/your/video.mp4"):
    """Performs asynchronous video annotation for logo recognition on a local file."""

    client = videointelligence.VideoIntelligenceServiceClient()

    with io.open(local_file_path, "rb") as f:
        input_content = f.read()
    features = [videointelligence.Feature.LOGO_RECOGNITION]

    operation = client.annotate_video(
        request={"features": features, "input_content": input_content}
    )

    print("Waiting for operation to complete...")
    response = operation.result()

    # Get the first response, since we sent only one video.
    annotation_result = response.annotation_results[0]

    # Annotations for list of logos detected, tracked and recognized in video.
    for logo_recognition_annotation in annotation_result.logo_recognition_annotations:
        entity = logo_recognition_annotation.entity

        # Opaque entity ID. Some IDs may be available in [Google Knowledge Graph
        # Search API](https://developers.google.com/knowledge-graph/).
        print("Entity Id : {}".format(entity.entity_id))

        print("Description : {}".format(entity.description))

        # All logo tracks where the recognized logo appears. Each track corresponds
        # to one logo instance appearing in consecutive frames.
        for track in logo_recognition_annotation.tracks:
            # Video segment of a track.
            print(
                "\n\tStart Time Offset : {}.{}".format(
                    track.segment.start_time_offset.seconds,
                    track.segment.start_time_offset.microseconds * 1000,
                )
            )
            print(
                "\tEnd Time Offset : {}.{}".format(
                    track.segment.end_time_offset.seconds,
                    track.segment.end_time_offset.microseconds * 1000,
                )
            )
            print("\tConfidence : {}".format(track.confidence))

            # The object with timestamp and attributes per frame in the track.
            for timestamped_object in track.timestamped_objects:
                # Normalized Bounding box in a frame, where the object is located.
                normalized_bounding_box = timestamped_object.normalized_bounding_box
                print("\n\t\tLeft : {}".format(normalized_bounding_box.left))
                print("\t\tTop : {}".format(normalized_bounding_box.top))
                print("\t\tRight : {}".format(normalized_bounding_box.right))
                print("\t\tBottom : {}".format(normalized_bounding_box.bottom))

                # Optional. The attributes of the object in the bounding box.
                for attribute in timestamped_object.attributes:
                    print("\n\t\t\tName : {}".format(attribute.name))
                    print("\t\t\tConfidence : {}".format(attribute.confidence))
                    print("\t\t\tValue : {}".format(attribute.value))

            # Optional. Attributes in the track level.
            for track_attribute in track.attributes:
                print("\n\t\tName : {}".format(track_attribute.name))
                print("\t\tConfidence : {}".format(track_attribute.confidence))
                print("\t\tValue : {}".format(track_attribute.value))

        # All video segments where the recognized logo appears. There might be
        # multiple instances of the same logo class appearing in one VideoSegment.
        for segment in logo_recognition_annotation.segments:
            print(
                "\n\tStart Time Offset : {}.{}".format(
                    segment.start_time_offset.seconds,
                    segment.start_time_offset.microseconds * 1000,
                )
            )
            print(
                "\tEnd Time Offset : {}.{}".format(
                    segment.end_time_offset.seconds,
                    segment.end_time_offset.microseconds * 1000,
                )
            )

その他の言語

C#: クライアント ライブラリ ページの C# の設定手順を実行してから、.NET の Video Intelligence のリファレンス ドキュメントをご覧ください。

PHP: クライアント ライブラリ ページのPHP の設定手順を実行してから、PHP の Video Intelligence のリファレンス ドキュメントをご覧ください。

Ruby: クライアント ライブラリ ページの Ruby の設定手順を実行してから、Ruby の Video Intelligence のリファレンス ドキュメントをご覧ください。