# Copyright (c) OpenMMLab. All rights reserved. import sys import cv2 import numpy as np import pyclipper from mmcv.utils import print_log from shapely.geometry import Polygon as plg import mmocr.utils.check_argument as check_argument class BaseTextDetTargets: """Generate text detector ground truths.""" def __init__(self): pass def point2line(self, xs, ys, point_1, point_2): """Compute the distance from point to a line. This is adapted from https://github.com/MhLiao/DB. Args: xs (ndarray): The x coordinates of size hxw. ys (ndarray): The y coordinates of size hxw. point_1 (ndarray): The first point with shape 1x2. point_2 (ndarray): The second point with shape 1x2. Returns: result (ndarray): The distance matrix of size hxw. """ # suppose a triangle with three edge abc with c=point_1 point_2 # a^2 a_square = np.square(xs - point_1[0]) + np.square(ys - point_1[1]) # b^2 b_square = np.square(xs - point_2[0]) + np.square(ys - point_2[1]) # c^2 c_square = np.square(point_1[0] - point_2[0]) + np.square(point_1[1] - point_2[1]) # -cosC=(c^2-a^2-b^2)/2(ab) neg_cos_c = ( (c_square - a_square - b_square) / (np.finfo(np.float32).eps + 2 * np.sqrt(a_square * b_square))) # sinC^2=1-cosC^2 square_sin = 1 - np.square(neg_cos_c) square_sin = np.nan_to_num(square_sin) # distance=a*b*sinC/c=a*h/c=2*area/c result = np.sqrt(a_square * b_square * square_sin / (np.finfo(np.float32).eps + c_square)) # set result to minimum edge if C