0

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
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Koek
  • 101
  • 3
  • 9
  • Umm. Remove `recognedText.add(block.text)`? – OneCricketeer Mar 05 '23 at 16:34
  • This is only a List that i can see what its added ? I wanna only know how can i add a complete Line in a String then i can Parse it Like "SUMME:" and "100.00€" – Koek Mar 05 '23 at 19:03
  • Like I said remove that line, then only add/parse inside of `if (line.text.contains("Summe"))`... But that's case sensitive. So, did you try SUMME there? Why do you need a list? – OneCricketeer Mar 05 '23 at 23:02
  • I removed this line , and i tried also if with "SUMME" then extracting only SUMME , i think its not understanding that ist a Line, because its has too mucht space for Line, SUMME 963,00 € Its extracting two different Variable as Summe and 963,00 € – Koek Mar 06 '23 at 11:58
  • 1
    Correct. The ocr scanner sees *sections* of text, or `TextBlock`, not "lines"... You'd need to find the block starting with `Umsatz exkl`, then parse each sub-block to find the "last SUMME line", along with the values on the right – OneCricketeer Mar 06 '23 at 15:08
  • Can you write a Codeblock for me ? I can not understand what u mean about the CodeBlock ? Thx. Because the Umsatz exkl. text has got more Values like 3 another Prices, how can flutter choice about the Correct Variable ? You mean that i parse all Blocks, and find that starting with Umsatz Exkl. then in this block find Summe Line after that find it still no Price it shows again Summe ? if i parse it as Text Line then its returning again the same Var. – Koek Mar 09 '23 at 20:17

1 Answers1

1

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);
    }
  }
}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245