gpt4 book ai didi

ios - try catch x : y: positions using VNFaceObservation

转载 作者:行者123 更新时间:2023-11-30 11:50:47 24 4
gpt4 key购买 nike

我使用下面的代码来捕获和绘制脸部特征。但是, try catch 图像之间的 x: y: 位置并不一致。我意识到我没有正确捕获它,所以如果有人可以提供一些指导。我想使用 x: y: 添加另一个图像的 subview 。非常感激。约翰。

 var noseCrestPoint = CGPoint()
var noseCrestPointX = CGFloat()
var noseCrestPointY = CGFloat()

context?.saveGState()
context?.setStrokeColor(UIColor.yellow.cgColor)
if let landmark = face.landmarks?.noseCrest {
for i in 0...landmark.pointCount - 1 { // last point is 0,0
let point = landmark.normalizedPoints[i]
if i == 0 {
context?.move(to: CGPoint(x: x + CGFloat(point.x) * w, y: y + CGFloat(point.y) * h))
noseCrestPoint = CGPoint(x: x + CGFloat(point.x) * w, y: y + CGFloat(point.y) * h)
noseCrestPointX = noseCrestPoint.x
noseCrestPointY = noseCrestPoint.y
} else {
context?.addLine(to: CGPoint(x: x + CGFloat(point.x) * w, y: y + CGFloat(point.y) * h))
}
}
}
context?.setLineWidth(3.0)
context?.drawPath(using: .stroke)
context?.saveGState()

最佳答案

好的,我找到了解决方案。第一个代码块是绘制脸部,第二个代码块是将我的其他图像放置在靠近眼睛的边界框中。

    // draw the face rect
let w = face.boundingBox.size.width * image.size.width
let h = face.boundingBox.size.height * image.size.height
let x = face.boundingBox.origin.x * image.size.width
let y = face.boundingBox.origin.y * image.size.height
let faceRect = CGRect(x: x, y: y, width: w, height: h)
context?.saveGState()
context?.setStrokeColor(UIColor.red.cgColor)
context?.setLineWidth(3.0)
context?.addRect(faceRect)
context?.drawPath(using: .stroke)
context?.restoreGState()



let eyeImageView = UIImageView()
let eyeImage = UIImage (named:"eyes2.png")
// eyeImageView.frame = CGRect(x: x, y: y, width: w/2, height: h/4)
eyeImageView.image = eyeImage;
eyeImageView.image?.draw(in: CGRect(x: x + (w/4), y: y, width: w/1.5, height: h/3))

这解决了我的问题。希望有人觉得这有帮助。问候JZ

关于ios - try catch x : y: positions using VNFaceObservation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48330136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com