<jupyter_start><jupyter_text>Explicit, suggestive and violent content detection using Amazon Rekognition ***This notebook provides a walkthrough of [content moderation APIs](https://docs.aws.amazon.com/rekognition/latest/dg/moderation.html) in Amazon Rekognition. You can quickly identify inappropriate content in your video and image libraries.***  Initialize stuff<jupyter_code># Initialise Notebook
import boto3
from IPython.display import Image as IImage, display
from IPython.display import HTML, display
from PIL import Image, ImageDraw, ImageFont
import time
import os
# Curent AWS Region. Use this to choose corresponding S3 bucket with sample content

mySession = boto3.session.Session()
awsRegion = mySession.region_name
# Init clients
rekognition = boto3.client('rekognition')
s3 = boto3.client('s3')
# S3 bucket that contains sample images and videos

# We are providing sample images and videos in this bucket so
# you do not have to manually download/upload test images and videos.

bucketName = "aws-workshops-" + awsRegion<jupyter_output><empty_output><jupyter_text>Content moderation in Images***<jupyter_code>imageName = "content-moderation/media/weapon.png"<jupyter_output><empty_output><jupyter_text>Call Rekognition to detect objects in the image<jupyter_code># Call Amazon Rekognition to detect unsafe content in the image
# https://docs.aws.amazon.com/rekognition/latest/dg/moderation.html

detectModerationLabelsResponse = rekognition.detect_moderation_labels(
   Image={
       'S3Object': {
           'Bucket': bucketName,
           'Name': imageName,
       }
   }
)
display(IImage(url=s3.generate_presigned_url('get_object', Params={'Bucket': bucketName, 'Key': imageName})))<jupyter_output><empty_output><jupyter_text>Review the raw JSON reponse from Rekognition<jupyter_code># Show JSON response returned by Rekognition Moderation API
# In the JSON response below, you will see Moderation Labels, confidence score and additional information.

display(detectModerationLabelsResponse)<jupyter_output><empty_output><jupyter_text>Display list of detected moderation labels<jupyter_code>for label in detectModerationLabelsResponse["ModerationLabels"]:
    print("- {} (Confidence: {})".format(label["Name"], label["Confidence"]))
    print("  - Parent: {}".format(label["ParentName"]))<jupyter_output><empty_output><jupyter_text>Content moderation in videosContent Moderation in video is an async operation. https://docs.aws.amazon.com/rekognition/latest/dg/API_StartContentModeration.html - We first start content moderation job which returns a Job Id. - We can then call `get_content_moderation` to get the job status and after job is complete, we can get moderation results. - In production use cases, you would usually use StepFucntion or SNS topic to get notified when job is complete.***<jupyter_code>videoName = "content-moderation/media/weapon.mov"<jupyter_output><empty_output><jupyter_text>Call Rekognition to start a job for content moderation<jupyter_code># Start content moderation job
startModerationLabelDetection = rekognition.start_content_moderation(
    Video={
        'S3Object': {
            'Bucket': bucketName,
            'Name': videoName,
        }
    },
)

moderationJobId = startModerationLabelDetection['JobId']
display("Job Id: {0}".format(moderationJobId))<jupyter_output><empty_output><jupyter_text>Additional (Optional) Request AttributesClientRequestTokenLhttps://docs.aws.amazon.com/rekognition/latest/dg/API_StartContentModeration.htmlrekognition-StartContentModeration-request-ClientRequestTokenJobTag:https://docs.aws.amazon.com/rekognition/latest/dg/API_StartContentModeration.htmlrekognition-StartContentModeration-request-JobTagMinConfidence:https://docs.aws.amazon.com/rekognition/latest/dg/API_StartContentModeration.htmlrekognition-StartContentModeration-request-MinConfidenceNotificationChannel:https://docs.aws.amazon.com/rekognition/latest/dg/API_StartContentModeration.htmlrekognition-StartContentModeration-request-NotificationChannel  Wait for content moderation job to complete<jupyter_code># Wait for content moderation job to complete
# In production use cases, you would usually use StepFucntion or SNS topic to get notified when job is complete.
getContentModeration = rekognition.get_content_moderation(
    JobId=moderationJobId,
    SortBy='TIMESTAMP'
)

while(getContentModeration['JobStatus'] == 'IN_PROGRESS'):
    time.sleep(5)
    print('.', end='')
 
    getContentModeration = rekognition.get_content_moderation(
    JobId=moderationJobId,
    SortBy='TIMESTAMP')
    
display(getContentModeration['JobStatus'])<jupyter_output><empty_output><jupyter_text>Review raw JSON reponse from Rekognition<jupyter_code># Show JSON response returned by Rekognition Content Moderation API
# In the JSON response below, you will see list of detected content
# For each detected object, you will see information like Timestamp

display(getContentModeration)<jupyter_output><empty_output><jupyter_text>Display list of potentially unsafe content in the video<jupyter_code>theObjects = {}

strDetail = "Moderation labels in video<br>=======================================<br>"
strOverall = "Moderation labels in the overall video:<br>=======================================<br>"

# Potentially unsafe detected in each frame
for obj in getContentModeration['ModerationLabels']:
    ts = obj ["Timestamp"]
    cconfidence = obj['ModerationLabel']["Confidence"]
    oname = obj['ModerationLabel']["Name"]
    strDetail = strDetail + "At {} ms: {} (Confidence: {})<br>".format(ts, oname, round(cconfidence,2))
    if oname in theObjects:
        cojb = theObjects[oname]
        theObjects[oname] = {"Name" : oname, "Count": 1+cojb["Count"]}
    else:
        theObjects[oname] = {"Name" : oname, "Count": 1}

# Unique objects detected in video
for theObject in theObjects:
    strOverall = strOverall + "Name: {}, Count: {}<br>".format(theObject, theObjects[theObject]["Count"])

# Display results
display(HTML(strOverall))
#display(HTML(strDetail))<jupyter_output><empty_output><jupyter_text>Show video in the player<jupyter_code># Show video in a player

s3VideoUrl = s3.generate_presigned_url('get_object', Params={'Bucket': bucketName, 'Key': videoName})

videoTag = "<video controls='controls' autoplay width='640' height='360' name='Video' src='{0}'></video>".format(s3VideoUrl)

videoui = "<table><tr><td style='vertical-align: top'>{}</td></tr></table>".format(videoTag)

display(HTML(videoui))
listui = "<table><tr><td style='vertical-align: top'>{}</td></tr></table>".format(strDetail)
display(HTML(listui))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11850845410628019, 'fraction_numerical': 0.00392512077294686, 'mean_word_length': 6.418812989921612, 'pattern_counts': {'":': 2, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '447294', 'license': 'MIT-0', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1935, 'n_words': 485, 'path': 'rekognition-notebooks/2-content-moderation.ipynb', 'repo_name': 'everestuar/amazon-rekognition-custom-labels-feedback-solution', 'size': '6624'}