12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- # coding:utf-8
- import numpy as np
- import config
- def ground_truth_to_word(ground_truth):
- """
- Return the word string based on the input ground_truth
- """
- try:
- return ''.join([config.CHAR_VECTOR[i] for i in ground_truth if i != -1])
- except Exception as ex:
- print(ground_truth)
- print(ex)
- input()
- def restore_rectangle_rbox(origin, geometry):
- d = geometry[:, :4]
- angle = geometry[:, 4]
- # for angle > 0
- origin_0 = origin[angle >= 0]
- d_0 = d[angle >= 0]
- angle_0 = angle[angle >= 0]
- if origin_0.shape[0] > 0:
- p = np.array([np.zeros(d_0.shape[0]), -d_0[:, 0] - d_0[:, 2],
- d_0[:, 1] + d_0[:, 3], -d_0[:, 0] - d_0[:, 2],
- d_0[:, 1] + d_0[:, 3], np.zeros(d_0.shape[0]),
- np.zeros(d_0.shape[0]), np.zeros(d_0.shape[0]),
- d_0[:, 3], -d_0[:, 2]])
- p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
- rotate_matrix_x = np.array([np.cos(angle_0), np.sin(angle_0)]).transpose((1, 0))
- rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
- rotate_matrix_y = np.array([-np.sin(angle_0), np.cos(angle_0)]).transpose((1, 0))
- rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
- p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
- p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
- p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
- p3_in_origin = origin_0 - p_rotate[:, 4, :]
- new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
- new_p1 = p_rotate[:, 1, :] + p3_in_origin
- new_p2 = p_rotate[:, 2, :] + p3_in_origin
- new_p3 = p_rotate[:, 3, :] + p3_in_origin
- new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
- new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
- else:
- new_p_0 = np.zeros((0, 4, 2))
- # for angle < 0
- origin_1 = origin[angle < 0]
- d_1 = d[angle < 0]
- angle_1 = angle[angle < 0]
- if origin_1.shape[0] > 0:
- p = np.array([-d_1[:, 1] - d_1[:, 3], -d_1[:, 0] - d_1[:, 2],
- np.zeros(d_1.shape[0]), -d_1[:, 0] - d_1[:, 2],
- np.zeros(d_1.shape[0]), np.zeros(d_1.shape[0]),
- -d_1[:, 1] - d_1[:, 3], np.zeros(d_1.shape[0]),
- -d_1[:, 1], -d_1[:, 2]])
- p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
- rotate_matrix_x = np.array([np.cos(-angle_1), -np.sin(-angle_1)]).transpose((1, 0))
- rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
- rotate_matrix_y = np.array([np.sin(-angle_1), np.cos(-angle_1)]).transpose((1, 0))
- rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
- p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
- p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
- p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
- p3_in_origin = origin_1 - p_rotate[:, 4, :]
- new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
- new_p1 = p_rotate[:, 1, :] + p3_in_origin
- new_p2 = p_rotate[:, 2, :] + p3_in_origin
- new_p3 = p_rotate[:, 3, :] + p3_in_origin
- new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
- new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
- else:
- new_p_1 = np.zeros((0, 4, 2))
- return np.concatenate([new_p_0, new_p_1])
- def restore_rectangle(origin, geometry):
- return restore_rectangle_rbox(origin, geometry)
- if __name__ == '__main__':
- pass
|