iOS上的MLKit文本检测适用于从Assets.xassets拍摄的照片,但与在相机上拍摄/从相机胶卷上传的照片不同



我使用MLKit中的Google文本检测API来检测图像中的文本。它似乎在屏幕截图上非常有效,但当我尝试在应用程序中拍摄的图像(使用AVFoundation(或从相机胶卷上传的照片上使用它时,它会吐出少量看似随机的字符。

这是我运行实际文本检测的代码:

func runTextRecognition(with image: UIImage) {
let visionImage = VisionImage(image: image)
textRecognizer.process(visionImage) { features, error in
self.processResult(from: features, error: error)
}
}
func processResult(from text: VisionText?, error: Error?) {
guard error == nil, let text = text else {
print("oops")
return
}
let detectedText = text.text
let okAlert = UIAlertAction(title: "OK", style: .default) { (action) in
// handle user input
}
let alert = UIAlertController(title: "Detected text", message: detectedText, preferredStyle: .alert)
alert.addAction(okAlert)
self.present(alert, animated: true) {
print("alert was presented")
}
}

这是我使用相机胶卷图像的代码(适用于屏幕截图,不适用于相机拍摄的图像(:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[.originalImage] as? UIImage {
self.runTextRecognition(with: image)
uploadView.image = image
} else {
print("error")
}
self.dismiss(animated: true, completion: nil)
}

这是我在应用程序内使用相机拍摄的照片的代码(永远不起作用,结果总是胡说八道(:

func photoOutput(_ output: AVCapturePhotoOutput,
didFinishProcessingPhoto photo: AVCapturePhoto,
error: Error?) {
PHPhotoLibrary.shared().performChanges( {
let creationRequest = PHAssetCreationRequest.forAsset()
creationRequest.addResource(with: PHAssetResourceType.photo, data: photo.fileDataRepresentation()!, options: nil)
}, completionHandler: nil)
let testImage = UIImage(data: photo.fileDataRepresentation()!)
self.runTextRecognition(with: testImage!)
}

这就是我所做的,使用我放在Assets.xassets中的测试图像(这是唯一一个始终运行良好的图像(:

let uiimage = UIImage(named: "testImage")
self.runTextRecognition(with: uiimage!)

我想我的问题可能在于UIImage的方向,但我不确定。任何帮助都将不胜感激!

如果您的图像选择器工作正常,问题可能是图像方向。为了进行快速测试,您可以捕捉不同方向的多个图像,看看它是否有效。

我的问题是文本识别工作从画廊中挑选的图像,但不是从相机。这是方向问题。

解决方案1

在转换为视觉图像之前,请按如下方式固定图像方向。

let fixedImage = pickedImage.fixImageOrientation()

添加此扩展名。

extension UIImage {
func fixImageOrientation() -> UIImage {
UIGraphicsBeginImageContext(self.size)
self.draw(at: .zero)
let fixedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return fixedImage ?? self
} }

解决方案2

Firebase文档提供了一种针对所有方向进行修复的方法。

func imageOrientation(
deviceOrientation: UIDeviceOrientation,
cameraPosition: AVCaptureDevice.Position
) -> VisionDetectorImageOrientation {
switch deviceOrientation {
case .portrait:
return cameraPosition == .front ? .leftTop : .rightTop
case .landscapeLeft:
return cameraPosition == .front ? .bottomLeft : .topLeft
case .portraitUpsideDown:
return cameraPosition == .front ? .rightBottom : .leftBottom
case .landscapeRight:
return cameraPosition == .front ? .topRight : .bottomRight
case .faceDown, .faceUp, .unknown:
return .leftTop
}
}

创建元数据:

let cameraPosition = AVCaptureDevice.Position.back  // Set to the capture device you used.
let metadata = VisionImageMetadata()
metadata.orientation = imageOrientation(
deviceOrientation: UIDevice.current.orientation,
cameraPosition: cameraPosition
)

将元数据设置为视觉图像。

let image = VisionImage(buffer: sampleBuffer)
image.metadata = metadata

最新更新