鸿蒙应用开发之基础视觉服务人脸比对基础

一、工具


DevEco Studio

二、项目介绍


开发步骤
在使用人脸比对时,将实现人脸比对相关的类添加至工程。
import { faceComparator } from '@kit.CoreVisionKit';
简单配置页面的布局,并在Button组件添加点击事件,拉起图库,选择图片。
Button('选择图片')
  .type(ButtonType.Capsule)
  .fontColor(Color.White)
  .alignSelf(ItemAlign.Center)
  .width('80%')
  .margin(10)
  .onClick(() => {
    // 拉起图库,获取图片资源
    this.selectImage();
  })
通过图库获取图片资源,将图片转换为PixelMap,并添加初始化和释放方法。
async aboutToAppear(): Promise {
  const initResult = await faceComparator.init();
  hilog.info(0x0000, TAG, `Face comparator initialization result:${initResult}`);
}


async aboutToDisappear(): Promise {
  await faceComparator.release();
  hilog.info(0x0000, TAG, 'Face comparator released successfully');
}


private async selectImage() {
  let uri = await this.openPhoto()
  if (uri === undefined) {
    hilog.error(0x0000, 'faceCompare', "Failed to get two image uris.");
  }
  this.loadImage(uri);
}


private openPhoto(): Promise {
  return new Promise((resolve, reject) => {
    let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
    photoPicker.select({
      MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
      maxSelectNumber: 2
    }).then(res => {
      resolve(res.photoUris);
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, TAG, `Failed to get photo image uris. code: ${err.code}, message: ${err.message}`);
      reject();
    });
  });
}


private loadImage(names: string[]) {
  setTimeout(async () => {
    let imageSource: image.ImageSource | undefined = undefined;
    let fileSource = await fileIo.open(names[0], fileIo.OpenMode.READ_ONLY);
    imageSource = image.createImageSource(fileSource.fd);
    this.chooseImage = await imageSource.createPixelMap();
    fileSource = await fileIo.open(names[1], fileIo.OpenMode.READ_ONLY);
    imageSource = image.createImageSource(fileSource.fd);
    this.chooseImage1 = await imageSource.createPixelMap();
    hilog.info(0x0000, 'faceCompare', 'this.chooseImage:', this.chooseImage, 'this.chooseImage1:', this.chooseImage1);
  }, 100
  )
}
实现人脸比对功能。实例化VisionInfo对象,传入两张图片的PixelMap,调用faceComparator.compareFaces方法进行人脸比对。
// 调用人脸比对接口
let visionInfo: faceComparator.VisionInfo = {
  pixelMap: this.chooseImage,
};
let visionInfo1: faceComparator.VisionInfo = {
  pixelMap: this.chooseImage1,
};
let data:faceComparator.FaceCompareResult = await faceComparator.compareFaces(visionInfo, visionInfo1);
(可选)如果需要将结果展示在界面上,可以用下列代码。
let data:faceComparator.FaceCompareResult = await faceComparator.compareFaces(visionInfo, visionInfo1);
let faceString = "degree of similarity:"+ this.toPercentage(data.similarity)+((data.isSamePerson)?". is":". no")+ " same person";
hilog.info(0x0000, 'testTag', "faceString data is " + faceString);
this.dataValues = faceString;
开发实例
import { faceComparator } from '@kit.CoreVisionKit';
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo } from '@kit.CoreFileKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';


const TAG: string = "FaceCompareSample";


@Entry
@Component
struct Index {
  @State chooseImage: PixelMap | undefined = undefined
  @State chooseImage1: PixelMap | undefined = undefined
  @State dataValues: string = ''


  async aboutToAppear(): Promise {
    const initResult = await faceComparator.init();
    hilog.info(0x0000, TAG, `Face comparator initialization result:${initResult}`);
  }


  async aboutToDisappear(): Promise {
    await faceComparator.release();
    hilog.info(0x0000, TAG, 'Face comparator released successfully');
  }


  build() {
    Column() {
      Image(this.chooseImage)
        .objectFit(ImageFit.Fill)
        .height('30%')
        .accessibilityDescription("默认图片1")
      Image(this.chooseImage1)
        .objectFit(ImageFit.Fill)
        .height('30%')
        .accessibilityDescription("默认图片2")
      Text(this.dataValues)
        .copyOption(CopyOptions.LocalDevice)
        .height('15%')
        .margin(10)
        .width('60%')
      Button('选择图片')
        .type(ButtonType.Capsule)
        .fontColor(Color.White)
        .alignSelf(ItemAlign.Center)
        .width('80%')
        .margin(10)
        .onClick(() => {
          // 拉起图库
          this.selectImage()
        })
      Button('人脸比对')
        .type(ButtonType.Capsule)
        .fontColor(Color.White)
        .alignSelf(ItemAlign.Center)
        .width('80%')
        .margin(10)
        .onClick(() => {
          if(!this.chooseImage || !this.chooseImage1) {
            hilog.error(0x0000, TAG, "Failed to choose image");
            return;
          }
          // 调用人脸比对接口
          let visionInfo: faceComparator.VisionInfo = {
            pixelMap: this.chooseImage,
          };
          let visionInfo1: faceComparator.VisionInfo = {
            pixelMap: this.chooseImage1,
          };
          faceComparator.compareFaces(visionInfo, visionInfo1)
            .then((data: faceComparator.FaceCompareResult) => {
              let faceString = "degree of similarity:"+ this.toPercentage(data.similarity)+((data.isSamePerson)?". is":". no")+ " same person";
              hilog.info(0x0000, TAG, "faceString data is " + faceString);
              this.dataValues = faceString;
            })
            .catch((error: BusinessError) => {
              hilog.error(0x0000, TAG, `Face comparison failed. Code: ${error.code}, message: ${error.message}`);
              this.dataValues = `Error: ${error.message}`;
            });
        })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }


  private toPercentage(num: number): string {
    return `${(num * 100).toFixed(2)}%`;
  }


  private async selectImage() {
    let uri = await this.openPhoto()
    if (uri === undefined) {
      hilog.error(0x0000, TAG, "Failed to get two image uris.");
    }
    this.loadImage(uri);
  }


  private openPhoto(): Promise {
    return new Promise((resolve, reject) => {
      let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
      photoPicker.select({
        MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE,
        maxSelectNumber: 2
      }).then(res => {
        resolve(res.photoUris);
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, TAG, `Failed to get photo image uris. code: ${err.code}, message: ${err.message}`);
        reject();
      });
    });
  }


  private loadImage(names: string[]) {
    setTimeout(async () => {
      let imageSource: image.ImageSource | undefined = undefined;
      let fileSource = await fileIo.open(names[0], fileIo.OpenMode.READ_ONLY);
      imageSource = image.createImageSource(fileSource.fd);
      this.chooseImage = await imageSource.createPixelMap();
      fileSource = await fileIo.open(names[1], fileIo.OpenMode.READ_ONLY);
      imageSource = image.createImageSource(fileSource.fd);
      this.chooseImage1 = await imageSource.createPixelMap();
      hilog.info(0x0000, TAG, 'this.chooseImage:', this.chooseImage, 'this.chooseImage1:', this.chooseImage1);
    }, 100
    )
  }
}


posted @ 2025-03-26 16:39  同步—TLNX  阅读(23)  评论(0)    收藏  举报