【问题标题】:Face detection swift vision kit人脸检测快速视觉套件
【发布时间】:2017-12-31 01:26:21
【问题描述】:

我正在尝试适用于 iOS 11 的 Vision 套件。我可以使用 Vision,并且可以找到边界框值 face。但我不知道如何使用这些点绘制一个矩形。我希望我的问题很清楚。

【问题讨论】:

  • 你到现在做了什么,从现在开始你想做什么?请按代码显示。

标签: face-detection vision ios11 swift4


【解决方案1】:

希望您能够使用VNDetectFaceRectanglesRequest 并能够检测到人脸。要显示矩形框,有很多方法可以实现。但最简单的方法是使用CAShapeLayer 在您检测到的每张脸的图像上绘制图层。

假设您有VNDetectFaceRectanglesRequest,如下所示

let request = VNDetectFaceRectanglesRequest { [unowned self] request, error in
            if let error = error {
                // somthing is not working as expected
            }
            else {
                //  we got some face detected
                self.handleFaces(with: request)
            }
        }
        let handler = VNImageRequestHandler(ciImage: ciImage, options: [:])
        do {
            try handler.perform([request])
        }
        catch {
           // catch exception if any
        }

您可以为检测到的每个人脸实现一个名为handleFace 的简单方法,并使用VNFaceObservation 属性绘制CAShapeLayer

func handleFaces(with request: VNRequest) {
        imageView.layer.sublayers?.forEach { layer in
            layer.removeFromSuperlayer()
        }
        guard let observations = request.results as? [VNFaceObservation] else {
            return
        }
        observations.forEach { observation in
            let boundingBox = observation.boundingBox
            let size = CGSize(width: boundingBox.width * imageView.bounds.width,
                              height: boundingBox.height * imageView.bounds.height)
            let origin = CGPoint(x: boundingBox.minX * imageView.bounds.width,
                                 y: (1 - observation.boundingBox.minY) * imageView.bounds.height - size.height)

            let layer = CAShapeLayer()
            layer.frame = CGRect(origin: origin, size: size)
            layer.borderColor = UIColor.red.cgColor
            layer.borderWidth = 2

            imageView.layer.addSublayer(layer)
        }
    }

更多信息可以在这里找到 Github repo iOS-11-by-Examples

【讨论】:

  • 我接受了你的回答。但我的问题为什么 x = boundingBox.minX * imageView.bounds.width 和 y = (1 - observation.boundingBox.minY) * imageView.bounds.height - size.height)
  • 这是因为我们必须根据原始图像视图进行缩放和变换,否则在不同的地方会看起来很奇怪。
  • 对于那些对为什么需要这样做感到困惑的人1 - observation.boundingBox.minY 这是因为边界框的坐标被标准化为处理后图像的尺寸,原点位于图像的左下角.见:[stackoverflow.com/a/45317950/6942666](this)
【解决方案2】:

这是绘制方框的简单方法。

let faceRequest = VNDetectFaceRectanglesRequest(completionHandler:self.faceDetection)

func faceDetection (request: VNRequest, error: Error?) {
        guard let observations = request.results as? [VNFaceObservation]
            else { print("unexpected result type from VNFaceObservation")
                return }
        guard observations.first != nil else {
            return
        }
        // Show the pre-processed image
        DispatchQueue.main.async {
            self.resultImageView.subviews.forEach({ (subview) in
                subview.removeFromSuperview()
            })
            for face in observations
            {
                let view = self.CreateBoxView(withColor: UIColor.red)
                view.frame = self.transformRect(fromRect: face.boundingBox, toViewRect: self.analyzedImageView)
                self.analyzedImageView.image = self.originalImageView.image
                self.resultImageView.addSubview(view)                
        }
    }
}

 //MARK - Instance Methods
func boxView(withColor : UIColor) -> UIView {
    let view = UIView()
    view.layer.borderColor = withColor.cgColor
    view.layer.borderWidth = 2.0
    view.backgroundColor = UIColor.clear
    return view
}


//Convert Vision Frame to UIKit Frame
func transformRect(fromRect: CGRect , toViewRect :UIView) -> CGRect {

    var toRect = CGRect()
    toRect.size.width = fromRect.size.width * toViewRect.frame.size.width
    toRect.size.height = fromRect.size.height * toViewRect.frame.size.height
    toRect.origin.y =  (toViewRect.frame.height) - (toViewRect.frame.height * fromRect.origin.y )
    toRect.origin.y  = toRect.origin.y -  toRect.size.height
    toRect.origin.x =  fromRect.origin.x * toViewRect.frame.size.width

    return toRect
}

【讨论】:

  • 有没有办法通过文本检测来做到这一点,只在特定区域内的文本周围显示框,例如屏幕中心的矩形而不是框外的文本?
  • 仅文本检测是什么意思?
  • 使用视觉文本检测,在它找到的文本周围放置一个矩形。我正在寻找某种掩码,因此只有掩码区域中的文本会显示它周围的框,而不是掩码之外。
  • 我不确定但是,我认为您可以在裁剪后通过图像部分(蒙版)来检测文本,以便您可以检测矩形内的文本。
猜你喜欢
  • 2016-07-03
  • 2016-03-10
  • 2017-09-29
  • 2018-07-14
  • 1970-01-01
  • 2018-10-30
  • 2016-09-16
  • 2019-11-18
  • 2013-01-31
相关资源
最近更新 更多