如何正确定位VNRecognizedObjectObservation边界框?

时间:2019-01-10 14:34:39

标签: ios swift

我第一次使用MLCore,并且刚刚训练了一个对象检测器。现在的问题是VNRecognizedObjectObservation位置与我的图像预览层不对齐,该图像预览层仅占用一部分屏幕。进行适当的转换以使包围盒在预览层上的位置正确是什么?

这就是我现在拥有的:

func drawVisionRequestResults(_ results: [Any]) {
    self.imageView.layer.sublayers?.removeSubrange(1...) // remove all the old recognized objects
    for observation in results where observation is VNRecognizedObjectObservation {
        guard let objectObservation = observation as? VNRecognizedObjectObservation else {
            continue
        }
        self.highlightLicensePlate(box: objectObservation.boundingBox)
    }
}


func getRectOutline(box: CGRect) -> CGRect?  {
    let xCord = box.maxX * imageView.frame.size.width
    let yCord = (1 - box.minY) * imageView.frame.size.height
    let width = (box.minX - box.maxX) * imageView.frame.size.width
    let height = (box.minY - box.maxY) * imageView.frame.size.height

    return CGRect(x: xCord, y: yCord, width: width, height: height)
}

func highlightLicensePlate(box: CGRect) {
    let outline = CALayer()
    guard let outlineRectangle = getRectOutline(box: box) else {
        return
    }
    outline.frame = outlineRectangle
    outline.borderWidth = 2.0
    outline.borderColor = UIColor.green.cgColor

    imageView.layer.addSublayer(outline)
}

Y坐标离得很远。

0 个答案:

没有答案