Updated on 2024-11-04 GMT+08:00

Image Moderation (V2)

Function

This API analyzes and identifies whether the uploaded images contain pornographic elements or terrorism-related content, and returns the result to you.

  1. By default, the maximum number of concurrent API calls is 10. To increase concurrency, contact Huawei technical support.
  2. You can configure moderation rules to detect pornographic content in different scenarios. For details, see Configuring Moderation Rules.

URI

POST /v2/{project_id}/moderation/image

Table 1 Path parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token

Used to obtain the permission to operate APIs. For details about how to obtain the token, see Authentication. The value of X-Subject-Token in the response header is the token.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

url

No

String

Configure either this parameter or image.

Indicates the URL of an image. The options are as follows:

  • HTTP/HTTPS URLs on the public network
  • HUAWEI CLOUD OBS URLs. To use OBS data, authorization is required, including service authorization, temporary authorization, and anonymous public authorization. For details, see Configuring the Access Permission of OBS.
NOTE:

The API response time depends on the image download time. If the image download takes a long time, the API call will fail. Ensure that the storage service where the image to be detected resides is stable and reliable. You are advised to use HUAWEI CLOUD OBS.

image

No

String

Configure either this parameter or url.

Indicates the Base64 character string converted from the image. The size cannot exceed 10 MB.

The supported image formats include JPEG, PNG, BMP, WEBP, and GIF.

moderation_rule

No

String

Indicates the name of the moderation rule. The default rule is used by default.

NOTE:

For details about how to create and use moderation rules, see Configuring Moderation Rules.

categories

No

Array of strings

Indicates the detection scenario. The following scenarios are supported:

  • porn: pornographic elements

You can set the parameter to the preceding values to check the corresponding elements.

NOTE:

The number of detection times in each scenario is calculated by category.

threshold

No

Float

Indicates the threshold for filtering the detection results. Only the results whose confidence score is greater than or equal to the threshold are displayed in the detail list. The value ranges from 0 to 1. If this parameter is left unspecified, the default values of different scenarios are used.

If there is no special requirement, do not pass this parameter, or leave this parameter unspecified as shown in the example request.

NOTE:
  1. If the maximum confidence score of a specific detection scenario is smaller than the value of threshold, the detail list is empty. If the value of threshold is too small, too many results are displayed in the detail list.
  2. The threshold parameter does not support filtering of the porn scenario.
  3. The threshold parameter does not affect the suggestion in the response.

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

result

ImageDetectionResultBody object

Indicates the calling result of a successful API call.

This parameter is not included when the API fails to be called.

Table 5 ImageDetectionResultBody parameters

Parameter

Type

Description

detail

ImageDetectionResultDetail object

Indicates a list of detection results of a specific scenario. . porn lists the detection results of pornographic elements. If the maximum confidence score of a specific detection scenario is smaller than the value of threshold, the detail list is empty.

suggestion

String

Indicates whether the check is passed.

  • block: indicates that sensitive information is detected and the information fails to pass the check.
  • pass: indicates that no sensitive information is detected and the information passes the check.
  • review: indicates that the image needs to be manually reviewed.
NOTE:
  1. When multiple scenarios are detected at the same time, the value of suggestion is subject to the scenario where sensitive information is most likely to be included. That is, if a block occurs in any scenario, the value of suggestion is block. If all scenarios pass the check, the value of suggestion is pass. In addition, if manual review is required in any scenario, the value of suggestion is review.
  2. This service does not save your API calling results (such as historical block information). You are advised to retain the logs of API calling results.

category_suggestions

Map<String,String>

Indicates the check result of each scenario. Available values are as follows:

  • block: indicates that sensitive information is detected and the information fails to pass the check.
  • pass: indicates that no sensitive information is detected and the information passes the check.
  • review: indicates that the image needs to be manually reviewed.
NOTE:

This service does not save your API calling results.

Table 6 ImageDetectionResultDetail parameters

Parameter

Type

Description

porn

Array of ImageDetectionResultSimpleDetail objects

Indicates that pornographic content was detected.

Table 7 ImageDetectionResultSimpleDetail parameters

Parameter

Type

Description

confidence

Float

Indicates the confidence score. The value ranges from 0 to 1.

label

String

Indicates the label of each detection result. The options are as follows:

In the porn scenario, label indicates pornographic elements (porn information or suggestive content).

  • The porn scenario supports the following labels:
    • normal: The uploaded image does not contain pornography.
    • porn: The image contains porn information.
    • sexy: The image contains suggestive content.

Status code: 400

Table 8 Response body parameters

Parameter

Type

Description

error_code

String

Indicates the error code of a failed API call. For details, see Error Codes.

This parameter is not included when the API is successfully called.

error_msg

String

Indicates the error message of a failed API call.

This parameter is not included when the API is successfully called.

fail_category

String

Indicates the API calling failure scenario when there are multiple scenarios.

Example Requests

endpoint is the request URL for calling an API. Endpoints vary depending on services and regions. For details, see Endpoints.

For example, the endpoint of the service deployed in the CN-Hong Kong region is moderation.ap-southeast-1.myhuaweicloud.com, the request URL is https://moderation.ap-southeast-1.myhuaweicloud.com/v2/{project_id}/moderation/image, and project_id is the project ID. For details, see Obtaining a Project ID and Name.

  • Method 1: Use the Base64 code of an uploaded image to check whether the image contains pornographic content. Use the default moderation rule.
    POST https://{endpoint}/v2/{project_id}/moderation/image
    
    {
      "categories" : [ "porn" ],
      "moderation_rule" : "default",
      "url" : "",
      "image" : "/9j/4AAQSkZJRgABAgEASABIAAD/4RFZRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAA...",
      "threshold" : ""
    }
  • Method 2: Use the URL of an uploaded image to check whether the image contains pornographic content. Use the default moderation rule.
    POST https://{endpoint}/v2/{project_id}/moderation/image
    
    {
      "categories" : [ "porn"],
      "moderation_rule" : "default",
      "url" : "https://BucketName.obs.myhuaweicloud.com/ObjectName",
      "image" : "",
      "threshold" : ""
    }

Example Responses

Status code: 200

Example of a successful response

{
    "result": {
        "detail": {
            "porn": [
                {
                    "confidence": 0.7315528178215026,
                    "label": "xxx"
                },
                {
                    "confidence": 0.26844718217849733,
                    "label": "yyy"
                }
            ]
        },
        "suggestion": "block",
        "category_suggestions": {
            "porn": "review"
        }
    }
}

Status code: 400

Example of a failed response

{
  "error_code" : "AIS.0402",
  "error_msg" : "The image format is not supported."
}

Status Codes

Status Code

Description

200

Example of a successful response

400

Example of a failed response

Error Codes

For details, see Error Codes.