Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't break overall processing on a bad image #10216

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -540,6 +540,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 @@ -548,6 +551,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:
UserUnknownFactor marked this conversation as resolved.
Show resolved Hide resolved
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