Working With Video Intelligence In Python Detect explicit content
You can use the Video Intelligence API to detect explicit content in a video. Explicit content is adult content generally inappropriate for those under 18 years of age and includes, but is not limited to, nudity, sexual activities, and pornography. Detection is performed based on per-frame visual signals only (audio is not used). The response includes likelihood values ranging from VERY_UNLIKELY to VERY_LIKELY.
Copy the following code into your IPython session:
from google.cloud import videointelligencefrom google.cloud.videointelligence import enums, typesdef detect_explicit_content(video_uri, segments=None):video_client = videointelligence.VideoIntelligenceServiceClient()features = [enums.Feature.EXPLICIT_CONTENT_DETECTION]context = types.VideoContext(segments=segments)print(f'Processing video "{video_uri}"...')operation = video_client.annotate_video(input_uri=video_uri,features=features,video_context=context,)return operation.result()
Take a moment to study the code and see how it uses the annotate_video client library method with the EXPLICIT_CONTENT_DETECTION parameter to analyze a video and detect explicit content.
Call the function to analyze the first 10 seconds of the video:
video_uri = 'gs://cloudmleap/video/next/JaneGoodall.mp4'segment = types.VideoSegment()segment.start_time_offset.FromSeconds(0)segment.end_time_offset.FromSeconds(10)response = detect_explicit_content(video_uri, [segment])
Wait a moment for the video to be processed:
Processing video "gs://cloudmleap/video/next/JaneGoodall.mp4"...
Add this function to print out the different likelihood counts:
def print_explicit_content(response):from collections import Counter# First result only, as a single video is processedframes = response.annotation_results[0].explicit_annotation.frameslikelihood_counts = Counter([f.pornography_likelihood for f in frames])print(f' Explicit content frames: {len(frames)} '.center(40, '-'))for likelihood in enums.Likelihood:print(f'{likelihood.name:<22}: {likelihood_counts[likelihood]:>3}'
Call the function:
print_explicit_content(response)
You should see something like this:
----- Explicit content frames: 10 ------
LIKELIHOOD_UNSPECIFIED: 0
VERY_UNLIKELY : 10
UNLIKELY : 0
POSSIBLE : 0
LIKELY : 0
VERY_LIKELY : 0
Add this function to print out frame details:
def print_frames(response, likelihood):# First result only, as a single video is processedframes = response.annotation_results[0].explicit_annotation.framesframes = [f for f in frames if f.pornography_likelihood == likelihood]print(f' {likelihood.name} frames: {len(frames)} '.center(40, '-'))for frame in frames:print(f'{frame.time_offset.ToTimedelta()}')
Call the function:
print_frames(response, enums.Likelihood.VERY_UNLIKELY)
You should see something like this:
------- VERY_UNLIKELY frames: 10 -------
0:00:00.365992
0:00:01.279206
0:00:02.268336
0:00:03.289253
0:00:04.400163
0:00:05.291547
0:00:06.449558
0:00:07.452751
0:00:08.577405
0:00:09.554514
Comments
Post a Comment