功能介绍
仅支持识别拍摄的企业表单原始图片是清晰还是模糊,并基于此构建图片审核系统。
前提条件
清晰度检测默认API调用最大并发为3,如需调整更高并发限制请联系华为专业工程师 为您服务。
URI
URI格式
POST /v1.0/moderation/image/clarity-detect
请求消息
请求参数请参见表5-3。
表5-3 请求参数说明
名称 是否必选 类型 说明
image 与url二选 一
String 图片文件Base64编码字符串。要求base64编码后 大小不超过10M,支持JPG/PNG/BMP格式。
url 与image二 选一
String 图片的URL路径,目前支持:
● 公网HTTP/HTTPS URL
● 华为云OBS提供的URL,使用OBS数据需要进 行授权。包括对服务授权、临时授权、匿名公 开授权。详请参见配置OBS访问权限。
说明
接口响应时间依赖图片的下载时间,如果图片下载时间 过长,会返回接口调用失败。请保证被审核图片所在的 存储服务稳定可靠,建议您使用华为云OBS存储。
thresho
ld 否 Numbe
r 判断图片是否清晰的阈值,取值范围 0-1,如果 不设置,默认值为0.8。
响应消息
表5-4 响应参数说明
名称 类型 说明
result Object 调用成功时表示调用结果。
调用失败时无此字段。
category String 结果判定标签。
● clarity:清晰
● blur:模糊 detail List 检测结果详情。
label String 标签名。
● clarity:清晰
● blur:模糊
confidence Number 置信度,取值范围 0-1。
error_code String 调用失败时的错误码,具体请参见错误码。
调用成功时无此字段。
error_msg String 调用失败时的错误信息。
调用成功时无此字段。
示例
● 请求示例(方式一,使用图片的BASE64编码)
POST https://moderation.cn-north-1.myhuaweicloud.com/v1.0/moderation/image/clarity-detect Request Header:
Content-Type:application/json X-Auth-Token:
MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{ "image":"/9j/4AAQSkZJRgABAgEASABIAAD/
4RFZRXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAAag
"url": "",
"threshold":0.8 }
● 请求示例(方式二,使用图片URL)
POST https://moderation.cn-north-1.myhuaweicloud.com/v1.0/moderation/image/clarity-detect Request Header:
Content-Type:application/json X-Auth-Token:
MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
Request Body:
{
"threshold":0.8 }
● 成功响应示例
{ "result":{
"category": "blur", "detail": [ {
"label":"clarity", "confidence":0.2907 },
{
"label":"blur", "confidence":0.7093 }
] }}
● 失败响应示例
{ "error_code": "AIS.0005",
"error_msg": "The service does not exist."
}
状态码
● 正常 200
● 失败
返回值 说明
400 ● 语义有误,当前请求无法被服务器解析。除非进行修改,否则 客户端不应重复提交这个请求。
● 请求参数有误。
401 当前请求需要用户验证。
403 没有操作权限。
404 请求失败,在服务器上未找到请求所希望得到的资源。
500 服务器遇到了一个未曾预料的状况,导致无法完成对请求的处理。
6 应用示例
6.1 文本内容审核
● Python3语言请求代码示例
# encoding:utf-8 import requests import base64
url = "https://{endpoint}/v1.0/moderation/image"
token = "用户获取得到的实际token值"
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
data= {
"categories": ['ad', 'politics', 'porn', 'abuse', 'contraband', 'flood'], "items": [
{"text": "666666luo聊请+110亚砷酸钾六位qq,fuck666666666666666", "type": "content"}
] }
response = requests.post(url, headers=headers, json=data, verify=False) print(response.content.decode('unicode_escape'))
● Java语言请求代码示例
package com.huawei.ais.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.huawei.ais.sdk.util.HttpClientUtils;
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 com.alibaba.fastjson.JSONObject;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
/** * 此demo仅供测试使用,强烈建议使用SDK
* 使用前需配置依赖jar包。jar包可通过下载SDK获取 */
public class TextModerationDemo {
TokenDemo();
}
public static void TokenDemo() throws URISyntaxException, UnsupportedOperationException, IOException {
String url = "https://{endpoint}/v1.0/moderation/text";
String token = "用户获取得到的实际token值";
JSONObject params = new JSONObject();
try {
params.put("categories", new String[] {"porn","politics", "ad", "abuse", "contraband",
"flood"});
JSONObject text = new JSONObject();
text.put("text", "666666luo聊请+110亚砷酸钾六位qq,fuck666666666666666");
text.put("type", "content");
JSONArray items = new JSONArray();
items.add(text);
params.put("items", items);
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);
JSONObject jsonObject =
JSON.parseObject(HttpClientUtils.convertStreamToString(response.getEntity().getContent()));
System.out.println(JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat));
}
function TokenRequest() {
$url = "https://{endpoint}/v1.0/moderation/text";
$token = "用户获取得到的实际token值";
$items = array(
array(
"text" => "666666luo聊请+110亚砷酸钾六位qq,fuck666666666666666", "type" => "content"
) );
$categories = array("ad", "abuse", "politics", "porn", "contraband");
$data = array(
"Content-Type:application/json", "X-Auth-Token:" . $token );
/* 设置请求体 */
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
echo $response;
}TokenRequest();
表6-1 参数说明
参数 参数说明
url API请求URL,例如本示例中https://{endpoint}/v1.0/moderation/
image。
token Token是用户的访问令牌,承载了用户的身份、权限等信息,用户 调用API接口时,需要使用Token进行鉴权。
获取Token方法请参见认证鉴权。
6.2 图像内容审核
● Python3语言请求代码示例
# encoding:utf-8 import requests import base64
url = "https://{endpoint}/v1.0/moderation/image"
token = "用户获取得到的实际token值"
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") # 使用图片的base64编码
data= {"image": image_base64, "categories": ["porn", "politics", "terrorism", "ad"]} # url与image参数 二选一
response = requests.post(url, headers=headers, json=data, verify=False) print(response.text)
● Java语言请求代码示例
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;
/**
*/
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 = "用户获取得到的实际token值";
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"}); //检测内容 if (imgPath.indexOf("http://") != -1 || imgPath.indexOf("https://") != -1) { 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);
function TokenRequest() {
$url = "https://{endpoint}/v1.0/moderation/image";
$token = "用户获取得到的实际token值";
$imagePath = __DIR__.'data/moderation-terrorism.jpg';
$data = array();
if (stripos($imagePath, 'http://') !== false || stripos($imagePath, 'https://') !== false) { $data['url'] = $imagePath;
);
/* 设置请求体 */
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();
表6-2 参数说明
参数 参数说明
url API请求URL,例如本示例中https://{endpoint}/v1.0/moderation/
image。
token Token是用户的访问令牌,承载了用户的身份、权限等信息,用户 调用API接口时,需要使用Token进行鉴权。
获取Token方法请参见认证鉴权。
imagePa
th 图片路径。支持图片文件路径或图片url路径。其中,图片的url路径 支持公网http/https url或OBS提供的url。