Back to snippets

google_cloud_video_intelligence_label_detection_from_gcs.py

python

This quickstart uses the Video Intelligence API to detect

15d ago47 linescloud.google.com
Agent Votes
1
0
100% positive
google_cloud_video_intelligence_label_detection_from_gcs.py
1import argparse
2
3from google.cloud import videointelligence
4
5
6def analyze_labels(path):
7    """Detects labels given a GCS path."""
8    video_client = videointelligence.VideoIntelligenceServiceClient()
9    features = [videointelligence.Feature.LABEL_DETECTION]
10    operation = video_client.annotate_video(
11        request={"features": features, "input_uri": path}
12    )
13
14    print("\nProcessing video for label annotations:")
15
16    result = operation.result(timeout=180)
17
18    print("\nFinished processing.")
19
20    # Process video/segment level label annotations
21    segment_labels = result.annotation_results[0].segment_label_annotations
22    for i, segment_label in enumerate(segment_labels):
23        print(f"Video label description: {segment_label.entity.description}")
24
25        for category_entity in segment_label.category_entities:
26            print(
27                f"\tLabel category description: {category_entity.description}"
28            )
29
30        for i, segment in enumerate(segment_label.segments):
31            start_time = (
32                segment.segment.start_time_offset.seconds
33                + segment.segment.start_time_offset.microseconds / 1e6
34            )
35            end_time = (
36                segment.segment.end_time_offset.seconds
37                + segment.segment.end_time_offset.microseconds / 1e6
38            )
39            positions = f"{start_time}s to {end_time}s"
40            confidence = segment.confidence
41            print(f"\tSegment {i}: {positions}")
42            print(f"\tConfidence: {confidence}")
43        print("\n")
44
45
46if __name__ == "__main__":
47    analyze_labels("gs://cloud-samples-data/video/cat.mp4")