VNDetectFaceRectangles请求检测人脸

VNDetectFaceRectanglesRequest detecting of the face

我正在使用 Vision 框架来处理图像。我为此使用的函数 运行 很好,并且在完成处理程序中没有 return 任何错误,但结果为空。

这是我的功能:

 func recognizeImage() {
        let request = VNDetectFaceRectanglesRequest { (res: VNRequest, error: Error?) in
            print("Reuslt : \(res.accessibilityActivationPoint)")
        }

        if let cgContet = image.image.cgImage  {
            let handler = VNImageRequestHandler(cgImage: cgContet)
            try? handler.perform([request])
        }
    }

函数的结果是:

Reuslt : (0.0, 0.0)

这里没有足够的信息可以确定,但可能...

人脸识别需要知道图像方向。 (因为当你只寻找正面朝上的面孔时,准确地找出哪些像素块是面孔,哪些不是面孔要容易得多。)

CGImage 不知道它自己的方向,因此您必须单独获取该信息并将其传递给 VNImageRequestHandler 初始值设定项之一 that takes an orientation

这些初始化器采用 EXIF 方向值(又名 CGImagePropertyOrientation). If you're starting from a UIImage, that enum's underlying numeric values don't match those of UIImageOrientation, so you'll need to convert them. There's a handy method for doing that in the sample code attached to the Vision session from WWDC17

如果你想检测人脸并在每个人身上画一个矩形,试试这个:

let request=VNDetectFaceRectanglesRequest{request, error in
    var final_image=UIImage(named: image_to_process)

    if let results=request.results as? [VNFaceObservation]{
        print(results.count, "faces found")
        for face_obs in results{
          //draw original image
          UIGraphicsBeginImageContextWithOptions(final_image.size, false, 1.0)
          final_image.draw(in: CGRect(x: 0, y: 0, width: final_image.size.width, height: final_image.size.height))

          //get face rect
          var rect=face_obs.boundingBox
          let tf=CGAffineTransform.init(scaleX: 1, y: -1).translatedBy(x: 0, y: -final_image.size.height)
          let ts=CGAffineTransform.identity.scaledBy(x: final_image.size.width, y: final_image.size.height)
          let converted_rect=rect.applying(ts).applying(tf)    

          //draw face rect on image
          let c=UIGraphicsGetCurrentContext()!
          c.setStrokeColor(UIColor.red.cgColor)
          c.setLineWidth(0.01*final_image.size.width)
          c.stroke(converted_rect)

          //get result image
          let result=UIGraphicsGetImageFromCurrentImageContext()
          UIGraphicsEndImageContext()

          final_image=result!
        }
    }

    //display final image
    DispatchQueue.main.async{
        self.image_view.image=final_image
    }
}


guard let ciimage=CIImage(image:image_to_process) else{
  fatalError("couldn't convert uiimage to ciimage")
}

let handler=VNImageRequestHandler(ciImage: ciimage)
DispatchQueue.global(qos: .userInteractive).async{
    do{
        try handler.perform([request])
    }catch{
        print(error)
    }
}

这个问题也让我抓狂。事实证明,最初的问题是图像的方向,cgiImage 或 ciiImage 都无法正确处理。我从某个地方复制的一些代码通过简单的转换错误地将方向从图像转换为 cgi(它们的顺序不同)。

我创建了一个方向转换器,下面的代码适用于我:

let handler = VNImageRequestHandler(cgImage: image.cgImage!, orientation: self.convertImageOrientation(orientation: image.imageOrientation))

...

func convertImageOrientation(orientation: UIImageOrientation) -> CGImagePropertyOrientation  {
    let cgiOrientations : [ CGImagePropertyOrientation ] = [
        .up, .down, .left, .right, .upMirrored, .downMirrored, .leftMirrored, .rightMirrored
    ]

    return cgiOrientations[orientation.rawValue]
}