Introduction
Images abundant on web platforms in today's digital environment. Images can be an effective means of expression and communication, but they can also be used to spread harmful content such hate speech, violence, and nudity. Image moderation is useful in this situation. Automatic picture analysis is used by image moderation systems to find and mark potentially offensive or dangerous content.
This post will discuss how to use AWS Rekognition, Nest.js, and React to create a reliable image moderation system.Conclusion
Development
We created a demo application to demonstrate the capabilities of AWS Rekognition. A React frontend serves as the user interface in this demo, while a Nest.js backend API manages the laborious task of content analysis.
For this demo, we will use this GitHub repository where you can set the settings such as API Key, connection string, etc.
Prerequisites:
- AWS Credits
- Node.js
- MongoDB database
First, we need to create a S3 Bucket and enable the public access. After that, create a folder called images.
Once it's created, add the following Bucket policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<YOUR_BUCKET_NAME>/images/*"
}
]
}
Then go to Security Credentials and create two users (in the same AWS Region) with the following permissions:
- User 1 (AWS-Rekognition): AmazonRekognitionFullAccess and AmazonS3ReadOnlyAccess
- User 2 (AWS-S3): AmazonS3FullAccess
Generate an access key and secret key for both and paste them in the .env file from the API project.
MONGO_URI=
AWS_S3_ACCESS_KEY=
AWS_S3_SECRET_ACCESS_KEY=
AWS_S3_REGION=
AWS_S3_BUCKET_NAME=
AWS_S3_BUCKET_FOLDER=
AWS_REKOGNITION_REGION=
AWS_REKOGNITION_ACCESS_KEY=
AWS_REKOGNITION_SECRET_ACCESS_KEY=
Create a MongoDB and add the connection string in the MONGO_URI key.
In the UI project you need to add base url for the API in REACT_APP_API_BASE_URL key.
Rekognition Integration:
Use the AWS SDK to call the Rekognition DetectModerationLabels API. This API analyzes the image and returns a list of detected labels, including confidence scores.
Define rules based on the detected labels and confidence scores to determine if an image is flagged as inappropriate. Check the constants/category-thesholds.contstant.ts file in API project to see the thresholds per category.
See this documentation to know more about the label categories.
This is an example for the response returned from AWS Rekognition:
{
"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"
}
]
}
To test the demo, you submit some images.
If the image content is categorized as harmful (based on threshold categories) in any category, it will be blurred by default. But you can turn off the blur by clicking the button displayed in the center for each image.
Images are rejected if the rejection threshold is equal to or greater than the confidence score of a category.
Conclusion
A strong and effective picture moderation system can be created by utilizing the capabilities of AWS Rekognition, Nest.js, and React. By proactively identifying and eliminating dangerous information, this approach can assist online platforms in making the internet a safer and more enjoyable place for all users.
This is a basic example, and you can further enhance this system by adding features like:
- Customizable moderation rules: Allow administrators to define and adjust moderation rules based on their specific needs.
- Human review: Implement a human review process for images that are flagged by the system.
Thanks for reading
Thank you very much for reading. I hope you found this article interesting and may be useful in the future. If you have any questions or ideas you need to discuss, it will be a pleasure to collaborate and exchange knowledge.
Top comments (0)