You can use the DetectModerationLabels operation to determine if an image contains inappropriate or offensive content. For a list of moderation labels in Amazon Rekognition, see Using the image and video moderation APIs.
Detecting inappropriate content in an image
The image must be in either a .jpg or a .png format. You can provide the input image as an image byte array (base64-encoded image bytes), or specify an Amazon S3 object. In these procedures, you upload an image (.jpg or .png) to your S3 bucket.
To run these procedures, you need to have the AWS CLI or the appropriate AWS SDK installed. For more information, see Getting started with Amazon Rekognition. The AWS account you use must have access permissions to the Amazon Rekognition API. For more information, see Actions Defined by Amazon Rekognition.
To detect moderation labels in an image (SDK)
If you haven't already:
Create or update an user with
AmazonRekognitionFullAccess
andAmazonS3ReadOnlyAccess
permissions. For more information, see Step 1: Set up an AWS account and create a User.Install and configure the AWS CLI and the AWS SDKs. For more information, see Step 2: Set up the AWS CLI and AWS SDKs.
-
Upload an image to your S3 bucket.
For instructions, see Uploading Objects into Amazon S3 in the Amazon Simple Storage Service User Guide.
Use the following examples to call the
DetectModerationLabels
operation.This example outputs detected inappropriate content label names, confidence levels, and the parent label for detected moderation labels.
Replace the values of
amzn-s3-demo-bucket
andphoto
with the S3 bucket name and the image file name that you used in step 2.//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) package aws.example.rekognition.image; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.DetectModerationLabelsRequest; import com.amazonaws.services.rekognition.model.DetectModerationLabelsResult; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.ModerationLabel; import com.amazonaws.services.rekognition.model.S3Object; import java.util.List; public class DetectModerationLabels { public static void main(String[] args) throws Exception { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectModerationLabelsRequest request = new DetectModerationLabelsRequest() .withImage(new Image().withS3Object(new S3Object().withName(photo).withBucket(bucket))) .withMinConfidence(60F); try { DetectModerationLabelsResult result = rekognitionClient.detectModerationLabels(request); List<ModerationLabel> labels = result.getModerationLabels(); System.out.println("Detected labels for " + photo); for (ModerationLabel label : labels) { System.out.println("Label: " + label.getName() + "\n Confidence: " + label.getConfidence().toString() + "%" + "\n Parent:" + label.getParentName()); } } catch (AmazonRekognitionException e) { e.printStackTrace(); } } }
DetectModerationLabels operation request
The input to DetectModerationLabels
is an image. In this example JSON
input, the source image is loaded from an Amazon S3 bucket. MinConfidence
is
the minimum confidence that Amazon Rekognition Image must have in the accuracy of the detected label
for it to be returned in the response.
{
"Image": {
"S3Object": {
"Bucket": "amzn-s3-demo-bucket",
"Name": "input.jpg"
}
},
"MinConfidence": 60
}
DetectModerationLabels operation response
DetectModerationLabels
can retrieve input images from an S3 bucket, or
you can provide them as image bytes. The following example is the response from a
call to DetectModerationLabels
.
In the following example JSON response, note the following:
-
Inappropriate Image Detection information – The example shows a list of labels for inappropriate or offensive content found in the image. The list includes the top-level label and each second-level label that are detected in the image.
Label – Each label has a name, an estimation of the confidence that Amazon Rekognition has that the label is accurate, and the name of its parent label. The parent name for a top-level label is
""
.Label confidence – Each label has a confidence value between 0 and 100 that indicates the percentage confidence that Amazon Rekognition has that the label is correct. You specify the required confidence level for a label to be returned in the response in the API operation request.
{
"ModerationLabels": [
{
"Confidence": 99.44782257080078,
"Name": "Smoking",
"ParentName": "Drugs & Tobacco Paraphernalia & Use",
"TaxonomyLevel": 3
},
{
"Confidence": 99.44782257080078,
"Name": "Drugs & Tobacco Paraphernalia & Use",
"ParentName": "Drugs & Tobacco",
"TaxonomyLevel": 2
},
{
"Confidence": 99.44782257080078,
"Name": "Drugs & Tobacco",
"ParentName": "",
"TaxonomyLevel": 1
}
],
"ModerationModelVersion": "7.0",
"ContentTypes": [
{
"Confidence": 99.9999008178711,
"Name": "Illustrated"
}
]
}