pythongoogle-cloud-platformvideo-intelligence-api

Google Video Intelligence API in Python: How to download response JSON?


I'm trying to use below Python documentation to use the OBJECT_TRACKING METHOD. The script works but I'm wondering how can I download the JSON. I'm just starting out in Python.

Note: This question has now been answered by Javier below. I have amended the script below to show the answer for others looking for a same solution. There is an output_uri variable and in the operation function, the output_uri parameter has been added to create a response.json in your GCS location declared earlier.

import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='your-service-credentials-json-file.json'
"""Object tracking in a video stored on GCS."""
from google.cloud import videointelligence

video_client = videointelligence.VideoIntelligenceServiceClient()
features = [videointelligence.enums.Feature.OBJECT_TRACKING]
gcs_uri = 'gs://video_intel/video1.mp4'
output_uri = 'gs://video_intel/response.json'

operation = video_client.annotate_video(input_uri=gcs_uri, features=features, 
output_uri=output_uri)
print("\nProcessing video for object annotations.")

result = operation.result(timeout=300)
print("\nFinished processing.\n")

# The first result is retrieved because a single video was processed.
object_annotations = result.annotation_results[0].object_annotations

for object_annotation in object_annotations:
    print("Entity description: {}".format(object_annotation.entity.description))
    if object_annotation.entity.entity_id:
        print("Entity id: {}".format(object_annotation.entity.entity_id))

    print(
        "Segment: {}s to {}s".format(
            object_annotation.segment.start_time_offset.seconds
            + object_annotation.segment.start_time_offset.nanos / 1e9,
            object_annotation.segment.end_time_offset.seconds
            + object_annotation.segment.end_time_offset.nanos / 1e9,
        )
    )

    print("Confidence: {}".format(object_annotation.confidence))

    # Here we print only the bounding box of the first frame in the segment
    frame = object_annotation.frames[0]
    box = frame.normalized_bounding_box
    print(
        "Time offset of the first frame: {}s".format(
            frame.time_offset.seconds + frame.time_offset.nanos / 1e9
        )
    )
    print("Bounding box position:")
    print("\tleft  : {}".format(box.left))
    print("\ttop   : {}".format(box.top))
    print("\tright : {}".format(box.right))
    print("\tbottom: {}".format(box.bottom))
    print("\n")

enter image description here


Solution

  • In the documentation of the annotate_video method you can find there's a parameter called output_uri you can use for this purpose:

    output_uri (str) – Optional. Location where the output (in JSON format) should be stored. Currently, only Google Cloud Storage URIs are supported, which must be specified in the following format: gs://bucket-id/object-id (other URI formats return google.rpc.Code.INVALID_ARGUMENT). For more information, see Request URIs.