关于Video Intelligence API标签片段时间及标签检测教程的技术咨询
获取Video Intelligence API的标签片段时间
嘿,我来帮你搞定标签片段时间的获取问题!你提到的Label Detection教程只展示了视频级标注,要拿到具体的时间片段,咱们得深入到API返回的结构里挖一挖。
首先,先理清楚返回数据的层级:
- 你拿到的
AnnotateVideoResponse里,annotationResults列表的第一个元素(因为只传了一个视频)包含所有标注结果 - 其中的
segmentLabelAnnotations是标签集合,每个标签对象里的segments数组,就是这个标签出现的所有时间片段的地方
给你调整后的代码逻辑(和教程风格对齐,以Python为例):
# 假设已经成功调用API并拿到response对象 annotation_result = response.annotation_results[0] # 遍历所有检测到的标签 for segment_label in annotation_result.segment_label_annotations: print(f"检测到标签: {segment_label.entity.description}") # 检查该标签是否有时间片段标注 if segment_label.segments: # 遍历每个时间片段 for segment in segment_label.segments: # 把API返回的Duration类型转换成易读的秒数 start_time = segment.segment.start_time_offset.seconds + segment.segment.start_time_offset.nanos / 1e9 end_time = segment.segment.end_time_offset.seconds + segment.segment.end_time_offset.nanos / 1e9 # 获取该片段的置信度 confidence = segment.confidence print(f" 出现时间段: {start_time:.2f}s -> {end_time:.2f}s,置信度: {confidence:.2f}") else: print(" 仅视频级全局标注,无具体时间片段")
几个关键细节要注意:
- API返回的时间偏移是
Duration类型,包含秒和纳秒,需要转换成统一的秒数格式才好理解 - 每个时间片段都带
confidence字段,代表这个标签在该时间段内的识别准确率(0-1之间) - 部分标签可能只有全局的视频级标注,没有细分的时间片段,所以加个判断避免报错
这样就能轻松获取到每个标签出现的具体时间段啦!
内容的提问来源于stack exchange,提问作者ray an
相关产品推荐
相关产品推荐

