Skip to content

Commit

Permalink
实现功能:当--savefile为true时,在--output下以当前图片名称后接“.txt”为文件名保存ocr推理结果,解决了issues:
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamQf-AI committed Aug 11, 2023
1 parent bf6ff0b commit 9b3bffb
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions paddleocr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -408,6 +408,7 @@ def parse_args(mMain=True):
parser.add_argument("--det", type=str2bool, default=True)
parser.add_argument("--rec", type=str2bool, default=True)
parser.add_argument("--type", type=str, default='ocr')
parser.add_argument("--savefile", type=str2bool, default=False)
parser.add_argument(
"--ocr_version",
type=str,
Expand Down Expand Up @@ -619,7 +620,7 @@ def __init__(self, **kwargs):
def ocr(self, img, det=True, rec=True, cls=True):
"""
ocr with paddleocr
args:
args锛?
img: img for ocr, support ndarray, img_path and list or ndarray
det: use text detection or not. If false, only rec will be exec. Default is True
rec: use text recognition or not. If false, only det will be exec. Default is True
Expand Down Expand Up @@ -768,10 +769,25 @@ def main():
rec=args.rec,
cls=args.use_angle_cls)
if result is not None:
lines = []
for idx in range(len(result)):
res = result[idx]
for line in res:
logger.info(line)
val = '['
for box in line[0]:
val += str(box[0]) + ',' + str(box[1]) + ','

val = val[:-1]
val += '],' + line[1][0] + ',' + str(line[1][1]) + '\n'
lines.append(val)
if args.savefile:
if os.path.exists(args.output) is False:
os.mkdir(args.output)
outfile = args.output + '/' + img_name + '.txt'
with open(outfile,'w',encoding='utf-8') as f:
f.writelines(lines)

elif args.type == 'structure':
img, flag_gif, flag_pdf = check_and_read(img_path)
if not flag_gif and not flag_pdf:
Expand Down Expand Up @@ -833,4 +849,4 @@ def main():
item.pop('img')
item.pop('res')
logger.info(item)
logger.info('result save to {}'.format(args.output))
logger.info('result save to {}'.format(args.output))

0 comments on commit 9b3bffb

Please sign in to comment.