Skip to content

Commit

Permalink
Don't break overall processing on a bad image (#10216)
Browse files Browse the repository at this point in the history
  • Loading branch information
UserUnknownFactor authored Jul 19, 2023
1 parent fac0387 commit 1dad0a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 12 additions & 6 deletions paddleocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,20 @@ def __init__(self, **kwargs):

def ocr(self, img, det=True, rec=True, cls=True):
"""
ocr with paddleocr
OCR with PaddleOCR
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
cls: use angle classifier or not. Default is True. If true, the text with rotation of 180 degrees can be recognized. If no text is rotated by 180 degrees, use cls=False to get better performance. Text with rotation of 90 or 270 degrees can be recognized even if cls=False.
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
cls: use angle classifier or not. Default is True. If True, the text with rotation of 180 degrees can be recognized. If no text is rotated by 180 degrees, use cls=False to get better performance. Text with rotation of 90 or 270 degrees can be recognized even if cls=False.
"""
assert isinstance(img, (np.ndarray, list, str, bytes))
if isinstance(img, list) and det == True:
logger.error('When input a list of images, det must be false')
exit(0)
if cls == True and self.use_angle_cls == False:
logger.warning(
'Since the angle classifier is not initialized, the angle classifier will not be uesd during the forward process'
'Since the angle classifier is not initialized, it will not be used during the forward process'
)

img = check_img(img)
Expand All @@ -541,6 +541,9 @@ def ocr(self, img, det=True, rec=True, cls=True):
ocr_res = []
for idx, img in enumerate(imgs):
dt_boxes, rec_res, _ = self.__call__(img, cls)
if not dt_boxes and not rec_res:
ocr_res.append(None)
continue
tmp_res = [[box.tolist(), res]
for box, res in zip(dt_boxes, rec_res)]
ocr_res.append(tmp_res)
Expand All @@ -549,6 +552,9 @@ def ocr(self, img, det=True, rec=True, cls=True):
ocr_res = []
for idx, img in enumerate(imgs):
dt_boxes, elapse = self.text_detector(img)
if not dt_boxes:
ocr_res.append(None)
continue
tmp_res = [box.tolist() for box in dt_boxes]
ocr_res.append(tmp_res)
return ocr_res
Expand Down
22 changes: 16 additions & 6 deletions tools/infer/predict_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,25 @@ def draw_crop_rec_res(self, output_dir, img_crop_list, rec_res):
self.crop_image_res_index += bbox_num

def __call__(self, img, cls=True):
time_dict = {'det': 0, 'rec': 0, 'csl': 0, 'all': 0}
time_dict = {'det': 0, 'rec': 0, 'cls': 0, 'all': 0}

if img is None:
logger.debug("no valid image provided")
return None, None, time_dict

start = time.time()
ori_im = img.copy()
dt_boxes, elapse = self.text_detector(img)
time_dict['det'] = elapse
logger.debug("dt_boxes num : {}, elapse : {}".format(
len(dt_boxes), elapse))

if dt_boxes is None:
return None, None
logger.debug("no dt_boxes found, elapsed : {}".format(elapse))
end = time.time()
time_dict['all'] = end - start
return None, None, time_dict
else:
logger.debug("dt_boxes num : {}, elapsed : {}".format(
len(dt_boxes), elapse))
img_crop_list = []

dt_boxes = sorted_boxes(dt_boxes)
Expand All @@ -89,12 +99,12 @@ def __call__(self, img, cls=True):
img_crop_list, angle_list, elapse = self.text_classifier(
img_crop_list)
time_dict['cls'] = elapse
logger.debug("cls num : {}, elapse : {}".format(
logger.debug("cls num : {}, elapsed : {}".format(
len(img_crop_list), elapse))

rec_res, elapse = self.text_recognizer(img_crop_list)
time_dict['rec'] = elapse
logger.debug("rec_res num : {}, elapse : {}".format(
logger.debug("rec_res num : {}, elapsed : {}".format(
len(rec_res), elapse))
if self.args.save_crop_res:
self.draw_crop_rec_res(self.args.crop_res_save_dir, img_crop_list,
Expand Down

0 comments on commit 1dad0a9

Please sign in to comment.