Theory
除了通过常规自拍相机捕获的 RGB 通道之外,TrueDepth 传感器还可以让 iPhone X ... iPhone 14 生成高质量的 ZDepth 通道。 ZDepth 通道使我们能够在视觉上做出区分,无论是真实的人脸还是照片。在 ZDepth 通道中,人脸表示为渐变,但照片几乎具有纯色,因为照片平面上的所有像素与相机的距离相等。
AV基金会
目前 AVFoundation API 没有 Bool 类型的实例属性,可以让你判断它是真人脸还是照片,但 AVFoundation 的捕获子系统为你提供了AV深度数据 https://developer.apple.com/documentation/avfoundation/avdepthdata类 – 相机设备捕获的每像素距离数据(深度图)的容器。深度图描述了每个像素到物体的距离(以米为单位)。
@available(iOS 11.0, *)
open class AVDepthData: NSObject {
open var depthDataType: OSType { get }
open var depthDataMap: CVPixelBuffer { get }
open var isDepthDataFiltered: Bool { get }
open var depthDataAccuracy: AVDepthDataAccuracy { get }
}
像素缓冲区能够包含深度数据的每像素深度或视差图 https://learn.foundry.com/ocula/Content/user_guide/disparitygenerator/DispGen_Generating.html.
var depthDataMap: CVPixelBuffer { get }
ARKit
感谢 AVFoundation 和 ARKit 的心跳芯动 https://developer.apple.com/documentation/coremotion会话(在某种程度上它也使用Vision)。当然,您可以使用此框架进行人脸检测,但请记住,由于其“重金属”跟踪子系统,ARKit 是一个计算密集型模块。要成功检测真人脸(而不是照片),请使用ARF面锚 https://stackoverflow.com/questions/52893075/what-is-aranchor-exactly/52899502#52899502允许您以 60 fps 记录头部的运动和方向,面部混合形状 https://developer.apple.com/documentation/arkit/arfaceanchor/2928251-blendshapes允许您实时记录用户的面部表情。为了使无法使用视频而不是真人来模拟面部表情,请使用 AR 应用程序的随机文本命令强制您显示特定的面部表情。
Vision
实施苹果视觉 https://developer.apple.com/documentation/vision and CoreML https://developer.apple.com/documentation/coreml识别和分类包含在其中的人脸的技术CVPixelBuffer。但请记住,您需要 ZDepth 到 RGB 的转换才能与 Apple Vision 配合使用 - AI / ML 移动框架目前无法直接使用深度图数据。当您想要使用 RGBD 数据进行身份验证,并且只需要识别一两个用户的面部时,它会大大简化模型学习过程的任务。您所要做的就是创建一个mlmodel
用于包含 ZDepth 面部图像的许多变化的 Vision。
您可以使用 Apple Create ML 应用程序来生成轻量级且有效的mlmodel
files.
有用的链接
要可视化来自 TrueDepth 相机的深度数据,请使用以下命令示例代码 https://developer.apple.com/documentation/avfoundation/additional_data_capture/streaming_depth_data_from_the_truedepth_camera。您可以找到使用 Vision 检测和分类图像的示例代码here https://developer.apple.com/documentation/vision/detecting_objects_in_still_images and here https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml。另外,看看this https://stackoverflow.com/questions/49589495/map-avdepthdata-to-rgba-from-pixelbuffer-in-swift发帖了解如何将 AVDepthData 转换为常规 RGB 模式。