Google Python 中的视频情报 API:如何下载响应 JSON?

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

我正在尝试使用下面的 Python 文档来使用 OBJECT_TRACKING 方法。该脚本有效,但我想知道如何下载 JSON。我刚开始 Python。

注:此问题现已由下面的 Javier 回答。我已经修改了下面的脚本以向其他寻求相同解决方案的人显示答案。有一个 output_uri 变量,在操作函数中,已添加 output_uri 参数以在您之前声明的 GCS 位置创建一个 response.json。

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")

annotate_video 方法的文档中,您可以找到一个名为 output_uri 的参数,您可以将其用于此目的:

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.