-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
277 lines (222 loc) · 8.32 KB
/
Copy pathfunctions.py
File metadata and controls
277 lines (222 loc) · 8.32 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import pytesseract
from pytesseract import Output
from pdf2image import convert_from_path
from PIL import Image
import os
import re
#pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
pytesseract.pytesseract.tesseract_cmd = fr'{os.getcwd()}\Tesseract-OCR\tesseract.exe'
#FUNÇÃO PARA REMOVER OS CARACTERES INDESEJADOS
def rreplace(string: str, find: str, replace: str, n_occurences: int) -> str:
"""
Given a `string`, `find` and `replace` the first `n_occurences`
found from the right of the string.
"""
temp = string.rsplit(find, n_occurences)
return replace.join(temp)
#FUNÇÃO PARA CHECAR SE TODOS OS ELEMENTOS DA LISTA SÃO IGUAIS
def check(l):
if(len(l) < 0):
r = True
r = all(ele == l[0] for ele in l)
if(r):
return True
else:
return False
#FUNÇÃO PARA ACHAR A DATA DE VENCIMENTO
def get_due_date(arq):
#Padrão da data para o re
date_pattern = "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{2,4}$"
# TRANSFORMAR PDF EM IMG
if arq.endswith('.pdf'):
bill = arq.replace('pdf', 'png')
lista = convert_from_path(arq)
page = lista[0]
page.save(os.path.join(os.getcwd(), bill), 'png')
else:
bill = arq
#Lendo a Imagem
img = Image.open(bill)
#Usando o OCR
try:
img_data = pytesseract.image_to_data(img, output_type=Output.DICT, lang='por')
except:
os.remove(bill)
return None
#Limpando a lista de palavras
img_data['text'] = [x for x in img_data['text'] if x!='' and x!=' ']
#Achando a Data pelo Metodo 1 ('in')
dates = []
for i, word in enumerate(img_data['text']):
#Achando a palavra 'Vencimento'
if 'Vencimento' in word or 'vencimento' in word or 'Vencinento' in word or 'vencinento' in word:
#Buscando pela Data a partir da palavra 'Vencimento'
for wor in img_data['text'][i:]:
if re.match(date_pattern, wor) != None:
dates.append(wor)
break
# Achando a Data pelo Metodo 2 ('==')
if not check(dates) or dates==[]:
dates = []
for i, word in enumerate(img_data['text']):
# Achando a palavra 'Vencimento'
if 'Vencimento' == word or 'vencimento' == word or 'Vencinento' == word or 'vencinento' == word:
# Buscando pela Data a partir da palavra 'Vencimento'
for wor in img_data['text'][i:]:
if re.match(date_pattern, wor) != None:
dates.append(wor)
break
#Removendo o arquivo de img criado
os.remove(bill)
#Checando se a Data é válida
if check(dates) and dates!=[]:
return dates[0]
else:
return 'ERROR-DATE'
#FUNÇÃO PARA EXTRAIR A MAIOR SEQUENCIA NUMERICA DE UMA STRING
def mar(string):
number_s = re.findall(pattern='[0-9]+', string=string)
len_a = [len(x) for x in number_s]
ma = str(number_s[len_a.index(max(len_a))])
return ma
#FUNÇÃO PARA PEGAR O SERIAL CODE
def get_scd(text):
#FORMATANDO O TEXTO
lista = text.split('\n')
text2 = text
#ACHANDO A LINHA DA SERIAL CODE
#Maior sequencia numerica do boleto
ma_l = mar(text2)
#Achando a linha com a maior sequencia numerica do boleto
linha = ''
for line in text2.split('\n'):
if ma_l in line:
linha = line
#FORMATANDO A LINHA
l = rreplace(string=linha, find='.', replace='', n_occurences=linha.count('.'))
for elem in [' ', "'", '"', '-', '_', '/']:
l = rreplace(string=l, find=elem, replace='', n_occurences=linha.count(elem))
#PEGANDO O CODIGO DA LINHA
scd = mar(l)
#print(f'{l}\n{scd}\n-------')
#CONFERINDO O RESULTADO -- TODE SERIAL CODE TEM 47 OU 48 CARACTERES
if (len(scd) != 47 and len(scd) != 48):
t = text2
while len(scd) != 47 and len(scd) != 48:
# TIRANDO A LINHA INDESEJADA
t = rreplace(string=t, find=linha, replace='', n_occurences=1)
# DEVOLVENDO O VALOR CORRIGIDO
scd = get_scd(t)
#DEVOLVENDO O VALOR
return scd
def get_scd_of_any(arq):
#TRANSFORMAR PDF EM IMG
if arq.endswith('.pdf'):
bill = arq.replace('pdf', 'png')
lista = convert_from_path(arq)
page = lista[0]
page.save(os.path.join(os.getcwd(), bill), 'png')
else:
bill = arq
#EXTRAIR O TEXTO DO ARQUIVO
try:
try:
myconfig = r'--psm 11 --oem 3'
text = pytesseract.image_to_string(Image.open(bill), config=myconfig)
l_text = text.split('\n')
l_text = [l for l in l_text if l != '']
text = '\n'.join(l_text)
scd = get_scd(text)
except:
#print('-='*30)
myconfig = r'--psm 6 --oem 3'
text = pytesseract.image_to_string(Image.open(bill), config=myconfig)
l_text = text.split('\n')
l_text = [l for l in l_text if l != '']
text = '\n'.join(l_text)
scd = get_scd(text)
except:
try:
myconfig = r'--psm 11 --oem 3'
text = pytesseract.image_to_string(Image.open(bill), config=myconfig)
try:
l_text = text.split('\n')
l_text = [l for l in l_text if l != '']
text = '\n'.join(l_text)
scd = get_scd(text)
except:
scd = 'Error -- scd'
except:
#print('-='*30)
myconfig = r'--psm 6 --oem 3'
text = pytesseract.image_to_string(Image.open(bill), config=myconfig)
try:
l_text = text.split('\n')
l_text = [l for l in l_text if l != '']
text = '\n'.join(l_text)
scd = get_scd(text)
except:
scd = 'Error -- scd'
#EXCLUINDO O ARQIVO CRIADO
#os.remove(bill)
#RETORNANDO O SCD
return scd
#FUNÇÃO PARA ACHAR O VALOR TOTAL
def get_total(arq):
value_pattern = "^[0-9]+.{1,25}\\,[0-9]{1,2}$"
# TRANSFORMAR PDF EM IMG
if arq.endswith('.pdf'):
bill = arq.replace('pdf', 'png')
lista = convert_from_path(arq)
page = lista[0]
page.save(os.path.join(os.getcwd(), bill), 'png')
else:
bill = arq
#Lendo a Imagem
img = Image.open(bill)
#Usando o OCR
try:
img_data = pytesseract.image_to_data(img, output_type=Output.DICT, lang='por')
except:
os.remove(bill)
return None
#Limpando a lista de palavras
img_data['text'] = [x for x in img_data['text'] if x!='' and x!=' ']
valores = []
#Achando o Valor pelo Metodo 1
for i, word in enumerate(img_data['text']):
# Achando a palavra 'Vencimento'
if ('Valor' in word) or ('valor' in word):
# Buscando pela Data a partir da palavra 'Valor'
for wor in img_data['text'][i + 1:]:
if re.match(value_pattern, wor) != None:
valores.append(wor)
break
#print(f'1- {valores}')
# Achando a Data pelo Metodo 2
if not check(valores) or valores==[]:
valores = []
for i, word in enumerate(img_data['text']):
# Achando a palavra 'Vencimento'
if ('Pagar' in word) or ('pagar' in word) or ('Cobrado' in word) or ('cobrado' in word):
# Buscando pela Data a partir da palavra 'Vencimento'
for wor in img_data['text'][i + 1:]:
if re.match(value_pattern, wor) != None:
valores.append(wor)
break
#print(f'2- {valores}')
#Removendo o arquivo de img criado
os.remove(bill)
#Checando se o valor é válido
if check(valores) and valores!=[]:
#Formatando o valor para ficar com virgula nos cetavos
vv = valores[0]
vn = rreplace(string=vv, find='.', replace='', n_occurences=vv.count('.'))
vn = rreplace(string=vn, find=',', replace='.', n_occurences=vn.count(','))
vn = "{0:.2f}".format(float(vn))
return vn
else:
vn = [rreplace(string=x, find='.', replace='', n_occurences=x.count('.')) for x in valores]
vn = [rreplace(string=x, find=',', replace='.', n_occurences=x.count(',')) for x in vn]
vn = "{0:.2f}".format(max([float(x) for x in vn]))
return vn