用于面部检测的iOS Vision API:如何检测多张图像上的面部?

时间:2019-04-10 06:11:06

标签: ios swift computer-vision uikit vision

我正在使用以下在单个图像上运行良好的方法。如何针对多个图像(来自图像阵列)调整它?我尝试将VNImageRequestHandlerrequestHandler.perform放入数组中所有图像的for循环中,但是由于completionHandler是异步的,因此我的addFaceLandmarksToImage方法被随机调用。所以我不知道我要哪个数组索引图像。

  1. 没有for图像循环,还有更好的方法吗?
  2. 如何从completionHandler的VNRequest参数取回原始图像?

代码:

@IBAction func process(_ sender: UIButton) {
        var orientation:CGImagePropertyOrientation = .down

        switch image.imageOrientation {
        case .up:
            orientation = .up
        case .right:
            orientation = .right
        case .down:
            orientation = .down
        case .left:
            orientation = .left
        default:
            orientation = .down
        }

        let requestHandler = VNImageRequestHandler(cgImage: image.cgImage!, orientation: orientation ,options: [:])
        do {
            try requestHandler.perform([VNDetectFaceLandmarksRequest(completionHandler: self.handleFaceFeatures)])
        } catch {
            print(error)
        }
    }

func handleFaceFeatures(request: VNRequest, errror: Error?) {
        guard let observations = request.results as? [VNFaceObservation] else {
            fatalError("unexpected result type!")
        }

        for face in observations {
            addFaceLandmarksToImage(face)
        }
    }

0 个答案:

没有答案