你好我们正在尝试整合Vision框架来检测一张脸并跟踪双眼,然后我试图测量Vision检测到的脸部中心之间的距离。我现在的问题是如何根据Vision给我的眼睛中心的两个点来获得现实世界中的这个距离。识别到脸部后,我使用以下代码计算眼睛中心之间的距离
guard let sceneView = self.view as? ARSKView, let currentFrame = sceneView.session.currentFrame else {
return
}
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
// left pupil
context?.saveGState()
context?.setStrokeColor(UIColor.yellow.cgColor)
var leftIrisLocation = CGPoint()
if let landmark = face.landmarks?.leftPupil {
for i in 0...landmark.pointCount - 1 { // last point is 0,0
let point = landmark.normalizedPoints[i]
if i == 0 {
leftIrisLocation = CGPoint(x: x + CGFloat(point.x) * w, y: y + CGFloat(point.y) * h)
context?.move(to: leftIrisLocation)
context?.addEllipse(in: CGRect(origin: leftIrisLocation, size: CGSize(width: 10, height: 10)))
}
}
}
context?.closePath()
context?.setLineWidth(8.0)
context?.drawPath(using: .stroke)
context?.saveGState()
// right pupil
context?.saveGState()
context?.setStrokeColor(UIColor.yellow.cgColor)
var rightIrisLocation = CGPoint()
if let landmark = face.landmarks?.rightPupil {
for i in 0...landmark.pointCount - 1 { // last point is 0,0
let point = landmark.normalizedPoints[i]
if i == 0 {
rightIrisLocation = CGPoint(x: x + CGFloat(point.x) * w, y: y + CGFloat(point.y) * h)
context?.move(to: rightIrisLocation)
context?.addEllipse(in: CGRect(origin: rightIrisLocation, size: CGSize(width: 10, height: 10)))
}
}
}
let distanceFromIris = self.distanceFrom(p1: leftIrisLocation, to: rightIrisLocation)
此时我在图像上有距离但没有真实世界距离,所以我尝试在当前帧上点击测试以获得从每只眼睛到相机的距离,然后使用毕达哥拉斯定理计算最终距离如下:
for leftResult in currentFrame.hitTest(leftIrisLocation, types: [.featurePoint, .existingPlaneUsingExtent]) {
print("Distance left iris to camera")
print(leftResult.distance)
}
for rightResult in currentFrame.hitTest(rightIrisLocation, types: [.featurePoint, .existingPlaneUsingExtent]) {
print("Distance right iris to camera")
print(rightResult.distance)
}
let distanceBetweenIris = sqrt((leftResult.distance * leftResult.distance) - (rightResult.distance * rightResult.distance))
但是这个distanceBetweenIris
变得非常大,像20厘米,应该接近5厘米~6厘米,任何想法如何减少这个大错误?