[Utils] Migrate models/textdet/postprocessors/utils.py

pull/1178/head
wangxinyu 2022-07-04 09:59:26 +00:00 committed by gaotongxiao
parent d942427161
commit c8589f2af4
7 changed files with 27 additions and 47 deletions

View File

@ -23,6 +23,11 @@ mmocr/models/textdet/detectors/single_stage_text_detector.py
# It will be removed after all utils are moved to mmocr.utils
mmocr/core/evaluation/utils.py
mmocr/models/textdet/postprocessors/utils.py
utils/postprocessor_utils.py
# It will be removed after all models have been refactored
mmocr/utils/ocr.py
mmocr/models/textdet/modules/proposal_local_graph.py
mmocr/utils/bbox_utils.py
mmocr/datasets/pipelines/crop.py

View File

@ -5,7 +5,7 @@ import torch
from lanms import merge_quadrangle_n9 as la_nms
from mmcv.ops import RoIAlignRotated
from mmocr.models.textdet.postprocessors.utils import fill_hole
from mmocr.utils import fill_hole
from .utils import (euclidean_distance_matrix, feature_embedding,
normalize_adjacent_matrix)

View File

@ -9,8 +9,8 @@ from numpy.fft import ifft
from mmocr.core import TextDetDataSample
from mmocr.registry import MODELS
from mmocr.utils import fill_hole
from .base_postprocessor import BaseTextDetPostProcessor
from .utils import fill_hole
@MODELS.register_module()

View File

@ -11,8 +11,8 @@ from skimage.morphology import skeletonize
from mmocr.core import TextDetDataSample
from mmocr.registry import MODELS
from mmocr.utils import fill_hole
from .base_postprocessor import BaseTextDetPostProcessor
from .utils import fill_hole
@MODELS.register_module()

View File

@ -4,50 +4,7 @@ import operator
import cv2
import numpy as np
import pyclipper
from numpy.linalg import norm
from shapely.geometry import Polygon
def filter_instance(area, confidence, min_area, min_confidence):
return bool(area < min_area or confidence < min_confidence)
def box_score_fast(bitmap, _box):
h, w = bitmap.shape[:2]
box = _box.copy()
xmin = np.clip(np.floor(box[:, 0].min()).astype(np.int32), 0, w - 1)
xmax = np.clip(np.ceil(box[:, 0].max()).astype(np.int32), 0, w - 1)
ymin = np.clip(np.floor(box[:, 1].min()).astype(np.int32), 0, h - 1)
ymax = np.clip(np.ceil(box[:, 1].max()).astype(np.int32), 0, h - 1)
mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)
box[:, 0] = box[:, 0] - xmin
box[:, 1] = box[:, 1] - ymin
cv2.fillPoly(mask, box.reshape(1, -1, 2).astype(np.int32), 1)
return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]
def unclip(box, unclip_ratio=1.5):
poly = Polygon(box)
distance = poly.area * unclip_ratio / poly.length
offset = pyclipper.PyclipperOffset()
offset.AddPath(box, pyclipper.JT_ROUND, pyclipper.ET_CLOSEDPOLYGON)
expanded = np.array(offset.Execute(distance))
return expanded
def fill_hole(input_mask):
h, w = input_mask.shape
canvas = np.zeros((h + 2, w + 2), np.uint8)
canvas[1:h + 1, 1:w + 1] = input_mask.copy()
mask = np.zeros((h + 4, w + 4), np.uint8)
cv2.floodFill(canvas, mask, (0, 0), 1)
canvas = canvas[1:h + 1, 1:w + 1].astype(np.bool)
return ~canvas | input_mask
class Node:

View File

@ -23,6 +23,7 @@ from .polygon_utils import (boundary_iou, crop_polygon, is_poly_inside_rect,
poly_intersection, poly_iou, poly_make_valid,
poly_union, polys2shapely, rescale_polygon,
rescale_polygons)
from .postprocessor_utils import fill_hole
from .setup_env import register_all_modules
from .string_util import StringStrip
@ -40,5 +41,5 @@ __all__ = [
'disable_text_recog_aug_test', 'box_center_distance', 'box_diag',
'compute_hmean', 'filter_2dlist_result', 'many2one_match_ic13',
'one2one_match_ic13', 'select_top_boundary', 'boundary_iou',
'point_distance', 'points_center'
'point_distance', 'points_center', 'fill_hole'
]

View File

@ -0,0 +1,17 @@
# Copyright (c) OpenMMLab. All rights reserved.
import cv2
import numpy as np
def fill_hole(input_mask):
# TODO typehints & test & docstring
h, w = input_mask.shape
canvas = np.zeros((h + 2, w + 2), np.uint8)
canvas[1:h + 1, 1:w + 1] = input_mask.copy()
mask = np.zeros((h + 4, w + 4), np.uint8)
cv2.floodFill(canvas, mask, (0, 0), 1)
canvas = canvas[1:h + 1, 1:w + 1].astype(np.bool)
return ~canvas | input_mask