Tesseract text extraction

Viewed 308

I am a newbie with python, but I am trying to make a receipt scanner project. I have watched several tutorials and combined the techniques from them and managed to make the preprocessing part to work quite good (for what I need). Now, I want to extract text from the final processed image using tesseract, but I only want to extract some specific data. This is my code for text extraction:

pricePattern = r'([0-9]+\.[0-9]+)'
datePattern = r'\d{2}-\d{2}-\d{4}'
#idPattern = r'(iD)+(BF), iD BF'
#totalPattern = r'[A-Z]{4}, [A-Z]{3}, .[0-9]'


for row in img2char.split("\n"):
    if re.search(pricePattern, row) is not None:
        print(row)
    if re.search(datePattern, row) is not None:
        print(row)
    #if re.search(idPattern, row) is not None:
        #print(row)
    #if re.search(totalPattern, row) is not None:
        #print(row)

This is a part of the output from the bill using tesseract:

TOTAL LEI 90.00 | CARD 50.00 REST 0.00 TOTAL TUA A - 19% 1.92 TOTAL TAC - 5% al TOTAL TUA BON 3.73 2:0764 BF +0049 AR. ANEF 0001, iD BF: 3000856168202 1 129015100807640049 DATA: 30-12-2021 ORA: 15:10:08

My code above extracts the date correctly, but besides the date I only want to extract the lines with "TOTAL LEI" (and the the value that follows) and "iD BF" (with the value as well).

Does anyone have any tips how modify my code to extract those 2 lines?

@will First of all, thank you very much for taking time to answer. My application scans bills. From those scanned bills, I need to output with tesseract specific data (see bill.jpg)enter image description here

I need to output only the data in the red rectangles. After the preprocessing of the image, using tesseract, I get pretty decent text information from the image, but I don't really know how to use regex to only filter that data from the red rectangles (both the text and the value). This is an output example from a bill:

B ART CASTRONONIE SRL
STR. MICHAEL WEISS HR.2?
EXTINOERE STR.REPUBLICI) HR.1?
PARTER, AP.6, ET-PARTER. AP. 1
BRASOV, JUD. BRASOU
CIF: ROIBRO4E81
VIN FIERT 1 BUC X 12.005 12,00 A
CIORBA DE FASOLE CU CIOLAH
1 BLC X 17.00= 17.00 C
STRUDEL CU HERE 1 BUC xX 21.00= 21.00 C
TOTAL LEI 90.00    <--- output
| CARD 50.00
REST 0.00
TOTAL TUA A - 19% 1.92
TOTAL TAC - 5% al
TOTAL TUA BON 3.73
2:0764 BF +0049 AR. ANEF 0001,
iD BF: 3000856168202 1 129015100807640049   <--- output
DATA: 30-12-2021 ORA: 15:10:08   <--- output
S/N:0B4700019053 Tb:00083731
CASIER 30: CASLER 30
7 GON FISCAL
AZ 3000556168
ā€˜ UA MULTUNIN 

I hope this is clear enough. Thanks a lot again!

2 Answers

Testing your price Regex using regex101.com I can see that it's matching many of the numbers in your bill output, not just the price. If the price always has the string TOTAL LEI before it, you can use regex capture groups to search for the number following that string, but only extract the number from it:

import re

row = "TOTAL LEI 90.00 | CARD 50.00 REST 0.00 TOTAL TUA A - 19%"

result = re.search(r"TOTAL LEI ([0-9]+\.[0-9]+)", row)

# Extract match value of capture group 0
print(result.group(0))

# Expected output: '90.00'

You could use the same strategy for the iD.

[BUT ALSO it's not clear from your question what output you want from your code. Why are you printing the row if the pattern is found? What are you trying to do with the output? I can try to provide a better answer if you edit your question to be more specific]

As an alternative you could use regex Named Groups, this way your result will be in the form of a dictionary containing all the named subgroups of the match, keyed by the subgroup name.

Regex101 example here

import re

string = '''TOTAL LEI 90.00 | CARD 50.00 REST 0.00 TOTAL TUA A - 19% 1.92 TOTAL TAC - 5% al TOTAL TUA BON 3.73 2:0764 BF +0049 AR. ANEF 0001, iD BF: 3000856168202 1 129015100807640049 DATA: 30-12-2021 ORA: 15:10:08'''

regex= re.compile(
    r"TOTAL LEI (?P<TotalLei>\d+\.\d+)\s.*"
    r"iD BF: (?P<iD_BF>\d+\s\d\s\d+)\s.*"
    r"DATA: (?P<DATA>[^\s]+)\s"
)

s = re.search(regex, string)
print(s.groupdict())
{
 'TotalLei': '90.00',
 'iD_BF': '3000856168202 1 129015100807640049',
 'DATA': '30-12-2021'
}
Related