Working With Video Intelligence In Python Shot Changes Detect


Working With Video Intelligence In Python Shot Changes Detect

You can use the Video Intelligence API to detect shot changes in a video. A shot is a segment of the video, a series of frames with visual continuity.

Copy the following code into your IPython session:


from google.cloud import videointelligence
from google.cloud.videointelligence import enums


def detect_shot_changes(video_uri):
    video_client = videointelligence.VideoIntelligenceServiceClient()
    features = [enums.Feature.SHOT_CHANGE_DETECTION]

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

Call the function to analyze the video:

video_uri = 'gs://cloudmleap/video/next/JaneGoodall.mp4'
response = detect_shot_changes(video_uri)

Wait a moment for the video to be processed:

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

Note: If you get a PermissionDenied error (403), verify the steps followed during the Authenticate API requests step.

Check that the credentials environment variable is defined:
echo $GOOGLE_APPLICATION_CREDENTIALS
You should see the full path to your credentials file:
/home/$USER/key.json
Then, check that the credentials were created:
cat $GOOGLE_APPLICATION_CREDENTIALS
You should see something similar to:
{
"type": "service_account",
"project_id": "PROJECT_ID",
...
}
If anything is incorrect, revisit the Authenticate API requests step.

Add this function to print out the video shots:

def print_video_shots(response):
    # First result only, as a single video is processed
    shots = response.annotation_results[0].shot_annotations
    print(f' Video shots: {len(shots)} '.center(40, '-'))
    for i, shot in enumerate(shots):
        start_ms = shot.start_time_offset.ToMilliseconds()
        end_ms = shot.end_time_offset.ToMilliseconds()
        print(f'{i+1:>3}',
              f'{start_ms:>7,}',
              f'{end_ms:>7,}',
              sep=' | ')
  

Call the function:

print_video_shots(response)

You should see something like this:

----------- Video shots: 35 ------------
  1 |       0 |  12,880
  2 |  12,920 |  21,680
  3 |  21,720 |  27,880
...
 33 | 138,360 | 146,200
 34 | 146,240 | 155,760
 35 | 155,800 | 162,520
 

Comments