簡體   English   中英

在 Flutter 中使用 Google ML-Kit On-Device 文本識別

[英]Using Google ML-Kit On-Device Text Recognition in Flutter

是否可以在 Flutter 中使用 Google ML-Kit On-Device Text Recognition? 我在網上找到的所有教程和資源都是firebase_ml_vision ,但我正在尋找使用 Google ML-Kit 的免費 OCR 的教程和資源。 我將如何在 Flutter 中執行此操作?

是的,你當然可以使用這個 package [https://pub.dev/packages/mlkit][1] 這是谷歌的 mlkit。 OCR 還支持 ios 和 android。 快樂編碼;)

正如@Sayan Nath 所說,您可以使用mlkit ,但我認為更好的選擇是google_ml_kit ,曾在 firebase_ml_vision 上工作的 firebase 團隊也推薦使用它。

使用這個 package https://pub.dev/packages/camera_google_ml_vision和 firebase_ml_vision 的使用方法完全一樣

用於文本識別的機器學習套件

安裝 Google ML Kit ( https://pub.dev/packages/google_ml_kit )。 (顫振 2.8)

設置這個東西

bool hasImage = false;
File? image;
TextDetector textDetector = GoogleMlKit.vision.textDetector();
String? imagePath;
String scanText = '';

要獲取圖像,請使用圖像選擇器: https://pub.dev/packages/image_picker

Future getImage(ImageSource source) async {
    try {
      final image = await ImagePicker().pickImage(source: source);
      if (image == null) return;
      final imageTemporary = File(image.path);
      setState(() {
        this.image = imageTemporary;
        imagePath = imageTemporary.path;
        debugPrint(imagePath);
        hasImage = true;
      });
    } on PlatformException catch (e) {
      debugPrint('Failed to pick image: $e');
    }
  }

從圖像中獲取文本的代碼

Future getText(String path) async {
    final inputImage = InputImage.fromFilePath(path);
    final RecognisedText recognisedText =
        await textDetector.processImage(inputImage);

    for (TextBlock block in recognisedText.blocks) {
      for (TextLine line in block.lines) {
        for (TextElement element in line.elements) {
          setState(() {
            scanText = scanText + '  ' + element.text;
            debugPrint(scanText);
          });
        }
        scanText = scanText + '\n';
      }
    }
  }

調用getText function,傳遞圖片路徑。 getText(imagePath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM