The 1:1 face comparison with masks feature compares the largest face in two images to determine if they belong to the same person. It leverages advanced optimization techniques, including mask generation, occlusion-resistant keypoint detection, and occlusion-resistant feature attention. This topic provides sample code in several programming 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
For a detailed description of the 1:1 face comparison with masks feature and its request parameters, see CompareFaceWithMask.
Install the SDK package
For information about the SDK dependencies for popular 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
Files from OSS (Shanghai)
/*
Import the dependency package.
The minimum required SDK version for facebody20191230 is 3.0.7.
You can find the latest SDK version in this repository: https://mvnrepository.com/artifact/com.aliyun/facebody20191230
<!-- 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.CompareFaceWithMaskResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
public class CompareFaceWithMask {
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 parameters such as AccessKeyId, AccessKeySecret, 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 {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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 = CompareFaceWithMask.createClient(accessKeyId, accessKeySecret);
com.aliyun.facebody20191230.models.CompareFaceWithMaskRequest compareFaceWithMaskRequest = new com.aliyun.facebody20191230.models.CompareFaceWithMaskRequest()
.setImageURLA("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg")
.setImageURLB("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
CompareFaceWithMaskResponse compareFaceWithMaskResponse = client.compareFaceWithMaskWithOptions(compareFaceWithMaskRequest, runtime);
// Get the complete response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(compareFaceWithMaskResponse)));
} catch (TeaException teaException) {
// Get the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Get a specific field from the error message.
System.out.println(teaException.getCode());
}
}
}
# -*- coding: utf-8 -*-
# Import the dependency package.
# The minimum required SDK version for facebody20191230 is 4.0.8.
# You can find the latest SDK version in this repository: https://pypi.org/project/alibabacloud-facebody20191230/
# pip install alibabacloud_facebody20191230
import os
from alibabacloud_facebody20191230.client import Client
from alibabacloud_facebody20191230.models import CompareFaceWithMaskRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey pair, 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 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'
)
compare_face_with_mask_request = CompareFaceWithMaskRequest(
image_urla='https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg',
image_urlb='https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg'
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.compare_face_with_mask_with_options(compare_face_with_mask_request, runtime)
# Get the complete response.
print(response.body)
# Tip: You can view attribute names by using response.body.__dict__.
except Exception as error:
# Get the complete error message.
print(error)
# Get a specific field from the error message.
print(error.code)
<?php
// Install the dependency package.
// The minimum required SDK version for facebody-20191230 is 3.0.8.
// You can find the latest SDK version in this repository: https://packagist.org/packages/alibabacloud/facebody-20191230
// composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Facebody\V20191230\Models\CompareFaceWithMaskRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class CompareFaceWithMask {
/**
* 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 parameters such as accessKeyId, accessKeySecret, 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){
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 running the sample code.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
$compareFaceWithMaskRequest = new CompareFaceWithMaskRequest([
"imageURLA" => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg",
"imageURLB" => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg"
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->compareFaceWithMaskWithOptions($compareFaceWithMaskRequest, $runtime);
# Get the complete response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the complete error message.
echo Utils::toJSONString($exception);
# Get 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 and has no practical significance. You do not need to modify it.
CompareFaceWithMask::main(array_slice($argv, 1));
// Install the dependency package.
// npm install @alicloud/facebody20191230
// The minimum required SDK version for facebody20191230 is 4.0.7.
// You can find the latest SDK version in this repository: https://npmjs.com/package/@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 pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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 compareFaceWithMaskRequest = new FacebodyClient.CompareFaceWithMaskRequest({
imageURLA: "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg",
imageURLB: "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg",
});
let runtime = new TeaUtil.RuntimeOptions({});
client.compareFaceWithMaskWithOptions(compareFaceWithMaskRequest, runtime)
.then(function (compareFaceWithMaskResponse) {
// Get the complete response.
console.log(compareFaceWithMaskResponse);
// Get a specific field.
console.log(compareFaceWithMaskResponse.body.data);
}, function (error) {
// Get the complete error message.
console.log(error);
// Get a specific field from the error message.
console.log(error.data.Code);
})
/**
The minimum required SDK version for facebody-20191230 is 4.0.7.
You can find the latest SDK version in this repository: https://pkg.go.dev/github.com/alibabacloud-go/facebody-20191230/v4
This depends on github.com/alibabacloud-go/facebody-20191230.
We recommend that you use `go mod tidy` to install dependencies.
*/
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() {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 running the sample 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 parameters such as AccessKeyId, AccessKeySecret, 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 {
panic(err)
}
compareFaceWithMaskRequest := &facebody20191230.CompareFaceWithMaskRequest{
ImageURLA: tea.String("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg"),
ImageURLB: tea.String("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg"),
}
runtime := &util.RuntimeOptions{}
compareFaceWithMaskResponse, err := client.CompareFaceWithMaskWithOptions(compareFaceWithMaskRequest, runtime)
if err != nil {
// Get the complete error message.
fmt.Println(err.Error())
} else {
// Get the complete response.
fmt.Println(compareFaceWithMaskResponse)
}
}
// Install the dependency package.
// dotnet add package AlibabaCloud.SDK.Facebody20191230
// The minimum required SDK version for facebody20191230 is 3.0.7.
// You can find the latest SDK version in this repository: https://nuget.org/packages/AlibabaCloud.SDK.Facebody20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
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 endpoint of the service.
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 pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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.CompareFaceWithMaskRequest compareFaceWithMaskRequest = new AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceWithMaskRequest
{
ImageURLA = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg",
ImageURLB = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg",
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceWithMaskResponse compareFaceWithMaskResponse = client.CompareFaceWithMaskWithOptions(compareFaceWithMaskRequest, runtime);
// Get the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(compareFaceWithMaskResponse.Body));
// Get a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(compareFaceWithMaskResponse.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 files or accessible URLs
/*
Import the dependency package.
The minimum required SDK version for facebody20191230 is 3.0.7.
You can find the latest SDK version in this repository: https://mvnrepository.com/artifact/com.aliyun/facebody20191230
<!-- 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.CompareFaceWithMaskResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import java.io.FileInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
public class CompareFaceWithMask {
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 parameters such as AccessKeyId, AccessKeySecret, 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 {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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 = CompareFaceWithMask.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
// InputStream inputStream1 = new FileInputStream(new File("/tmp/CompareFaceWithMask-left1.jpeg"));
// InputStream inputStream2 = new FileInputStream(new File("/tmp/CompareFaceWithMask-right1.jpeg"));
// Scenario 2: Use an accessible URL.
URL url1 = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg");
InputStream inputStream1 = url1.openConnection().getInputStream();
URL url2 = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg");
InputStream inputStream2 = url2.openConnection().getInputStream();
com.aliyun.facebody20191230.models.CompareFaceWithMaskAdvanceRequest compareFaceWithMaskAdvanceRequest = new com.aliyun.facebody20191230.models.CompareFaceWithMaskAdvanceRequest()
.setImageURLAObject(inputStream1)
.setImageURLBObject(inputStream2);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
CompareFaceWithMaskResponse compareFaceWithMaskAdvanceResponse = client.compareFaceWithMaskAdvance(compareFaceWithMaskAdvanceRequest, runtime);
// Get the complete response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(compareFaceWithMaskAdvanceResponse)));
} catch (TeaException teaException) {
// Get the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Get 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 CompareFaceWithMaskAdvanceRequest
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
config = Config(
# For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
# If you use a RAM user's AccessKey pair, 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 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 local files.
# imgA = open(r'/tmp/CompareFaceWithMask-left1.jpeg', 'rb')
# imgB = open(r'/tmp/CompareFaceWithMask-right1.jpeg', 'rb')
# Scenario 2: Use an accessible URL.
urlA = 'https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg'
imgA = io.BytesIO(urlopen(urlA).read())
urlB = 'https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg'
imgB = io.BytesIO(urlopen(urlB).read())
compare_face_with_mask_request = CompareFaceWithMaskAdvanceRequest()
compare_face_with_mask_request.image_urlaobject = imgA
compare_face_with_mask_request.image_urlbobject = imgB
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.compare_face_with_mask_advance(compare_face_with_mask_request, runtime)
# Get the complete response.
print(response.body)
# Tip: You can view attribute names by using response.body.__dict__.
except Exception as error:
# Get the complete error message.
print(error)
# Get a specific field from the error message.
print(error.code)
<?php
// Install the dependency package.
// The minimum required SDK version for facebody-20191230 is 3.0.8.
// You can find the latest SDK version in this repository: https://packagist.org/packages/alibabacloud/facebody-20191230
// composer require alibabacloud/facebody-20191230
use AlibabaCloud\SDK\Facebody\V20191230\Facebody;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Facebody\V20191230\Models\CompareFaceWithMaskAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
class CompareFaceWithMaskAdvance {
/**
* 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 parameters such as accessKeyId, accessKeySecret, 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){
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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.
// $fileA = fopen('/tmp/CompareFaceWithMask-left1.jpeg', 'rb');
// $streamA = new Stream($fileA);
// Scenario 2: Use an accessible URL.
$fileB = fopen('https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg', 'rb');
$streamB = new Stream($fileB);
$compareFaceWithMaskAdvanceRequest = new CompareFaceWithMaskAdvanceRequest([
// "imageURLAObject" => $streamA,
"imageURLBObject" => $streamB
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->compareFaceWithMaskAdvance($compareFaceWithMaskAdvanceRequest, $runtime);
# Get the complete response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Get the complete error message.
echo Utils::toJSONString($exception);
# Get 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 and has no practical significance. You do not need to modify it.
CompareFaceWithMaskAdvance::main(array_slice($argv, 1));
// Install the dependency package.
// npm install @alicloud/facebody20191230
// The minimum required SDK version for facebody20191230 is 4.0.7.
// You can find the latest SDK version in this repository: https://npmjs.com/package/@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 pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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 {
let compareFaceWithMaskAdvanceRequest = new FacebodyClient.CompareFaceWithMaskAdvanceRequest();
// Scenario 1: Use a local file.
// const fileStream0 = fs.createReadStream('/tmp/CompareFaceWithMask-left1.jpeg');
// const fileStream1 = fs.createReadStream('/tmp/CompareFaceWithMask-right1.jpeg');
// compareFaceWithMaskAdvanceRequest.imageURLAObject = fileStream0;
// compareFaceWithMaskAdvanceRequest.imageURLBObject = fileStream1;
// Scenario 2: Use an accessible URL.
const urlA = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg");
const httpClientA = (urlA.protocol == "https:") ? https : http;
const urlB = new URL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg");
const httpClientB = (urlB.protocol == "https:") ? https : http;
compareFaceWithMaskAdvanceRequest.imageURLAObject = await getResponse(httpClientA, urlA);
compareFaceWithMaskAdvanceRequest.imageURLBObject = await getResponse(httpClientB, urlB);
let runtime = new TeaUtil.RuntimeOptions({});
client.compareFaceWithMaskAdvance(compareFaceWithMaskAdvanceRequest, runtime)
.then(function(compareFaceWithMaskResponse) {
// Get the complete response.
console.log(compareFaceWithMaskResponse);
// Get a specific field.
console.log(compareFaceWithMaskResponse.body.data);
}, function(error) {
// Get the complete error message.
console.log(error);
// Get a specific field from the error message.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();
/**
The minimum required SDK version for facebody-20191230 is 4.0.7.
You can find the latest SDK version in this repository: https://pkg.go.dev/github.com/alibabacloud-go/facebody-20191230/v4
This depends on github.com/alibabacloud-go/facebody-20191230.
We recommend that you use `go mod tidy` to install dependencies.
*/
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"
"net/http"
)
func main() {
// For information about how to create an AccessKey pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 running the sample 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 parameters such as AccessKeyId, AccessKeySecret, 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 {
panic(err)
}
// Scenario 1: Use a local file.
// file1, err := os.Open("/tmp/CompareFaceWithMask-left1.jpeg")
// if err != nil {
// fmt.Println("cannot open file", err)
// panic(err)
// }
// file2, err := os.Open("/tmp/CompareFaceWithMask-right1.jpeg")
// if err != nil {
// fmt.Println("cannot open file", err)
// panic(err)
// }
// compareFaceWithMaskAdvanceRequest := &facebody20191230.CompareFaceWithMaskAdvanceRequest{
// ImageURLAObject: file1,
// ImageURLBObject: file2,
// }
// Scenario 2: Use an accessible URL.
httpClient := http.Client{}
resp1, _ := httpClient.Get("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg")
resp2, _ := httpClient.Get("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg")
compareFaceWithMaskAdvanceRequest := &facebody20191230.CompareFaceWithMaskAdvanceRequest{
ImageURLAObject: resp1.Body,
ImageURLBObject: resp2.Body,
}
runtime := &util.RuntimeOptions{}
compareFaceWithMaskAdvanceResponse, err := client.CompareFaceWithMaskAdvance(compareFaceWithMaskAdvanceRequest, runtime)
if err != nil {
// Get the complete error message.
fmt.Println(err.Error())
} else {
// Get the complete response.
fmt.Println(compareFaceWithMaskAdvanceResponse)
}
}
// Install the dependency package.
// dotnet add package AlibabaCloud.SDK.Facebody20191230
// The minimum required SDK version for facebody20191230 is 3.0.7.
// You can find the latest SDK version in this repository: https://nuget.org/packages/AlibabaCloud.SDK.Facebody20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
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 endpoint of the service.
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 pair, see https://help.aliyun.com/document_detail/175144.html.
// If you use a RAM user's AccessKey pair, 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 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.CompareFaceWithMaskAdvanceRequest compareFaceWithMaskAdvanceRequest = new AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceWithMaskAdvanceRequest
();
// Scenario 1: Use a local file.
// using (FileStream fileA = new FileStream(@"/tmp/CompareFaceWithMask-left1.jpeg", FileMode.Open))
// {
// compareFaceWithMaskAdvanceRequest.ImageURLAObject = fileA;
// }
// using (FileStream fileB = new FileStream(@"/tmp/CompareFaceWithMask-right1.jpeg", FileMode.Open))
// {
// compareFaceWithMaskAdvanceRequest.ImageURLBObject = fileB;
// }
// Scenario 2: Use an accessible URL.
HttpWebRequest requestA = (HttpWebRequest)WebRequest.Create("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-left1.jpeg");
WebResponse responseA = requestA.GetResponse();
Stream streamA = responseA.GetResponseStream();
compareFaceWithMaskAdvanceRequest.ImageURLAObject = streamA;
HttpWebRequest requestB = (HttpWebRequest)WebRequest.Create("https://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/CompareFaceWithMask/CompareFaceWithMask-right1.jpeg");
WebResponse responseB = requestB.GetResponse();
Stream streamB = responseB.GetResponseStream();
compareFaceWithMaskAdvanceRequest.ImageURLBObject = streamB;
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
AlibabaCloud.SDK.Facebody20191230.Models.CompareFaceWithMaskResponse compareFaceWithMaskResponse = client.CompareFaceWithMaskAdvance(compareFaceWithMaskAdvanceRequest, runtime);
// Get the complete response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(compareFaceWithMaskResponse.Body));
// Get a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(compareFaceWithMaskResponse.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);
}
}
}
}