คู่มือนักพัฒนาซอฟต์แวร์ Augmented Faces สำหรับ Android

ดูวิธีใช้ฟีเจอร์ Augmented Faces ในแอปของคุณเอง

ข้อกำหนดเบื้องต้น

ตรวจสอบว่าคุณเข้าใจแนวคิด AR พื้นฐาน และวิธีกำหนดค่าเซสชัน ARCore ก่อนดำเนินการต่อ

การใช้ Augmented Faces ใน Android

  1. กำหนดค่าเซสชัน ARCore
  2. เข้าถึงใบหน้าที่ตรวจพบ

กำหนดค่าเซสชัน ARCore

เลือกกล้องหน้าในเซสชัน ARCore ที่มีอยู่เพื่อเริ่มใช้ Augmented Faces โปรดทราบว่าการเลือกกล้องหน้าจะทำให้มีการเปลี่ยนแปลงหลายอย่างในลักษณะการทำงานของ ARCore

Java

// Set a camera configuration that usese the front-facing camera.
CameraConfigFilter filter =
    new CameraConfigFilter(session).setFacingDirection(CameraConfig.FacingDirection.FRONT);
CameraConfig cameraConfig = session.getSupportedCameraConfigs(filter).get(0);
session.setCameraConfig(cameraConfig);

Kotlin

// Set a camera configuration that usese the front-facing camera.
val filter = CameraConfigFilter(session).setFacingDirection(CameraConfig.FacingDirection.FRONT)
val cameraConfig = session.getSupportedCameraConfigs(filter)[0]
session.cameraConfig = cameraConfig

เปิดใช้ AugmentedFaceMode:

Java

Config config = new Config(session);
config.setAugmentedFaceMode(Config.AugmentedFaceMode.MESH3D);
session.configure(config);

Kotlin

val config = Config(session)
config.augmentedFaceMode = Config.AugmentedFaceMode.MESH3D
session.configure(config)

การวางแนวแบบตาข่ายของใบหน้า

สังเกตการวางแนวของตาข่ายด้านใบหน้า:

เข้าถึงใบหน้าที่ตรวจพบ

รับ Trackable สำหรับแต่ละเฟรม Trackable คือสิ่งที่ ARCore ติดตามได้และสามารถแนบ Anchor ได้

Java

// ARCore's face detection works best on upright faces, relative to gravity.
Collection<AugmentedFace> faces = session.getAllTrackables(AugmentedFace.class);

Kotlin

// ARCore's face detection works best on upright faces, relative to gravity.
val faces = session.getAllTrackables(AugmentedFace::class.java)

รับ TrackingState สำหรับแต่ละ Trackable หากเป็นคำว่า TRACKING ตอนนี้ ARCore จะรู้จักท่าของท่าดังกล่าว

Java

for (AugmentedFace face : faces) {
  if (face.getTrackingState() == TrackingState.TRACKING) {
    // UVs and indices can be cached as they do not change during the session.
    FloatBuffer uvs = face.getMeshTextureCoordinates();
    ShortBuffer indices = face.getMeshTriangleIndices();
    // Center and region poses, mesh vertices, and normals are updated each frame.
    Pose facePose = face.getCenterPose();
    FloatBuffer faceVertices = face.getMeshVertices();
    FloatBuffer faceNormals = face.getMeshNormals();
    // Render the face using these values with OpenGL.
  }
}

Kotlin

faces.forEach { face ->
  if (face.trackingState == TrackingState.TRACKING) {
    // UVs and indices can be cached as they do not change during the session.
    val uvs = face.meshTextureCoordinates
    val indices = face.meshTriangleIndices
    // Center and region poses, mesh vertices, and normals are updated each frame.
    val facePose = face.centerPose
    val faceVertices = face.meshVertices
    val faceNormals = face.meshNormals
    // Render the face using these values with OpenGL.
  }
}