小编Fil*_*p Z的帖子

Swift iOS - 视觉框架文本识别和矩形

我试图在使用 Vision 框架找到的文本区域上绘制矩形,但它们总是有点偏离。我这样做是这样的:

    public func drawOccurrencesOnImage(_ occurrences: [CGRect], _ image: UIImage) -> UIImage? {

    UIGraphicsBeginImageContextWithOptions(image.size, false, 0.0)

    image.draw(at: CGPoint.zero)
    let currentContext = UIGraphicsGetCurrentContext()

    currentContext?.addRects(occurrences)
    currentContext?.setStrokeColor(UIColor.red.cgColor)
    currentContext?.setLineWidth(2.0)
    currentContext?.strokePath()

    guard let drawnImage = UIGraphicsGetImageFromCurrentImageContext() else { return UIImage() }

    UIGraphicsEndImageContext()
    return drawnImage
}
Run Code Online (Sandbox Code Playgroud)

但返回的图像看起来总是差不多,但并不真正正确: 第一张图片

第二张图片

第三张图片

这就是我创建盒子的方式,与苹果的方式完全相同:

        let boundingRects: [CGRect] = observations.compactMap { observation in

        guard let candidate = observation.topCandidates(1).first else { return .zero }

        let stringRange = candidate.string.startIndex..<candidate.string.endIndex
        let boxObservation = try? candidate.boundingBox(for: stringRange)

        let boundingBox = boxObservation?.boundingBox ?? .zero

        return VNImageRectForNormalizedRect(boundingBox,
                                            Int(UIViewController.chosenImage?.width …
Run Code Online (Sandbox Code Playgroud)

vision uiview text-recognition ios swift

2
推荐指数
1
解决办法
2559
查看次数

标签 统计

ios ×1

swift ×1

text-recognition ×1

uiview ×1

vision ×1