Moderation (Image)
This section provides an example of how to use a Java SDK to call Moderation (Image). You can directly call APIs to use SDK functions. The procedure is as follows:
Step 1: Subscribing to a Service. In the left navigation pane, choose Service List or Service Management, and subscribe to Image Moderation.
(Optional) Step 2: Enabling Object Storage Service (OBS) Authorization. If you want to use the data stored on OBS, enable OBS authorization.
Step 3: (Optional) Configuring a Moderation Rule. Detect violent, pornographic, and politically sensitive content in different scenarios.
Step 4: Configuring the Environment. Obtain the SDK and sample project and import them to the development environment.
Step 5: Modifying the Configuration. Use AK/SK-based authentication.
Step 5: Calling a Service API. Call the service API. You can view the status code and error code at any time during the usage.
Step 1: Subscribing to a Service
To subscribe to Moderation (Image), perform the following steps:
- Log in to HUAWEI CLOUD, apply for a HUAWEI CLOUD account, and complete real-name authentication.
- On the top of the HUAWEI CLOUD page, choose .
- On the product page, click Try Now and enter the username and password to access the Content Moderation management console.
- Subscribe to your desired service on the Service List or Service Management page. For example, to subscribe to Image Moderation on the Service Management page, select Image Moderation or Image Anti-Porn Moderation in Commercial Services and click Subscribe.
- You only need to subscribe to the service once.
- After a commercial service is successfully subscribed, the service is displayed in My Services on the Service Management page. In this case, you can call the API to use Moderation (Image).
(Optional) Step 2: Enabling Object Storage Service (OBS) Authorization
If you want to use the data stored on OBS, enable OBS authorization. The procedure is as follows:
- The region of OBS must be consistent with that of Content Moderation.
- OBS authorization must be performed by an account, not by an IAM user.
- Log in to the management console and choose Service List > Content Moderation. In the navigation pane, choose Service Management .
- Enable OBS Authorization.
Step 3: (Optional) Configuring a Moderation Rule
You can configure review rules to detect violent, pornographic, and politically sensitive content in different scenarios. For details, see Configuring Moderation Rules.
Step 4: Configuring the Environment
You can compile code to call the API of Moderation (Text) based on the SDK. You need to configure the environment when using the SDK and calling APIs. The procedure is as follows:
- To obtain the SDK and related document of Content Moderation, click here.
- For details about how to configure the environment, see Preparing a Java Development Environment.
- Import an SDK project on Eclipse.
- Open Eclipse and configure the correct JRE path in Windows > Preferences > Java > Installed JREs.
- In the navigation pane on the left, choose General > Workspace. In the Text file encoding area, select Other, set the parameter value to UTF-8, click Apply, and click OK.
- On the Eclipse menu bar, choose File > Import. In the dialog box that is displayed, choose Maven > Existing Maven Projects, click Next, click Browse, and select the local path where ais-moderation-java-sdk resides.
- Click Finish to import the SDK. After the SDK is imported, open the project. Figure 1 shows the project directory.
Step 5: Modifying the Configuration
Content Moderation supports token-based and AK/SK-based authentication. This section uses AK/SK-based authentication as an example.
- Obtain an AK/SK.
The AK/SK is the access key. To obtain the AK/SK, log in to the My Credentials page, choose Access Keys in the left navigation pane, and click Create Access Key in the right pane.
- For AK/SK-based authentication, configure the AK/SK in the Java SDK.
Change the values of AK and SK of the function in the ModerationImageContentDemo.java file of the project to the obtained AK/SK. The sample code is as follows:
1 2 3 4 5 6 7 8 9
// 1. Configure the basic information for accessing Image Moderation and generate a client connection object. AisAccess service = ServiceAccessBuilder.builder() .ak("######") // your ak .sk("######") // your sk .region("cn-north-4") // Content Moderation in CN North-Beijing4 (cn-north-4) and CN East-Shanghai1 (cn-east-3) .connectionTimeout(5000) // Timeout limit for connecting to the target URL .connectionRequestTimeout(1000) // Timeout limit for obtaining available connections from the connection pool .socketTimeout(20000) // Timeout limit for obtaining server response data .build();
- Select a local image or use the default image of the sample project, and modify the image file path (data/moderation-demo-1.jpg) in the ModerationImageContentDemo.java file.
1 2 3 4 5 6
public static void main(String[] args) throws IOException { ModerationImageContentDemo tool = new ModerationImageContentDemo(); tool.imageContentCheck("https://sdk-obs-source-save.obs.cn-north-4.myhuaweicloud.com/terrorism.jpg"); byte[] imageBytes = FileUtils.readFileToByteArray(new File("data/moderation-demo-1.jpg")); tool.imageContentCheck(imageBytes); }
Step 5: Calling a Service API
- Execute the ModerationImageContentDemo.java file. If 200 is displayed on the console, the program is successfully executed. The image moderation result is displayed on the console. See Figure 2.
- View the number of API calls. Choose , and view call statistics and call details. See Figure 3.
- The Call Statistics section provides three statistical charts, displaying daily calls, daily blocked text, and daily reviewed text. You can set a time range to view the change of the number of API calls within the time range.
- The Call Details section lists the total number of calls, blocks, reviews, and passes every day. You can click Export to download and view the total number of API calls, helping you better understand the service calling and review status.
- Images are stored in OBS buckets. Therefore, the images that are determined as block or review images cannot be exported.
- block indicates that sensitive information is contained and the information is blocked.
- pass indicates that sensitive information is not contained and the information is approved.
- review indicates that manual review is required.
- View monitoring metrics. You can click View Metric to view historical data such as successful and failed calls on the Cloud Eye console. See Figure 4.
Last Article: Moderation (Text)




Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.