Updated on 2024-05-20 GMT+08:00

Face Verification

Function

This API is used to compare two faces to verify whether they belong to the same person and return the confidence level. If each input image contains multiple faces, the API selects the largest face for comparison.

Prerequisites:

Ensure that you have enabled FRS. For detailed operations, see Applying for FRS.

Restrictions:
  • The total size of the two images input for face verification is less than 8 MB.
  • Only images in JPG, PNG, JPEG, or BMP format can be recognized.
  • Use standard JSON format in the body of the application/json request.
  • Do not use carriage return characters in Base64 code.
  • The system does not save images of users.
  • The image size must be less than 8 MB. If the image is too large, the latency is long and the image information volume is small. It is recommended that the image size be less than 1 MB.
  • The image resolution must be less than 4,096 x 2,160. The face resolution in an image must be greater than 80 x 80. It is recommended that the face resolution be greater than 120 x 120.
  • To ensure the recognition effect, facial images need to meet the following requirements:
    1. The illumination should be greater than 200 lux and there is no light reflection or shadow caused by strong light.
    2. The overall image is clear without obvious motion blur and the face in it is not blocked.
    3. The side face angle does not exceed 30°, and the tilt angle and horizontal angle do not exceed 15°. The face in an image must be a vertically placed front face.
  • For details about other restrictions, see Restrictions and Limitations.
Suggestions:
  • A larger image does not significantly improve the recognition algorithm precision but will cause a long latency. Therefore, you are advised to upload an image smaller than 1 MB. Generally, 500 KB is enough.
  • It is recommended that the size of an image stored on OBS be less than 1 MB.
  • It is recommended that the face resolution in an image be greater than 120 x 120.

URI

POST /v2/{project_id}/face-compare

Table 1 Path parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID. For details about how to obtain the ID, see Obtaining the Project ID/Account Name/AK/SK.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token.

During API authentication using a token, the token is added to requests to obtain permissions for calling the API. The value of X-Subject-Token in the response header is the obtained token.

Content-Type

Yes

String

MIME type of the request body. The value is application/json.

Table 3 Request body parameters

Parameter

Type

Mandatory

Description

image1_url

String

Either image1_url, image1_file, or image1_base64 is mandatory.

Image URL. Currently, only the URL of an OBS bucket on Huawei Cloud is supported and FRS must have the permission to read data in the OBS bucket. For details about how to enable the read permission, see Service Authorization.

image1_file

File

Either image1_file, image1_url, or image1_base64 is mandatory.

Local image file, whose size cannot exceed 8 MB. It is recommended that the image size be less than 1 MB. The request format is Multipart.

image1_base64

String

Either image1_base64, image1_url, or image1_file is mandatory.

Image data (Base64-encoded). The value must meet the following requirements:
  • The image size after Base64 encoding cannot exceed 8 MB. It is recommended that the image size be less than 1 MB.
  • The image is in JPG, JPEG, BMP, or PNG format.

image2_url

String

Either image2_url, image2_file, or image2_base64 is mandatory.

Image URL. Currently, only the URL of an OBS bucket on Huawei Cloud is supported and FRS must have the permission to read data in the OBS bucket. For details about how to enable the read permission, see Service Authorization.

image2_file

File

Either image2_file, image2_url, or image2_base64 is mandatory.

Local image file, whose size cannot exceed 8 MB. It is recommended that the image size be less than 1 MB. The request format is Multipart.

image2_base64

String

Either image2_base64, image2_url, or image2_file is mandatory.

Image data (Base64-encoded). The value must meet the following requirements:
  • The image size after Base64 encoding cannot exceed 8 MB. It is recommended that the image size be less than 1 MB.
  • The image is in JPG, JPEG, BMP, or PNG format.

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

image1_face

CompareFace object

Face detected in the first image. For details, see DetectFace. This parameter is not included when the API fails to be called.

image2_face

CompareFace object

Face detected in the second image. For details, see DetectFace.

similarity

Double

Face similarity. The value ranges from 0 to 1. A larger value indicates a higher similarity degree. Generally, if the value is greater than 0.93, the faces in two images belong to one person. This parameter is not included when the API fails to be called.

Table 5 CompareFace

Parameter

Type

Description

bounding_box

BoundingBox object

Position of a face in an image

Table 6 BoundingBox

Parameter

Type

Description

width

Integer

Width of a rectangle

top_left_y

Integer

Vertical coordinate of the upper-left corner of a rectangle

top_left_x

Integer

Horizontal coordinate of the upper-left corner of a rectangle

height

Integer

Height of a rectangle

Status code: 400

Table 7 Response body parameters

Parameter

Type

Description

error_code

String

Error code when calling the API failed. This parameter is not included when the API is successfully called.

error_msg

String

Error message returned after the API fails to be called. This parameter is not included when the API is successfully called.

Example Requests

For details about how to obtain the value of X-Auth-Token, see Authentication.

  • Example request (Method 1: Use a Base64-encoded image.)
    POST https://{endpoint}/v2/{project_id}/face-compare
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT...
    
    Request Body:
    {
      "image1_base64":"/9j/4AAQSkZJRgABAgEASABIAAD...",
      "image2_base64":"/9j/4AAQSkZJRgABAgEASABIAAD..."
    }
  • Example request (Method 2: Use an image file.)
    POST https://{endpoint}/v2/{project_id}/face-compare
    Request Header:
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT...
    
    Request Body:
    image1_file: File (image file)
    image2_file: File (image file)
  • Example request (Method 3: Use the image URL.)
    POST https://{endpoint}/v2/{project_id}/face-compare
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDT...
    
    Request Body:
    {
      "image1_url":"/BucketName/ObjectName",
      "image2_url":"/BucketName/ObjectName"
    }

Example Responses

Status code: 200

Response example (successful request)
{
  "image1_face": {
    "bounding_box": {
      "width": 174,
      "top_left_y": 37,
      "top_left_x": 22,
      "height": 174
    }
  },
  "similarity": 0.4078676104545593,
  "image2_face": {
    "bounding_box": {
      "width": 118,
      "top_left_y": 28,
      "top_left_x": 94,
      "height": 118
    }
  }
}

Status code: 400

Example response (failed request)
{
  "error_code": "FRS.0501",
  "error_msg": "Detect no face, check out your picture."
}

Status Code

For details about the status code, see Status Codes.

Error Code

For details about the error code, see Error Codes.