我正在尝试使用下面的 Python 文档来使用 OBJECT_TRACKING METHOD。该脚本有效,但我想知道如何下载 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")