swift - MLVision 不正确的旋转

标签 swift firebase mlvision

尝试在纵向模式下使用 MLVision,但是在我以纵向模式拍照后,由于旋转不正确,它只会输出几个字符。图片必须在横向拍摄,否则无法识别文字。试图查看 Firebase 提供的示例,在该示例中,当这种情况发生在风景中并在肖像中工作时,我遇到了完全相反的问题,但是无法找到除了我试图更改的元数据之外可以确定图像方向的设置但没有任何成功。

 var vision: Vision?

override func viewDidLoad() {
    super.viewDidLoad()
    vision = Vision.vision()

}


func recognize(Image: VisionImage){
    let textRecognizer = vision?.onDeviceTextRecognizer()
    textRecognizer?.process(Image) { result, error in
        guard error == nil, let result = result else {
            // ...
            return
        }
        print(result.text)

        // Recognized text
    }
}
@IBAction func scanDocument(_ sender: Any) {
    let vc = UIImagePickerController()
    vc.sourceType = .camera
    vc.allowsEditing = false
    vc.delegate = self
    present(vc, animated: true)

}


func imagePickerController(_ picker: UIImagePickerController,
                           didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    picker.dismiss(animated: true, completion: nil)
    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }
     recognize(Image: VisionImage(image: selectedImage))

}

最佳答案

要引导识别器,您应该 supply the the orientation你的形象

// Define the metadata for the image.
let imageMetadata = VisionImageMetadata()
imageMetadata.orientation = UIUtilities.visionImageOrientation(from: image.imageOrientation)

// Initialize a VisionImage object with the given UIImage.
let visionImage = VisionImage(image: image)
visionImage.metadata = imageMetadata

这是一个helper method从您的图像中找到正确的方向

public static func imageOrientation(fromDevicePosition devicePosition: AVCaptureDevice.Position = .back
) -> UIImageOrientation {
  var deviceOrientation = UIDevice.current.orientation
  if deviceOrientation == .faceDown || deviceOrientation == .faceUp ||
    deviceOrientation == .unknown {
    deviceOrientation = currentUIOrientation()
  }
  switch deviceOrientation {
  case .portrait:
    return devicePosition == .front ? .leftMirrored : .right
  case .landscapeLeft:
    return devicePosition == .front ? .downMirrored : .up
  case .portraitUpsideDown:
    return devicePosition == .front ? .rightMirrored : .left
  case .landscapeRight:
    return devicePosition == .front ? .upMirrored : .down
  case .faceDown, .faceUp, .unknown:
    return .up
  }
}

关于swift - MLVision 不正确的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52489129/

相关文章:

ios - Swift Firebase 电子邮件验证总是返回 false

ios - 无法使用 firebase ML 套件检测眨眼事件

firebase - Face 对象的属性在 firebase ml vision flutter 中始终为 null

swift - 为什么 FireBase Swift 函数返回 nil

ios - 无法使用 UIActivityViewController 共享 doc/ppt/Xlsx

ios - 快速返回回调

android - getKey() 返回值而不是 ID

ios - 用于生产时 Firebase 身份验证电话无法工作

ios - 在 Firebase DataSnapshot 数组中查找自定义对象的索引