3

我已经Firebase MLKit SDK根据文档集成了文本,可以正确检测到back camera照片中的文本。当我使用从前置摄像头拍摄的照片时,文本检测错误并且出现不良结果。

 @IBAction func findTextDidTouch(_ sender: UIButton) {
    runTextRecognition(with:imageView.image!)
  }

func runCloudTextRecognition(with image: UIImage) {
    let visionImage = VisionImage(image: image)
    cloudTextDetector.detect(in: visionImage) { features, error in
      if let error = error {
        print("Received error: \(error)")
        return
      }
      self.processCloudResult(from: features, error: error)
    }
  }


  for text in features {
      if let block = text as? VisionTextBlock {
        print(block.text)
      }
    }

我无法弄清楚。我需要更换相机或 firebase ml 套件吗?

4

1 回答 1

6

您还需要提供图像方向。添加代码片段可能会对您有所帮助。

let visionImage = VisionImage(image: image)

   let metadata = VisionImageMetadata()
        metadata.orientation = self.detectorOrientation(in: image)
        visionImage.metadata = metadata
 textDetector.process(visionImage) { (features, error) in
            if features != nil
            {
                self.processResult(from: [features!], error: error)
            }
        }
//Detects orientation of the selected or captured image
    func detectorOrientation(in image: UIImage) -> VisionDetectorImageOrientation {
        switch image.imageOrientation {
        case .up:
            return .topLeft
        case .down:
            return .bottomRight
        case .left:
            return .leftBottom
        case .right:
            return .rightTop
        case .upMirrored:
            return .topRight
        case .downMirrored:
            return .bottomLeft
        case .leftMirrored:
            return .leftTop
        case .rightMirrored:
            return .rightBottom
        }
    }
于 2018-09-26T06:53:21.317 回答