Working With Video Intelligence API Logo Detection In Video Using Python



Working With Video Intelligence API Logo Detection In Video Using Python

You can use the Video Intelligence API to detect and track logos in a video. Over 100,000 brands and logos can be detected.

Copy the following code into your IPython session:

from google.cloud import videointelligence
from google.cloud.videointelligence import enums, types


def detect_logos(video_uri, segments=None):
    video_client = videointelligence.VideoIntelligenceServiceClient()
    features = [enums.Feature.LOGO_RECOGNITION]
    context = types.VideoContext(segments=segments)

    print(f'Processing video "{video_uri}"...')
    operation = video_client.annotate_video(
        input_uri=video_uri,
        features=features,
        video_context=context,
    )
    return operation.result()
Take a moment to study the code and see how it uses the annotate_video client library method with the LOGO_RECOGNITION parameter to analyze a video and detect logos.

Call the function to analyze the penultimate sequence of the video:

video_uri = 'gs://cloudmleap/video/next/JaneGoodall.mp4'
segment = types.VideoSegment()
segment.start_time_offset.FromSeconds(146)
segment.end_time_offset.FromSeconds(156)
response = detect_logos(video_uri, [segment])

Wait a moment for the video to be processed:

Processing video "gs://cloudmleap/video/next/JaneGoodall.mp4"...


Add this function to print out the list of detected logos:

def print_detected_logos(response):
    # First result only, as a single video is processed
    annotations = response.annotation_results[0].logo_recognition_annotations

    print(f' Detected logos: {len(annotations)} '.center(80, '-'))
    for annotation in annotations:
        entity = annotation.entity
        entity_id = entity.entity_id
        description = entity.description
        for track in annotation.tracks:
            confidence = track.confidence
            start_ms = track.segment.start_time_offset.ToMilliseconds()
            end_ms = track.segment.end_time_offset.ToMilliseconds()
            logo_frames = len(track.timestamped_objects)
            print(f'{confidence:4.0%}',
                  f'{start_ms:>7,}',
                  f'{end_ms:>7,}',
                  f'{logo_frames:>3} fr.',
                  f'{entity_id:<15}',
                  f'{description}',
                  sep=' | ')
  

Call the function:

print_detected_logos(response)

You should see something like this:

------------------------------ Detected logos: 1 -------------------------------
 92% | 150,680 | 155,720 |  43 fr. | /m/055t58       | Google Maps
 

 Add this function to print out the list of detected logo frames and bounding boxes:
 
 def print_logo_frames(response, entity_id):
    def keep_annotation(annotation):
        return annotation.entity.entity_id == entity_id

    # First result only, as a single video is processed
    annotations = response.annotation_results[0].logo_recognition_annotations
    annotations = [a for a in annotations if keep_annotation(a)]
    for annotation in annotations:
        description = annotation.entity.description
        for track in annotation.tracks:
            confidence = track.confidence
            print(f' {description},'
                  f' confidence: {confidence:.0%},'
                  f' frames: {len(track.timestamped_objects)} '
                  .center(80, '-'))
            for timestamped_object in track.timestamped_objects:
                frame_ms = timestamped_object.time_offset.ToMilliseconds()
                box = timestamped_object.normalized_bounding_box
                print(f'{frame_ms:>7,}',
                      f'({box.left:.5f}, {box.top:.5f})',
                      f'({box.right:.5f}, {box.bottom:.5f})',
                      sep=' | ')
  
Call the function with Google Map logo entity ID:

print_logo_frames(response, '/m/055t58')

You should see something like this:

------------------- Google Maps, confidence: 92%, frames: 43 -------------------
150,680 | (0.42024, 0.28633) | (0.58192, 0.64220)
150,800 | (0.41713, 0.27822) | (0.58318, 0.63556)
...
155,600 | (0.41775, 0.27701) | (0.58372, 0.63986)
155,720 | (0.41688, 0.28005) | (0.58335, 0.63954)

Comments