【发布时间】:2022-08-18 16:21:31
【问题描述】:
我试图在使用 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
}
这就是我创建盒子的方式,与 Apple 完全相同:
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 ?? 0),
Int(UIViewController.chosenImage?.height ?? 0))
}
(来源:https://developer.apple.com/documentation/vision/recognizing_text_in_images)
谢谢你。
-
你的 y 坐标被翻转。查看Detecting Objects in Still Images 并查看
boundingBox例程,注意它们翻转了y坐标。如果没有看到您是如何构建occurrences的[CGRect]数组,我们无法进一步评论。 -
@Rob 根据 Apple 文档 (developer.apple.com/documentation/vision/…)。我编辑了问题并将其添加进去。
标签: ios swift uiview vision text-recognition