Image Moderation
- Sample code for a Python 3 request
# encoding:utf-8 import requests import base64 url = "https://{endpoint}/v1.0/moderation/image" token = "Actual token value obtained by the user" headers = {'Content-Type': 'application/json', 'X-Auth-Token': token} imagepath = r'data/moderation-terrorism.jpg' with open(imagepath, "rb") as bin_data: image_data = bin_data.read() image_base64 = base64.b64encode(image_data).decode("utf-8") # Use Base64 encoding of images. data= {"image": image_base64, "categories": ["porn", "politics", "terrorism", "ad"]} # Set either the URL or the image. response = requests.post(url, headers=headers, json=data, verify=False) print(response.text) - Sample code for a Java request
package com.huawei.ais.demo; import com.huawei.ais.sdk.util.HttpClientUtils; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import com.alibaba.fastjson.JSONObject; import org.apache.http.entity.ContentType; import org.apache.http.message.BasicHeader; /** * This demo is used only for tests. You are advised to use the SDK. * Before using this demo, configure the dependent JAR package. Obtain this package by downloading the SDK. */ public class ImageModerationDemo { public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException{ TokenDemo(); } public static void TokenDemo() throws URISyntaxException, UnsupportedOperationException, IOException { String url = "https://{endpoint}/v1.0/moderation/image"; String token = "Actual token value obtained by the user"; String imgPath = "data/moderation-terrorism.jpg"; //File path or URL of the image to be recognized. JSONObject params = new JSONObject(); try { params.put("categories", new String[] {"politics", "terrorism", "porn", "ad"}); // Content for moderation if (imgPath.indexOf("http://") != -1 || imgPath.indexOf("https://") != -1) { params.put("url", imgPath); } else { byte[] fileData = FileUtils.readFileToByteArray(new File(imgPath)); String fileBase64Str = Base64.encodeBase64String(fileData); params.put("image", fileBase64Str); } Header[] headers = new Header[]{new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString())}; StringEntity stringEntity = new StringEntity(params.toJSONString(), "utf-8"); HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); String content = IOUtils.toString(response.getEntity().getContent(), "utf-8"); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } } } - Sample code for a PHP request
<?php function TokenRequest() { $url = "https://{endpoint}/v1.0/moderation/image"; $token = "Actual token value obtained by the user"; $imagePath = __DIR__.'data/moderation-terrorism.jpg'; $data = array(); if (stripos($imagePath, 'http://') !== false || stripos($imagePath, 'https://') !== false) { $data['url'] = $imagePath; } else { if($fp = fopen($imagePath,"rb", 0)) { $gambar = fread($fp,filesize($imagePath)); fclose($fp); $fileBase64 = chunk_split(base64_encode($gambar)); } else { echo "Failed to read the image."; return; } $data['image'] = $fileBase64; } $data['categories'] = array("politics", "terrorism", "porn", "ad"); $curl = curl_init(); $headers = array( "Content-Type:application/json", "X-Auth-Token:" . $token ); /* Setting the request body */ curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_NOBODY, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_TIMEOUT, 30); $response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); echo $response; } TokenRequest();Table 1 Parameter description Parameter
Description
url
API request URL, for example, https://{endpoint}/v1.0/moderation/image.
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.
Last Article: Text Moderation
Next Article: Appendix
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.