天天看點

圖檔内容安全實戰教程

圖檔内容安全實戰教程

内容安全技術是基于阿裡雲視覺分析技術和深度識别技術。本教程為您介紹如何通過智能視覺平台的圖檔檢測能力保證内容安全。

背景資訊

内容安全技術是基于阿裡雲視覺分析技術和深度識别技術,并經過在阿裡經濟體内和雲上客戶的多領域、多場景的廣泛應用和不斷優化,可提供風險和治理領域的圖像識别、定位、檢索等全面服務能力,不僅可以降低色情、涉恐、涉政、廣告、垃圾資訊等違規風險,而且能大幅度降低人工稽核成本。

前提條件

在開始之前,請確定完成以下步驟。

  1. 開通内容安全能力,請參見上述開發前準備。
    圖檔内容安全實戰教程
  2. 在您的Java工程中添加内容安全能力的pom依賴:
<dependencies>
    <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-facebody -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-imageaudit</artifactId>
        <version>1.0.6</version>
    </dependency>
</dependencies>           

圖檔内容安全

支援檢測的場景包括有圖檔智能鑒黃、圖檔涉恐涉政識别、圖文違規識别、圖檔二維碼識别、圖檔不良場景識别和圖檔logo識别等。

例如:識别以下圖檔是否涉嫌違規。

示例代碼如下:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.imageaudit.model.v20191230.*;

public class ScanImage {

    private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<access key id>", "<access key secret>");
    private static IAcsClient client = new DefaultAcsClient(profile);

    public static void main(String[] args) {

        ScanImageRequest request = new ScanImageRequest();
        List<ScanImageRequest.Task> taskList = new ArrayList<ScanImageRequest.Task>();
        ScanImageRequest.Task task1 = new ScanImageRequest.Task();
        // 資料ID。需要保證在一次請求中所有的ID不重複。
        task1.setDataId(UUID.randomUUID().toString());
        // 待檢測圖像的URL。支援HTTP和HTTPS協定。目前僅支援上海地域的OSS連結。
        task1.setImageURL("https://visionapi-test.oss-cn-shanghai.aliyuncs.com/TB1k8mYCpY7gK0jSZKzXXaikpXa-692-440%5B1%5D.jpg");
        taskList.add(task1);
        request.setTasks(taskList);

        // 指定圖檔檢測的應用場景
        List<String> sceneList = new ArrayList<String>();
        // 圖檔智能鑒黃
        sceneList.add("porn");
        // 圖檔涉恐涉政識别
        sceneList.add("terrorism");
        // 圖文違規識别
        sceneList.add("ad");
        // 圖檔不良場景識别
        sceneList.add("live");
        // 圖檔logo識别
        sceneList.add("logo");
        request.setScenes(sceneList);

        try {
            ScanImageResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}           

代碼傳回結果類似如下:

{
  "RequestId": "B2B68CC0-62E8-4DE4-9657-1FDDB8718FDC",
  "Data": {
    "Results": [
      {
        "DataId": "3213132132131",
        "ImageURL": "https://visionapi-test.oss-cn-shanghai.aliyuncs.com/TB1k8mYCpY7gK0jSZKzXXaikpXa-692-440%5B1%5D.jpg",
        "SubResults": [
          {
            "Suggestion": "pass",
            "Rate": 100,
            "Label": "normal",
            "Scene": "porn"
          },
          {
            "Suggestion": "block",
            "Rate": 99.88,
            "Label": "weapon",
            "Scene": "terrorism"
          },
          {
            "Suggestion": "pass",
            "Rate": 99.9,
            "Label": "normal",
            "Scene": "ad"
          },
          {
            "Suggestion": "pass",
            "Rate": 100,
            "Label": "normal",
            "Scene": "live"
          },
          {
            "Suggestion": "pass",
            "Rate": 99.9,
            "Label": "normal",
            "Scene": "logo"
          }
        ]
      }
    ]
  }
}           

從傳回結果中得到的該圖檔識别結果如下:

  • 智能鑒黃:通過
  • 涉恐涉政識别:不通過
  • 圖文違規識别:通過
  • 圖檔不良場景識别:通過
  • 圖檔logo識别:通過