Search code examples
flutterocrgoogle-mlkittext-recognition

Flutter MLKIT Text Recognition Extract a Specified Line


How can i Extract a Line from Flutter ML Kit Text Recognition ?

Example Image for Recognition

I wanna extract the Line from "SUMME" i need only this line as double Variable,

    List recognedText = [];
  bool textScanning = false;
  var scannedText = "Scanned Text will be shown here".obs;
  takeImageforScan() async {
    recognedText.clear();
    try {
      textScanning = true;
      final pickedImage =
          await ImagePicker().pickImage(source: ImageSource.camera);
      if (pickedImage != null) {
        textScanning = true;
        final inputImage = InputImage.fromFilePath(pickedImage.path);
        final recognizer = GoogleMlKit.vision.textRecognizer();
        final visionText = await recognizer.processImage(
          inputImage,
        );
        //scannedText.value = visionText.text;
        recognizer.close();
        textScanning = false;

         for (TextBlock block in visionText.blocks) {
      for (TextLine line in block.lines) {
        recognedText.add(block.text);
        //Extract the Summe Line from the Text
        if (line.text.contains("Summe")) {
          recognedText.add(line.text);
        }
      }
    }
  }
      }
      print(recognedText);
      Get.to(() => const RegisterAutomat());
    } catch (e) {
      print(e);

      textScanning = false;
      Get.snackbar(
          "Fehler", "Keine Daten gefunden,bitte tragen Sie manuell ein");
    }
  }

Its returning Actually the List from the Image all of Texts, but i wanna extract only a Price from Summe (Total Price) , i think i have to extract it from the returned Text List , or can i make it with AI Package ? OUTPUT From Flutter

√  Built build\app\outputs\flutter-apk\app-debug.apk.
I/flutter ( 3895): 0
I/flutter ( 3895): Creating Screen für(1)
I/SurfaceView@c9d0980( 3895): onWindowVisibilityChanged(8) false io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ........ 0,0-960,2613} of ViewRootImpl@6c1a062[MainActivity]
I/SurfaceView@c9d0980( 3895): surfaceDestroyed callback.size 1 #2 io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ........ 0,0-960,2613}
I/SurfaceView@c9d0980( 3895): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ........ 0,0-960,2613} of ViewRootImpl@6c1a062[MainActivity]
I/SurfaceView@c9d0980( 3895): onWindowVisibilityChanged(0) false io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ......ID 0,0-960,2613} of ViewRootImpl@6c1a062[MainActivity]
I/SurfaceView@c9d0980( 3895): windowStopped(false) true io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ......ID 0,0-960,2613} of ViewRootImpl@6c1a062[MainActivity]
I/SurfaceView@c9d0980( 3895): surfaceCreated 1 #1 io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ......ID 0,0-960,2613}
I/SurfaceView@c9d0980( 3895): surfaceChanged (960,2613) 1 #1 io.flutter.embedding.android.FlutterSurfaceView{c9d0980 V.E...... ......ID 0,0-960,2613}
I/flutter ( 3895): [ETRON Soft wareentwicklungs -und
I/flutter ( 3895): Vertriebs Gmbh, ETRON Soft wareentwicklungs -und
I/flutter ( 3895): Vertriebs Gmbh, Text, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abschluss durch: johann
I/flutter ( 3895): 4 Belege K15201/20- 000053 bis
I/flutter ( 3895): K15201/20 - 000056, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abschluss durch: johann
I/flutter ( 3895): 4 Belege K15201/20- 000053 bis
I/flutter ( 3895): K15201/20 - 000056, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abschluss durch: johann
I/flutter ( 3895): 4 Belege K15201/20- 000053 bis
I/flutter ( 3895): K15201/20 - 000056, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abschluss durch: johann
I/flutter ( 3895): 4 Belege K15201/20- 000053 bis
I/flutter ( 3895): K15201/20 - 000056, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abschluss durch: johann
I/flutter ( 3895): 4 Belege K15201/20- 000053 bis
I/flutter ( 3895): K15201/20 - 000056, Kassa -ID: 152 ("Natalie4")
I/flutter ( 3895): Sitzungs-ID: POS15201-000002
I/flutter ( 3895): Druckdatum: 28.01. 2021 12:00:47
I/flutter ( 3895): Abs

Solution

  • Disclaimer - I dont know Flutter

    Looking at your code, you have

    for (TextBlock block in visionText.blocks) {
    

    This loops over blocks of text, not "lines". For example, all text in the "header" of the image may be "one block", but not necessarily "multiple lines"

    However, you do have

    for (TextLine line in block.lines) {
    

    And this should loop over any line-breaks found within that block.

    Then, text found by image recognition is case-sensitive, so you want

    if (line.text.contains("SUMME")) {
    

    However, there is no guarantee that the 5.401,73 is on that same line, or even the same block, because the image contains multiple blocks of text. You'll need to do more debugging to find which block that number exists.

    And there are multiple SUMME in your image. Therefore, you should also be checking the block/line that contains Umsatz, for example.

    if (block.lines[0].text.contains("Umsatz")) {
      for (TextLine line in block.lines) {
        if (line.text.contains("SUMME")) {
           print(line.text);
        }
      }
    }