-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
165 lines (120 loc) · 4.97 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
import os
import glob
import pandas as pd
import argparse
import sys
import platform
doc_spans_multiple_page = ["Certificate of incorporation", "incorporation"]
CWD_PATH = os.getcwd()
p1 = os.path.join(CWD_PATH,'ml','models')
p2 = os.path.join(CWD_PATH,'ml','models','research')
p3 = os.path.join(CWD_PATH,'ml','models','research','slim')
p4 = os.path.join(CWD_PATH,'ml','models','research','object_detection')
sys.path.append(p1)
sys.path.append(p2)
sys.path.append(p3)
sys.path.append(p4)
from docx import Document
from pathlib import Path
from doc_iden import main_doc_iden
from lib.word_doc_iden import main_word_doc_iden
from lib.word_doc_iden import remove_doc_files
from lib.get_move_list import main_get_move_list
from ml.ml_predict import predict
from ml.ml_train import train
from ml.ml_predict_multiple import predict_multiple
def sort_doc(path, ml_flag, train_flag):
for file in glob.glob(path + '/*.jpg'):
if(ml_flag):
print ("Using cutom ML model to process ",file)
output_path, confidence = predict(file)
output_path = os.path.join(os.getcwd(), ("output\\"+output_path))
elif(train_flag):
output_path = os.path.join(os.getcwd(), ("ml\\data"))
else:
print ("Using GCP APIs to process ", file)
output_path, confidence = main_doc_iden(file)
output_path = os.path.join(os.getcwd(), ("output\\"+output_path))
move_file(file, output_path)
if (train_flag):
train()
def move_file(input_path, output_path):
file_name = input_path.rsplit('\\', 1)[1]
if not os.path.exists(output_path):
os.makedirs(output_path)
print ("Saving {} to folder : {}.".format(input_path, output_path))
os.rename(input_path, output_path+"\\"+file_name)
def move_file_to_docx(input_path, output_path):
output_path = os.path.join(os.getcwd(), ("output\\"+output_path+".docx"))
file_name = input_path.rsplit('\\', 1)[1]
myFile = Path (output_path)
print ("Writing {} to file : {}.".format(input_path, output_path))
if myFile.is_file():
document = Document(output_path)
p = document.add_paragraph()
r = p.add_run()
r.add_picture(input_path)
document.save(output_path)
else:
document = Document()
p = document.add_paragraph()
r = p.add_run()
r.add_picture(input_path)
document.save(output_path)
os.remove(input_path)
if (platform.system() == "Windows"):
os.system('cls')
else:
os.system('clear')
parser = argparse.ArgumentParser(description='Sort the identification docs into output folder')
parser.add_argument('-i', '--input' , default="input",
dest='i',
help='define the input dir')
parser.add_argument('-m', '--useML' , action='store_true', default=False,
dest='m_arg',
help='Use ML for docuemnt prediction')
parser.add_argument('-t', '--train' , action='store_true', default=False,
dest='t_arg',
help='Train the model')
parser.add_argument('-s', '--segregate' , action='store_true', default=False,
dest='s_arg',
help='Segregate multiple docs from a image')
parser.add_argument('-w', '--word_doc' , action='store_true', default=False,
dest='w_arg',
help='Sort images from word document')
args = parser.parse_args()
if args.i == 'input':
parser.print_help()
print ("\nUsing default dir for input")
folder = args.i
ml_flag = args.m_arg
train_flag = args.t_arg
seg_flag = args.s_arg
word_doc_flag = args.w_arg
image_dir = os.path.join(os.getcwd(), (folder))
if(seg_flag):
print ("Segregating the doc")
predict_multiple(image_dir+"\\test.jpg")
elif(word_doc_flag):
print ("Processing images from word doc")
#returns file names
file_names = main_word_doc_iden(image_dir)
file_names = sorted(file_names)
remove_doc_files(image_dir)
output_folder_list = []
confidence_list = []
for file in file_names:
print ("Using GCP APIs to process ", file)
output_path, confidence = main_doc_iden(file)
output_folder_list.append((output_path,file))
confidence_list.append(confidence)
# if there is no "No_Match" after a doc with matches category of span_multiple pages
# save Images after No_match and untill some other doc is recognised
final_move_list, final_move_list_doc = main_get_move_list(output_folder_list, doc_spans_multiple_page)
for data in final_move_list:
output_path = os.path.join(os.getcwd(), ("output\\"+data[1]))
move_file(data[0], output_path)
for data in final_move_list_doc:
move_file_to_docx(data[0], data[1])
else:
sort_doc(image_dir,ml_flag,train_flag)