-
Notifications
You must be signed in to change notification settings - Fork 13
/
eva_hwdb_with_gt_box.py
213 lines (178 loc) · 8.29 KB
/
eva_hwdb_with_gt_box.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
import torch
import numpy as np
import cv2
from utils.hwdb2_0_chars import char_set
from torchvision import transforms
from PIL import Image, ImageDraw, ImageFont
from utils.get_dgrl_data import get_pred_data
from utils.pred_utils import get_ar_cr, get_pred_str, normal_leven
from torch.cuda.amp import autocast as autocast
from models.model_with_TCN_big_new_one_batch_hwdb import Model
def predict(model, pred_iter, show=False):
with torch.no_grad():
img_np, img_tensor, boxes, page_label = next(pred_iter)
label_np = np.ones_like(img_np, dtype=np.uint8) * 255
boxes = boxes[0]
imgs = img_tensor.to(device)
# with autocast():
kernel, out_chars, sub_img_nums = model(imgs, [boxes], is_train=False)
prediction_char = out_chars
prediction_char = prediction_char.log_softmax(-1)
pred_strs = get_pred_str(prediction_char, char_set)
pred_str_group = pred_strs
# CR, AR, All = get_ar_cr(''.join(pred_str_group), ''.join(page_label))
CR, AR, All = 0, 0, 0
char_c = len(''.join(page_label))
edit_d = normal_leven(''.join(pred_str_group), ''.join(page_label))
for sub_p, sub_l in zip(pred_str_group, page_label):
sub_cr, sub_ar, sub_all = get_ar_cr(sub_p, sub_l)
CR += sub_cr
AR += sub_ar
All += sub_all
if show:
for box in boxes:
box = np.int_(box)
cv2.polylines(img_np, [box], True, 128, 1)
char_size = int(label_np.shape[1] / len(page_label) / 5)
if isinstance(label_np, np.ndarray):
label_np = Image.fromarray(cv2.cvtColor(label_np, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(label_np)
fontText = ImageFont.truetype('simfang.ttf', char_size, encoding="utf-8")
draw.text((0, 0), 'CR:{:.6f} AR:{:.6f}'.format(CR / All, AR / All), (0, 0, 0), font=fontText)
for i in range(len(pred_str_group)):
left = boxes[i][0][0]
top = boxes[i][0][1]
draw.text((left, top), 'label:' + page_label[i], (0, 0, 0), font=fontText)
draw.text((left, top + char_size), 'preds:' + pred_str_group[i], (0, 0, 0), font=fontText)
label_np = cv2.cvtColor(np.asarray(label_np), cv2.COLOR_RGB2BGR)
# show_np = np.hstack([img_np, label_np])
# show_np = cv2.resize(show_np, None, fx=0.7, fy=0.7)
if img_np.shape[1] > 1600:
scale = 1600 / img_np.shape[1]
img_np = cv2.resize(img_np, None, fx=scale, fy=scale)
label_np = cv2.resize(label_np, None, fx=scale, fy=scale)
cv2.imshow('1', img_np)
cv2.imshow('label', label_np)
cv2.waitKey()
return CR, AR, All, edit_d, char_c
if __name__ == '__main__':
from tqdm import tqdm
import os
device = torch.device('cuda')
img_transform = transforms.ToTensor()
model = Model(num_classes=3000, line_height=32, is_transformer=True, is_TCN=True).to(device)
model.load_state_dict(torch.load(
r'./output/hwdb2'
r'/model.pth'))
model.eval()
file_paths = []
for root_path in [r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.0Test/dgrl',
r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.1Test/dgrl',
r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.2Test/dgrl']:
for file_path in os.listdir(root_path):
if file_path.endswith('dgrl'):
file_paths.append(os.path.join(root_path, file_path))
CR_all, AR_all, All_all = 0, 0, 0
edit_d_a, char_c_a = 0, 0
TP_all, FP_all, FN_all = 0, 0, 0
pbar = tqdm(total=len(file_paths))
pred_iter = iter(get_pred_data(file_paths, 1600))
for i in range(len(file_paths)):
cr, ar, all, edit_d, char_c = predict(model, pred_iter, False)
CR_all += cr
AR_all += ar
All_all += all
edit_d_a += edit_d
char_c_a += char_c
pbar.display('CR:{:.6f} AR:{:.6f} edit_d:{:.6f}\n'.format(
CR_all / All_all, AR_all / All_all, (char_c_a - edit_d_a) / char_c_a))
pbar.update(1)
import torch
import numpy as np
import cv2
from utils.hwdb2_0_chars import char_set
from torchvision import transforms
from PIL import Image, ImageDraw, ImageFont
from utils.get_dgrl_data import get_pred_data
from utils.pred_utils import get_ar_cr, get_pred_str, normal_leven
from torch.cuda.amp import autocast as autocast
from models.model_with_TCN_big_new_one_batch_hwdb import Model
def predict(model, pred_iter, show=False):
with torch.no_grad():
img_np, img_tensor, boxes, page_label = next(pred_iter)
label_np = np.ones_like(img_np, dtype=np.uint8) * 255
boxes = boxes[0]
imgs = img_tensor.to(device)
# with autocast():
kernel, out_chars, sub_img_nums = model(imgs, [boxes], is_train=False)
prediction_char = out_chars
prediction_char = prediction_char.log_softmax(-1)
pred_strs = get_pred_str(prediction_char, char_set)
pred_str_group = pred_strs
# CR, AR, All = get_ar_cr(''.join(pred_str_group), ''.join(page_label))
CR, AR, All = 0, 0, 0
char_c = len(''.join(page_label))
edit_d = normal_leven(''.join(pred_str_group), ''.join(page_label))
for sub_p, sub_l in zip(pred_str_group, page_label):
sub_cr, sub_ar, sub_all = get_ar_cr(sub_p, sub_l)
CR += sub_cr
AR += sub_ar
All += sub_all
if show:
for box in boxes:
box = np.int_(box)
cv2.polylines(img_np, [box], True, 128, 1)
char_size = int(label_np.shape[1] / len(page_label) / 5)
if isinstance(label_np, np.ndarray):
label_np = Image.fromarray(cv2.cvtColor(label_np, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(label_np)
fontText = ImageFont.truetype('simfang.ttf', char_size, encoding="utf-8")
draw.text((0, 0), 'CR:{:.6f} AR:{:.6f}'.format(CR / All, AR / All), (0, 0, 0), font=fontText)
for i in range(len(pred_str_group)):
left = boxes[i][0][0]
top = boxes[i][0][1]
draw.text((left, top), 'label:' + page_label[i], (0, 0, 0), font=fontText)
draw.text((left, top + char_size), 'preds:' + pred_str_group[i], (0, 0, 0), font=fontText)
label_np = cv2.cvtColor(np.asarray(label_np), cv2.COLOR_RGB2BGR)
# show_np = np.hstack([img_np, label_np])
# show_np = cv2.resize(show_np, None, fx=0.7, fy=0.7)
if img_np.shape[1] > 1600:
scale = 1600 / img_np.shape[1]
img_np = cv2.resize(img_np, None, fx=scale, fy=scale)
label_np = cv2.resize(label_np, None, fx=scale, fy=scale)
cv2.imshow('1', img_np)
cv2.imshow('label', label_np)
cv2.waitKey()
return CR, AR, All, edit_d, char_c
if __name__ == '__main__':
from tqdm import tqdm
import os
device = torch.device('cuda')
img_transform = transforms.ToTensor()
model = Model(num_classes=3000, line_height=32, is_transformer=True, is_TCN=True).to(device)
model.load_state_dict(torch.load(
r'./output/hwdb2'
r'/model.pth'))
model.eval()
file_paths = []
for root_path in [r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.0Test/dgrl',
r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.1Test/dgrl',
r'D:\git\OCR\handwritind_dect_reco\data/hwdb2/HWDB2.2Test/dgrl']:
for file_path in os.listdir(root_path):
if file_path.endswith('dgrl'):
file_paths.append(os.path.join(root_path, file_path))
CR_all, AR_all, All_all = 0, 0, 0
edit_d_a, char_c_a = 0, 0
TP_all, FP_all, FN_all = 0, 0, 0
pbar = tqdm(total=len(file_paths))
pred_iter = iter(get_pred_data(file_paths, 1600))
for i in range(len(file_paths)):
cr, ar, all, edit_d, char_c = predict(model, pred_iter, False)
CR_all += cr
AR_all += ar
All_all += all
edit_d_a += edit_d
char_c_a += char_c
pbar.display('CR:{:.6f} AR:{:.6f} edit_d:{:.6f}\n'.format(
CR_all / All_all, AR_all / All_all, (char_c_a - edit_d_a) / char_c_a))
pbar.update(1)