文档管理中心
指南图形AR Engine(AR引擎服务)人脸识别与跟踪人脸跟踪(ArkTS)

人脸跟踪(ArkTS)

约束与限制

从6.1.0(23)开始,人脸跟踪能力支持部分Phone、部分Tablet、TV设备。请参考硬件要求判断设备是否支持人脸识别与跟踪特性(ARENGINE_FEATURE_TYPE_FACE)。

接口说明

人脸跟踪主要依赖ARFace,以下接口为人脸跟踪的相关接口。详细接口和说明,请参考AR Engine API参考

展开
接口名 描述
ARSession.getFrame 获取AR Engine处理后的一帧数据。
ARSession.getAllTrackables 获取当前session中包含的人脸对象。
ARFace.getGeometry 返回一个人脸几何对象。
ARFace.getBlendShapes 返回一个人脸微表情对象。

开发步骤

对于使用ArkTS的任何AR应用,首先需要参考AR特性检查接口检查当前设备是否支持该特性。若设备支持,创建一个AR会话ARViewContext,用于管理AR Engine的系统状态。AR会话ARViewContext的创建可以参考管理AR会话章节。

导入模块

人脸跟踪能力所需要导入的模块如下:

收起
自动换行
深色代码主题
复制
  1. import { arEngine, ARView, arViewController } from '@kit.AREngine';
  2. import { Node, Scene } from '@kit.ArkGraphics3D';
  3. import { BusinessError } from '@kit.BasicServicesKit';

定义变量

定义变量face接收人脸对象,定义变量faceGeometry接收人脸几何对象,定义变量faceBlendShapes接收人脸微表情对象。

收起
自动换行
深色代码主题
复制
  1. let face: arEngine.ARFace;
  2. let faceGeometry: arEngine.ARGeometry;
  3. let faceBlendShapes: arEngine.ARBlendShapes;

显示预览流

首先初始化AR会话和AR场景,可以参考初始化AR会话和AR场景章节。

更改type为ARType.FACE,更改cameraLensFacing为ARCameraLensFacing.FRONT,更改multiFaceMode为ARMultiFaceMode.MULTIFACE_ENABLE,启用前置相机的人脸跟踪能力。

收起
自动换行
深色代码主题
复制
  1. @Builder
  2. export function ARFaceBuilder(): void {
  3. ARFace();
  4. }
  5. @Component
  6. struct ARFace {
  7. @State arContext?: arViewController.ARViewContext = undefined;
  8. build(): void {
  9. NavDestination() {
  10. RelativeContainer() {
  11. if (this.arContext) {
  12. ARView({ context: this.arContext })
  13. .height('100%')
  14. .width('100%')
  15. .alignRules({
  16. center: { anchor: '__container__', align: VerticalAlign.Center },
  17. middle: { anchor: '__container__', align: HorizontalAlign.Center }
  18. })
  19. }
  20. }
  21. }
  22. .onAppear(() => {
  23. this.initARView();
  24. })
  25. .onWillDisappear(() => {
  26. this.stopARView();
  27. })
  28. .onShown(() => {
  29. this.resumeARView();
  30. })
  31. .onHidden(() => {
  32. this.pauseARView();
  33. })
  34. .hideTitleBar(true)
  35. .hideBackButton(true)
  36. .hideToolBar(true)
  37. }
  38. private initARView(): void {
  39. Scene.load().then((scene: Scene) => {
  40. let viewContext: arViewController.ARViewContext = new arViewController.ARViewContext();
  41. viewContext.scene = scene;
  42. viewContext.callback = new ARViewCallbackImpl();
  43. viewContext.config = {
  44. type: arEngine.ARType.FACE,
  45. planeFindingMode: arEngine.ARPlaneFindingMode.DISABLED,
  46. semanticMode: arEngine.ARSemanticMode.NONE,
  47. meshMode: arEngine.ARMeshMode.DISABLED,
  48. focusMode: arEngine.ARFocusMode.AUTO,
  49. cameraLensFacing: arEngine.ARCameraLensFacing.FRONT,
  50. multiFaceMode: arEngine.ARMultiFaceMode.MULTIFACE_DISABLE
  51. }
  52. viewContext.init().then(() => {
  53. this.arContext = viewContext;
  54. console.info('Succeeded in initializing ARView.');
  55. }).catch((err: BusinessError) => {
  56. console.error(`Failed to init ARView. Code is ${err.code}, message is ${err.message}.`);
  57. })
  58. })
  59. }
  60. private stopARView(): void {
  61. // ...
  62. }
  63. private resumeARView(): void {
  64. // ...
  65. }
  66. private pauseARView(): void {
  67. // ...
  68. }
  69. }

获取人脸几何数据和微表情数据

调用ARViewCallback,使用其中的onFrameUpdate方法进行帧数据更新,通过ARSession.getFrame方法获取当前帧,通过ARSession.getAllTrackables获得当前会话包含的人脸对象数据,通过ARFace.getGeometryARFace.getBlendShapes从人脸对象数据中获取识别到的几何信息和微表情信息,相关变量定义参考定义变量

收起
自动换行
深色代码主题
复制
  1. class ARViewCallbackImpl extends arViewController.ARViewCallback {
  2. onAnchorAdd(ctx: arViewController.ARViewContext, node: Node, anchor: arEngine.ARAnchor): void {
  3. // ...
  4. }
  5. onAnchorUpdate(ctx: arViewController.ARViewContext, node: Node, anchor: arEngine.ARAnchor): void {
  6. // ...
  7. }
  8. onFrameUpdate(ctx: arViewController.ARViewContext, sysBootTs: number): void {
  9. if (!ctx.session) {
  10. return;
  11. }
  12. let arSession: arEngine.ARSession = ctx.session;
  13. try {
  14. let frame: arEngine.ARFrame = arSession.getFrame();
  15. if (frame) {
  16. // 获取face信息
  17. let trackables: Array<arEngine.ARTrackable> = arSession.getAllTrackables(arEngine.ARTrackableType.FACE);
  18. for (let i = 0; i < trackables.length; ++i) {
  19. if (trackables[i].state !== arEngine.ARTrackingState.TRACKING) {
  20. console.error('Face not in tracking state');
  21. continue;
  22. }
  23. face = trackables[i] as arEngine.ARFace;
  24. faceGeometry = face.getGeometry();
  25. faceBlendShapes = face.getBlendShapes();
  26. if(faceGeometry){
  27. let tmpVert = faceGeometry.getVertices();
  28. let tmpIndices = faceGeometry.getIndices();
  29. }
  30. if(faceBlendShapes){
  31. let tmpData = faceBlendShapes.getData();
  32. let tmpTypes = faceBlendShapes.getTypes();
  33. }
  34. faceGeometry.release();
  35. faceBlendShapes.release();
  36. }
  37. }
  38. } catch (error) {
  39. const err: BusinessError = error as BusinessError;
  40. console.error(`Failed to update data. Code is ${err.code}, message is ${err.message}.`);
  41. }
  42. }
  43. }
在 指南 中进行搜索
请输入您想要搜索的关键词