def streaming_annotate(stream_file): """Annotate a local video file through streaming API.""" client = videointelligence.StreamingVideoIntelligenceServiceClient() # Set the chunk size to 5MB (recommended less than 10MB). chunk_size = 5 * 1024 * 1024 # Open file. with open(stream_file) as video_file: requests = ( types.StreamingAnnotateVideoRequest(input_content=chunk) for chunk in stream(video_file, chunk_size)) # Set streaming config. config = types.StreamingVideoConfig( feature=enums.StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION) config_request = types.StreamingAnnotateVideoRequest(video_config=config) # streaming_annotate_video returns a generator. # timeout argument specifies the maximum allowable time duration between # the time that the last packet is sent to Google video intelligence API # and the time that an annotation result is returned from the API. # timeout argument is represented in number of seconds. responses = client.streaming_annotate_video( config_request, requests, timeout=10800) print('\nReading response.') # Retrieve results from the response generator. for response in responses: for frame in response.annotation_results.explicit_annotation.frames: likelihood = enums.Likelihood(frame.pornography_likelihood) frame_time = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 print('Time: {}s'.format(frame_time)) print('\tpornography: {}'.format(likelihood.name))
def analyze_explicit_content(path): # [START video_analyze_explicit_content] """ Detects explicit content from the GCS path to a video. """ video_client = videointelligence.VideoIntelligenceServiceClient() features = [videointelligence.enums.Feature.EXPLICIT_CONTENT_DETECTION] operation = video_client.annotate_video(path, features=features) print('\nProcessing video for explicit content annotations:') result = operation.result(timeout=90) print('\nFinished processing.') # first result is retrieved because a single video was processed for frame in result.annotation_results[0].explicit_annotation.frames: likelihood = enums.Likelihood(frame.pornography_likelihood) frame_time = frame.time_offset.seconds + frame.time_offset.nanos / 1e9 print('Time: {}s'.format(frame_time)) print('\tpornography: {}'.format(likelihood.name))