Face similarity comparison in Intelligent Media Management (IMM) compares a collected face image with the faces that are recorded in your system to provide a similarity score, which is used to determine whether the face matches a specific person. This feature can be used in business applications such as authentication, identify verification, and face recognition to provide accurate and efficient identification. This topic describes how to use the face similarity comparison feature.
Prerequisites
An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.
OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.
IMM is activated. For more information, see Activate IMM.
A project is created in the IMM console. For more information, see Create a project.
NoteYou can call the CreateProject operation to create a project. For more information, see CreateProject.
You can call the ListProjects operation to query the existing projects in a specific region. For more information, see ListProjects.
Usage
Call the CompareImageFaces operation to compare two faces in two images.
Images
IMM project: test-project
Image paths:
oss://test-bucket/test-object1.jpg
oss://test-bucket/test-object2.jpg
Sample request
{
"ProjectName": "test-project",
"Source": "{\"URI1\":\"oss://test-bucket/test-object1.jpg\",\"URI2\":\"oss://test-bucket/test-object2.jpg\"}"
}
Sample response
{
"RequestId": "23AFD925-06CD-56AA-B521-76EC6F******",
"Similarity": 0.18605702
}
The highest similarity score is 1. The higher the similarity score, the more similar the faces. In the preceding example, the face similarity score is 0.18605702. If the similarity score is higher than 0.7, the faces are considered to match the same person. Face occlusion may affect the similarity score.
Sample code
The following sample code provides an example on how to use IMM SDK for Python to compare facial similarity:
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
from typing import List
from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> imm20200930Client:
"""
Use your AccessKey ID and AccessKey secret to initialize the client.
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
access_key_id=access_key_id,
access_key_secret=access_key_secret
)
# Specify the IMM endpoint.
config.endpoint = f'imm.cn-beijing.aliyuncs.com'
return imm20200930Client(config)
@staticmethod
def main(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://help.aliyun.com/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
source = imm_20200930_models.CompareImageFacesRequestSource(
uri1='oss://test-bucket/test-object1.jpg',
uri2='oss://test-bucket/test-object2.jpg'
)
compare_image_faces_request = imm_20200930_models.CompareImageFacesRequest(
project_name='test-project',
source=source
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
client.compare_image_faces_with_options(compare_image_faces_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
@staticmethod
async def main_async(
args: List[str],
) -> None:
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To prevent security risks, we recommend that you call API operations or perform routine O&M as a RAM user.
# We recommend that you do not include your AccessKey pair (AccessKey ID and AccessKey secret) in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources within your account may be compromised.
# In this example, the AccessKey pair is read from the environment variables to implement identity verification for API access. For information about how to configure environment variables, see https://help.aliyun.com/document_detail/2361894.html.
imm_access_key_id = os.getenv("AccessKeyId")
imm_access_key_secret = os.getenv("AccessKeySecret")
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
source = imm_20200930_models.CompareImageFacesRequestSource(
uri1='oss://test-bucket/test-object1.jpg',
uri2='oss://test-bucket/test-object2.jpg'
)
compare_image_faces_request = imm_20200930_models.CompareImageFacesRequest(
project_name='test-project',
source=source
)
runtime = util_models.RuntimeOptions()
try:
# You can choose to print the response of the API operation.
await client.compare_image_faces_with_options_async(compare_image_faces_request, runtime)
except Exception as error:
# Print the error message if necessary.
UtilClient.assert_as_string(error.message)
if __name__ == '__main__':
Sample.main(sys.argv[1:])