使用videointelligence_v1p3beta1 API遇403权限错误求助
谷歌云Video Intelligence流式标签检测API权限问题(403错误)
使用Google Cloud的videointelligence_v1p3beta1 API中的流式标签检测(STREAMING_LABEL_DETECTION)功能时,触发「403 The caller does not have permission」权限错误,但调用普通版本Video Intelligence API的内容检测功能完全正常,需要解决该权限问题。
可正常运行的代码
def analyze_explicit_content_file(self, path): from google.cloud import videointelligence # [START video_analyze_explicit_content] """Detects explicit content from the GCS path to a video.""" video_client = videointelligence.VideoIntelligenceServiceClient() features = [videointelligence.Feature.EXPLICIT_CONTENT_DETECTION] with io.open(path, "rb") as movie: input_content = movie.read() operation = video_client.annotate_video( request={"features": features, "input_content": input_content} ) print("\nProcessing video for explicit content annotations:") result = operation.result(timeout=90) print("\nFinished processing.") # Retrieve first result because a single video was processed for frame in result.annotation_results[0].explicit_annotation.frames: likelihood = videointelligence.Likelihood(frame.pornography_likelihood) frame_time = frame.time_offset.seconds + frame.time_offset.microseconds / 1e6 print("Time: {}s".format(frame_time)) print("\tpornography: {}".format(likelihood.name)) # [END video_analyze_explicit_content]
无法正常运行的代码
def detect_labels_streaming_file(self, path): # [START video_streaming_label_detection_beta] from google.cloud import videointelligence_v1p3beta1 as videointelligence # path = 'path_to_file' client = videointelligence.StreamingVideoIntelligenceServiceClient() # Set streaming config. config = videointelligence.StreamingVideoConfig( feature=(videointelligence.StreamingFeature.STREAMING_LABEL_DETECTION) ) # config_request should be the first in the stream of requests. config_request = videointelligence.StreamingAnnotateVideoRequest( video_config=config ) # Set the chunk size to 5MB (recommended less than 10MB). chunk_size = 5 * 1024 * 1024 # Load file content. stream = [] with io.open(path, "rb") as video_file: while True: data = video_file.read(chunk_size) if not data: break stream.append(data) def stream_generator(): yield config_request for chunk in stream: yield videointelligence.StreamingAnnotateVideoRequest(input_content=chunk) requests = stream_generator() # streaming_annotate_video returns a generator. # The default timeout is about 300 seconds. # To process longer videos it should be set to # larger than the length (in seconds) of the stream. responses = client.streaming_annotate_video(requests, timeout=600) # Each response corresponds to about 1 second of video. for response in responses: # Check for errors. if response.error.message: print(response.error.message) break label_annotations = response.annotation_results.label_annotations # label_annotations could be empty if not label_annotations: continue for annotation in label_annotations: # Each annotation has one frame, which has a timeoffset. frame = annotation.frames[0] time_offset = ( frame.time_offset.seconds + frame.time_offset.microseconds / 1e6 ) description = annotation.entity.description confidence = annotation.frames[0].confidence # description is in Unicode print( "{}s: {} (confidence: {})".format(time_offset, description, confidence) ) # [END video_streaming_label_detection_beta]
解决建议
- 核对权限差异:流式Video Intelligence API需要
videointelligence.streamingAnnotateVideo权限,确保服务账号已被授予包含该权限的角色(如roles/videointelligence.editor),普通API的权限不覆盖流式接口。 - 确认API启用状态:检查GCP控制台中
Cloud Video Intelligence API是否已启用,虽然普通接口可用,但需确保整个API服务处于激活状态。 - 验证凭据一致性:确认两段代码使用的是同一套认证凭据,比如环境变量
GOOGLE_APPLICATION_CREDENTIALS指向的服务账号密钥文件无变更。 - 检查配额状态:虽然403属于权限错误,但也需确认流式API的调用配额未耗尽,避免因配额限制触发的权限类提示。
内容的提问来源于stack exchange,提问作者gluck0101
相关产品推荐
相关产品推荐

