-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
254 lines (196 loc) · 7.29 KB
/
main.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
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
# -*- coding: utf-8 -*-
"""Untitled0.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1GJPdFMjb049-ylHyng_M1MaLtF2R9gOa
"""
!apt install ghostscript python3-tk
!apt-get install libmagic-dev
!pip install camelot-py
!pip install python-magic
!pip install pytesseract
!pip install ghostscript
!pip install sqlalchemy
!pip install kraken
!pip install --ignore-installed Pillow==9.0.0
!pip install openpyxl
import magic
import pprint
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import csv
import pytesseract
import camelot
import ghostscript
from PIL import Image
import warnings
from openpyxl import Workbook, load_workbook
from openpyxl.utils import get_column_interval
import re
import itertools
try:
from PIL import Image
except ImportError:
import Image
from sqlalchemy import create_engine, inspect
from kraken.lib.models import load_any
from kraken import rpred, binarization
import random
from subprocess import call
from imutils import contours
import argparse
def detect_file(path):
f = magic.Magic(mime=True, uncompress=True)
print(f.from_file(path))
return f.from_file(path)
def load_workbook_range(range_string, ws):
col_start, col_end = re.findall("[A-Z]+", range_string)
data_rows = []
for row in ws[range_string]:
data_rows.append([cell.value for cell in row])
return pd.DataFrame(data_rows, columns=get_column_interval(col_start, col_end))
warnings.filterwarnings("ignore", category=FutureWarning)
## ---Loading Kraken Model---
model = load_any("en-default.mlmodel")
def preprocessing_non_tabular(path):
img = cv2.imread(path)
## ---Binarization of image---
genrator_image = Image.fromarray(img)
genrator_image = binarization.nlbin(genrator_image)
# ----Grayscaling Image----
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# --- performing Otsu threshold ---
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
# cv2.imwrite("processed_image/threshold.png", thresh1)
# cv2.imshow('thresh1', thresh1)
# cv2.waitKey(0)
# ----Image dialation----
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 3))
dilation = cv2.dilate(thresh1, rect_kernel, iterations=1)
# cv2.imwrite("processed_image/dilation.png", dilation)
# cv2.imshow('dilation', dilation)
# cv2.waitKey(0)
# ---Finding contours ---
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
return img, genrator_image, contours[::-1]
def preprocessing_tabular(path):
# Load image
img = cv2.imread(path)
## ---Binarization of image---
genrator_image = Image.fromarray(img)
genrator_image = binarization.nlbin(genrator_image)
# ----Grayscaling Image----
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# --- performing Otsu threshold ---
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove text characters with morph open and contour filtering
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
cnts = cv2.findContours(opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area < 500:
cv2.drawContours(opening, [c], -1, (0, 0, 0), -1)
# Repair table lines, sort contours, and extract ROI
close = 255 - cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=1)
cnts = cv2.findContours(close, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts, _ = contours.sort_contours(cnts, method="top-to-bottom")
return img, genrator_image, cnts
def imgtry(path):
img, genrator_image, cnts = preprocessing_non_tabular(path)
if len(cnts) < 8:
img, genrator_image, cnts = preprocessing_tabular(path)
row_list = list()
old_y = 0
single_row = list()
for idx, cnt in enumerate(cnts):
x, y, w, h = cv2.boundingRect(cnt)
if idx == 0:
single_row.append([x, y, w, h])
else:
if y - old_y < 5:
single_row.append([x, y, w, h])
else:
row_list.append(single_row)
single_row = list()
single_row.append([x, y, w, h])
old_y = y
# color = (np.random.random(size=3) * 256)
dummy_image = img.copy()
all_text = list()
row_count = list()
for row_boxes in row_list:
row_text = list()
for one_box in row_boxes:
x, y, w, h = one_box
## Different color for each row
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
# Drawing box
cv2.rectangle(dummy_image, (x, y), (x + w, y + h), (b, g, r), 2)
#########################################################################################
## Kraken Text Extraction
cord = [x, y, x + w, y + h]
bound = {'boxes': [tuple(cord)], 'text_direction': 'horizontal-lr'}
## Using Kraken API
generator = rpred.rpred(network=model, im=genrator_image, bounds=bound)
nxt_gen = next(generator)
box_text = nxt_gen.prediction
print("Box_Text = {} | Y = {}".format(box_text, y))
##########################################################################################
row_text.append(box_text)
print(row_text)
row_count.append(len(row_text))
all_text.append(row_text)
print("======================================================================")
print(all_text)
# cv2.imwrite("processed_image/show_box.png", dummy_image)
# cv2.imshow('final', dummy_image)
# cv2.waitKey(0)
updated_text_rows = list()
columns = max(set(row_count), key=row_count.count)
for rows in all_text:
diff = columns - len(rows)
rows = rows + [" "] * diff
updated_text_rows.append(rows)
# Creating a dataframe of the generated OCR list
arr = np.array(updated_text_rows)
dataframe = pd.DataFrame(arr, columns=range(0, columns))
dataframe.to_csv("output.csv", index=False)
def to_pandasDF(path):
ft = detect_file(path)
df = pd.DataFrame()
if 'xml' in ft:
# df = pd.read_excel(path)
pp = pprint.PrettyPrinter(indent=4)
workbook = load_workbook(filename=path)
pp.pprint(workbook.sheetnames)
ws = workbook[input('sheet-name: ')]
df = load_workbook_range(input("Range: "), ws)
elif 'plain' in ft:
print('csv-file')
df = pd.read_csv(path)
elif 'image' in ft:
# ---Image_Path---
# path = "images/patient.png"
imgtry(path)
elif 'pdf' in ft:
# pdf tables working
p_DF = camelot.read_pdf(path, pages="1-end")
df = p_DF[0].df
else:
print('Invalid file format.')
disk_engine = create_engine('sqlite:///my_lite_store.db')
# two types of storage sql and csv
rnd = str(random.randint(0,100))+path.split('.')[0]
df.to_sql(rnd, disk_engine, if_exists='replace')
# df.to_csv(rnd+".csv")
return df
# output format to csv or to excel
path = input("Enter filename: ")
DF = to_pandasDF(path)