更新时间:2023-11-02 GMT+08:00
分享

图像内容审核(V3)

  • Python3语言请求代码示例
    # encoding:utf-8
    import requests
    import base64
    
    url = "https://{endpoint}/v3/{project_id}/moderation/image"
    token = "用户获取得到的实际token值"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    data= {
      "event_type": "head_image",
      "categories": [
        "porn",
        "terrorism"
      ],
      "url": "待检测图片"
    } 
    // 此处图片是url,base64请传参数image
    response = requests.post(url, headers=headers, json=data, verify=False)
    print(response.text)
  • Java语言请求代码示例
    使用前请添加如下依赖(版本可自行更新,此处只是示例):
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>3.14.7</version>
    </dependency>
    package com.huawei.ais.demo;
    import java.io.IOException;
     
    import okhttp3.MediaType;
    import okhttp3.OkHttpClient;
    import okhttp3.Request;
    import okhttp3.RequestBody;
    import okhttp3.Response;
     
    /**
     *  此demo仅供测试使用,强烈建议使用SDK
     *  使用前请添加上方的okhttp3依赖
     */
    public class ImageModerationDemo {
     
        public static void main(String[] args) throws IOException {
            try {
                OkHttpClient client = new OkHttpClient().newBuilder()
                        .build();
                MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, "{\"event_type\":\"head_image\",\"categories\":[\"porn\",\"terrorism\"],\"url\":\"待检测图片url\"}");
    // 此处图片是url,base64请传参数image
                Request request = new Request.Builder()
                        .url("https://{endpoint}/v3/{project_id}/moderation/image")
                        .method("POST", body)
                        .header("X-Auth-Token", "用户获取得到的实际token值")
                        .build();
                Response response = client.newCall(request).execute();
                String string = response.body().string();
                System.out.println(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • PHP语言请求代码示例
    <?php
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://{endpoint}/v3/{project_id}/moderation/image
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => '{
      "event_type": "head_image",
      "categories": [
        "porn",
        "terrorism"
      ],
      "url": "待检测图片"
    }', 
    // 此处图片是url,base64请传参数image
        CURLOPT_HTTPHEADER => array(
            'X-Auth-Token: 用户获取得到的实际token值',
            'Content-Type: application/json'
        ),
    ));
     
    $response = curl_exec($curl);
     
    curl_close($curl);
    echo $response;
    表1 参数说明

    参数

    参数说明

    url

    API请求URL,例如本示例中https://{endpoint}/v3/{project_id}/moderation/image。

    endpoint表示终端节点,project_id表示项目ID

    token

    Token是用户的访问令牌,承载了用户的身份、权限等信息,用户调用API接口时,需要使用Token进行鉴权。

    获取Token方法请参见认证鉴权

    imagePath

    图片路径。支持图片文件路径或图片url路径。其中,图片的url路径支持公网http/https url或OBS提供的url。

相关文档