Use of Alibaba Cloud content review service (image review)

Note: During the project, we often review the content of resources (such as text, pictures) uploaded by users. The review includes two aspects. On the one hand, the content does not match the description, and on the other hand, it violates laws and regulations. This article introduces the use of the content review service provided by Alibaba to review image content.

Subscribe to a service

Step one: Open the official website

Go to the Alibaba Cloud official website , enter "content security" in the search box, click on the one below, and select "Free activation";

Insert image description here

Step 2: Activate content review service

Select "Open Now"

Insert image description here

Step Three: Online Debugging

Enter the "Console", go to the image review in "Service List", and select online debugging;

Insert image description here

Step 4: Debug the code

In the pop-up window, fill in the image URL and scene, and select the SDK sample and Java language;

Insert image description here
For the image URL, you can choose to upload a local image, or you can choose a network path, such as the image resource path on OSS and Minio. For example, I filled in the following one :

Insert image description here

The scene parameters for picture review are as follows. Here I fill in "porn (pornographic pictures)";

Insert image description here

Step 5: Initiate a call

Click "Initiate Call" to view the response result. You can see that the value of "Suggestion" is "pass", which means that the picture is OK and there is no problem;

Insert image description here

Code

Step 1: Add dependencies

The next step is very simple. Just copy the code called by the SDK on the right, but you need to introduce relevant dependencies. As follows, create a simple SpringBoot project. The pom.xml file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hzy</groupId>
    <artifactId>starter-simple</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <!--Springboot项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/>
    </parent>

    <dependencies>
        <!--启动类依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--图片审核依赖-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>imageaudit20191230</artifactId>
            <version>2.0.6</version>
        </dependency>
    </dependencies>
</project>

Note that dependencies are not introduced casually, they can be found in the SDK provided by Alibaba Cloud;

Insert image description here

Click on the GitHub link to find the dependent Maven coordinates;

Insert image description here

For the version number, you can click on the SDK link;

Insert image description here

Step 2: Code implementation

Copy the previous online debugging code. The provided code suggested that we configure AK&SK into the system environment variables. This was a bit outrageous. I simply ignored his suggestion and modified the code as follows:

public class Sample {
    
    

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
    
    
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }

    /**
     * main方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
    
    

        // 官方在这里是获取系统中环境变量中的值
        com.aliyun.imageaudit20191230.Client client = Sample.createClient("你的ak", "你的sk");

        // 设置ImageURL
        com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask task0 = new com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask()
                .setImageURL("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fsafe-img.xhscdn.com%2Fbw1%2F5723a630-077e-45c5-9e73-49ed809b3f43%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fsafe-img.xhscdn.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1694165323&t=149871d4e26b4b9a386e0e98bedc2929");

        // 添加图片任务、设置场景
        com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
                .setTask(java.util.Arrays.asList(
                        task0
                ))
                .setScene(java.util.Arrays.asList(
                        "porn"
                ));
        
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();

        // 打印图片审核结果
        String suggestion = client.scanImageWithOptions(scanImageRequest, runtime).getBody().getData().results.get(0).getSubResults().get(0).suggestion;
        
        System.out.println(suggestion);
    }
}

Get AK&SK

Step one: Enter AccessKey management

Move the mouse to your avatar in the upper right corner and select AccessKey management;

Insert image description here

Step 2: Use subuser AccessKey

After entering, select "Start using sub-user AccessKey";

Insert image description here

Step 3: Create a user

Select "Create User"

Insert image description here

Fill in the login name and the name displayed in the list, remember to check OpenAPI call access;

Insert image description here

After the creation is completed, there will be AK&SK, remember to copy it;

Step 4: Add permissions

Obtaining AK&SK is not the end, you also need to activate the user's image review service permissions;

Insert image description here

Search for "AliyunVIAPIFullAccess", click to join, and click OK;

Insert image description here

test

Fill in the AK&SK obtained above into the code, right-click the mouse to start the program;

Insert image description here

You can see this picture, the review recommendation is "pass";

Insert image description here

At this point, using the Alibaba content review service, the image content review function is completed. Of course, this is a very crude Demo and has the following areas to be optimized:

  • In actual projects, it is impossible for us to hard-code AK, SK, ImageURL and endpoint in the code. They should be written in the configuration file;

  • Verification should be added to the audit results. If there is an exception in the audit return value, it needs to be handled (judgment ==> capture/throw);

  • For the audit results, you cannot only return an audit suggestion, but also need to obtain other return values. A set of analysis of the returned results should be designed;

The audit return value is explained in the SDK officially provided by Alibaba Cloud , as follows:

Insert image description here

Guess you like

Origin blog.csdn.net/qq_42108331/article/details/132192971