Spaces:
Sleeping
Sleeping
File size: 3,819 Bytes
43bca44 | 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 | from utils import error_rates
import copy
import os
import cv2
import json
from copy import deepcopy
import numpy as np
def interpolate(key1, key2, lf, lf_idx, step_percent):
x0 = lf[lf_idx][key1]
y0 = lf[lf_idx][key2]
x1 = lf[lf_idx+1][key1]
y1 = lf[lf_idx+1][key2]
x = x1 * step_percent + x0 * (1.0 - step_percent)
y = y1 * step_percent + y0 * (1.0 - step_percent)
return x, y
def get_subdivide_pt(i, pred_full, lf):
percent = (float(i)+0.5) / float(len(pred_full))
lf_percent = (len(lf)-1) * percent
lf_idx = int(np.floor(lf_percent))
step_percent = lf_percent - lf_idx
x0, y0 = interpolate("x0", "y0", lf, lf_idx, step_percent)
x1, y1 = interpolate("x1", "y1", lf, lf_idx, step_percent)
return x0, y0, x1, y1
def save_improved_idxs(improved_idxs, decoded_hw, decoded_raw_hw, out, x, json_folder):
output_lines = [{
"gt": gt['gt']
} for gt in x['gt_json']]
# for i in improved_idxs:
for i in range(len(output_lines)):
if not i in improved_idxs:
output_lines[i] = x['gt_json'][i]
continue
k = improved_idxs[i]
# We want to trim the LF results
# good to keep around the full length of the prediciton
# so we can generate the full line-level images later
# at a different resolution
line_points = []
after_line_points = []
lf_path = out['lf']
end = out['ending'][k]
for j in range(len(lf_path)):
p = lf_path[j][k]
s = out['results_scale']
if j > end:
after_line_points.append({
"x0": p[0][1] * s,
"x1": p[0][0] * s,
"y0": p[1][1] * s,
"y1": p[1][0] * s
})
else:
line_points.append({
"x0": p[0][1] * s,
"x1": p[0][0] * s,
"y0": p[1][1] * s,
"y1": p[1][0] * s
})
begin = out['beginning'][k]
begin_f = int(np.floor(begin))
p0 = out['lf'][begin_f][k]
if begin_f+1 >= len(out['lf']):
p = p0
else:
p1 = out['lf'][begin_f+1][k]
t = begin - np.floor(begin)
p = p0 * (1 - t) + p1 * t
sol_point = {
"x0": p[0][1] * s,
"x1": p[0][0] * s,
"y0": p[1][1] * s,
"y1": p[1][0] * s
}
img_file_name = "{}_{}.png".format(x['img_key'], i)
output_lines[i]['pred'] = decoded_hw[k]
output_lines[i]['pred_full'] = decoded_raw_hw[k]
output_lines[i]['sol'] = sol_point
output_lines[i]['lf'] = line_points
output_lines[i]['after_lf'] = after_line_points
output_lines[i]['start_idx'] = 1 #TODO: update to backward idx
output_lines[i]['hw_path'] = img_file_name
line_img = out['line_imgs'][k]
full_img_file_name = os.path.join(json_folder, img_file_name)
cv2.imwrite(full_img_file_name, line_img)
json_path = x['json_path']
with open(json_path, 'w') as f:
# print('written data to:', f)
json.dump(output_lines, f)
def update_ideal_results(pick, costs, decoded_hw, gt_json):
most_ideal_pred = []
improved_idxs = {}
for i in range(len(gt_json)):
gt_obj = gt_json[i]
prev_pred = gt_obj.get('pred', '')
gt = gt_obj['gt']
pred = decoded_hw[pick[i]]
prev_cer = error_rates.cer(gt, prev_pred)
cer = costs[i]
if cer > prev_cer or len(pred) == 0:
most_ideal_pred.append(prev_pred)
continue
most_ideal_pred.append(pred)
improved_idxs[i] = pick[i]
return most_ideal_pred, improved_idxs
|