VNFaceObservation BoundingBox在纵向模式下无法缩放 [英] VNFaceObservation BoundingBox Not Scaling In Portrait Mode

查看:168
本文介绍了VNFaceObservation BoundingBox在纵向模式下无法缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为参考,这源于 Vision API 中的一个问题。我正在使用 Vision 通过 VNDetectFaceRectanglesRequest 检测图像中的人脸,该功能在确定正确的人脸数量方面已成功运行一张图像并为每个脸提供 boundingBox

For reference, this stems from a question in the Vision API. I am working to use Vision to detect faces in an image via a VNDetectFaceRectanglesRequest, which is successfully functioning in terms of determining the correct number of faces in an image and providing the boundingBox for each face.

我的麻烦是由于我的 UIImageView (包含有问题的 UIImage )正在使用 .scaleAspectFit 内容模式,我很难在纵向模式下正确绘制边界框(在横向情况下效果很好)。

My trouble is that due to my UIImageView (which holds the UIImage in question) is using a .scaleAspectFit content mode, I am having immense difficulty in properly drawing the bounding box in portrait mode (things work great in landscape).

这是我的代码;

func detectFaces(image: UIImage) {

    let detectFaceRequest = VNDetectFaceRectanglesRequest { (request, error) in
        if let results = request.results as? [VNFaceObservation] {
            for faceObservation in results {
                let boundingRect = faceObservation.boundingBox

                let transform = CGAffineTransform(scaleX: 1, y: -1).translatedBy(x: 0, y: -self.mainImageView.frame.size.height)
                let translate = CGAffineTransform.identity.scaledBy(x: self.mainImageView.frame.size.width, y: self.mainImageView.frame.size.height)
                let facebounds = boundingRect.applying(translate).applying(transform)

                let mask = CAShapeLayer()
                var maskLayer = [CAShapeLayer]()
                mask.frame = facebounds

                mask.backgroundColor = UIColor.yellow.cgColor
                mask.cornerRadius = 10
                mask.opacity = 0.3
                mask.borderColor = UIColor.yellow.cgColor
                mask.borderWidth = 2.0

                maskLayer.append(mask)
                self.mainImageView.layer.insertSublayer(mask, at: 1)
            }
         }

    let vnImage = VNImageRequestHandler(cgImage: image.cgImage!, options: [:])
    try? vnImage.perform([detectFaceRequest])

}



这是我所看到的最终结果,请注意,框在其 X 位置正确,但在纵向显示时在 Y 位置上不准确。


This is the end result of what I'm seeing, note that the boxes are correct in their X position, but largely inaccurate in their Y position when in portrait.

**人像放置错误**

** Incorrect Placement In Portrait**

**正确放置在景观中**

** Correct Placement In Landscape**

推荐答案

VNFaceObservation 边界框被标准化为处理后的图像。

VNFaceObservation bounding box are normalised to processed image. From documentation.


所检测对象的边界框。坐标已标准化为已处理图像的尺寸
,其原点位于图像的
的左下角。

The bounding box of detected object. The coordinates are normalized to the dimensions of the processed image, with the origin at the image's lower-left corner.

因此,您可以使用简单的计算方法为检测到的面部找到正确的尺寸/帧,如下所示。

So you can use a simple calculation to find the correct size/frame for detected face like below.

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)

然后您可以像下面一样形成 CAShapeLayer rect

then you can form the CAShapeLayer rect like below

layer.frame = CGRect(来源:来源,尺寸:大小)

这篇关于VNFaceObservation BoundingBox在纵向模式下无法缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆