MLKit文本识别:未检测到文本

时间:2018-11-02 19:18:33

标签: ios swift firebase machine-learning firebase-mlkit

我正在制作一个让用户拍照的iOS,然后我想使用Firebase中的Google MLKit来检测图片中的文本。我已经设置了一个自定义相机UIViewController,我们将其称为CameraViewController。用户将按下一个简单的按钮拍照。我遵循了Firebase的文档here,但是MLKit对我不起作用。这是我为您提供的代码,然后我们将讨论问题所在。

1。这是我的进口商品,班级代表和销售点:

import UIKit
import AVFoundation
import Firebase

    class CameraViewController: UIViewController, AVCapturePhotoCaptureDelegate {
        var captureSession: AVCaptureSession?
        var videoPreviewLayer: AVCaptureVideoPreviewLayer?
        var capturePhotoOutput: AVCapturePhotoOutput?
        @IBOutlet var previewView: UIView!
        @IBOutlet var captureButton: UIButton!
}

2。在viewDidLoad中,我设置了“ previewView”,以便用户拥有一个“取景器”:

override func viewDidLoad() {
    super.viewDidLoad()

    let captureDevice = AVCaptureDevice.default(for: .video)!
    do {
        let input = try AVCaptureDeviceInput(device: captureDevice)
        captureSession = AVCaptureSession()
        captureSession?.addInput(input)
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
        videoPreviewLayer?.frame = view.layer.bounds
        previewView.layer.addSublayer(videoPreviewLayer!)
        captureSession?.startRunning()
        capturePhotoOutput = AVCapturePhotoOutput()
        capturePhotoOutput?.isHighResolutionCaptureEnabled = true
        captureSession?.addOutput(capturePhotoOutput!)
    } catch {
        print(error)
    }
  }

3。这是拍摄图像的按钮的动作

@IBAction func captureButtonTapped(_ sender: Any) {

        guard let capturePhotoOutput = self.capturePhotoOutput else { return }
        let photoSettings = AVCapturePhotoSettings()
        photoSettings.isAutoStillImageStabilizationEnabled = true
        photoSettings.isHighResolutionPhotoEnabled = true
        photoSettings.flashMode = .off
        capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self)
    }

4。在这里,我收到使用didFinishProcessingPhoto委托方法拍摄的图片并开始使用MLKit

func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

    guard error == nil,
        let photoSampleBuffer = photoSampleBuffer else {
            print("Error capturing photo: \(String(describing: error))")
            return
    }
    guard let imageData =
        AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
            return
    }
    let capturedImage = UIImage.init(data: imageData , scale: 1.0)
    captureNormal()
    DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
        self.captureSession?.stopRunning()
        self.processText(with: capturedImage!)
        // Here is where I call the function processText where MLKit is run
    }

}

5。最后,这是我使用MLKit的函数processText(with:UIImage)

func processText(with image: UIImage) {
        let vision = Vision.vision()
        let textRecognizer = vision.onDeviceTextRecognizer()
        let visionImage = VisionImage(image: image)

        textRecognizer.process(visionImage) { result, error in

        if error != nil {
            print("MLKIT ERROR - \(error)")
        } else {
            let resultText = result?.text
            print("MLKIT RESULT - \(resultText)")
        }

     }

   }

好的,非常感谢您阅读所有内容。好吧,所以问题在于这行不通。我在第4步中确实得到了正确的UIImage,所以不是那样的。这是我要扫描的示例的屏幕截图...

example screenshot

MLKit应该能够轻松检测到此文本。但是,每次尝试时,result?.text总是打印为nil。我没主意了。有人对如何解决这个问题有任何想法吗?如果是这样,非常感谢!

0 个答案:

没有答案