Updated on 2023-11-20 GMT+08:00

Image Moderation (V3)

  • Sample code for a Python 3 request
    # encoding:utf-8
    import requests
    import base64
    
    url = "https://{endpoint}/v3/{project_id}/moderation/image"
    token = "Actual token value obtained by the user"
    headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
    data= {
      "event_type": "head_image",
      "categories": [
        "porn",
        "terrorism"
      ],
      "url": "Image to be detected"
    } 
    // The image is a URL. For Base64-encoded images, transfer parameter image.
    response = requests.post(url, headers=headers, json=data, verify=False)
    print(response.text)
  • Sample code for a Java request
    Add the following dependency before use. (The version is only an example.)
    <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;
     
    /**
     *  This demo is used only for tests. You are advised to use the SDK.
     *  Add the okhttp3 dependency before use.
     */
    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 of the image to be detected\"}");
    // The image is a URL. For Base64-encoded images, transfer parameter image.
                Request request = new Request.Builder()
                        .url("https://{endpoint}/v3/{project_id}/moderation/image")
                        .method("POST", body)
                        .header("X-Auth-Token", "Actual token value obtained by the user")
                        .build();
                Response response = client.newCall(request).execute();
                String string = response.body().string();
                System.out.println(string);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • Sample code for a PHP request
    <?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": "Image to be detected"
    }', 
    // The image is a URL. For Base64-encoded images, transfer parameter image.
        CURLOPT_HTTPHEADER => array(
            'X-Auth-Token: Actual token value obtained by the user',
            'Content-Type: application/json'
        ),
    ));
     
    $response = curl_exec($curl);
     
    curl_close($curl);
    echo $response;
    Table 1 Parameter description

    Parameter

    Description

    url

    API request URL, for example, https://{endpoint}/v3/{project_id}/moderation/image.

    endpoint indicates the endpoint, and project_id indicates the project ID.

    token

    A token is a user's access credential, which includes user identities and permissions. When you call an API to access a cloud service, a token is required for identity authentication.

    For details about how to obtain the token, see Authentication.

    imagePath

    Image path. An image file path or image URL is supported. The URL can be an HTTP/HTTPS or OBS URL.