鸿蒙人脸识别

鸿蒙人脸识别

参考文档

鸿蒙原生提供用于实现人脸识别的API。感觉能够实现一些好玩的效果。

javascript 复制代码
private detectFace(img: PixelMap) {
  // 调用人脸检测接口
  faceDetector.init();
  let visionInfo: faceDetector.VisionInfo = {
    pixelMap: img,
  };
  faceDetector.detect(visionInfo)
    .then((data: faceDetector.Face[]) => {
      if (data.length === 0) {
      } else {
        let faceString = JSON.stringify(data);
        hilog.info(0x0000, 'faceDetectorSample', "faceString data is " + faceString);
        promptAction.showDialog({
          message: `识别到信息  ${faceString}`
        })
      }
    })
    .catch((error: BusinessError) => {
      hilog.error(0x0000, 'faceDetectorSample', `Face detection failed. Code: ${error.code}, message: ${error.message}`);
    });
  faceDetector.release();
}

示例代码

javascript 复制代码
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { image } from '@kit.ImageKit';
import { fileIo } from '@kit.CoreFileKit';
import { faceDetector } from '@kit.CoreVisionKit';
import { promptAction } from '@kit.ArkUI';

const TAG: string = "index";

@Entry
@Component
struct Index {
  @State chooseImage: image.PixelMap | null = null
  @State segImage: image.PixelMap | null = null

  private openPhoto(): Promise<Array<string>> {
    return new Promise<Array<string>>((resolve, reject) => {
      let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
      PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
      PhotoSelectOptions.maxSelectNumber = 1;
      let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker();
      hilog.info(0x0000, TAG, 'PhotoViewPicker.select successfully, PhotoSelectResult uri: ');
      photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult) => {
        hilog.info(0x0000, TAG, `PhotoViewPicker.select successfully, PhotoSelectResult uri: ${PhotoSelectResult.photoUris}`);
        resolve(PhotoSelectResult.photoUris)
      }).catch((err: BusinessError) => {
        hilog.error(0x0000, TAG, `PhotoViewPicker.select failed with errCode: ${err.code}, errMessage: ${err.message}`);
        reject();
      });
    })
  }

  private async loadImage(path: string) {
    let imageSource: image.ImageSource | undefined = undefined
    let fileSource = await fileIo.open(path, fileIo.OpenMode.READ_ONLY)
    imageSource = image.createImageSource(fileSource.fd)
    this.chooseImage = await imageSource.createPixelMap()
    hilog.info(0x0000, TAG, `this.chooseImage===${this.chooseImage}`);
  }

  private detectFace(img: PixelMap) {
    // 调用人脸检测接口
    faceDetector.init();
    let visionInfo: faceDetector.VisionInfo = {
      pixelMap: img,
    };
    faceDetector.detect(visionInfo)
      .then((data: faceDetector.Face[]) => {
        if (data.length === 0) {
        } else {
          let faceString = JSON.stringify(data);
          hilog.info(0x0000, 'faceDetectorSample', "faceString data is " + faceString);
          promptAction.showDialog({
            message: `识别到信息  ${faceString}`
          })
        }
      })
      .catch((error: BusinessError) => {
        hilog.error(0x0000, 'faceDetectorSample', `Face detection failed. Code: ${error.code}, message: ${error.message}`);
      });
    faceDetector.release();
  }

  build() {
    Column() {
      Image(this.chooseImage)
        .objectFit(ImageFit.Contain)
        .width('100%')
        .height('40%')

      Row() {
        Image(this.segImage)
          .objectFit(ImageFit.Contain)
          .width('100%')
          .height('100%')
      }
      .width('100%')
      .height('40%')

      Blank()

      Row() {
        Text('选择图片')
          .height(50)
          .width(140)
          .borderRadius(25)
          .textAlign(TextAlign.Center)
          .backgroundColor(Color.Orange)
          .onClick(async () => {
            let urls = await this.openPhoto()
            if (urls.length > 0) {
              let url = urls[0]
              this.loadImage(url)
            }
          })

        Text('人脸识别')
          .height(50)
          .width(140)
          .borderRadius(25)
          .textAlign(TextAlign.Center)
          .backgroundColor(Color.Orange)
          .onClick(async () => {
            if (this.chooseImage) {
              this.detectFace(this.chooseImage)
            }
          })
      }
    }
    .height('100%')
    .width('100%')
  }
}

后记

最近利用这些知识上架了两个应用

小鱼证件照,免费便捷的证件照生成工具。

小鱼抽签。出门之前占卜吉凶,总会有好事发生。

第1签 锤离成道

天开地辟结良缘,日吉时良万事全,若得此签非小可,人行中正帝王宜

上上签

子宫

此卦盘古初开天地之象,诸事皆吉也

快使用鸿蒙next版本扫一扫体验一下吧~

相关推荐
ss2731 小时前
020:共享锁深度解析:从AQS原理到高并发实践
数据库·redis·缓存
向哆哆1 小时前
深入理解华为CANN静态Tensor编程范式:极致性能的算子开发之道
华为·算子·昇腾·cann
9***Y485 小时前
HarmonyOS在智能车载中的导航系统
华为·harmonyos
q***46525 小时前
Win10下安装 Redis
数据库·redis·缓存
晚霞的不甘6 小时前
CANN:华为全栈AI计算框架的深度解析(终极扩展版 · 完整篇)
人工智能·华为
不爱吃糖的程序媛10 小时前
华为 CANN:昇腾 AI 的异构计算架构核心与开源生态解析
人工智能·华为·架构
马剑威(威哥爱编程)10 小时前
鸿蒙6开发视频播放器的屏幕方向适配问题
java·音视频·harmonyos
1***Q78410 小时前
HarmonyOS在智能汽车中的V2X通信
华为·汽车·harmonyos
p***c94910 小时前
HarmonyOS应用分发
华为·harmonyos
晚风吹人醒.10 小时前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件