または DetectModerationLabelsAWS SDKで を使用する CLI - AWS SDK CLI コードの例

AWS Doc SDK Examples GitHub リポジトリには他にも AWS SDK例があります。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

または DetectModerationLabelsAWS SDKで を使用する CLI

以下のコード例は、DetectModerationLabels の使用方法を示しています。

詳細については、「不適切なイメージを検出する」を参照してください。

.NET
AWS SDK for .NET
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

using System; using System.Threading.Tasks; using Amazon.Rekognition; using Amazon.Rekognition.Model; /// <summary> /// Uses the Amazon Rekognition Service to detect unsafe content in a /// JPEG or PNG format image. /// </summary> public class DetectModerationLabels { public static async Task Main(string[] args) { string photo = "input.jpg"; string bucket = "bucket"; var rekognitionClient = new AmazonRekognitionClient(); var detectModerationLabelsRequest = new DetectModerationLabelsRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket, }, }, MinConfidence = 60F, }; try { var detectModerationLabelsResponse = await rekognitionClient.DetectModerationLabelsAsync(detectModerationLabelsRequest); Console.WriteLine("Detected labels for " + photo); foreach (ModerationLabel label in detectModerationLabelsResponse.ModerationLabels) { Console.WriteLine($"Label: {label.Name}"); Console.WriteLine($"Confidence: {label.Confidence}"); Console.WriteLine($"Parent: {label.ParentName}"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
  • API 詳細については、「 リファレンスDetectModerationLabels」の「」を参照してください。 AWS SDK for .NET API

CLI
AWS CLI

画像内の安全でないコンテンツを検出するには

次の detect-moderation-labels コマンドは、Amazon S3 バケットに保存されている指定された画像の安全でないコンテンツを検出します。

aws rekognition detect-moderation-labels \ --image "S3Object={Bucket=MyImageS3Bucket,Name=gun.jpg}"

出力:

{ "ModerationModelVersion": "3.0", "ModerationLabels": [ { "Confidence": 97.29618072509766, "ParentName": "Violence", "Name": "Weapon Violence" }, { "Confidence": 97.29618072509766, "ParentName": "", "Name": "Violence" } ] }

詳細については、「Amazon Rekognition 開発者ガイド」の「不適切なイメージの検出」を参照してください。

  • API 詳細については、「 コマンドリファレンスDetectModerationLabels」の「」を参照してください。 AWS CLI

Java
SDK for Java 2.x
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.rekognition.RekognitionClient; import software.amazon.awssdk.services.rekognition.model.RekognitionException; import software.amazon.awssdk.services.rekognition.model.Image; import software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsRequest; import software.amazon.awssdk.services.rekognition.model.DetectModerationLabelsResponse; import software.amazon.awssdk.services.rekognition.model.ModerationLabel; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectModerationLabels { public static void main(String[] args) { final String usage = """ Usage: <sourceImage> Where: sourceImage - The path to the image (for example, C:\\AWS\\pic1.png).\s """; if (args.length < 1) { System.out.println(usage); System.exit(1); } String sourceImage = args[0]; Region region = Region.US_EAST_1; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .build(); detectModLabels(rekClient, sourceImage); rekClient.close(); } public static void detectModLabels(RekognitionClient rekClient, String sourceImage) { try { InputStream sourceStream = new FileInputStream(sourceImage); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); Image souImage = Image.builder() .bytes(sourceBytes) .build(); DetectModerationLabelsRequest moderationLabelsRequest = DetectModerationLabelsRequest.builder() .image(souImage) .minConfidence(60F) .build(); DetectModerationLabelsResponse moderationLabelsResponse = rekClient .detectModerationLabels(moderationLabelsRequest); List<ModerationLabel> labels = moderationLabelsResponse.moderationLabels(); System.out.println("Detected labels for image"); for (ModerationLabel label : labels) { System.out.println("Label: " + label.name() + "\n Confidence: " + label.confidence().toString() + "%" + "\n Parent:" + label.parentName()); } } catch (RekognitionException | FileNotFoundException e) { e.printStackTrace(); System.exit(1); } } }
  • API 詳細については、「 リファレンスDetectModerationLabels」の「」を参照してください。 AWS SDK for Java 2.x API

Kotlin
SDK Kotlin 用
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

suspend fun detectModLabels(sourceImage: String) { val myImage = Image { this.bytes = (File(sourceImage).readBytes()) } val request = DetectModerationLabelsRequest { image = myImage minConfidence = 60f } RekognitionClient { region = "us-east-1" }.use { rekClient -> val response = rekClient.detectModerationLabels(request) response.moderationLabels?.forEach { label -> println("Label: ${label.name} - Confidence: ${label.confidence} % Parent: ${label.parentName}") } } }
  • API 詳細については、Kotlin リファレンス DetectModerationLabelsの「」の「」を参照してください。 AWS SDK API

Python
SDK for Python (Boto3)
注記

については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

class RekognitionImage: """ Encapsulates an Amazon Rekognition image. This class is a thin wrapper around parts of the Boto3 Amazon Rekognition API. """ def __init__(self, image, image_name, rekognition_client): """ Initializes the image object. :param image: Data that defines the image, either the image bytes or an Amazon S3 bucket and object key. :param image_name: The name of the image. :param rekognition_client: A Boto3 Rekognition client. """ self.image = image self.image_name = image_name self.rekognition_client = rekognition_client def detect_moderation_labels(self): """ Detects moderation labels in the image. Moderation labels identify content that may be inappropriate for some audiences. :return: The list of moderation labels found in the image. """ try: response = self.rekognition_client.detect_moderation_labels( Image=self.image ) labels = [ RekognitionModerationLabel(label) for label in response["ModerationLabels"] ] logger.info( "Found %s moderation labels in %s.", len(labels), self.image_name ) except ClientError: logger.exception( "Couldn't detect moderation labels in %s.", self.image_name ) raise else: return labels
  • API 詳細については、DetectModerationLabels「 for AWS SDK Python (Boto3) APIリファレンス」の「」を参照してください。