開發與維運

圖片內容安全實戰教程

圖片內容安全實戰教程

內容安全技術是基於阿里雲視覺分析技術和深度識別技術。本教程為您介紹如何通過智能視覺平臺的圖片檢測能力保證內容安全。

背景信息

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

前提條件

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

  1. 開通內容安全能力,請參見上述開發前準備。
    image.png
  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識別等。

例如:識別以下圖片是否涉嫌違規。
image.png

示例代碼如下:

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識別:通過

Leave a Reply

Your email address will not be published. Required fields are marked *