diff --git a/python/ch_ppocr_mobile_v2_cls/utils.py b/python/ch_ppocr_mobile_v2_cls/utils.py index edbd6a2..6fd2865 100755 --- a/python/ch_ppocr_mobile_v2_cls/utils.py +++ b/python/ch_ppocr_mobile_v2_cls/utils.py @@ -19,7 +19,7 @@ import imghdr def get_image_file_list(img_file): imgs_lists = [] if img_file is None or not os.path.exists(img_file): - raise ValueError("not found any img file in {}".format(img_file)) + raise FileNotFoundError(f"not found any img file in {img_file}") img_end = {'jpg', 'bmp', 'png', 'jpeg', 'rgb', 'tif', 'tiff', 'gif', 'GIF'} if os.path.isfile(img_file) and imghdr.what(img_file) in img_end: @@ -30,7 +30,7 @@ def get_image_file_list(img_file): if os.path.isfile(file_path) and imghdr.what(file_path) in img_end: imgs_lists.append(file_path) if len(imgs_lists) == 0: - raise ValueError("not found any img file in {}".format(img_file)) + raise FileNotFoundError(f"not found any img file in {img_file}") return imgs_lists diff --git a/python/ch_ppocr_server_v2_det/utils.py b/python/ch_ppocr_server_v2_det/utils.py index a3ac890..4ed6e55 100755 --- a/python/ch_ppocr_server_v2_det/utils.py +++ b/python/ch_ppocr_server_v2_det/utils.py @@ -314,7 +314,8 @@ class DBPostProcess(object): bitmap = _bitmap height, width = bitmap.shape - outs = cv2.findContours((bitmap * 255).astype(np.uint8), cv2.RETR_LIST, + outs = cv2.findContours((bitmap * 255).astype(np.uint8), + cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) if len(outs) == 3: img, contours, _ = outs[0], outs[1], outs[2] diff --git a/python/rapidOCR.py b/python/rapidOCR.py index a75418e..8c09c04 100644 --- a/python/rapidOCR.py +++ b/python/rapidOCR.py @@ -87,33 +87,40 @@ def draw_ocr_box_txt(image, boxes, txts, for idx, (box, txt) in enumerate(zip(boxes, txts)): if scores is not None and scores[idx] < drop_score: continue - color = (random.randint(0, 255), random.randint(0, 255), + + color = (random.randint(0, 255), + random.randint(0, 255), random.randint(0, 255)) draw_left.polygon(box, fill=color) - draw_right.polygon( - [ - box[0][0], box[0][1], box[1][0], box[1][1], box[2][0], - box[2][1], box[3][0], box[3][1] - ], - outline=color) - box_height = math.sqrt((box[0][0] - box[3][0])**2 + (box[0][1] - box[3][ - 1])**2) - box_width = math.sqrt((box[0][0] - box[1][0])**2 + (box[0][1] - box[1][ - 1])**2) + draw_right.polygon([box[0][0], box[0][1], + box[1][0], box[1][1], + box[2][0], box[2][1], + box[3][0], box[3][1]], + outline=color) + + box_height = math.sqrt((box[0][0] - box[3][0])**2 \ + + (box[0][1] - box[3][1])**2) + + box_width = math.sqrt((box[0][0] - box[1][0])**2 \ + + (box[0][1] - box[1][1])**2) + if box_height > 2 * box_width: font_size = max(int(box_width * 0.9), 10) - font = ImageFont.truetype(font_path, font_size, encoding="utf-8") + font = ImageFont.truetype(font_path, font_size, + encoding="utf-8") cur_y = box[0][1] for c in txt: char_size = font.getsize(c) - draw_right.text( - (box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font) + draw_right.text((box[0][0] + 3, cur_y), c, + fill=(0, 0, 0), font=font) cur_y += char_size[1] else: font_size = max(int(box_height * 0.8), 10) - font = ImageFont.truetype(font_path, font_size, encoding="utf-8") - draw_right.text( - [box[0][0], box[0][1]], txt, fill=(0, 0, 0), font=font) + font = ImageFont.truetype(font_path, font_size, + encoding="utf-8") + draw_right.text([box[0][0], box[0][1]], txt, + fill=(0, 0, 0), font=font) + img_left = Image.blend(image, img_left, 0.5) img_show = Image.new('RGB', (w * 2, h), (255, 255, 255)) img_show.paste(img_left, (0, 0, w, h)) @@ -132,8 +139,7 @@ def visualize(image_path, boxes, rec_res): draw_img_save = Path("./inference_results/") if not draw_img_save.exists(): - draw_img_save.mkdir(parents=True, - exist_ok=True) + draw_img_save.mkdir(parents=True, exist_ok=True) image_save = str(draw_img_save / f'infer_{Path(image_path).name}') cv2.imwrite(image_save, draw_img[:, :, ::-1]) @@ -220,16 +226,16 @@ class TextSystem(object): dt_boxes, elapse = self.text_detector(img) print("dt_boxes num : {}, elapse : {}".format( len(dt_boxes), elapse)) - if dt_boxes is None: + if dt_boxes is None or len(dt_boxes) < 1: return None, None - img_crop_list = [] - dt_boxes = self.sorted_boxes(dt_boxes) + img_crop_list = [] for bno in range(len(dt_boxes)): tmp_box = copy.deepcopy(dt_boxes[bno]) img_crop = self.get_rotate_crop_image(img, tmp_box) img_crop_list.append(img_crop) + if self.use_angle_cls: img_crop_list, angle_list, elapse = self.text_classifier( img_crop_list) @@ -239,7 +245,7 @@ class TextSystem(object): rec_res, elapse = self.text_recognizer(img_crop_list) print("rec_res num : {}, elapse : {}".format( len(rec_res), elapse)) - # self.print_draw_crop_rec_res(img_crop_list, rec_res) + filter_boxes, filter_rec_res = [], [] for box, rec_reuslt in zip(dt_boxes, rec_res): text, score = rec_reuslt