3

我正在使用Vision框架来处理图像。我正在使用的函数运行良好,并且不会在完成处理程序中返回任何错误,但结果为空。

这是我的功能:

 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)
4

3 回答 3

6

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

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)
    }
}
于 2017-06-24T08:56:52.353 回答
4

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

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

CGImage不知道它自己的方向,因此您必须单独获取该信息并将其传递给采用方向VNImageRequestHandler的初始化程序之一。

这些初始化程序采用 EXIF 方向值(aka CGImagePropertyOrientation)。如果您从 a 开始UIImage,则该枚举的基础数值与 的不匹配UIImageOrientation,因此您需要转换它们。WWDC17 附加到 Vision 会话的示例代码中有一个方便的方法。

于 2017-06-16T16:38:54.940 回答
1

这个问题也让我发疯了。原来的问题是 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]
}
于 2017-09-18T05:19:35.863 回答