Ошибка распознавания текста в Firebase MLKit - PullRequest
0 голосов
/ 09 мая 2018

Я пытаюсь распознать мое изображение с помощью Firebase MLKit, но оно не получается и возвращается с ошибкой

Обнаружение текста не удалось с ошибкой: не удалось запустить детектор текста, поскольку self равно nil.

/// Detects texts on the specified image and draws a frame for them.
func detectTexts() {
    let image = #imageLiteral(resourceName: "testocr")
    // Create a text detector.
    let textDetector = vision.textDetector()  // Check console for errors.

    // Initialize a VisionImage with a UIImage.
    let visionImage = VisionImage(image: image)
    textDetector.detect(in: visionImage) { (features, error) in
        guard error == nil, let features = features, !features.isEmpty else {
            let errorString = error?.localizedDescription ?? "No results returned."
            print("Text detection failed with error: \(errorString)")
            return
        }

        // Recognized and extracted text
        print("Detected text has: \(features.count) blocks")
        let resultText = features.map { feature in
            return "Text: \(feature.text)"
            }.joined(separator: "\n")
        print(resultText)
    }
}

1 Ответ

0 голосов
/ 09 мая 2018

Похоже, вам необходимо строгое указание на textDetector, в противном случае извещатель освобождается до вызова блока завершения.

Немного изменив код:

var textDetector: VisionTextDetector?   // NEW

/// Detects texts on the specified image and draws a frame for them.
func detectTexts() {
    // ... truncated ...
    textDetector = vision.textDetector()   // NEW
    let visionImage = VisionImage(image: image)
    textDetector?.detect(in: visionImage) { (features, error) in   // NEW
        // Callback implementation
    }
}

Вы также можете развернуть его, чтобы убедиться, что он не равен нулю после его назначения:

guard let textDetector = textDetector else { 
    print("Error: textDetector is nil.")
    return
}

Надеюсь, это поможет!

...