Content moderation with AWS Rekognition
By abdulmumin yaqeen
on June 16, 2023
Content moderation with AWS Rekognition
Introduction
AWS Rekognition provides a cloud-based pre-trained AI model that uses deep learning to analyze and extract information from images, videos, and text.
With Rekognition API, it becomes much easier to build a solution that moderates content in your platforms. It offers a ready-to-use and a wide range of computer vision capabilities, including object detection, text detection, image comparison, and much more.
Rekognition also becomes handy when building an automated identity verification system, with its image comparison API, it extracts features from images and matches them against another.
Image Sampling Demo
Rekognition provides a variety of APIs that analyze images. From detecting faces, objects, and text to comparing them against one another.
Using the DetectModerationLabels API from Rekognition, we will write a lambda function that reads an image from an S3 bucket and returns details about the image. This information includes inappropriate objects in the image, bounding boxes of where each is located, and others.
We will be setting up a simple image moderation function using lambda. Almost the same approach goes for videos, but it will require you to go through all frames in the video, store and sample them one after another.
Create a lambda function.
Login to your AWS console and create a new lambda function. No additional setting is required in setting up your lambda function, except that we will be using Python as the running environment for this demo.
Attach Policies
Our Lambda function will need permission to use both S3 and Rekognition.
We will attach the following policies to our function:
- AmazonS3ReadOnlyAccess
- AmazonRekognitionReadOnlyAccess
NOTE: If you need more than Read Only Access, you can instead attach the Full Access.
Go to the Configurations tab > Permissions in your lambda function.
Click the link in the Execution role to open up the IAM Console of this function.
Click Attach Policies.
Search s3 and press Enter, then select the AmazonS3ReadOnlyAccess.
Clear filters and search rekognition and press Enter, then select the AmazonRekognitionReadOnlyAccess.
Click Add Permissions, and you can review the attached policies.
Create Bucket
Create a bucket and upload some demo images.
Code Walkthrough
Back to the function.
- We imported json and boto3.
- boto3 is the AWS SDK for Python that allows you to access AWS services.
- We used boto3 to access Rekognition.
- Using the rekognition client, we call the detect_moderation_labels and pass the bucket name and photo, which will be passed to the moderate_image function as an argument.
- We will be getting a json response which we want to display the name and confidence.
Example of response
In the lambda_handler function, we specify the name of the image and bucket and then call the moderate_image function.
Testing
To Test the Function, create a new Test with an hello-world test.