Skip to content

Instantly share code, notes, and snippets.

@SamWSoftware
Created July 22, 2024 09:56
Show Gist options
  • Save SamWSoftware/bcfc31034499d169f7b7afd6db0b4499 to your computer and use it in GitHub Desktop.
Save SamWSoftware/bcfc31034499d169f7b7afd6db0b4499 to your computer and use it in GitHub Desktop.
Function to run content moderation on images using Rekognition
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