Created
July 22, 2024 09:56
-
-
Save SamWSoftware/bcfc31034499d169f7b7afd6db0b4499 to your computer and use it in GitHub Desktop.
Function to run content moderation on images using Rekognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { RekognitionClient, DetectModerationLabelsCommand } from "@aws-sdk/client-rekognition"; | |
const moderationCategories = [ | |
"Explicit Nudity", | |
"Explicit Sexual Activity", | |
"Hate Symbols", | |
]; | |
const runModeration = async (Body: ArrayBuffer) => { | |
const command = new DetectModerationLabelsCommand({ | |
Image: { Bytes: new Uint8Array(Body) }, | |
}); | |
const response = await rekognition.send(command); | |
const labels = response.ModerationLabels; | |
console.log("Moderation response Labels", labels); | |
const rejectImage = labels.some( | |
(label) => | |
moderationCategories.includes(label.Name) || | |
moderationCategories.includes(label.ParentName) | |
); | |
if (rejectImage) { | |
throw new Error("Image contains explicit content"); | |
} | |
return; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment