swift - VNDetectFaceRectanglesRequest 没有调用 completionHandler

标签 swift face-detection apple-vision

我正在尝试检测人脸。它无法检测到,因为未调用 handleFaces。我想知道为什么不调用它,如果我想检测人脸,我该如何解决。

faceDetectionRequest = VNDetectFaceRectanglesRequest(completionHandler: self.handleFaces)

func handleFaces(request: VNRequest, error: Error?) {
    DispatchQueue.main.async {
        //perform all the UI updates on the main queue
        guard let results = request.results as? [VNFaceObservation] else { return }
        self.myView.removeMask()
        for face in results {
            self.myView.drawFaceboundingBox(face: face)
        }
    }
}

请帮我解决这个问题。

最佳答案

我认为您应该使用请求作为参数在处理程序上调用 perform 以执行检测。如果您有静态图像,我认为这可行:

import UIKit
import Vision

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let faceDetectionRequest = VNDetectFaceRectanglesRequest(completionHandler: self.handleFaces)
        guard let image = UIImage(named: "sample") else { return }
        guard let cgImage = image.cgImage else { return }
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        do {
            try handler.perform([faceDetectionRequest])
        } catch let reqErr {
            print("Failed to perform request:", reqErr)
        }
    }

    func handleFaces(request: VNRequest, error: Error?) {
        DispatchQueue.main.async {
            //perform all the UI updates on the main queue
            guard let results = request.results as? [VNFaceObservation] else { return }
            self.myView.removeMask()
            for face in results {
                self.myView.drawFaceboundingBox(face: face)
            }
        }
    }
}

我必须感谢来自 Let's Build That App 的 Brian Voong 的代码:https://www.letsbuildthatapp.com/course_video?id=1572 .

关于swift - VNDetectFaceRectanglesRequest 没有调用 completionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50051863/

相关文章:

ios - 使用 CAKeyframeAnimation 反转 UIImageView 的旋转

algorithm - Viola Jones/AdaBoost 的学习阶段

swift - 如何在 Apple Vision 检测到的脸部上应用 3D 模型 "NO AR"

ios - VNImageBasedRequest regionOfInterest 不在规范化范围内

swift - 对象跟踪在 iOS 11 中使用视觉框架

swift - 类型 'string' 的值没有成员 'text'

swift - 将数据保存到数组中[Swift]

ios - SpriteKit 暂停恢复错误

image-processing - 是否可以保存用户的骨骼和面部数据以进行识别?

augmented-reality - 使用 ARKit 和 CoreML 从远距离检测物体