Add face data
You can add face data to a specified database. This document provides sample code in popular programming languages for this operation.
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 an overview of this feature and detailed parameter descriptions, see Add Face Data.
Install the SDK package
For information about SDK dependencies for popular 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
OSS URL in Shanghai
/*
Import the dependency package.
<!-- https://mvnrepository.com/artifact/com.aliyun/facebody20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>facebody20191230</artifactId>
<version>${aliyun.facebody.version}</version>
</dependency>
*/
import com.aliyun.facebody20191230.models.AddFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
public class AddFace {
public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
The Config object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new com.aliyun.facebody20191230.Client(config);
}
public static void main(String[] args_) throws Exception {
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.facebody20191230.Client client = AddFace.createClient(accessKeyId, accessKeySecret);
com.aliyun.facebody20191230.models.AddFaceRequest addFaceRequest = new com.aliyun.facebody20191230.models.AddFaceRequest()
.setDbName("Face1")
.setImageUrl("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png")
.setEntityId("Entity_id")
.setExtraData("TestUser");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// The sample code prints the API return value.
AddFaceResponse addFaceResponse = client.addFaceWithOptions(addFaceRequest, runtime);
// Obtain the complete response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(addFaceResponse)));
// Obtain a specific field from the response.
System.out.println(addFaceResponse.getBody().getData().getFaceId());
} catch (TeaException teaException) {
// Obtain the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field from the error message.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Import the dependency package.
# pip install alibabacloud_facebody20191230
import os
from alibabacloud_facebody20191230.models import AddFaceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_facebody20191230.client import Client
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
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='facebody.cn-shanghai.aliyuncs.com',
# The region ID of the endpoint.
region_id='cn-shanghai'
)
runtime_option = RuntimeOptions()
add_face_request = AddFaceRequest(
db_name='Face1',
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png',
entity_id='Entity_id',
extra_data='TestUser'
)
try:
# Initialize the client.
client = Client(config)
response = client.add_face_with_options(add_face_request, runtime_option)
# Obtain the complete response.
print(response.body)
except Exception as error:
# Obtain the complete error message.
print(error)
# Obtain a specific field from the error message.
print(error.code)
# Tip: You can view attribute names with `error.__dict__`.<?php
// Install the dependency package.
// composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Models\AddFaceRequest;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Psr7\Stream;
class AddFace {
/**
* Initialize the client by using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Facebody Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
// Required. Your AccessKey ID.
"accessKeyId" => $accessKeyId,
// Required. Your AccessKey Secret.
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new Facebody($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
# For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
$client = self::createClient("accessKeyId", "accessKeySecret");
$addFaceRequest = new AddFaceRequest([
"dbName" => "Face1",
"imageUrl" => 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png',
"entityId" => "Entity_id",
"extraData" => "TestUser"
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->addFaceWithOptions($addFaceRequest, $runtime);
# Obtain the complete response.
echo Utils::toJSONString($resp->body);
} catch (\Exception $exception) {
# Obtain the complete error message.
echo Utils::toJSONString($exception);
# Obtain a specific field from the error message.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// $argv is a reserved array parameter that is not used in this example. You do not need to modify it.
AddFace::main(array_slice($argv, 1));// Install the dependency package.
// npm install @alicloud/facebody20191230
const FacebodyClient = require('@alicloud/facebody20191230');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
const client = new FacebodyClient.default(config);
let addFaceRequest = new FacebodyClient.AddFaceRequest({
dbName: "Face3",
entityId:"Face3_Face3",
imageUrl: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png",
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.addFaceWithOptions(addFaceRequest, runtime)
.then(function(addFaceResponse) {
// Obtain the complete response.
console.log(addFaceResponse);
// Obtain a specific field from the response.
console.log(addFaceResponse.body.data);
}, function(error) {
// Obtain the complete error message.
console.log(error);
// Obtain a specific field from the error message.
console.log(error.data.Code);
})/**
Run `go mod tidy` to install dependencies.
*/
import (
"fmt"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
facebody20191230 "github.com/alibabacloud-go/facebody-20191230/v4/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. The Config object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The service endpoint.
config.Endpoint = tea.String("facebody.cn-shanghai.aliyuncs.com")
client, err := facebody20191230.NewClient(config)
if err != nil {
panic(err)
}
addFaceRequest := &facebody20191230.AddFaceRequest{
DbName: tea.String("Face1"),
EntityId: tea.String("Entity_id"),
ExtraData: tea.String("TestUser"),
ImageUrl: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png"),
}
runtime := &util.RuntimeOptions{}
addFaceResponse, err := client.AddFaceWithOptions(addFaceRequest, runtime)
if err != nil {
// Obtain the complete error message.
fmt.Println(err.Error())
} else {
// Obtain the complete response.
fmt.Println(addFaceResponse)
}
}// Install the dependency package.
// dotnet add package AlibabaCloud.SDK.Facebody20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Facebody20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client by using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Facebody20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The service endpoint.
config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Facebody20191230.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
AlibabaCloud.SDK.Facebody20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Facebody20191230.Models.AddFaceRequest addFaceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.AddFaceRequest
{
DbName = "Face",
EntityId = "Face3_Face3",
ImageUrl = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png"
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.AddFaceResponse addFaceResponse = client.AddFaceWithOptions(addFaceRequest, runtime);
// Obtain the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(addFaceResponse.Body));
// Obtain a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(addFaceResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(error.Message);
}
}
}
}Local file or public URL
/*
Import the dependency package.
<!-- https://mvnrepository.com/artifact/com.aliyun/facebody20191230 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>facebody20191230</artifactId>
<version>${aliyun.facebody.version}</version>
</dependency>
*/
import com.aliyun.facebody20191230.models.AddFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import java.io.InputStream;
import java.net.URL;
public class AddFace {
public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
The Config object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The service endpoint.
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new com.aliyun.facebody20191230.Client(config);
}
public static void main(String[] args_) throws Exception {
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.facebody20191230.Client client = AddFace.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
// InputStream inputStream = new FileInputStream(new File("/tmp/AddFace.png"));
// Scenario 2: Use a file from a publicly accessible URL.
URL url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png");
InputStream inputStream = url.openConnection().getInputStream();
com.aliyun.facebody20191230.models.AddFaceAdvanceRequest addFaceAdvanceRequest = new com.aliyun.facebody20191230.models.AddFaceAdvanceRequest()
.setDbName("Face1")
.setImageUrlObject(inputStream)
.setEntityId("Entity_id")
.setExtraData("TestUser");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// The sample code prints the API return value.
AddFaceResponse addFaceResponse = client.addFaceAdvance(addFaceAdvanceRequest, runtime);
// Obtain the complete response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(addFaceResponse)));
// Obtain a specific field from the response.
System.out.println(addFaceResponse.getBody().getData().getFaceId());
} catch (TeaException teaException) {
// Obtain the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field from the error message.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Import the dependency package.
# pip install alibabacloud_facebody20191230
import os
import io
from urllib.request import urlopen
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import AddFaceAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
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='facebody.cn-shanghai.aliyuncs.com',
# The region ID of the endpoint.
region_id='cn-shanghai'
)
request = AddFaceAdvanceRequest()
# Scenario 1: Use a local file.
#stream = open(r'/tmp/AddFace.jpg', 'rb')
#request.image_url_object = stream
# Scenario 2: Use a file from a publicly accessible URL.
url = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png'
img = urlopen(url).read()
request.image_url_object = io.BytesIO(img)
request.db_name = 'Face1'
request.entity_id = 'Entity_id'
request.extra_data = 'TestUser'
runtime_option = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.add_face_advance(request, runtime_option)
# Obtain the complete response.
print(response.body)
except Exception as error:
# Obtain the complete error message.
print(error)
# Obtain a specific field from the error message.
print(error.code)
# Tip: You can view attribute names with `error.__dict__`.
# Close the stream.
#stream.close()<?php
// Install the dependency package.
// composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Models\AddFaceAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;
use GuzzleHttp\Psr7\Stream;
class AddFaceAdvance {
/**
* Initialize the client by using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Facebody Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// The Config object stores configuration information such as accessKeyId, accessKeySecret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The service endpoint.
$config->endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new Facebody($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
# For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
// Scenario 1: Use a local file.
$file = fopen('/tmp/addface.png', 'rb');
// Scenario 2: Use a file from a publicly accessible URL.
// $file = fopen('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png', 'rb');
$stream = new Stream($file);
$addFaceAdvanceRequest = new AddFaceAdvanceRequest([
"dbName" => "Face1",
"imageUrlObject" => $stream,
"entityId" => "Entity_id",
"extraData" => "TestUser"
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->addFaceAdvance($addFaceAdvanceRequest, $runtime);
# Obtain the complete response.
echo Utils::toJSONString($resp->body);
} catch (\Exception $exception) {
# Obtain the complete error message.
echo Utils::toJSONString($exception);
# Obtain a specific field from the error message.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// $argv is a reserved array parameter that is not used in this example. You do not need to modify it.
AddFaceAdvance::main(array_slice($argv, 1));// Install the dependency package.
// npm install @alicloud/facebody20191230
const FacebodyClient = require('@alicloud/facebody20191230');
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({
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The service endpoint.
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
const client = new FacebodyClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
let addFaceAdvanceRequest = new FacebodyClient.AddFaceAdvanceRequest();
// Scenario 1: Use a local file.
const fileStream = fs.createReadStream('/tmp/addFace.jpg');
// addFaceAdvanceRequest.imageUrlObject = fileStream;
// addFaceAdvanceRequest.dbName = 'Face3',
// addFaceAdvanceRequest.entityId = 'Face3_Face3';
// Scenario 2: Use a file from a publicly accessible URL.
const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png");
const httpClient = (url.protocol == "https:") ? https : http;
addFaceAdvanceRequest.imageUrlObject = await getResponse(httpClient, url);
addFaceAdvanceRequest.dbName = "Face3",
addFaceAdvanceRequest.entityId = "Face3_Face3";
let runtime = new TeaUtil.RuntimeOptions({ });
client.addFaceAdvance(addFaceAdvanceRequest, runtime)
.then(function(addFaceResponse) {
// Obtain the complete response.
console.log(addFaceResponse);
// Obtain a specific field from the response.
console.log(addFaceResponse.body.data);
}, function(error) {
// Obtain the complete error message.
console.log(error);
// Obtain a specific field from the error message.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();/**
Run `go mod tidy` to install dependencies.
*/
import (
"fmt"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
facebody20191230 "github.com/alibabacloud-go/facebody-20191230/v4/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"net/http"
)
func main() {
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. The Config object stores configuration information such as AccessKeyId, AccessKeySecret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The service endpoint.
config.Endpoint = tea.String("facebody.cn-shanghai.aliyuncs.com")
client, err := facebody20191230.NewClient(config)
if err != nil {
panic(err)
}
// Scenario 1: Use a local file.
//file0, err := os.Open("/tmp/AddFace.png")
//if err != nil {
// fmt.Println("can not open file", err)
// panic(err)
//}
//addFaceAdvanceRequest := &facebody20191230.AddFaceAdvanceRequest{
// DbName: tea.String("Face1"),
// EntityId: tea.String("Entity_id"),
// ExtraData: tea.String("TestUser"),
// ImageUrlObject: file0,
//}
// Scenario 2: Use a file from a publicly accessible URL.
httpClient := http.Client{}
file1, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png")
addFaceAdvanceRequest := &facebody20191230.AddFaceAdvanceRequest{
DbName: tea.String("Face1"),
EntityId: tea.String("Entity_id"),
ExtraData: tea.String("TestUser"),
ImageUrlObject: file1.Body,
}
runtime := &util.RuntimeOptions{}
addFaceAdvanceResponse, err := client.AddFaceAdvance(addFaceAdvanceRequest, runtime)
if err != nil {
// Obtain the complete error message.
fmt.Println(err.Error())
} else {
// Obtain the complete response.
fmt.Println(addFaceAdvanceResponse)
}
}// Install the dependency package.
// dotnet add package AlibabaCloud.SDK.Facebody20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Facebody20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client by using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Facebody20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The service endpoint.
config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Facebody20191230.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey ID and AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. You must configure the environment variables before you run the code.
AlibabaCloud.SDK.Facebody20191230.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
AlibabaCloud.SDK.Facebody20191230.Models.AddFaceAdvanceRequest addFaceAdvanceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.AddFaceAdvanceRequest();
/* Scenario 1: Use a local file.
System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/addFace.jpg");
addFaceAdvanceRequest.ImageUrlObject = file.BaseStream;
addFaceAdvanceRequest.DbName = "Face3";
addFaceAdvanceRequest.EntityId = "Face3_Face3";
*/
// Scenario 2: Use a file from a publicly accessible URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/SearchFace/SearchFace1.png");
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
addFaceAdvanceRequest.ImageUrlObject = stream;
addFaceAdvanceRequest.DbName = "Face3";
addFaceAdvanceRequest.EntityId = "Face3_Face3";
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.AddFaceResponse addFaceResponse = client.AddFaceAdvance(addFaceAdvanceRequest, runtime);
// Obtain the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(addFaceResponse.Body));
// Obtain a specific field from the response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(addFaceResponse.Body.Data));
}
catch (TeaException error)
{
// Print the error message.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Print the error message.
Console.WriteLine(error.Message);
}
}
}
}