【问题标题】:Apple Vision framework – Text extraction from imageApple Vision 框架 - 从图像中提取文本
【发布时间】:2017-11-17 21:01:32
【问题描述】:

我正在使用适用于 iOS 11 的 Vision 框架来检测图像上的文本。

文本检测成功,但如何获取检测到的文本?

【问题讨论】:

  • 您现在需要使用 CoreML 并发送要读取的区域
  • @Alex 已经获取正在检测的区域。需要解决方案来读取检测到的区域。
  • @Abhishek 你能看看这个stackoverflow.com/questions/47864505/text-detection-in-images 并指导我吗?
  • @PoojaM.Bohora 根据我的经验,我的提取结果准确率为 70-80%。影响文本提取的因素有很多,例如“文本字体大小(如果字体小则不起作用)、Tesseract 配置设置(需要根据需要配置 Tesseract 引擎)”。在提取文本时使用 Tesseract 的“黑白”模式。还要考虑图片大小,越大越好。

标签: swift machine-learning computer-vision coreml apple-vision


【解决方案1】:

在 Apple Vision 中,您可以使用 VNRecognizeTextRequest 类轻松地从图像中提取文本,允许您发出图像分析请求以查找和识别图像中的文本。

VNRecognizeTextRequest 从 iOS 13.0 和 macOS 10.15 开始工作

这是一个代码 sn-p 向您展示如何做到这一点:

let requestHandler = VNImageRequestHandler(url: imageURL, options: [:])

let request = VNRecognizeTextRequest { (request, error) in

    guard let observations = request.results as? [VNRecognizedTextObservation] 
    else { return }

    for observation in observations {

        let topCandidate: [VNRecognizedText] = observation.topCandidates(1)

        if let recognizedText: VNRecognizedText = topCandidate.first {
            label.text = recognizedText.string
        }
    }
}

那么你必须为recognitionLevel实例属性赋值:

// non-realtime asynchronous but accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.accurate

// nearly realtime but not-accurate text recognition
request.recognitionLevel = VNRequestTextRecognitionLevel.fast

try? requestHandler.perform([request])

【讨论】:

    【解决方案2】:

    不完全是骗子,但类似于:Converting a Vision VNTextObservation to a String

    您需要使用 CoreML 或其他库来执行 OCR(SwiftOCR 等)

    【讨论】:

      【解决方案3】:

      这将在检测到的文本上返回一个带有矩形框的叠加图像

      这是完整的 xcode 项目 https://github.com/cyruslok/iOS11-Vision-Framework-Demo

      希望对你有帮助

      // Text Detect
      func textDetect(dectect_image:UIImage, display_image_view:UIImageView)->UIImage{
          let handler:VNImageRequestHandler = VNImageRequestHandler.init(cgImage: (dectect_image.cgImage)!)
          var result_img:UIImage = UIImage.init();
      
          let request:VNDetectTextRectanglesRequest = VNDetectTextRectanglesRequest.init(completionHandler: { (request, error) in
              if( (error) != nil){
                  print("Got Error In Run Text Dectect Request");
      
              }else{
                  result_img = self.drawRectangleForTextDectect(image: dectect_image,results: request.results as! Array<VNTextObservation>)
              }
          })
          request.reportCharacterBoxes = true
          do {
              try handler.perform([request])
              return result_img;
          } catch {
              return result_img;
          }
      }
      
      func drawRectangleForTextDectect(image: UIImage, results:Array<VNTextObservation>) -> UIImage {
          let renderer = UIGraphicsImageRenderer(size: image.size)
          var t:CGAffineTransform = CGAffineTransform.identity;
          t = t.scaledBy( x: image.size.width, y: -image.size.height);
          t = t.translatedBy(x: 0, y: -1 );
      
          let img = renderer.image { ctx in
              for item in results {
                  let TextObservation:VNTextObservation = item
                  ctx.cgContext.setFillColor(UIColor.clear.cgColor)
                  ctx.cgContext.setStrokeColor(UIColor.green.cgColor)
                  ctx.cgContext.setLineWidth(1)
                  ctx.cgContext.addRect(item.boundingBox.applying(t))
                  ctx.cgContext.drawPath(using: .fillStroke)
      
                  for item_2 in TextObservation.characterBoxes!{
                      let RectangleObservation:VNRectangleObservation = item_2
                      ctx.cgContext.setFillColor(UIColor.clear.cgColor)
                      ctx.cgContext.setStrokeColor(UIColor.red.cgColor)
                      ctx.cgContext.setLineWidth(1)
                      ctx.cgContext.addRect(RectangleObservation.boundingBox.applying(t))
                      ctx.cgContext.drawPath(using: .fillStroke)
                  }
              }
      
          }
          return img
      }
      

      【讨论】:

      • 与问题无关,请在代码中补充一些说明。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2017-09-24
      • 2019-10-06
      • 2014-07-31
      相关资源
      最近更新 更多