-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceipt2TextModel.py
More file actions
113 lines (88 loc) · 3.27 KB
/
Copy pathreceipt2TextModel.py
File metadata and controls
113 lines (88 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import cv2
import easyocr
import pytesseract as pyt
from PIL import Image
validPriceTagElementsString = ".$-"
def checkDollarSignExistence(item):
return (item.rfind('$') == 0 or item.rfind('$') == 1)
def checkDollarSignDuplicate(item, index, list):
if checkDollarSignExistence(item) and index != 0:
if not checkDollarSignExistence(list[index-1]):
return True
return False
def stringIsPriceTag(item):
if checkDollarSignExistence(item):
return True
elif len(item) < 3:
return False
elif item[-3] == ".":
return True
else:
return False
def elemIsPriceTagElem(elem):
return (elem.isdigit() or validPriceTagElementsString.rfind(elem) != -1)
def findValidString(globalIndex, localIndex, list):
item = ""
defaultItem = list[globalIndex-localIndex]
if globalIndex - localIndex < 0:
item = ""
elif checkDollarSignExistence(defaultItem):
item = ""
elif str(defaultItem).isascii():
localIndex += 1
item = findValidString(globalIndex, localIndex, list)
else:
item = defaultItem
return item
def findNonPriceTagString(index, list):
if index >= len(list):
return False
elif checkDollarSignExistence(list[index]):
return False
else:
return True
def receiptToTextContent(imagePath):
pytPriceTagList = []
resultArray = []
custom_config = r'--oem 1 --psm 6'
pytResult = pyt.image_to_string(Image.open(
imagePath), lang='chi_tra', config=custom_config)
pytResult = pytResult.rsplit("\n")
pytResultText = ""
for string in pytResult:
pytResultText += " " + string
pytResult = pytResultText.rsplit(" ")
for string in pytResult:
if (stringIsPriceTag(string)):
priceTag = ""
for char in string:
if elemIsPriceTagElem(char):
priceTag += char
pytPriceTagList.append(priceTag)
print(f"pyt Result List : {pytPriceTagList}")
ocrReader = easyocr.Reader(['ch_tra', 'en'])
image = cv2.imread(imagePath)
ocrResultList = ocrReader.readtext(image, detail=0)
for index in range(len(ocrResultList)):
itemInfo = [[], []]
listElem = ocrResultList[index]
if (checkDollarSignDuplicate(listElem, index, ocrResultList)):
for char in listElem:
if not elemIsPriceTagElem(char):
listElem = pytPriceTagList[len(resultArray)]
break
itemInfo[1].append(listElem)
itemNameElem = findValidString(index, 1, ocrResultList)
itemInfo[0].append(itemNameElem)
secondaryIndex = 0
while (findNonPriceTagString(index+secondaryIndex+2, ocrResultList)):
stringInCheck = ocrResultList[index+secondaryIndex+1]
if not (str(stringInCheck).isascii() or checkDollarSignExistence(stringInCheck)) and stringInCheck.rfind("餐具") == -1 and stringInCheck.rfind("飲管") == -1:
itemInfo[0].append(stringInCheck)
else:
break
secondaryIndex += 1
resultArray.append(itemInfo)
return resultArray
# finalArray = receiptToTextContent("./testFiles/testPhotos/cropped03.png")
# print(finalArray)