The face verification service recognizes faces, performs high-precision comparison of human faces, and provides confidence scores to determine whether the faces belong to the same person. This cutting-edge technology can be used to intelligently categorize and manage photos. The on-device intelligent image recognition algorithm ensures accurate face recognition, enabling your app to provide better user experience.
Since: 5.0.0(12)
Visual information to be recognized. Currently, only the visual information of the PixelMap type in color data format RGBA_8888 is supported.
System capability: SystemCapability.AI.Face.Comparator
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| pixelMap | image.PixelMap | Yes | No | Image to be recognized. The color data format must be RGBA_8888. For details about the image requirements, see Constraints. |
Face verification result.
System capability: SystemCapability.AI.Face.Comparator
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| isSamePerson | boolean | Yes | No | Indicates whether the faces belong to the same person. The value true indicates yes, and the value** false** indicates no. |
| similarity | number | Yes | No | Similarity. The value is a floating point number ranging from 0 to 1. A larger value indicates a higher similarity. The value 1 indicates that the two faces are identical. |
init(): Promise<boolean>
Initializes the face verification service. This API uses a promise to return the result.
System capability: SystemCapability.AI.Face.Comparator
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Returns:
| Type | Description |
|---|---|
| Promise<boolean> | Indicates whether the initialization is successful, which is returned through a promise. The value true indicates yes, and the value false indicates no. |
Example:
import { faceComparator } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
async function initAndReleaseFaceComparator() {
// Initialize the face verification service.
const initResult = await faceComparator.init();
hilog.info(0x0000, 'faceComparatorSample', `Face comparator initialization result:${initResult}`);
if (initResult) {
hilog.info(0x0000, 'faceComparatorSample', 'Face comparator initialized successfully');
// Add the code for using the face verification service.
// Release the face verification service after the usage is complete.
await faceComparator.release();
hilog.info(0x0000, 'faceComparatorSample', 'Face comparator released successfully');
} else {
hilog.error(0x0000, 'faceComparatorSample', 'Failed to initialize face comparator');
}
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('initAndReleaseFaceComparator').onClick(() => {
// Call the function.
void initAndReleaseFaceComparator();
})
}
}
} release(): Promise<void>
Releases the face verification service. This API uses a promise to return the result.
System capability: SystemCapability.AI.Face.Comparator
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Returns:
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Example:
import { faceComparator } from '@kit.CoreVisionKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
async function initAndReleaseFaceComparator() {
// Initialize the face verification service.
const initResult = await faceComparator.init();
hilog.info(0x0000, 'faceComparatorSample', `Face comparator initialization result:${initResult}`);
if (initResult) {
hilog.info(0x0000, 'faceComparatorSample', 'Face comparator initialized successfully');
// Add the code for using the face verification service.
// Release the face verification service after the usage is complete.
await faceComparator.release();
hilog.info(0x0000, 'faceComparatorSample', 'Face comparator released successfully');
} else {
hilog.error(0x0000, 'faceComparatorSample', 'Failed to initialize face comparator');
}
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('initAndReleaseFaceComparator').onClick(() => {
// Call the function.
void initAndReleaseFaceComparator();
})
}
}
} compareFaces(visionInfo1: VisionInfo, visionInfo2: VisionInfo): Promise<FaceCompareResult>
Creates a face verification instance. This API uses a promise to return the result.
System capability: SystemCapability.AI.Face.Comparator
Model restriction: This API can be used only in the stage model.
Since: 5.0.0(12)
Parameters:
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| visionInfo1 | VisionInfo | Yes | First image that contains a face. |
| visionInfo2 | VisionInfo | Yes | Second image that contains a face. |
Returns:
| Type | Description |
|---|---|
| Promise<FaceCompareResult> | Face verification result returned through a promise. |
Error codes:
For details about the error codes, please refer to Core Vision Kit Error Codes.
| ID | Error Message |
|---|---|
| 200 | Run timed out, please try again later. |
| 401 | The parameter check failed. |
| 1008400001 | Failed to run face comparator, please try again. |
| 1008400002 | The face comparator service is abnormal. |
Example:
import { faceComparator } from '@kit.CoreVisionKit';
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { fileIo } from '@kit.CoreFileKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
async function faceCompareTest() {
let chooseImage: PixelMap | undefined = undefined;
let chooseImage1: PixelMap | undefined = undefined;
// Select two images from the gallery.
let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 2;
let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
let photoSelectResult = await photoPicker.select(photoSelectOptions);
let uris = photoSelectResult.photoUris;
if (uris.length !== 2) {
hilog.info(0x0000, 'testTag', 'selected uris length is not 2');
return;
}
// Convert the selected images into PixelMap.
let fileSource = await fileIo.open(uris[0], fileIo.OpenMode.READ_ONLY);
let imageSource = image.createImageSource(fileSource.fd);
chooseImage = await imageSource.createPixelMap();
fileSource = await fileIo.open(uris[1], fileIo.OpenMode.READ_ONLY);
imageSource = image.createImageSource(fileSource.fd);
chooseImage1 = await imageSource.createPixelMap();
if (!chooseImage || !chooseImage1) {
hilog.info(0x0000, 'testTag', 'chooseImage or chooseImage1 is undefined');
return;
}
// Call the face verification API.
let visionInfo: faceComparator.VisionInfo = {
pixelMap: chooseImage
};
let visionInfo1: faceComparator.VisionInfo = {
pixelMap: chooseImage1
};
let data: faceComparator.FaceCompareResult = await faceComparator.compareFaces(visionInfo, visionInfo1);
let similarity = (data.similarity * 100).toFixed(2);
let isSamePerson = data.isSamePerson ? 'is' : 'is not';
let faceString = `Similarity: ${similarity}%. ${isSamePerson} the same person`;
hilog.info(0x0000, 'testTag', 'faceString data is ' + faceString);
// Release resources.
if (chooseImage && chooseImage1) {
void chooseImage.release();
void chooseImage1.release();
}
if (fileSource) {
await fileIo.close(fileSource);
}
}
@Entry
@Component
struct Page {
build() {
Column(){
Button('faceCompareTest').onClick(() => {
// Call the function.
void faceCompareTest();
})
}
}
}