不適切なイメージの検出 - Amazon Rekognition

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

不適切なイメージの検出

DetectModerationLabels オペレーションを使用して、イメージに不適切または不快なコンテンツが含まれているかどうかを判断できます。Amazon Rekognition のモデレーションラベルのリストについては、イメージおよびビデオのモデレーション API の使用 を参照してください。

イメージ内の不適切なコンテンツの検出

イメージは、.jpg または .png 形式である必要があります。入力イメージとして、イメージのバイト配列 (base64 でエンコードされたイメージのバイト) を指定するか、Amazon S3 オブジェクトを指定することができます。以下の手順では、イメージ (.jpg または .png) を S3 バケットにアップロードします。

これらの手順を実行するには、 AWS CLI または適切な AWS SDK がインストールされている必要があります。詳細については、「Amazon Rekognition の開始方法」を参照してください。使用する AWS アカウントには、Amazon Rekognition API へのアクセス権限が必要です。詳細については、[Amazon Rekognition で定義されるアクション]を参照してください。

イメージでモデレーションラベルを検出するには (SDK)
  1. まだ実行していない場合:

    1. AmazonRekognitionFullAccessAmazonS3ReadOnlyAccess のアクセス権限を持つユーザーを作成または更新します。詳細については、「ステップ 1: AWS アカウントを設定してユーザーを作成する」を参照してください。

    2. と AWS SDKsをインストール AWS CLI して設定します。詳細については、「ステップ 2: AWS CLI と AWS SDKsを設定する」を参照してください。

  2. イメージを S3 バケットにアップロードします。

    手順については、「Amazon Simple Storage Service ユーザーガイド」の「Amazon S3 へのオブジェクトのアップロード」を参照してください。

  3. 以下の例を使用して、DetectModerationLabels オペレーションを呼び出します。

    Java

    この例では、検出された安全でないコンテンツのラベル名、信頼度、検出されたモデレーションラベルの親ラベルを出力します。

    bucketphoto の値は、ステップ 2 で使用した S3 バケット名とイメージファイル名に置き換えます。

    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) package aws.example.rekognition.image; import com.amazonaws.services.rekognition.AmazonRekognition; import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder; import com.amazonaws.services.rekognition.model.AmazonRekognitionException; import com.amazonaws.services.rekognition.model.DetectModerationLabelsRequest; import com.amazonaws.services.rekognition.model.DetectModerationLabelsResult; import com.amazonaws.services.rekognition.model.Image; import com.amazonaws.services.rekognition.model.ModerationLabel; import com.amazonaws.services.rekognition.model.S3Object; import java.util.List; public class DetectModerationLabels { public static void main(String[] args) throws Exception { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognition rekognitionClient = AmazonRekognitionClientBuilder.defaultClient(); DetectModerationLabelsRequest request = new DetectModerationLabelsRequest() .withImage(new Image().withS3Object(new S3Object().withName(photo).withBucket(bucket))) .withMinConfidence(60F); try { DetectModerationLabelsResult result = rekognitionClient.detectModerationLabels(request); List<ModerationLabel> labels = result.getModerationLabels(); System.out.println("Detected labels for " + photo); for (ModerationLabel label : labels) { System.out.println("Label: " + label.getName() + "\n Confidence: " + label.getConfidence().toString() + "%" + "\n Parent:" + label.getParentName()); } } catch (AmazonRekognitionException e) { e.printStackTrace(); } } }
    Java V2

    このコードは AWS Documentation SDK サンプル GitHub リポジトリから取得されます。詳しい事例はこちらです。

    //snippet-start:[rekognition.java2.recognize_video_text.import] //snippet-start:[rekognition.java2.detect_mod_labels.import] import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; 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; //snippet-end:[rekognition.java2.detect_mod_labels.import] /** * 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 ModerateLabels { public static void main(String[] args) { final String usage = "\n" + "Usage: " + " <sourceImage>\n\n" + "Where:\n" + " sourceImage - The path to the image (for example, C:\\AWS\\pic1.png). \n\n"; if (args.length < 1) { System.out.println(usage); System.exit(1); } String sourceImage = args[0]; Region region = Region.US_WEST_2; RekognitionClient rekClient = RekognitionClient.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create("profile-name")) .build(); detectModLabels(rekClient, sourceImage); rekClient.close(); } // snippet-start:[rekognition.java2.detect_mod_labels.main] 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); } } // snippet-end:[rekognition.java2.detect_mod_labels.main]
    AWS CLI

    この AWS CLI コマンドは、 CLI オペレーションの JSON detect-moderation-labels 出力を表示します。

    bucketinput.jpg は、ステップ 2 で使用した S3 バケット名とイメージファイル名に置き換えます。profile_name の値を自分のデベロッパープロファイル名に置き換えます。アダプターを使用するには、project-version パラメータにプロジェクトバージョンの ARN を指定します。

    aws rekognition detect-moderation-labels --image "{S3Object:{Bucket:<bucket-name>,Name:<image-name>}}" \ --profile profile-name \ --project-version "ARN"

    Windows デバイスで CLI にアクセスする場合は、パーサーエラーの発生に対処するため、一重引用符の代わりに二重引用符を使用し、内側の二重引用符をバックスラッシュ (\) でエスケープします。例として以下を参照してください。

    aws rekognition detect-moderation-labels --image "{\"S3Object\":{\"Bucket\":\"bucket-name\",\"Name\":\"image-name\"}}" \ --profile profile-name
    Python

    この例では、検出された安全でないコンテンツのラベル名、信頼度、検出されたモデレーションラベルの親ラベルを出力します。

    関数 main で、bucketphoto の値は、ステップ 2 で使用した S3 バケット名とイメージファイル名に置き換えます。Rekognition セッションを作成する行の profile_name の値を、自分のデベロッパープロファイル名に置き換えます。

    #Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) import boto3 def moderate_image(photo, bucket): session = boto3.Session(profile_name='profile-name') client = session.client('rekognition') response = client.detect_moderation_labels(Image={'S3Object':{'Bucket':bucket,'Name':photo}}) print('Detected labels for ' + photo) for label in response['ModerationLabels']: print (label['Name'] + ' : ' + str(label['Confidence'])) print (label['ParentName']) return len(response['ModerationLabels']) def main(): photo='image-name' bucket='bucket-name' label_count=moderate_image(photo, bucket) print("Labels detected: " + str(label_count)) if __name__ == "__main__": main()
    .NET

    この例では、検出された安全でないコンテンツのラベル名、信頼度、検出されたモデレーションラベルの親ラベルを出力します。

    bucketphoto の値は、ステップ 2 で使用した S3 バケット名とイメージファイル名に置き換えます。

    //Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. //PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.) using System; using Amazon.Rekognition; using Amazon.Rekognition.Model; public class DetectModerationLabels { public static void Example() { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); DetectModerationLabelsRequest detectModerationLabelsRequest = new DetectModerationLabelsRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket }, }, MinConfidence = 60F }; try { DetectModerationLabelsResponse detectModerationLabelsResponse = rekognitionClient.DetectModerationLabels(detectModerationLabelsRequest); Console.WriteLine("Detected labels for " + photo); foreach (ModerationLabel label in detectModerationLabelsResponse.ModerationLabels) Console.WriteLine("Label: {0}\n Confidence: {1}\n Parent: {2}", label.Name, label.Confidence, label.ParentName); } catch (Exception e) { Console.WriteLine(e.Message); } } }

DetectModerationLabels オペレーションリクエスト

DetectModerationLabels への入力はイメージです。以下の JSON 入力の例では、ソースイメージを Amazon S3 バケットからロードします。Amazon Rekognition は、必要最小限のMinConfidence の信頼度であり、これ以上の精度の検出済みラベルがレスポンスで返されます。

{ "Image": { "S3Object": { "Bucket": "bucket", "Name": "input.jpg" } }, "MinConfidence": 60 }

DetectModerationLabels オペレーションレスポンス

DetectModerationLabels は、S3 バケットから入力イメージを取得できます。または、これらをイメージのバイトとしてユーザーが提供できます。DetectModerationLabels への呼び出しのレスポンス例を以下に示します。

ここで示す JSON レスポンスの例では、以下の点に留意してください。

  • 不適切なイメージ検出情報 — この例では、イメージで検出された不適切または不快なコンテンツのラベルが一覧表示されます。この一覧には、イメージで検出された最上位ラベルと第 2 レベルの各ラベルが表示されます。

    ラベル – ラベルごとに、名前、ラベルの精度を示す Amazon Rekognition の推定信頼度、および親ラベルの名前があります。最上位ラベルの親の名前は "" です。

    ラベルの信頼度 – 各ラベルには0から100の信頼度値があり、 Amazon Rekognition がそのラベルが正しいという確信度を示しています。レスポンスで返させるラベルの必要な信頼度は、API オペレーションリクエストで指定できます。

{ "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" } ] }