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