-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_process.py
225 lines (196 loc) · 7.35 KB
/
image_process.py
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
from PyPDF2 import PdfFileWriter, PdfFileReader
from pdf2image import convert_from_path
import os
from PIL import Image
import pytesseract
import cv2
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from Configuration import fl_Dict,sales,debt,assets,netWorth,pbit,capEmp,qrCA,cfa
import fnmatch,re
def pdf_split(filePath,file):
with open(os.path.join(filePath, file), "rb") as inputpdf1:
inputpdf = PdfFileReader(inputpdf1)
for i in range(inputpdf.numPages):
output = PdfFileWriter()
output.addPage(inputpdf.getPage(i))
with open(os.path.join(filePath, "split-page%s.pdf") % i, "wb") as outputStream:
output.write(outputStream)
return inputpdf.numPages
#### Convert PDF into image ####
def image_conversion(filePath, file,page_num):
pages = convert_from_path(os.path.join(filePath, "split-page%s.pdf") % page_num, 500)
for page in pages:
page.save(os.path.join(filePath, "split-page%s.png") % page_num, 'PNG')
#### OCR read ####
def OCR_read(filePath,file,page_num):
inputPath = os.path.join(filePath, "split-page%s.png") % page_num
inputImage = os.path.join(filePath, "split-page%s-grayscale.png") % page_num
### Convert image into grayscale ###
image = cv2.imread(inputPath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
### Image pre processing ###
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
### Save grayscale image to temporary folder ###
cv2.imwrite(inputImage, gray)
### load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file ##
#text = pytesseract.image_to_string(Image.open(inputPath),lang = 'eng',config = '--psm 6')
text1 = pytesseract.image_to_string(Image.open(inputImage),lang = 'eng',config = '--psm 6')
### Creating block from text
text2 = text1.split('\n')
text3 = np.array(text2)
################## Translate to excel ########
for i in range(0,len(text3)):
cnt=1
fir_str=""
fir_st=""
if(sum(c.isdigit() for c in text3[i])<7):
a.append(text3[i])
b.append("")
c.append("")
d.append("")
e.append("")
f.append("")
g.append("")
h.append("")
m.append("")
n.append("")
elif(text3[i].find("$")!=-1):
st_ring=text3[i].split()
count=1
for p in range(0,len(st_ring)):
if(count==1):
a.append(st_ring[p])
count=count+1
elif(count==2):
b.append(st_ring[p])
count=count+1
elif(count==3):
c.append(st_ring[p])
count=count+1
elif(count==4):
d.append(st_ring[p])
count=count+1
elif(count==5):
e.append(st_ring[p])
count=count+1
elif(count==6):
f.append(st_ring[p])
count=count+1
elif(count==7):
g.append(st_ring[p])
count=count+1
elif(count==8):
h.append(st_ring[p])
count=count+1
elif(count==9):
m.append(st_ring[p])
count=count+1
elif(count==10):
n.append(st_ring[p])
count=count+1
else:
ls_t1=text3[i].split()
for j in range(0,len(ls_t1)):
if(sum(c.isdigit() for c in ls_t1[j])==0):
ls_t = ls_t1[j].replace(",","")
ls_t = ls_t.replace("(","")
ls_t = ls_t.replace(")","")
else:
ls_t = ls_t1[j].replace(",","")
if((((ls_t.isalpha()==True and ls_t.isdigit()== False) or (ls_t.isalpha()==False and ls_t.isdigit()== False))and(ls_t!="." and ls_t!="-") and ls_t.find(")")==-1 )):
fir_st=fir_st+" "+ls_t
fir_str=fir_st
#print(fir_str)
elif((ls_t.isdigit()==True and sum(c.isdigit() for c in ls_t)>2 and ls_t.isalpha()==False)or ls_t.find(")")!=-1 or ls_t.find(".")!=-1 or ls_t.find("-")!=-1):
if(cnt==1):
a.append(fir_str)
b.append(ls_t)
cnt=cnt+1
elif(cnt==2):
c.append(ls_t)
cnt=cnt+1
elif(cnt==3):
d.append(ls_t)
cnt=cnt+1
elif(cnt==4):
e.append(ls_t)
cnt=cnt+1
elif(cnt==5):
f.append(ls_t)
cnt=cnt+1
elif(cnt==6):
g.append(ls_t)
cnt=cnt+1
elif(cnt==7):
h.append(ls_t)
cnt=cnt+1
elif(cnt==8):
m.append(ls_t)
cnt=cnt+1
elif(cnt==9):
n.append(ls_t)
cnt=cnt+1
try:
df.iloc[:,0]=a
except:(e)
try:
df.iloc[:,1]=b
except:(e)
try:
df.iloc[:,2]=c
except:(e)
try:
df.iloc[:,3]=d
except:(e)
try:
df.iloc[:,4]=e
except:(e)
try:
df.iloc[:,5]=f
except:(e)
try:
df.iloc[:,6]=g
except:(e)
try:
df.iloc[:,7]=h
except:(e)
try:
df.iloc[:,8]=m
except:(e)
try:
df.iloc[:,9]=n
except:(e)
return df
###################################
def OCR_read1(filePath,file,page_num):
inputPath = os.path.join(filePath, "split-page%s.png") % page_num
inputImage = os.path.join(filePath, "split-page%s-grayscale.png") % page_num
### Convert image into grayscale ###
image = cv2.imread(inputPath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
### Image pre processing ###
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
### Save grayscale image to temporary folder ###
cv2.imwrite(inputImage, gray)
### load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file ##
#text = pytesseract.image_to_string(Image.open(inputPath),lang = 'eng',config = '--psm 6')
#text1 = pytesseract.image_to_string(Image.open(inputImage),lang = 'eng',config = '--psm 6')
### Creating block from text
#text2 = text1.split('\n')
#text3 = np.array(text2)
return 0
####################################
###################################
def OCR_read2(filePath,file,page_num):
inputPath = os.path.join(filePath, "split-page%s.png") % page_num
inputImage = os.path.join(filePath, "split-page%s-grayscale.png") % page_num
### load the image as a PIL/Pillow image, apply OCR, and then delete the temporary file ##
#text = pytesseract.image_to_string(Image.open(inputPath),lang = 'eng',config = '--psm 6')
text1 = pytesseract.image_to_string(Image.open(inputImage),lang = 'eng',config = '--psm 6')
### Creating block from text
text2 = text1.split('\n')
text3 = np.array(text2)
return text3
####################################