mirror of
https://github.com/PaddlePaddle/PaddleOCR.git
synced 2026-09-24 23:33:08 +08:00
cherry pick PRs from community (#7273)
* Merge pull request #6824 from ChenNima/release/2.5-kie-save-res [kie]add write_kie_result to kie infer tool * Merge pull request #6677 from TonyJiangWJ/release/2.5 修复内存泄露问题 * Update native.cpp (#6650) fix issue 6640 * Merge pull request #6625 from ynjang/ynjang update sorted_boxes * fix DeprecationWarning, (#6604) DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead * Merge pull request #6585 from maxbachmann/release/2.5 replace GPL licensed components * Merge pull request #6575 from Eling486/release/2.5 update win doc * Merge pull request #6477 from MikoyChinese/fix-copy-paste Fix copy_paste no texts augment. * Merge pull request #6361 from mohamadmansourX/patch-9 Update README_en.md Co-authored-by: Double_V <liuvv0203@163.com> Co-authored-by: shawn <1021362695@qq.com> Co-authored-by: paopjian <672034519@qq.com>
This commit is contained in:
co-authored by
Double_V
shawn
paopjian
parent
dd063fc98b
commit
7e4e87dd6d
@@ -120,11 +120,14 @@ def sorted_boxes(dt_boxes):
|
||||
_boxes = list(sorted_boxes)
|
||||
|
||||
for i in range(num_boxes - 1):
|
||||
if abs(_boxes[i + 1][0][1] - _boxes[i][0][1]) < 10 and \
|
||||
(_boxes[i + 1][0][0] < _boxes[i][0][0]):
|
||||
tmp = _boxes[i]
|
||||
_boxes[i] = _boxes[i + 1]
|
||||
_boxes[i + 1] = tmp
|
||||
for j in range(i, 0, -1):
|
||||
if abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10 and \
|
||||
(_boxes[j + 1][0][0] < _boxes[j][0][0]):
|
||||
tmp = _boxes[j]
|
||||
_boxes[j] = _boxes[j + 1]
|
||||
_boxes[j + 1] = tmp
|
||||
else:
|
||||
break
|
||||
return _boxes
|
||||
|
||||
|
||||
|
||||
@@ -549,7 +549,7 @@ def text_visual(texts,
|
||||
def base64_to_cv2(b64str):
|
||||
import base64
|
||||
data = base64.b64decode(b64str.encode('utf8'))
|
||||
data = np.fromstring(data, np.uint8)
|
||||
data = np.frombuffer(data, np.uint8)
|
||||
data = cv2.imdecode(data, cv2.IMREAD_COLOR)
|
||||
return data
|
||||
|
||||
|
||||
+26
-1
@@ -88,6 +88,29 @@ def draw_kie_result(batch, node, idx_to_cls, count):
|
||||
cv2.imwrite(save_path, vis_img)
|
||||
logger.info("The Kie Image saved in {}".format(save_path))
|
||||
|
||||
def write_kie_result(fout, node, data):
|
||||
"""
|
||||
Write infer result to output file, sorted by the predict label of each line.
|
||||
The format keeps the same as the input with additional score attribute.
|
||||
"""
|
||||
import json
|
||||
label = data['label']
|
||||
annotations = json.loads(label)
|
||||
max_value, max_idx = paddle.max(node, -1), paddle.argmax(node, -1)
|
||||
node_pred_label = max_idx.numpy().tolist()
|
||||
node_pred_score = max_value.numpy().tolist()
|
||||
res = []
|
||||
for i, label in enumerate(node_pred_label):
|
||||
pred_score = '{:.2f}'.format(node_pred_score[i])
|
||||
pred_res = {
|
||||
'label': label,
|
||||
'transcription': annotations[i]['transcription'],
|
||||
'score': pred_score,
|
||||
'points': annotations[i]['points'],
|
||||
}
|
||||
res.append(pred_res)
|
||||
res.sort(key=lambda x: x['label'])
|
||||
fout.writelines([json.dumps(res, ensure_ascii=False) + '\n'])
|
||||
|
||||
def main():
|
||||
global_config = config['Global']
|
||||
@@ -114,7 +137,7 @@ def main():
|
||||
|
||||
warmup_times = 0
|
||||
count_t = []
|
||||
with open(save_res_path, "wb") as fout:
|
||||
with open(save_res_path, "w") as fout:
|
||||
with open(config['Global']['infer_img'], "rb") as f:
|
||||
lines = f.readlines()
|
||||
for index, data_line in enumerate(lines):
|
||||
@@ -139,6 +162,8 @@ def main():
|
||||
node = F.softmax(node, -1)
|
||||
count_t.append(time.time() - st)
|
||||
draw_kie_result(batch, node, idx_to_cls, index)
|
||||
write_kie_result(fout, node, data)
|
||||
fout.close()
|
||||
logger.info("success!")
|
||||
logger.info("It took {} s for predict {} images.".format(
|
||||
np.sum(count_t), len(count_t)))
|
||||
|
||||
Reference in New Issue
Block a user