视觉框架:在跟踪用户的面部时,如何使图像(如帽子)与面部同步移动?

时间:2019-01-10 19:40:17

标签: ios swift vision

我正在尝试执行以下操作:

  1. 启动前置摄像头
  2. 检测人的脸
  3. 将3D /图像叠加在他们的脸部(例如帽子)上

使用来自Apple(here)的示例项目,前两个步骤可以正常工作。但是,现在我想完成第三步,并添加UIImage作为子视图,并使其与脸部同步移动,以使帽子(例如)在用户移动相机时保持在用户头部的顶部。

我能够添加图像,并使其在脸上移动,从而可以从示例项目中编辑以下代码:

fileprivate func addIndicators(to faceRectanglePath: CGMutablePath, faceLandmarksPath: CGMutablePath, for faceObservation: VNFaceObservation) {
    let displaySize = self.captureDeviceResolution

    let faceBounds = VNImageRectForNormalizedRect(faceObservation.boundingBox, Int(displaySize.width), Int(displaySize.height))
    faceRectanglePath.addRect(faceBounds)

    if let landmarks = faceObservation.landmarks {
        // Landmarks are relative to -- and normalized within --- face bounds
        let affineTransform = CGAffineTransform(translationX: faceBounds.origin.x, y: faceBounds.origin.y)
            .scaledBy(x: faceBounds.size.width, y: faceBounds.size.height)

        // Treat eyebrows and lines as open-ended regions when drawing paths.
        let openLandmarkRegions: [VNFaceLandmarkRegion2D?] = [
            landmarks.leftEyebrow,
            landmarks.rightEyebrow,
            landmarks.faceContour,
            landmarks.noseCrest,
            landmarks.medianLine
        ]
        for openLandmarkRegion in openLandmarkRegions where openLandmarkRegion != nil {
            self.addPoints(in: openLandmarkRegion!, to: faceLandmarksPath, applying: affineTransform, closingWhenComplete: false)
        }

        // Draw eyes, lips, and nose as closed regions.
        let closedLandmarkRegions: [VNFaceLandmarkRegion2D?] = [
            landmarks.leftEye,
            landmarks.rightEye,
            landmarks.outerLips,
            landmarks.innerLips,
            landmarks.nose
        ]
        for closedLandmarkRegion in closedLandmarkRegions where closedLandmarkRegion != nil {
            self.addPoints(in: closedLandmarkRegion!, to: faceLandmarksPath, applying: affineTransform, closingWhenComplete: true)
        }



        self.imageView.frame = CGRect(x: faceBounds.origin.x, y: faceBounds.origin.y, width: 300, height: 165)
        self.previewView?.addSubview(self.imageView)

    }
}

但是我不确定用于imageView.frame的X和Y值是多少,因为使用faceBounds.origin结果并不十分一致。

如果您想尝试一下,这里是修改后的示例项目的链接:https://files.fm/f/zqzrd6gf

谢谢。

0 个答案:

没有答案