AVCaptureVideoPreviewLayer

AVCaptureVideoPreviewLayer

我正在尝试绘制一个圆圈来表示VNDetectFaceLandmarksRequest检测到的leftPupil的结果。

按照normalizedPoints的命名约定,我猜它已经通过一些规则进行了规范化。那么,如何缩放该点并将其转换为AVCaptureVideoPreviewLayer或其他视图?

从一些Vision教程中,我看到了如下所示的转换:

let faceLandmarkPoints = convertedPoints.map { (point: (x: CGFloat, y: CGFloat)) -> (x: CGFloat, y: CGFloat) in
                    let pointX = point.x * boundingBox.width + boundingBox.origin.x
                    let pointY = point.y * boundingBox.height + boundingBox.origin.y

                    return (x: pointX, y: pointY)
                }

但是,它实际上对我不起作用。我的完整代码如下。

转型:
// Rescale the points from vision coordinates
func convertPointsForFace(landmark: VNFaceLandmarkRegion2D?, _ boundingBox: CGRect)
    -> [(x: CGFloat, y: CGFloat)]?{
        if let points = landmark?.normalizedPoints{
            let convertedPoints = self.convertToCGFloat(points: points)

            let faceLandmarkPoints = convertedPoints.map { (point: (x: CGFloat, y: CGFloat)) -> (x: CGFloat, y: CGFloat) in
                let pointX = point.x * boundingBox.width + boundingBox.origin.x
                let pointY = point.y * boundingBox.height + boundingBox.origin.y

                return (x: pointX, y: pointY)
            }
            return faceLandmarkPoints
        }
        return nil
}

得出一点:
// Draw a point on preview layer
func drawPoint(point: (x: CGFloat, y: CGFloat)){
    // We use a CAShaperLayer to draw the circle
    let shapeLayer = CAShapeLayer()
    let circlePath = UIBezierPath(arcCenter: CGPoint(x: point.x,y: point.y),
                                  radius: CGFloat(20),
                                  startAngle: CGFloat(0),
                                  endAngle:CGFloat(Double.pi * 2),
                                  clockwise: true)
    shapeLayer.path = circlePath.cgPath
    shapeLayer.fillColor = UIColor.red.cgColor
    shapeLayer.strokeColor = UIColor.red.cgColor
    shapeLayer.lineWidth = 1.0
    self.previewLayer.addSublayer(shapeLayer)
}
VNDetectFaceLandmarksRequest完成处理程序:
func landmarkDetectCcompletionHandler(request: VNRequest, _ error: Error?){
    if error != nil {
        print("Errors in the landmark detection: \(error.debugDescription)")
    }
    guard let observations = request.results as? [VNFaceObservation] else {
        fatalError("Failed to convert result to VNFaceObservation type")
    }
    for face in observations {

        DispatchQueue.main.async {
            [weak self] in
            let faceBox = face.boundingBox
            let leftEye = face.landmarks?.leftPupil
            let leftEyePoints = self?.convertPointsForFace(landmark: leftEye,
                                                           faceBox)
            print(leftEyePoints?.first!.x, leftEyePoints?.first!.y)
            self?.drawPoint(point: (leftEyePoints?.first)!)
        }
    }
}

实心圆圈始终显示在AVCaptureVideoPreviewLayer的左上方。

最佳答案

您是否尝试过AVCaptureVideoPreviewLayer

  • layerPointConverted(fromCaptureDevicePoint:)
  • layerRectConverted(fromMetadataOutputRect:)吗?
  • 07-28 06:28