尝试在纵向模式下使用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))
}
答案 0 :(得分:1)
要引导识别器,您应该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
}
}