-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
115 lines (90 loc) · 3.6 KB
/
app.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
import app.preprocessing as preprocessing
import app.text_detection as text_detection
import app.text_recognition as text_recognition
import app.post_proccess as post_process
import app.util as util
import cv2
import os
import pathlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# High = Less boxes
TEXT_DETECTION_THRESHOLD = 0.5
# Low = Less overlap
TEXT_OVERLAP_THRESHOLD = 0.1
def ocr_engine(input_image):
# ______ Preprocess ____________
preprocessed_image = preprocessing.preprocess_image(
image=input_image, grayscale=True, noise_removal=True, deskew=False
)
# util.show_image(preprocessed_image)
# ______________________________
# ______ Text detection ________
# Create 3-layer image to fit text detection
merged_image = cv2.merge(
[preprocessed_image, preprocessed_image, preprocessed_image]
)
text_detected_image, boxes = text_detection.detect_text(
merged_image,
threshold=TEXT_DETECTION_THRESHOLD,
overlap_threshold=TEXT_OVERLAP_THRESHOLD,
)
# plt.imshow(text_detected_image)
# plt.show()
# util.show_image(text_detected_image)
# ______________________________
# ______ Text recognition ______
boxes = text_recognition.recognize_text(preprocessed_image, boxes)
# _______________________________
return boxes
def blank_clustering(file_name: str):
# ______ INPUT IMAGE ____________
input_image = cv2.imread(file_name, cv2.IMREAD_COLOR)
# util.show_image(input_image)
# _______________________________
boxes = ocr_engine(input_image)
# ________ Post-process ________
grouped_boxes = post_process.post_process_blank(boxes)
# _______________________________
return grouped_boxes
def information_extraction(file_name: str, grouped_boxes: list):
# ______ INPUT IMAGE ____________
input_image = cv2.imread(file_name, cv2.IMREAD_COLOR)
# util.show_image(input_image)
# _______________________________
boxes = ocr_engine(input_image)
# ________ Post-process ________
clustered_image, output_list = post_process.post_process_filled(
boxes, grouped_boxes, input_image
)
# util.show_image(clustered_image)
return clustered_image, output_list
# _______________________________
if __name__ == "__main__":
PATH = str(pathlib.Path(__file__).parent.resolve())
FOLDER_NAMES = []
for (dirpath, dirnames, filenames) in os.walk(PATH + "\\input\\form_blank\\"):
FOLDER_NAMES.extend(dirnames)
break
# print(FOLDER_NAMES)
for folder in FOLDER_NAMES:
print("_________ BLANK CLUSTERING __________")
grouped_boxes = blank_clustering("input\\form_blank\\" + folder + "\\blank.png")
print("_____________________________________")
print("\n" + "\n" + "\n" + "\n")
for (dirpath, dirnames, filenames) in os.walk(
PATH + "\\input\\form_filled\\" + folder + "\\"
):
for file in filenames:
full_file_path = PATH + "\\input\\form_filled\\" + folder + "\\" + file
# print(full_file_path)
print("_________ INFORMATION EXTRACTION __________")
print(f"File: {full_file_path}")
image, output_list = information_extraction(
full_file_path, grouped_boxes
)
util.write_image(PATH + "\\output\\" + folder + "\\" + file, image)
util.save_csv(
PATH + "\\output\\" + folder + "\\" + file[:-3] + "csv", output_list
)
print("___________________________________________")