本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
偵測不適當的映像
您可以使用 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)
如果您尚未執行:
建立或更新具有 AmazonRekognitionFullAccess
和 AmazonS3ReadOnlyAccess
權限的使用者。如需詳細資訊,請參閱步驟 1:設定AWS帳戶並建立使用者。
安裝和設定 AWS CLI 和 AWS SDKs。如需詳細資訊,請參閱步驟 2:設定 AWS CLI 以及 AWS SDKs。
-
將映像上傳至您的 S3 儲存貯體。
如需指示說明,請參閱《Amazon Simple Storage Service 使用者指南》中的上傳物件至 Amazon S3。
使用下列範例來呼叫 DetectModerationLabels
操作。
- Java
此範例輸出偵測到的不適當內容標籤名稱、信心度以及偵測到的管制標籤的父標籤。
將 amzn-s3-demo-bucket
與 photo
的值取代為您在步驟 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 文件開發套件範例 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 detect-moderation-labels
操作的 JSON 輸出。
將 amzn-s3-demo-bucket
與 input.jpg
取代為您在步驟 2 中所使用的 S3 儲存貯體名稱與映像檔案名稱。使用您開發人員設定檔的名稱取代 profile_name
的值。若要使用轉接器,請將專案版本的 ARN 提供給 project-version
參數。
aws rekognition detect-moderation-labels --image "{S3Object:{Bucket:<amzn-s3-demo-bucket
>,Name:<image-name
>}}" \
--profile profile-name
\
--project-version "ARN
"
如果您在 Windows 裝置上存取 CLI,請使用雙引號而非單引號,並以反斜線 (即\) 替代內部雙引號,以解決您可能遇到的任何剖析器錯誤。例如,請參閱下列內容:
aws rekognition detect-moderation-labels --image "{\"S3Object\":{\"Bucket\":\"amzn-s3-demo-bucket\",\"Name\":\"image-name\"}}" \
--profile profile-name
- Python
此範例輸出偵測到的不適當或令人反感的內容標籤名稱、信心度以及偵測到的不安全內容標籤的父標籤。
在函數 main
中,將 amzn-s3-demo-bucket
與 photo
的值取代為您在步驟 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='amzn-s3-demo-bucket'
label_count=moderate_image(photo, bucket)
print("Labels detected: " + str(label_count))
if __name__ == "__main__":
main()
- .NET
此範例輸出偵測到的令人反感的內容標籤名稱、信心度以及偵測到的管制標籤的父標籤。
將 amzn-s3-demo-bucket
與 photo
的值取代為您在步驟 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 = "amzn-s3-demo-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 儲存貯體載入。MinConfidence
是在偵測到的標籤的準確性中 Amazon Rekognition Image 必須具有的最低可信度,以便在回應中傳回。
{
"Image": {
"S3Object": {
"Bucket": "amzn-s3-demo-bucket",
"Name": "input.jpg"
}
},
"MinConfidence": 60
}
DetectModerationLabel 操作回應
DetectModerationLabels
可從 S3 儲存貯體擷取輸入映像,您也可以用映像位元組的方式提供輸入映像。下列範例是來自 DetectModerationLabels
呼叫的回應。
在下列範例 JSON 回應中,請注意以下事項:
-
不適當的映像偵測資訊:此範例會顯示映像中發現不當或冒犯性內容的標籤清單。此清單包含最上層標籤與映像中偵測到的每個第二層標籤。
標籤:每個標籤都具有名稱、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"
}
]
}