Apple Vision文字识别Bounding Box Y轴偏移问题求助
静态图片文字识别Bounding Box Y轴偏移问题
我已通过Apple官方文档成功实现静态图片的文字识别,随后参照同一文档添加Bounding Box功能,使用的识别代码如下:
func recongizeText() { // 创建新的图片请求处理器 let cgImage = self.nsImage!.cgImage(forProposedRect: nil, context: nil, hints: nil) let requestHandler = VNImageRequestHandler(cgImage: cgImage!) // 创建识别文字的新请求 let request = VNRecognizeTextRequest(completionHandler: recognizeTextHandler) do { // 执行文字识别请求 try requestHandler.perform([request]) } catch { print("无法执行请求: \(error).") } } func recognizeTextHandler(request: VNRequest, error: Error?) { guard let observations = request.results as? [VNRecognizedTextObservation] else { return } let recognizedStrings = observations.compactMap { observation in // 返回排名第一的VNRecognizedText实例的字符串 return observation.topCandidates(1).first?.string } 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) // 获取归一化的CGRect值 let boundingBox = boxObservation?.boundingBox ?? .zero // 将矩形从归一化坐标转换为图片坐标 return VNImageRectForNormalizedRect(boundingBox, Int(self.nsImage!.size.width), Int(self.nsImage!.size.height)) } self.boundingRects = boundingRects }
UI部分添加了简单的覆盖层代码:
struct BoundingBoxView: View { var nsImage: NSImage // 要显示的NSImage var normalizedBoundingBoxes: [CGRect] // 归一化边界框数组 var body: some View { Image(nsImage: nsImage) .resizable() .aspectRatio(contentMode: .fit) .overlay( GeometryReader { geometry in // 绘制归一化边界框 ForEach(normalizedBoundingBoxes.indices, id: \.self) { index in let normalizedBox = normalizedBoundingBoxes[index] Rectangle() .stroke(Color.red, lineWidth: 2) .frame(width: normalizedBox.size.width, height: normalizedBox.size.height) .position(x: normalizedBox.midX, y: normalizedBox.midY) } } ) } }
运行后出现Bounding Box Y轴偏移的情况,偏移效果如图:
内容的提问来源于stack exchange,提问作者flex_
相关产品推荐
相关产品推荐

