def rotate_vertices(vertices, theta, anchor=None): '''rotate vertices around anchor Input: vertices: vertices of text region <numpy.ndarray, (8,)> theta : angle in radian measure anchor : fixed position during rotation Output: rotated vertices <numpy.ndarray, (8,)> ''' v = vertices.reshape((4, 2)).T if anchor is None: anchor = v[:, :1] rotate_mat = get_rotate_mat(theta) res = np.dot(rotate_mat, v - anchor) return (res + anchor).T.reshape(-1)
def restore_polys(valid_pos, valid_geo, score_shape, scale=4): """ restore polys from feature maps in given positions. Input: valid_pos : potential text positions <numpy.ndarray, (n,2)>, each one is in form (x ,y) valid_geo : geometry in valid_pos <numpy.ndarray, (5,n)>, value of d1,d2,d3,d4,angle for each point score_shape: shape of score map scale : image / feature map Output: restored polys <numpy.ndarray, (n,8)>, index """ polys = [] index = [] # project to image size valid_pos *= scale d = valid_geo[:4, :] # 4 x N angle = valid_geo[4, :] # N, for i in range(valid_pos.shape[0]): x = valid_pos[i, 0] y = valid_pos[i, 1] y_min = y - d[0, i] y_max = y + d[1, i] x_min = x - d[2, i] x_max = x + d[3, i] # 角度设置为负值,代表逆时针旋转 rotate_mat = get_rotate_mat(-angle[i]) # 以(x, y)为中心点逆时针旋转 temp_x = np.array([[x_min, x_max, x_max, x_min]]) - x temp_y = np.array([[y_min, y_min, y_max, y_max]]) - y coordidates = np.concatenate((temp_x, temp_y), axis=0) # shape: (2, 4) res = np.dot(rotate_mat, coordidates) res[0, :] += x res[1, :] += y if is_valid_poly(res, score_shape, scale): index.append(i) polys.append([res[0, 0], res[1, 0], res[0, 1], res[1, 1], res[0, 2], res[1, 2], res[0, 3], res[1, 3]]) return np.array(polys), index
def restore_polys(valid_pos, valid_geo, score_shape, scale=4): '''restore polys from feature maps in given positions Input: valid_pos : potential text positions <numpy.ndarray, (n,2)> valid_geo : geometry in valid_pos <numpy.ndarray, (5,n)> score_shape: shape of score map scale : image / feature map Output: restored polys <numpy.ndarray, (n,8)>, index ''' polys = [] index = [] valid_pos *= scale d = valid_geo[:4, :] # 4 x N angle = valid_geo[4, :] # N, for i in range(valid_pos.shape[0]): x = valid_pos[i, 0] y = valid_pos[i, 1] # d1, d2, d3, d4 y_min = y - d[0, i] x_max = x + d[1, i] y_max = y + d[2, i] x_min = x - d[3, i] rotate_mat = get_rotate_mat(-angle[i]) temp_x = np.array([[x_min, x_max, x_max, x_min]]) - x temp_y = np.array([[y_min, y_min, y_max, y_max]]) - y coordidates = np.concatenate((temp_x, temp_y), axis=0) res = np.dot(rotate_mat, coordidates) res[0, :] += x res[1, :] += y if is_valid_poly(res, score_shape, scale): index.append(i) polys.append([ res[0, 0], res[1, 0], res[0, 1], res[1, 1], res[0, 2], res[1, 2], res[0, 3], res[1, 3] ]) return np.array(polys), index