forked from soap117/DeepRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pipe_type.py
268 lines (251 loc) · 11.6 KB
/
test_pipe_type.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
import os
import json
import torch
import pprint
import argparse
import matplotlib
matplotlib.use("Agg")
import cv2
from tqdm import tqdm
from config import system_configs
from nnet.py_factory import NetworkFactory
from db.datasets import datasets
import importlib
import json
from RuleGroup.Cls import GroupCls
from RuleGroup.Bar import GroupBarRaw
from RuleGroup.LineQuiry import GroupQuiryRaw
from RuleGroup.LIneMatch import GroupLineRaw
from RuleGroup.Pie import GroupPie
import math
from PIL import Image, ImageDraw, ImageFont
torch.backends.cudnn.benchmark = False
import requests
import time
import re
def parse_args():
parser = argparse.ArgumentParser(description="Test CornerNet")
parser.add_argument("--cfg_file", dest="cfg_file", help="config file", default="CornerNetLine", type=str)
parser.add_argument("--testiter", dest="testiter",
help="test at iteration i",
default=50000, type=int)
parser.add_argument("--split", dest="split",
help="which split to use",
default="validation", type=str)
parser.add_argument('--cache_path', dest="cache_path", type=str)
parser.add_argument('--result_path', dest="result_path", type=str)
parser.add_argument('--tar_data_path', dest="tar_data_path", type=str)
parser.add_argument("--suffix", dest="suffix", default=None, type=str)
parser.add_argument("--debug", action="store_true")
parser.add_argument("--data_dir", dest="data_dir", default="data/linedata(1028)", type=str)
parser.add_argument("--image_dir", dest="image_dir", default="C:/work/linedata(1028)/line/images/test2019/f4b5dac780890c2ca9f43c3fe4cc991a_d3d3LmVwc2lsb24uaW5zZWUuZnIJMTk1LjEwMS4yNTEuMTM2.xls-3-0.png", type=str)
args = parser.parse_args()
return args
def make_dirs(directories):
for directory in directories:
if not os.path.exists(directory):
os.makedirs(directory)
def load_net(testiter, cfg_name, data_dir, cache_dir, result_dir, cuda_id=0):
cfg_file = os.path.join(system_configs.config_dir, cfg_name + ".json")
with open(cfg_file, "r") as f:
configs = json.load(f)
configs["system"]["snapshot_name"] = cfg_name
configs["system"]["data_dir"] = data_dir
configs["system"]["cache_dir"] = cache_dir
configs["system"]["result_dir"] = result_dir
configs["system"]["tar_data_dir"] = "Cls"
system_configs.update_config(configs["system"])
train_split = system_configs.train_split
val_split = system_configs.val_split
test_split = system_configs.test_split
split = {
"training": train_split,
"validation": val_split,
"testing": test_split
}["validation"]
result_dir = system_configs.result_dir
result_dir = os.path.join(result_dir, str(testiter), split)
make_dirs([result_dir])
test_iter = system_configs.max_iter if testiter is None else testiter
print("loading parameters at iteration: {}".format(test_iter))
dataset = system_configs.dataset
db = datasets[dataset](configs["db"], split)
print("building neural network...")
nnet = NetworkFactory(db)
print("loading parameters...")
nnet.load_params(test_iter)
if torch.cuda.is_available():
nnet.cuda(cuda_id)
nnet.eval_mode()
return db, nnet
def Pre_load_nets(type, id_cuda):
methods = {}
if type == 0:
db_bar, nnet_bar = load_net(50000, "CornerNetPureBar", "data/bardata(1031)", "data/bardata(1031)/cache",
"data/bardata(1031)/result", id_cuda)
path = 'testfile.test_%s' % "CornerNetPureBar"
testing_bar = importlib.import_module(path).testing
methods['Bar'] = [db_bar, nnet_bar, testing_bar]
if type == 2:
db_pie, nnet_pie = load_net(50000, "CornerNetPurePie", "data/piedata(1008)", "data/piedata(1008)/cache",
"data/piedata(1008)/result", id_cuda)
path = 'testfile.test_%s' % "CornerNetPurePie"
testing_pie = importlib.import_module(path).testing
methods['Pie'] = [db_pie, nnet_pie, testing_pie]
if type == 1:
db_line, nnet_line = load_net(50000, "CornerNetLine", "data/linedata(1028)", "data/linedata(1028)/cache",
"data/linedata(1028)/result", id_cuda)
path = 'testfile.test_%s' % "CornerNetLine"
testing_line = importlib.import_module(path).testing
methods['Line'] = [db_line, nnet_line, testing_line]
db_line_cls, nnet_line_cls = load_net(20000, "CornerNetLineClsReal", "data/linedata(1028)",
"data/linedata(1028)/cache",
"data/linedata(1028)/result", id_cuda)
path = 'testfile.test_%s' % "CornerNetLineCls"
testing_line_cls = importlib.import_module(path).testing
methods['LineCls'] = [db_line_cls, nnet_line_cls, testing_line_cls]
return methods
def ocr_result(image_path):
subscription_key = "ad143190288d40b79483aa0d5c532724"
vision_base_url = "https://westus2.api.cognitive.microsoft.com/vision/v2.0/"
ocr_url = vision_base_url + "read/core/asyncBatchAnalyze"
headers = {'Ocp-Apim-Subscription-Key': subscription_key, 'Content-Type': 'application/octet-stream'}
params = {'language': 'unk', 'detectOrientation': 'true'}
image_data = open(image_path, "rb").read()
response = requests.post(ocr_url, headers=headers, params=params, data=image_data)
response.raise_for_status()
op_location = response.headers['Operation-Location']
analysis = {}
while "recognitionResults" not in analysis.keys():
time.sleep(3)
binary_content = requests.get(op_location, headers=headers, params=params).content
analysis = json.loads(binary_content.decode('ascii'))
line_infos = [region["lines"] for region in analysis["recognitionResults"]]
word_infos = []
for line in line_infos:
for word_metadata in line:
for word_info in word_metadata["words"]:
word_infos.append(word_info)
return word_infos
def check_intersection(box1, box2):
if (box1[2] - box1[0]) + ((box2[2] - box2[0])) > max(box2[2], box1[2]) - min(box2[0], box1[0]) \
and (box1[3] - box1[1]) + ((box2[3] - box2[1])) > max(box2[3], box1[3]) - min(box2[1], box1[1]):
Xc1 = max(box1[0], box2[0])
Yc1 = max(box1[1], box2[1])
Xc2 = min(box1[2], box2[2])
Yc2 = min(box1[3], box2[3])
intersection_area = (Xc2-Xc1)*(Yc2-Yc1)
return intersection_area/((box2[3]-box2[1])*(box2[2]-box2[0]))
else:
return 0
def try_math(image_path, cls_info):
title_list = [1, 2, 3]
title2string = {}
max_value = 1
min_value = 0
max_y = 0
min_y = 1
word_infos = ocr_result(image_path)
for id in title_list:
if id in cls_info.keys():
predicted_box = cls_info[id]
words = []
for word_info in word_infos:
word_bbox = [word_info["boundingBox"][0], word_info["boundingBox"][1], word_info["boundingBox"][4], word_info["boundingBox"][5]]
if check_intersection(predicted_box, word_bbox) > 0.5:
words.append([word_info["text"], word_bbox[0], word_bbox[1]])
words.sort(key=lambda x: x[1]+10*x[2])
word_string = ""
for word in words:
word_string = word_string + word[0] + ' '
title2string[id] = word_string
if 5 in cls_info.keys():
plot_area = cls_info[5]
y_max = plot_area[1]
y_min = plot_area[3]
x_board = plot_area[0]
dis_max = 10000000000000000
dis_min = 10000000000000000
for word_info in word_infos:
word_bbox = [word_info["boundingBox"][0], word_info["boundingBox"][1], word_info["boundingBox"][4], word_info["boundingBox"][5]]
word_text = word_info["text"]
word_text = re.sub('[^-+0123456789.]', '', word_text)
word_text_num = re.sub('[^0123456789]', '', word_text)
word_text_pure = re.sub('[^0123456789.]', '', word_text)
if len(word_text_num) > 0 and word_bbox[2] <= x_board+4:
dis2max = math.sqrt(math.pow((word_bbox[0]+word_bbox[2])/2-x_board, 2)+math.pow((word_bbox[1]+word_bbox[3])/2-y_max, 2))
dis2min = math.sqrt(math.pow((word_bbox[0] + word_bbox[2]) / 2 - x_board, 2) + math.pow(
(word_bbox[1] + word_bbox[3]) / 2 - y_min, 2))
y_mid = (word_bbox[1]+word_bbox[3])/2
if dis2max <= dis_max:
dis_max = dis2max
max_y = y_mid
max_value = float(word_text_pure)
if word_text[0] == '-':
max_value = -max_value
if dis2min <= dis_min:
dis_min = dis2min
min_y = y_mid
min_value = float(word_text_pure)
if word_text[0] == '-':
min_value = -min_value
delta_min_max = max_value-min_value
delta_mark = min_y - max_y
delta_plot_y = y_min - y_max
delta = delta_min_max/delta_mark
if abs(min_y-y_min)/delta_plot_y > 0.1:
print(abs(min_y-y_min)/delta_plot_y)
print("Predict the lower bar")
min_value = int(min_value + (min_y-y_min)*delta)
return title2string, round(min_value, 2), round(max_value, 2)
def test(image_path, data_type=0, debug=False, suffix=None, min_value_official=None, max_value_official=None):
image_cls = Image.open(image_path)
image = cv2.imread(image_path)
image_draw = Image.fromarray(image)
with torch.no_grad():
#results = methods['Cls'][2](image, methods['Cls'][0], methods['Cls'][1], debug=False)
#info = results[0]
#tls = results[1]
#brs = results[2]
#plot_area = []
#image_painted, cls_info = GroupCls(image_cls, tls, brs)
#title2string, min_value, max_value = try_math(image_path, cls_info)
if data_type == 0:
print("Predicted as BarChart")
results = methods['Bar'][2](image, methods['Bar'][0], methods['Bar'][1], debug=False)
tls = results[0]
brs = results[1]
bar_data = GroupBarRaw(image_draw, tls, brs)
return bar_data
if data_type == 2:
print("Predicted as PieChart")
results = methods['Pie'][2](image, methods['Pie'][0], methods['Pie'][1], debug=False)
cens = results[0]
keys = results[1]
image_draw, pie_data = GroupPie(image_draw, cens, keys)
return pie_data
if data_type== 1:
print("Predicted as LineChart")
results = methods['Line'][2](image, methods['Line'][0], methods['Line'][1], debug=False, cuda_id=1)
keys = results[0]
hybrids = results[1]
image_painted, quiry, keys, hybrids = GroupQuiryRaw(image_draw, keys, hybrids)
results = methods['LineCls'][2](image, methods['LineCls'][0], quiry, methods['LineCls'][1], debug=False, cuda_id=1)
line_data = GroupLineRaw(image_painted, keys, hybrids, results)
return line_data
if __name__ == "__main__":
methods = Pre_load_nets(2, 0)
tar_path = '/home/t-juluo/WebData/real_pie/image'
save_path = '/home/t-juluo/WebData/real_pie/result.json'
rs_dict = {}
images = os.listdir(tar_path)
for image in tqdm(images):
path = os.path.join(tar_path, image)
try:
data = test(path, 2)
rs_dict[image] = data
except:
continue
with open(save_path, "w") as f:
json.dump(rs_dict, f)