我正在尝试绘制一个圆圈来表示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
的左上角。
答案 0 :(得分:0)