我目前正在使用 Vision 和 ARKit 来查找框架中的任何面孔。我用来做这个的代码如下:
func runFaceDetection() {
let pixelBuffer : CVPixelBuffer? = (sceneView.session.currentFrame?.capturedImage)
if pixelBuffer == nil { return }
let ciImage = CIImage(cvPixelBuffer: pixelBuffer!)
let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage, options: [:])
do {
try imageRequestHandler.perform(self.visionRequests)
} catch {
print(error)
}
}
我使用以下代码尝试将这些返回的矩形映射到给定的面:
func faceRectanglesCompletionHandler(request: VNRequest, error: Error?) {
if error != nil {
print("error: " + (error?.localizedDescription)!)
return
}
guard let faces = request.results else {
print("No faces found in frame")
return
}
DispatchQueue.main.async {
for faceRect in self.faceRects {
faceRect.removeFromSuperview()
}
self.faceRects = []
}
for face in faces {
let observation = face as! VNFaceObservation
print(observation.boundingBox)
let newWidth = bounds.width * observation.boundingBox.width
let newHeight = bounds.height * observation.boundingBox.height
let newX = bounds.width * observation.boundingBox.origin.x
let newY = bounds.height * observation.boundingBox.origin.y
let newBoundingBox = CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
DispatchQueue.main.async {
let faceView = UIView.init(frame: newBoundingBox)
faceView.backgroundColor = .red
faceView.alpha = 0.5
self.faceRects.append(faceView)
}
}
DispatchQueue.main.async {
for faceRect in self.faceRects {
self.view.addSubview(faceRect)
}
}
}
问题是这不能正确地将面部矩形映射到屏幕上的点。我的 viewController 的 bounds 属性设置为bounds = sceneView.bounds,其中 sceneView 是我的 AR 场景。当面部矩形出现时,它们通常不会出现,它们将显示为具有太多高度和宽度不足的细长矩形。我在映射代码中做错了什么?问题的图像如下。