Recognizing logos

This page describes how to recognize a logo in a video using Video Intelligence API.

Video Intelligence can detect, track, and recognize the presence of over 100,000 brands and logos in video content.

Annotate a video on Cloud Storage

The following code sample demonstrates how to detect logos from a video on Cloud Storage

REST API

To perform annotation on a local video file, base64-encode the contents of the video file. Include the base64-encoded contents in the inputContent field of the request. For information on how to base64-encode the contents of a video file, see Base64 Encoding.

The following shows how to send a POST request to the videos:annotate method. The example uses the access token for a service account set up for the project using the Google Cloud Platform Cloud SDK. For instructions on installing the Cloud SDK, setting up a project with a service account, and obtaining an access token, see the Video Intelligence API Quickstart.

curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json; charset=utf-8" --data '{
     "inputUri":"gs://cloud-samples-data/video/googlework_short.mp4",
     "features": [
       "LOGO_RECOGNITION"
    ]
  }'  https://videointelligence.googleapis.com/v1p3beta1/videos:annotate

If the request is successful, the Video Intelligence API returns the name for your operation. The following shows an example of such a response, where project-name is the name of your project and operation-id is the ID of the long running operation created for the request.


{
  "name": "projects/project-name/locations/us-west1/operations/operation-id"
}

To retrieve the result of the operation, make a GET request, using the operation name returned from the call to videos:annotate, as shown in the following example.

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

{
  "name": "projects/147301782967/locations/us-west1/operations/15087998751694875775",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1p3beta1.AnnotateVideoProgress",
    "annotationProgress": [
      {
        "inputUri": "/cloud-samples-data/video/googlework_short.mp4",
        "progressPercent": 100,
        "startTime": "2019-07-31T16:27:44.889439Z",
        "updateTime": "2019-07-31T16:27:56.526050Z"
      }
    ]
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse",
    "annotationResults": [
      {
        "inputUri": "/cloud-samples-data/video/googlework_short.mp4",
        "segment": {
          "startTimeOffset": "0s",
          "endTimeOffset": "34.234200s"
        },
        "logoRecognitionAnnotations": [
          {
            "entity": {
              "entityId": "/m/045c7b",
              "description": "Google",
              "languageCode": "en-US"
            },
            "tracks": [
              {
                "segment": {
                  "startTimeOffset": "10.543866s",
                  "endTimeOffset": "12.345666s"
                },
                "timestampedObjects": [
                  {
                    "normalizedBoundingBox": {
                      "left": 0.3912032,
                      "top": 0.26212785,
                      "right": 0.6469412,
                      "bottom": 0.4434373
                    },
                    "timeOffset": "10.543866s"
                  },
                  ...
                ],
                "confidence": 0.8588119
              },
              {
                "segment": {
                  "startTimeOffset": "15.348666s",
                  "endTimeOffset": "18.752066s"
                },
                "timestampedObjects": [
                  {
                    "normalizedBoundingBox": {
                      "left": 0.69989866,
                      "top": 0.79943377,
                      "right": 0.76465744,
                      "bottom": 0.9271479
                    },
                    "timeOffset": "15.348666s"
                  },
                  {
                    "normalizedBoundingBox": {
                      "left": 0.68997324,
                      "top": 0.78775305,
                      "right": 0.75723547,
                      "bottom": 0.91808647
                    },
                    "timeOffset": "15.448766s"
                  },
               ...
             }
          }
        ]
      }
    ]
  }
}

Java

/*
 * Please include the following imports to run this sample.
 *
 * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
 * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
 * import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
 * import com.google.cloud.videointelligence.v1p3beta1.Entity;
 * import com.google.cloud.videointelligence.v1p3beta1.Feature;
 * import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
 * import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
 * import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
 * import com.google.cloud.videointelligence.v1p3beta1.Track;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
 * import com.google.protobuf.Duration;
 * import java.util.Arrays;
 * import java.util.List;
 */

/** Performs asynchronous video annotation for logo recognition on a file hosted in GCS. */
public static void sampleAnnotateVideo() {
    try (VideoIntelligenceServiceClient videoIntelligenceServiceClient =
                 VideoIntelligenceServiceClient.create()) {
        String inputUri = "gs://cloud-samples-data/video/googlework_short.mp4";
        Feature featuresElement = Feature.LOGO_RECOGNITION;
        List<Feature> features = Arrays.asList(featuresElement);
        AnnotateVideoRequest request =
                AnnotateVideoRequest.newBuilder().setInputUri(inputUri).addAllFeatures(features).build();
        AnnotateVideoResponse response =
                videoIntelligenceServiceClient.annotateVideoAsync(request).get();
        // Get the first response, since we sent only one video.
        VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(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());
            // Textual description, e.g. `Google`.
            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.
                VideoSegment segment = track.getSegment();
                Duration segmentStartTimeOffset = segment.getStartTimeOffset();
                System.out.printf(
                        "\n\tStart Time Offset : %s.%s\n",
                        segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos());
                Duration segmentEndTimeOffset = segment.getEndTimeOffset();
                System.out.printf(
                        "\tEnd Time Offset : %s.%s\n",
                        segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.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 logoRecognitionAnnotationSegment :
                    logoRecognitionAnnotation.getSegmentsList()) {
                Duration logoRecognitionAnnotationSegmentStartTimeOffset =
                        logoRecognitionAnnotationSegment.getStartTimeOffset();
                System.out.printf(
                        "\n\tStart Time Offset : %s.%s\n",
                        logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(),
                        logoRecognitionAnnotationSegmentStartTimeOffset.getNanos());
                Duration logoRecognitionAnnotationSegmentEndTimeOffset =
                        logoRecognitionAnnotationSegment.getEndTimeOffset();
                System.out.printf(
                        "\tEnd Time Offset : %s.%s\n",
                        logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(),
                        logoRecognitionAnnotationSegmentEndTimeOffset.getNanos());
            }
        }
    } catch (Exception exception) {
        System.err.println("Failed to create the client due to: " + exception);
    }
}

Node.js


const videoIntelligence = require('@google-cloud/video-intelligence').v1p3beta1;

/** Performs asynchronous video annotation for logo recognition on a file hosted in GCS. */
async function sampleAnnotateVideo() {
  const client = new videoIntelligence.VideoIntelligenceServiceClient();
  const inputUri = 'gs://cloud-samples-data/video/googlework_short.mp4';
  const featuresElement = 'LOGO_RECOGNITION';
  const features = [featuresElement];
  const request = {
    inputUri: inputUri,
    features: features,
  };

  // Create a job whose results you can either wait for now, or get later
  const [operation] = await client.annotateVideo(request);

  // Get a Promise representation of the final result of the job
  const [response] = await operation.promise();

  // Get the first response, since we sent only one video.
  const annotationResult = response.annotationResults[0];
  // Annotations for list of logos detected, tracked and recognized in video.
  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}`);
    // Textual description, e.g. `Google`.
    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) {
      // Video segment of a track.
      const segment = track.segment;
      const segmentStartTimeOffset = segment.startTimeOffset;
      console.log(
        `\n\tStart Time Offset : ${segmentStartTimeOffset.seconds}.${segmentStartTimeOffset.nanos}`
      );
      const segmentEndTimeOffset = segment.endTimeOffset;
      console.log(
        `\tEnd Time Offset : ${segmentEndTimeOffset.seconds}.${segmentEndTimeOffset.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 logoRecognitionAnnotationSegment of logoRecognitionAnnotation.segments) {
      const logoRecognitionAnnotationSegmentStartTimeOffset =
        logoRecognitionAnnotationSegment.startTimeOffset;
      console.log(
        `\n\tStart Time Offset : ${logoRecognitionAnnotationSegmentStartTimeOffset.seconds}.${logoRecognitionAnnotationSegmentStartTimeOffset.nanos}`
      );
      const logoRecognitionAnnotationSegmentEndTimeOffset =
        logoRecognitionAnnotationSegment.endTimeOffset;
      console.log(
        `\tEnd Time Offset : ${logoRecognitionAnnotationSegmentEndTimeOffset.seconds}.${logoRecognitionAnnotationSegmentEndTimeOffset.nanos}`
      );
    }
  }
}

Python


from google.cloud import videointelligence_v1p3beta1
from google.cloud.videointelligence_v1p3beta1 import enums

def sample_annotate_video():

  client = videointelligence_v1p3beta1.VideoIntelligenceServiceClient()

  input_uri = 'gs://cloud-samples-data/video/googlework_short.mp4'
  features_element = enums.Feature.LOGO_RECOGNITION
  features = [features_element]

  operation = client.annotate_video(input_uri=input_uri, features=features)

  print(u'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(u'Entity Id : {}'.format(entity.entity_id))
    # Textual description, e.g. `Google`.
    print(u'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.
      segment = track.segment
      segment_start_time_offset = segment.start_time_offset
      print(u'\n\tStart Time Offset : {}.{}'.format(segment_start_time_offset.seconds, segment_start_time_offset.nanos))
      segment_end_time_offset = segment.end_time_offset
      print(u'\tEnd Time Offset : {}.{}'.format(segment_end_time_offset.seconds, segment_end_time_offset.nanos))
      print(u'\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(u'\n\t\tLeft : {}'.format(normalized_bounding_box.left))
        print(u'\t\tTop : {}'.format(normalized_bounding_box.top))
        print(u'\t\tRight : {}'.format(normalized_bounding_box.right))
        print(u'\t\tBottom : {}'.format(normalized_bounding_box.bottom))
        # Optional. The attributes of the object in the bounding box.
        for attribute in timestamped_object.attributes:
          print(u'\n\t\t\tName : {}'.format(attribute.name))
          print(u'\t\t\tConfidence : {}'.format(attribute.confidence))
          print(u'\t\t\tValue : {}'.format(attribute.value))
      # Optional. Attributes in the track level.
      for track_attribute in track.attributes:
        print(u'\n\t\tName : {}'.format(track_attribute.name))
        print(u'\t\tConfidence : {}'.format(track_attribute.confidence))
        print(u'\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 logo_recognition_annotation_segment in logo_recognition_annotation.segments:
      logo_recognition_annotation_segment_start_time_offset = logo_recognition_annotation_segment.start_time_offset
      print(u'\n\tStart Time Offset : {}.{}'.format(logo_recognition_annotation_segment_start_time_offset.seconds, logo_recognition_annotation_segment_start_time_offset.nanos))
      logo_recognition_annotation_segment_end_time_offset = logo_recognition_annotation_segment.end_time_offset
      print(u'\tEnd Time Offset : {}.{}'.format(logo_recognition_annotation_segment_end_time_offset.seconds, logo_recognition_annotation_segment_end_time_offset.nanos))

Annotate a local video

The following code sample demonstrates how to detect logos in a local video file.

Java

/*
 * Please include the following imports to run this sample.
 *
 * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest;
 * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse;
 * import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute;
 * import com.google.cloud.videointelligence.v1p3beta1.Entity;
 * import com.google.cloud.videointelligence.v1p3beta1.Feature;
 * import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation;
 * import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox;
 * import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject;
 * import com.google.cloud.videointelligence.v1p3beta1.Track;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient;
 * import com.google.cloud.videointelligence.v1p3beta1.VideoSegment;
 * import com.google.protobuf.ByteString;
 * import com.google.protobuf.Duration;
 * import java.nio.file.Files;
 * import java.nio.file.Path;
 * import java.nio.file.Paths;
 * import java.util.Arrays;
 * import java.util.List;
 */

/**
 * Performs asynchronous video annotation for logo recognition on a local file.
 *
 * @param localFilePath Path to local video file, e.g. /path/video.mp4
 */
public static void sampleAnnotateVideo(String localFilePath) {
    try (VideoIntelligenceServiceClient videoIntelligenceServiceClient =
                 VideoIntelligenceServiceClient.create()) {
        // localFilePath = "resources/googlework_short.mp4";
        Path path = Paths.get(localFilePath);
        byte[] data = Files.readAllBytes(path);
        ByteString inputContent = ByteString.copyFrom(data);
        Feature featuresElement = Feature.LOGO_RECOGNITION;
        List<Feature> features = Arrays.asList(featuresElement);
        AnnotateVideoRequest request =
                AnnotateVideoRequest.newBuilder()
                        .setInputContent(inputContent)
                        .addAllFeatures(features)
                        .build();
        AnnotateVideoResponse response =
                videoIntelligenceServiceClient.annotateVideoAsync(request).get();
        // Get the first response, since we sent only one video.
        VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(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());
            // Textual description, e.g. `Google`.
            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.
                VideoSegment segment = track.getSegment();
                Duration segmentStartTimeOffset = segment.getStartTimeOffset();
                System.out.printf(
                        "\n\tStart Time Offset : %s.%s\n",
                        segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos());
                Duration segmentEndTimeOffset = segment.getEndTimeOffset();
                System.out.printf(
                        "\tEnd Time Offset : %s.%s\n",
                        segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.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 logoRecognitionAnnotationSegment :
                    logoRecognitionAnnotation.getSegmentsList()) {
                Duration logoRecognitionAnnotationSegmentStartTimeOffset =
                        logoRecognitionAnnotationSegment.getStartTimeOffset();
                System.out.printf(
                        "\n\tStart Time Offset : %s.%s\n",
                        logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(),
                        logoRecognitionAnnotationSegmentStartTimeOffset.getNanos());
                Duration logoRecognitionAnnotationSegmentEndTimeOffset =
                        logoRecognitionAnnotationSegment.getEndTimeOffset();
                System.out.printf(
                        "\tEnd Time Offset : %s.%s\n",
                        logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(),
                        logoRecognitionAnnotationSegmentEndTimeOffset.getNanos());
            }
        }
    } catch (Exception exception) {
        System.err.println("Failed to create the client due to: " + exception);
    }
}

Node.js


const videoIntelligence = require('@google-cloud/video-intelligence').v1p3beta1;

const fs = require('fs');
/**
 * Performs asynchronous video annotation for logo recognition on a local file.
 *
 * @param localFilePath {string} Path to local video file, e.g. /path/video.mp4
 */
async function sampleAnnotateVideo(localFilePath) {
  const client = new videoIntelligence.VideoIntelligenceServiceClient();
  // const localFilePath = 'resources/googlework_short.mp4';
  const inputContent = fs.readFileSync(localFilePath).toString('base64');
  const featuresElement = 'LOGO_RECOGNITION';
  const features = [featuresElement];
  const request = {
    inputContent: inputContent,
    features: features,
  };

  // Create a job whose results you can either wait for now, or get later
  const [operation] = await client.annotateVideo(request);

  // Get a Promise representation of the final result of the job
  const [response] = await operation.promise();

  // Get the first response, since we sent only one video.
  const annotationResult = response.annotationResults[0];
  // Annotations for list of logos detected, tracked and recognized in video.
  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}`);
    // Textual description, e.g. `Google`.
    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) {
      // Video segment of a track.
      const segment = track.segment;
      const segmentStartTimeOffset = segment.startTimeOffset;
      console.log(
        `\n\tStart Time Offset : ${segmentStartTimeOffset.seconds}.${segmentStartTimeOffset.nanos}`
      );
      const segmentEndTimeOffset = segment.endTimeOffset;
      console.log(
        `\tEnd Time Offset : ${segmentEndTimeOffset.seconds}.${segmentEndTimeOffset.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 logoRecognitionAnnotationSegment of logoRecognitionAnnotation.segments) {
      const logoRecognitionAnnotationSegmentStartTimeOffset =
        logoRecognitionAnnotationSegment.startTimeOffset;
      console.log(
        `\n\tStart Time Offset : ${logoRecognitionAnnotationSegmentStartTimeOffset.seconds}.${logoRecognitionAnnotationSegmentStartTimeOffset.nanos}`
      );
      const logoRecognitionAnnotationSegmentEndTimeOffset =
        logoRecognitionAnnotationSegment.endTimeOffset;
      console.log(
        `\tEnd Time Offset : ${logoRecognitionAnnotationSegmentEndTimeOffset.seconds}.${logoRecognitionAnnotationSegmentEndTimeOffset.nanos}`
      );
    }
  }
}

Python


from google.cloud import videointelligence_v1p3beta1
from google.cloud.videointelligence_v1p3beta1 import enums
import io

def sample_annotate_video(local_file_path):
  """
    Performs asynchronous video annotation for logo recognition on a local file.

    Args:
      local_file_path Path to local video file, e.g. /path/video.mp4
    """

  client = videointelligence_v1p3beta1.VideoIntelligenceServiceClient()

  # local_file_path = 'resources/googlework_short.mp4'
  with io.open(local_file_path, 'rb') as f:
    input_content = f.read()
  features_element = enums.Feature.LOGO_RECOGNITION
  features = [features_element]

  operation = client.annotate_video(input_content=input_content, features=features)

  print(u'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(u'Entity Id : {}'.format(entity.entity_id))
    # Textual description, e.g. `Google`.
    print(u'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.
      segment = track.segment
      segment_start_time_offset = segment.start_time_offset
      print(u'\n\tStart Time Offset : {}.{}'.format(segment_start_time_offset.seconds, segment_start_time_offset.nanos))
      segment_end_time_offset = segment.end_time_offset
      print(u'\tEnd Time Offset : {}.{}'.format(segment_end_time_offset.seconds, segment_end_time_offset.nanos))
      print(u'\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(u'\n\t\tLeft : {}'.format(normalized_bounding_box.left))
        print(u'\t\tTop : {}'.format(normalized_bounding_box.top))
        print(u'\t\tRight : {}'.format(normalized_bounding_box.right))
        print(u'\t\tBottom : {}'.format(normalized_bounding_box.bottom))
        # Optional. The attributes of the object in the bounding box.
        for attribute in timestamped_object.attributes:
          print(u'\n\t\t\tName : {}'.format(attribute.name))
          print(u'\t\t\tConfidence : {}'.format(attribute.confidence))
          print(u'\t\t\tValue : {}'.format(attribute.value))
      # Optional. Attributes in the track level.
      for track_attribute in track.attributes:
        print(u'\n\t\tName : {}'.format(track_attribute.name))
        print(u'\t\tConfidence : {}'.format(track_attribute.confidence))
        print(u'\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 logo_recognition_annotation_segment in logo_recognition_annotation.segments:
      logo_recognition_annotation_segment_start_time_offset = logo_recognition_annotation_segment.start_time_offset
      print(u'\n\tStart Time Offset : {}.{}'.format(logo_recognition_annotation_segment_start_time_offset.seconds, logo_recognition_annotation_segment_start_time_offset.nanos))
      logo_recognition_annotation_segment_end_time_offset = logo_recognition_annotation_segment.end_time_offset
      print(u'\tEnd Time Offset : {}.{}'.format(logo_recognition_annotation_segment_end_time_offset.seconds, logo_recognition_annotation_segment_end_time_offset.nanos))

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

Cloud Video Intelligence API Documentation