RecognizePublicFace
The RecognizePublicFace feature identifies public figures in images. This topic provides sample code for common use cases in several popular languages.
-
For real-time assistance, start an online consultation.
-
If you have questions about API access or usage for the Alibaba Cloud Vision AI Platform, join our DingTalk group (ID: 23109592) to contact us.
Overview
See RecognizePublicFace for a detailed description of the feature and its API parameters.
Install the SDK
For information about SDK dependencies, 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
/*
Add the dependency.
<!-- 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.RecognizePublicFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
public class RecognizePublicFace {
public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initializes the Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The endpoint of the service.
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new com.aliyun.facebody20191230.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 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. These must be configured before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.facebody20191230.Client client = RecognizePublicFace.createClient(accessKeyId, accessKeySecret);
com.aliyun.facebody20191230.models.RecognizePublicFaceRequest.RecognizePublicFaceRequestTask task0 = new com.aliyun.facebody20191230.models.RecognizePublicFaceRequest.RecognizePublicFaceRequestTask()
.setImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/facebody/RecognizePublicFace/u%3D2802364678%2C591996814%26fm%3D26%26gp%3D0.jpg");
com.aliyun.facebody20191230.models.RecognizePublicFaceRequest recognizePublicFaceRequest = new com.aliyun.facebody20191230.models.RecognizePublicFaceRequest()
.setTask(java.util.Arrays.asList(
task0
));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Print the API return value to view the result.
RecognizePublicFaceResponse response = client.recognizePublicFaceWithOptions(recognizePublicFaceRequest, runtime);
// Get the full response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Get the full error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Get a specific field.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_facebody20191230
import os
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import RecognizePublicFaceRequestTask, RecognizePublicFaceRequest
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 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. These must be configured before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The endpoint of the service.
endpoint='facebody.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
task_0 = RecognizePublicFaceRequestTask(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png'
)
task_1 = RecognizePublicFaceRequestTask(
image_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png'
)
recognize_public_face_request = RecognizePublicFaceRequest(
task=[
task_0,
task_1
]
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.recognize_public_face_with_options(recognize_public_face_request, runtime)
# Get the full response.
print(response.body)
except Exception as error:
# Get the full error message.
print(error)
# Get a specific field.
print(error.code)
# Tip: You can view all attributes of the error object by using error.__dict__.
<?php
// Install the dependency.
//composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Facebody\V20191230\Models\RecognizePublicFaceRequest\task;
use AlibabaCloud\SDK\Facebody\V20191230\Models\RecognizePublicFaceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class RecognizePublicFace {
/**
* Use an AccessKey pair to initialize the client.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Facebody Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initializes the Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The endpoint of the service.
$config->endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new Facebody($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 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. These must be configured before running the sample code.
$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/DetectFace/DetectFace4.png"
]);
$task1 = new task([
"imageURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png"
]);
$recognizePublicFaceRequest = new RecognizePublicFaceRequest([
"task" => [
$task0,
$task1
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->recognizePublicFaceWithOptions($recognizePublicFaceRequest, $runtime);
# Get the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the full error message.
echo Utils::toJSONString($exception);
# Get a specific field.
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 and has no practical effect. You do not need to modify it.
RecognizePublicFace::main(array_slice($argv, 1));
// Install the dependency.
// 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({
// 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. These must be configured before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The endpoint of the service.
config.endpoint = `facebody.cn-shanghai.aliyuncs.com`;
const client = new FacebodyClient.default(config);
let task0 = new FacebodyClient.RecognizePublicFaceRequestTask({
imageURL: "http://explorer-image.oss-cn-shanghai.aliyuncs.com/SBWf0x80dQgq2jAWu3BiUsc2/1.png?OSSAccessKeyId=LTAI****************&Expires=1672373662&Signature=WF%2BxQ5nf7dfK%2F****,
});
let recognizePublicFaceRequest = new FacebodyClient.RecognizePublicFaceRequest({
task: [
task0
],
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.recognizePublicFaceWithOptions(recognizePublicFaceRequest, runtime)
.then(function(recognizePublicFaceResponse) {
// Get the full response.
console.log(recognizePublicFaceResponse);
// Get a specific field.
console.log(recognizePublicFaceResponse.body.data);
}, function(error) {
// Get the full error message.
console.log(error);
// Get a specific field.
console.log(error.data.Code);
})
/**
Add the dependency: github.com/alibabacloud-go/facebody-20191230/v4
We recommend that you run `go mod tidy` to install the dependency.
*/
package main
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() {
// 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. These must be configured before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initializes the &openapi.Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
}
// The endpoint of the service.
config.Endpoint = tea.String("facebody.cn-shanghai.aliyuncs.com")
client, err := facebody20191230.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
task0 := &facebody20191230.RecognizePublicFaceRequestTask{
ImageURL: tea.String("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/facebody/RecognizePublicFace/u%3D2802364678%2C591996814%26fm%3D26%26gp%3D0.jpg"),
}
recognizePublicFaceRequest := &facebody20191230.RecognizePublicFaceRequest{
Task: []*facebody20191230.RecognizePublicFaceRequestTask{task0},
}
runtime := &util.RuntimeOptions{}
recognizePublicFaceResponse, _err := client.RecognizePublicFaceWithOptions(recognizePublicFaceRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Get the full response.
fmt.Println(recognizePublicFaceResponse)
// Get a specific field.
fmt.Println(recognizePublicFaceResponse.Body.Data)
}
}
// Install the dependency.
// dotnet add package AlibabaCloud.SDK.Facebody20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Facebody20191230.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Use an AccessKey pair to initialize the client.
* @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 endpoint of the service.
config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Facebody20191230.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 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. These must be configured before running the sample 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.RecognizePublicFaceRequest.RecognizePublicFaceRequestTask task0 = new AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceRequest.RecognizePublicFaceRequestTask
{
ImageURL = "http://explorer-image.oss-cn-shanghai.aliyuncs.com/SBWf0x80dQgq2jAWu3BiUsc2/1.png?OSSAccessKeyId=LTAI****************&Expires=1672373662&Signature=WF%2BxQ5nf7dfK%2FO****",
};
AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceRequest recognizePublicFaceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceRequest
{
Task = new List<AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceRequest.RecognizePublicFaceRequestTask>
{
task0
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceResponse recognizePublicFaceResponse = client.RecognizePublicFaceWithOptions(recognizePublicFaceRequest, runtime);
// Get the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizePublicFaceResponse.Body));
// Get a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizePublicFaceResponse.Body.Data));
}
catch (TeaException error)
{
// Prints the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Prints the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
}
}
}
Image from a local file or URL
/*
Add the dependency.
<!-- 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.RecognizePublicFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
public class RecognizePublicFace {
public static com.aliyun.facebody20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initializes the Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The endpoint of the service.
config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new com.aliyun.facebody20191230.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 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. These must be configured before running the sample code.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.facebody20191230.Client client = RecognizePublicFace.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
InputStream inputStream0 = new FileInputStream(new File("/tmp/RecognizePublicFace1.jpg"));
com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask task0 = new com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask()
.setImageURLObject(inputStream0);
// Scenario 2: Use a publicly accessible URL.
URL url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png");
InputStream inputStream1 = url.openConnection().getInputStream();
com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask task1 = new com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask()
.setImageURLObject(inputStream1);
com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest recognizePublicFaceAdvanceRequest = new com.aliyun.facebody20191230.models.RecognizePublicFaceAdvanceRequest()
.setTask(java.util.Arrays.asList(
task0, task1
));
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// Print the API return value to view the result.
RecognizePublicFaceResponse response = client.recognizePublicFaceAdvance(recognizePublicFaceAdvanceRequest, runtime);
// Get the full response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(response)));
} catch (TeaException error) {
// Get the full error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(error));
// Get a specific field.
System.out.println(error.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Install the dependency.
# pip install alibabacloud_facebody20191230
import os
import io
from urllib.request import urlopen
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import RecognizePublicFaceAdvanceRequestTask, RecognizePublicFaceAdvanceRequest
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 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. These must be configured before running the sample code.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The endpoint of the service.
endpoint='facebody.cn-shanghai.aliyuncs.com',
# The region ID that corresponds to the endpoint.
region_id='cn-shanghai'
)
# Scenario 1: Use a local file.
stream0 = open(r'/tmp/RecognizePublicFace.png', 'rb')
task_0 = RecognizePublicFaceAdvanceRequestTask()
task_0.image_urlobject = stream0
# Scenario 2: Use a publicly accessible URL.
url1 = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png'
img1 = urlopen(url1).read()
task_1 = RecognizePublicFaceAdvanceRequestTask()
task_1.image_urlobject = io.BytesIO(img1)
recognize_public_face_request = RecognizePublicFaceAdvanceRequest(
task=[
task_0,
task_1
]
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.recognize_public_face_advance(recognize_public_face_request, runtime)
# Get the full response.
print(response.body)
except Exception as error:
# Get the full error message.
print(error)
# Get a specific field.
print(error.code)
# Tip: You can view all attributes of the error object by using error.__dict__.
# Close the stream.
stream0.close()
<?php
// Install the dependency.
//composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use AlibabaCloud\SDK\Facebody\V20191230\Models\RecognizePublicFaceAdvanceRequest;
use AlibabaCloud\SDK\Facebody\V20191230\Models\RecognizePublicFaceAdvanceRequest\task;
use AlibabaCloud\Tea\Utils\Utils;
use \Exception;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
class RecognizePublicFaceAdvance {
/**
* Use an AccessKey pair to initialize the client.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Facebody Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initializes the Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The endpoint of the service.
$config->endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new Facebody($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 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. These must be configured before running the sample 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.
$file0 = fopen('/tmp/RecognizePublicFace.png', 'rb');
$stream0 = new Stream($file0);
// Scenario 2: Use a publicly accessible URL.
$file1 = fopen('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png', 'rb');
$stream1 = new Stream($file1);
$task0 = new task([
"imageURLObject" => $stream0
]);
$task1 = new task([
"imageURLObject" => $stream1
]);
$recognizePublicAdvanceFaceRequest = new RecognizePublicFaceAdvanceRequest([
"task" => [
$task0,
$task1
]
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->recognizePublicFaceAdvance($recognizePublicAdvanceFaceRequest, $runtime);
# Get the full response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the full error message.
echo Utils::toJSONString($exception);
# Get a specific field.
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 and has no practical effect. You do not need to modify it.
RecognizePublicFaceAdvance::main(array_slice($argv, 1));
// Install the dependency.
// 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({
// 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. These must be configured before running the sample code.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The endpoint of the service.
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 {
// Scenario 1: Use a local file.
const fileStream = fs.createReadStream('/tmp/RecognizePublicFace1.png');
let task0 = new FacebodyClient.RecognizePublicFaceAdvanceRequestTask();
task0.imageURLObject = fileStream;
// Scenario 2: Use a publicly accessible URL.
const url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png");
const httpClient = (url.protocol == "https:") ? https : http;
let task1 = new FacebodyClient.RecognizePublicFaceAdvanceRequestTask();
task1.imageURLObject = await getResponse(httpClient, url);
let recognizePublicFaceAdvanceRequest = new FacebodyClient.RecognizePublicFaceAdvanceRequest({
task: [
task0,
task1
],
});
let runtime = new TeaUtil.RuntimeOptions({ });
client.recognizePublicFaceAdvance(recognizePublicFaceAdvanceRequest, runtime)
.then(function(recognizePublicFaceResponse) {
// Get the full response.
console.log(recognizePublicFaceResponse);
// Get a specific field.
console.log(recognizePublicFaceResponse.body.data);
}, function(error) {
// Get the full error message.
console.log(error);
// Get a specific field.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();
/**
Add the dependency: github.com/alibabacloud-go/facebody-20191230/v4
We recommend that you run `go mod tidy` to install the dependency.
*/
package main
import (
"fmt"
"net/http"
"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() {
// 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. These must be configured before running the sample code.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initializes the &openapi.Config object, which stores your AccessKey ID, AccessKey Secret, and endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
}
// The endpoint of the service.
config.Endpoint = tea.String("facebody.cn-shanghai.aliyuncs.com")
client, err := facebody20191230.NewClient(config)
if err != nil {
fmt.Println(err.Error())
}
// Scenario 1: Use a local file.
file, err := os.Open("/tmp/RecognizePublicFace1.jpg")
if err != nil {
fmt.Println("cannot open file", err)
}
task0 := &facebody20191230.RecognizePublicFaceAdvanceRequestTask{
ImageURLObject: file,
}
// Scenario 2: Use a publicly accessible URL.
httpClient := http.Client{}
file1, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png")
task1 := &facebody20191230.RecognizePublicFaceAdvanceRequestTask{
ImageURLObject: file1.Body,
}
recognizePublicFaceAdvanceRequest := &facebody20191230.RecognizePublicFaceAdvanceRequest{
Task: []*facebody20191230.RecognizePublicFaceAdvanceRequestTask{task0, task1},
}
runtime := &util.RuntimeOptions{}
recognizePublicFaceResponse, _err := client.RecognizePublicFaceAdvance(recognizePublicFaceAdvanceRequest, runtime)
if _err != nil {
fmt.Println(_err.Error())
} else {
// Get the full response.
fmt.Println(recognizePublicFaceResponse)
// Get a specific field.
fmt.Println(recognizePublicFaceResponse.Body.Data)
}
}
// Install the dependency.
// 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
{
/**
* Use an AccessKey pair to initialize the client.
* @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 endpoint of the service.
config.Endpoint = "facebody.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Facebody20191230.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 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. These must be configured before running the sample 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.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask task0 = new AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask
();
// Scenario 1: Use a local file.
System.IO.StreamReader file = new System.IO.StreamReader(@"/tmp/RecognizePublicFace.png");
task0.ImageURLObject = file.BaseStream;
// Scenario 2: Use a publicly accessible URL.
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectFace/DetectFace4.png");
//WebResponse response = request.GetResponse();
//Stream stream = response.GetResponseStream();
//task0.ImageURLObject = stream;
AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceAdvanceRequest recognizePublicFaceAdvanceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceAdvanceRequest
{
Task = new List<AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceAdvanceRequest.RecognizePublicFaceAdvanceRequestTask>
{
task0
},
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.RecognizePublicFaceResponse recognizePublicFaceResponse = client.RecognizePublicFaceAdvance(recognizePublicFaceAdvanceRequest, runtime);
// Get the full response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizePublicFaceResponse.Body));
// Get a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(recognizePublicFaceResponse.Body.Data));
}
catch (TeaException error)
{
// Prints the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// Prints the error message.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
}
}
}
}