This document provides sample code in common programming languages for typical use cases of the image content moderation service.
For live support, visit Contact Us.
For inquiries about API integration, usage, or other topics related to the Vision AI Open Platform, join our DingTalk group (ID: 23109592).
Overview
For a detailed description of features and API parameters, see image content moderation.
Install the SDK
For information about SDK dependencies for common programming languages, see SDK Overview.
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.
Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
Configure environment variables on Linux and macOS
Open a terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>with the AccessKey ID of your RAM user and<access_key_secret>with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Configure environment variables on Windows
Create a new environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.Open File Explorer, right-click This PC, and then select Properties.
In the left-side navigation pane, click Advanced system settings.
On the Advanced tab of the System Properties dialog box, click Environment Variables.
In the Environment Variables dialog box, click New.
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.Restart the Windows operating system for the configurations to take effect.
Sample code
Image from an OSS bucket in Shanghai
/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/imageaudit20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageaudit20191230</artifactId>
<version>${aliyun.imageaudit.version}</version>
</dependency>
*/
import com.aliyun.imageaudit20191230.models.ScanImageRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.tea.TeaModel;
import java.util.ArrayList;
import java.util.List;
public class ScanImage {
public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new com.aliyun.imageaudit20191230.Client(config);
}
public static void main(String[] args) throws Exception {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.imageaudit20191230.Client client = ScanImage.createClient(accessKeyId, accessKeySecret);
ScanImageRequest.ScanImageRequestTask task0 = new ScanImageRequest.ScanImageRequestTask();
task0.setImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png");
task0.setDataId("uuid-xxxx-xxxx-123456");
task0.setImageTimeMillisecond(1L);
task0.setInterval(1);
task0.setMaxFrames(1);
ScanImageRequest.ScanImageRequestTask task1 = new ScanImageRequest.ScanImageRequestTask();
task1.setImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace2.png");
task1.setDataId("uuid-xxxx-xxxx-123456");
task1.setImageTimeMillisecond(1L);
task1.setInterval(1);
task1.setMaxFrames(1);
List<ScanImageRequest.ScanImageRequestTask> taskList = new ArrayList<>();
taskList.add(task0);
taskList.add(task1);
List<String> sceneList = new ArrayList<>();
sceneList.add("logo");
sceneList.add("porn");
com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
.setTask(taskList)
.setScene(sceneList);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ScanImageResponse scanImageResponse = client.scanImageWithOptions(scanImageRequest, runtime);
// Obtain the full response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse)));
// Obtain a specific field from the response.
System.out.println(scanImageResponse.getBody().getData());
} catch (com.aliyun.tea.TeaException teaException) {
// Obtain the full error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field from the error.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Add the dependency.
# pip install alibabacloud_imageaudit20191230
import os
from alibabacloud_imageaudit20191230.client import Client
from alibabacloud_imageaudit20191230.models import ScanImageRequestTask, ScanImageRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The service endpoint.
endpoint='imageaudit.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
task_0 = ScanImageRequestTask(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace4.jpg'
)
task_1 = ScanImageRequestTask(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace4.jpg'
)
scan_image_request = ScanImageRequest(
task=[
task_0,
task_1
],
scene=[
'logo',
'porn'
]
)
runtime_option = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.scan_image_with_options(scan_image_request, runtime_option)
# Obtain the full response.
print(response.body)
except Exception as error:
# Obtain the full error message.
print(error)
# Obtain a specific field from the error.
print(error.code)<?php
// Install the dependency.
// composer require alibabacloud/imageaudit-20191230
use AlibabaCloud\SDK\Imageaudit\V20191230\Imageaudit;
use AlibabaCloud\SDK\Imageaudit\V20191230\Models\ScanImageRequest;
use AlibabaCloud\SDK\Imageaudit\V20191230\Models\ScanImageRequest\task;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use Exception;
class ScanImage {
/**
* Initializes the client with an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Imageaudit Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new Imageaudit($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
$task0 = new task([
"imageURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace4.jpg"
]);
$task1 = new task([
"imageURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace4.jpg"
]);
$scanImageRequest = new ScanImageRequest([
"task" => [
$task0,
$task1
],
"scene" => [
"logo",
"porn"
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->scanImageWithOptions($scanImageRequest, $runtime);
# Obtain the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Obtain the full error message.
echo Utils::toJSONString($exception);
# Obtain a specific field from the error.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is a reserved array for input arguments. You do not need to modify it.
ScanImage::main(array_slice($argv, 1));// Install the dependency.
// npm install @alicloud/imageaudit20191230
// Import the SDK.
const ImageauditClient = require('@alicloud/imageaudit20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample, make sure that these variables are configured.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `imageaudit.cn-shanghai.aliyuncs.com`;
const client = new ImageauditClient.default(config);
let task0 = new ImageauditClient.ScanImageRequestTask({
imageURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png",
});
let scanImageRequest = new ImageauditClient.ScanImageRequest({
task: [
task0
],
scene: [
"porn",
"logo",
"terrorism",
"ad",
"live"
],
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.scanImageWithOptions(scanImageRequest, runtime)
.then(function(scanImageRequestResponse) {
// Obtain the full response.
console.log(scanImageRequestResponse);
// Obtain a specific field from the response.
console.log(scanImageRequestResponse.body.data);
}, function(error) {
// Obtain the full error message.
console.log(error);
// Obtain a specific field from the error.
console.log(error.data.Code);
})/**
The dependency is on github.com/alibabacloud-go/imageaudit-20191230/v3.
We recommend using 'go mod tidy' to install the dependency.
*/
import (
"fmt"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
imageaudit20191230 "github.com/alibabacloud-go/imageaudit-20191230/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample, make sure that these variables are configured.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The service endpoint.
config.Endpoint = tea.String("imageaudit.cn-shanghai.aliyuncs.com")
client, err := imageaudit20191230.NewClient(config)
if err != nil {
panic(err)
}
tasks0 := &imageaudit20191230.ScanImageRequestTask{
ImageURL: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace17.jpg"),
ImageTimeMillisecond: tea.Int64(1),
Interval: tea.Int32(1),
MaxFrames: tea.Int32(1),
DataId: tea.String("uuid-xxxx-xxxx-123456"),
}
tasks1 := &imageaudit20191230.ScanImageRequestTask{
ImageURL: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace18.jpg"),
ImageTimeMillisecond: tea.Int64(1),
Interval: tea.Int32(1),
MaxFrames: tea.Int32(1),
DataId: tea.String("uuid-xxxx-xxxx-1234567"),
}
scanImageRequest := &imageaudit20191230.ScanImageRequest{
Task: []*imageaudit20191230.ScanImageRequestTask{tasks0, tasks1},
Scene: []*string{tea.String("porn"), tea.String("terrorism"), tea.String("ad"), tea.String("live"), tea.String("logo")},
}
runtime := &util.RuntimeOptions{}
scanImageResponse, err := client.ScanImageWithOptions(scanImageRequest, runtime)
if err != nil {
// Obtain the full error message.
fmt.Println(err.Error())
} else {
// Obtain the full response.
fmt.Println(scanImageResponse)
// Obtain a specific field from the response.
fmt.Println(scanImageResponse.Body.Data)
}
}// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Imageaudit20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Imageaudit20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initializes the client with an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Imageaudit20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
config.Endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Imageaudit20191230.Client(config);
}
public static void Main(string[] args)
{
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
AlibabaCloud.SDK.Imageaudit20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageRequest.ScanImageRequestTask tasks0 = new AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageRequest.ScanImageRequestTask
{
ImageURL = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png",
};
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageRequest scanImageRequest = new AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageRequest
{
Task = new List<AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageRequest.ScanImageRequestTask>
{
tasks0,
},
Scene = new List<string>
{
"porn",
"logo",
"terrorism",
"ad",
"live",
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageResponse scanImageResponse = client.ScanImageWithOptions(scanImageRequest, runtime);
// Obtain the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(scanImageResponse.Body));
// Obtain a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(scanImageResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
Console.WriteLine(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(error.Message);
}
}
}
}Image from a local file or URL
/*
Add the dependency.
<!-- https://mvnrepository.com/artifact/com.aliyun/imageaudit20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>imageaudit20191230</artifactId>
<version>${aliyun.imageaudit.version}</version>
</dependency>
*/
import com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.tea.TeaModel;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class ScanImage {
public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new com.aliyun.imageaudit20191230.Client(config);
}
public static void main(String[] args) throws Exception {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.imageaudit20191230.Client client = ScanImage.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Process a local file.
// InputStream inputStream1 = new FileInputStream(new File("/tmp/ScanImage1.png"));
// InputStream inputStream2 = new FileInputStream(new File("/tmp/ScanImage2.png"));
// Scenario 2: Process an image from a public URL.
URL url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png");
InputStream inputStream1 = url.openConnection().getInputStream();
URL url2 = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace2.png");
InputStream inputStream2 = url2.openConnection().getInputStream();
ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task0 = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
task0.setDataId("uuid-xxxx-xxxx-123456");
task0.setImageTimeMillisecond(1L);
task0.setInterval(1);
task0.setMaxFrames(1);
task0.setImageURLObject(inputStream1);
ScanImageAdvanceRequest.ScanImageAdvanceRequestTask task1 = new ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
task1.setDataId("uuid-xxxx-xxxx-1234567");
task1.setImageTimeMillisecond(1L);
task1.setInterval(1);
task1.setMaxFrames(1);
task1.setImageURLObject(inputStream2);
List<ScanImageAdvanceRequest.ScanImageAdvanceRequestTask> taskList = new ArrayList<>();
taskList.add(task0);
taskList.add(task1);
List<String> sceneList = new ArrayList<>();
sceneList.add("logo");
sceneList.add("porn");
com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest scanImageAdvanceRequest = new com.aliyun.imageaudit20191230.models.ScanImageAdvanceRequest()
.setTask(taskList)
.setScene(sceneList);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
ScanImageResponse scanImageResponse = client.scanImageAdvance(scanImageAdvanceRequest, runtime);
// Obtain the full response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(scanImageResponse)));
// Obtain a specific field from the response.
System.out.println(scanImageResponse.getBody().getData());
} catch (com.aliyun.tea.TeaException teaException) {
// Obtain the full error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field from the error.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Add the dependency.
# pip install alibabacloud_imageaudit20191230
import os
import io
from urllib.request import urlopen
from alibabacloud_imageaudit20191230.client import Client
from alibabacloud_imageaudit20191230.models import ScanImageAdvanceRequestTask, ScanImageAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The service endpoint.
endpoint='imageaudit.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
runtime_option = RuntimeOptions()
# Scenario 1: Process a local file.
stream0 = open(r'/tmp/ScanImage.jpg', 'rb')
task_0 = ScanImageAdvanceRequestTask()
task_0.image_urlobject = stream0
# Scenario 2: Process an image from a public URL.
url1 = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace2.png'
img1 = urlopen(url1).read()
task_1 = ScanImageAdvanceRequestTask()
task_1.image_urlobject=io.BytesIO(img1)
scan_image_request = ScanImageAdvanceRequest(
task=[
task_0,
task_1
],
scene=[
'logo',
'porn'
]
)
try:
# Initialize the client.
client = Client(config)
response = client.scan_image_advance(scan_image_request, runtime_option)
# Obtain the full response.
print(response.body)
except Exception as error:
# Obtain the full error message.
print(error)
# Obtain a specific field from the error.
print(error.code)
# Tip: You can view attribute names by using error.__dict__.
# Close the file stream.
stream0.close()<?php
// Install the dependency.
// composer require alibabacloud/imageaudit-20191230
use AlibabaCloud\SDK\Imageaudit\V20191230\Imageaudit;
use AlibabaCloud\SDK\Imageaudit\V20191230\Models\ScanImageAdvanceRequest;
use AlibabaCloud\SDK\Imageaudit\V20191230\Models\ScanImageAdvanceRequest\task;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use \Exception;
use GuzzleHttp\Psr7\Stream;
class ScanImageAdvance {
/**
* Initializes the client with an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Imageaudit Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new Imageaudit($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
# To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
// Scenario 1: Process a local file.
$file0 = fopen('/tmp/123.jpg', 'rb');
$stream0 = new Stream($file0);
// Scenario 2: Process an image from a public URL.
$file1 = fopen('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png', 'rb');
$stream1 = new Stream($file1);
$task0 = new task([
"imageURLObject" => $stream0
]);
$task1 = new task([
"imageURLObject" => $stream1
]);
$scanImageAdvanceRequest = new ScanImageAdvanceRequest([
"task" => [
$task0,
$task1
],
"scene" => [
"logo",
"porn"
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->scanImageAdvance($scanImageAdvanceRequest, $runtime);
# Obtain the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Obtain the full error message.
echo Utils::toJSONString($exception);
# Obtain a specific field from the error.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is a reserved array for input arguments. You do not need to modify it.
ScanImageAdvance::main(array_slice($argv, 1));// Install the dependency.
// npm install @alicloud/imageaudit20191230
const ImageauditClient = require('@alicloud/imageaudit20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');
let config = new OpenapiClient.Config({
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample, make sure that these variables are configured.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `imageaudit.cn-shanghai.aliyuncs.com`;
const client = new ImageauditClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
const task0 = new ImageauditClient.ScanImageAdvanceRequestTask();
// Scenario 1: Process a local file.
const fileStream = fs.createReadStream('/tmp/ScanImage1.png');
task0.imageURLObject = fileStream;
// Scenario 2: Process an image from a public URL.
const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace2.png");
const httpClient = (url.protocol == "https:") ? https : http;
const task1 = new ImageauditClient.ScanImageAdvanceRequestTask();
task1.imageURLObject = await getResponse(httpClient, url);
let scanImageAdvanceRequest = new ImageauditClient.ScanImageAdvanceRequest({
task: [
task0,
task1,
],
scene: [
"porn",
"logo",
"terrorism",
"ad",
"live"
],
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.scanImageAdvance(scanImageAdvanceRequest, runtime)
.then(function(scanImageResponse) {
// Obtain the full response.
console.log(scanImageResponse);
// Obtain a specific field from the response.
console.log(scanImageResponse.body.data);
}, function(error) {
// Obtain the full error message.
console.log(error);
// Obtain a specific field from the error.
console.log(error.data.Code);
});
} catch (error) {
console.log(error);
}
}();/**
The dependency is on github.com/alibabacloud-go/imageaudit-20191230/v3.
We recommend using 'go mod tidy' to install the dependency.
*/
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
imageaudit20191230 "github.com/alibabacloud-go/imageaudit-20191230/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"net/http"
"os"
)
func main() {
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample, make sure that these variables are configured.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
}
// The service endpoint.
config.Endpoint = tea.String("imageaudit.cn-shanghai.aliyuncs.com")
client, err := imageaudit20191230.NewClient(config)
if err != nil {
panic(err)
}
// Scenario 1: Process a local file.
file, err := os.Open("/tmp/ScanImage1.jpg")
if err != nil {
fmt.Println("can not open file", err)
panic(err)
}
tasks0 := &imageaudit20191230.ScanImageAdvanceRequestTask{
ImageURLObject: file,
}
// Scenario 2: Process an image from a public URL.
httpClient := http.Client{}
file1, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png")
tasks1 := &imageaudit20191230.ScanImageAdvanceRequestTask{
ImageURLObject: file1.Body,
ImageTimeMillisecond: tea.Int64(1),
Interval: tea.Int32(1),
MaxFrames: tea.Int32(1),
DataId: tea.String("uuid-xxxx-xxxx-123456"),
}
scanImageAdvanceRequest := &imageaudit20191230.ScanImageAdvanceRequest{
Task: []*imageaudit20191230.ScanImageAdvanceRequestTask{tasks0, tasks1},
Scene: []*string{tea.String("porn"), tea.String("terrorism"), tea.String("ad"), tea.String("live"), tea.String("logo")},
}
runtime := &util.RuntimeOptions{}
scanImageResponse, err := client.ScanImageAdvance(scanImageAdvanceRequest, runtime)
if err != nil {
// Obtain the full error message.
fmt.Println(err.Error())
} else {
// Obtain the full response.
fmt.Println(scanImageResponse)
// Obtain a specific field from the response.
fmt.Println(scanImageResponse.Body.Data)
}
}// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Imageaudit20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Imageaudit20191230.Models;
using Tea;
using Tea.Utils;
using static System.Net.WebRequestMethods;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initializes the client with an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Imageaudit20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
config.Endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Imageaudit20191230.Client(config);
}
public static void Main(string[] args)
{
// To create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you are using a RAM user's AccessKey, you also need to grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// This sample reads the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that these variables are configured.
AlibabaCloud.SDK.Imageaudit20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageAdvanceRequest.ScanImageAdvanceRequestTask scanImageAdvanceRequestTask = new AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageAdvanceRequest.ScanImageAdvanceRequestTask();
/* Scenario 1: Process a local file.
System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/DetectLivingFace4.jpg");
scanImageAdvanceRequestTask.ImageURLObject = file.BaseStream;
*/
// Scenario 2: Process an image from a public URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace1.png");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
scanImageAdvanceRequestTask.ImageURLObject = stream;
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageAdvanceRequest scanImageAdvanceRequest = new AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageAdvanceRequest
{
Task = new List<AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageAdvanceRequest.ScanImageAdvanceRequestTask>
{
scanImageAdvanceRequestTask,
},
Scene = new List<string>
{
"porn",
"logo",
"terrorism",
"ad",
"live",
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Imageaudit20191230.Models.ScanImageResponse scanImageResponse = client.ScanImageAdvance(scanImageAdvanceRequest, runtime);
// Obtain the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(scanImageResponse.Body));
// Obtain a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(scanImageResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
Console.WriteLine(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(error.Message);
}
}
}
}