icdar.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # coding:utf-8
  2. import numpy as np
  3. import config
  4. def ground_truth_to_word(ground_truth):
  5. """
  6. Return the word string based on the input ground_truth
  7. """
  8. try:
  9. return ''.join([config.CHAR_VECTOR[i] for i in ground_truth if i != -1])
  10. except Exception as ex:
  11. print(ground_truth)
  12. print(ex)
  13. input()
  14. def restore_rectangle_rbox(origin, geometry):
  15. d = geometry[:, :4]
  16. angle = geometry[:, 4]
  17. # for angle > 0
  18. origin_0 = origin[angle >= 0]
  19. d_0 = d[angle >= 0]
  20. angle_0 = angle[angle >= 0]
  21. if origin_0.shape[0] > 0:
  22. p = np.array([np.zeros(d_0.shape[0]), -d_0[:, 0] - d_0[:, 2],
  23. d_0[:, 1] + d_0[:, 3], -d_0[:, 0] - d_0[:, 2],
  24. d_0[:, 1] + d_0[:, 3], np.zeros(d_0.shape[0]),
  25. np.zeros(d_0.shape[0]), np.zeros(d_0.shape[0]),
  26. d_0[:, 3], -d_0[:, 2]])
  27. p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
  28. rotate_matrix_x = np.array([np.cos(angle_0), np.sin(angle_0)]).transpose((1, 0))
  29. rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
  30. rotate_matrix_y = np.array([-np.sin(angle_0), np.cos(angle_0)]).transpose((1, 0))
  31. rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
  32. p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
  33. p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
  34. p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
  35. p3_in_origin = origin_0 - p_rotate[:, 4, :]
  36. new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
  37. new_p1 = p_rotate[:, 1, :] + p3_in_origin
  38. new_p2 = p_rotate[:, 2, :] + p3_in_origin
  39. new_p3 = p_rotate[:, 3, :] + p3_in_origin
  40. new_p_0 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
  41. new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
  42. else:
  43. new_p_0 = np.zeros((0, 4, 2))
  44. # for angle < 0
  45. origin_1 = origin[angle < 0]
  46. d_1 = d[angle < 0]
  47. angle_1 = angle[angle < 0]
  48. if origin_1.shape[0] > 0:
  49. p = np.array([-d_1[:, 1] - d_1[:, 3], -d_1[:, 0] - d_1[:, 2],
  50. np.zeros(d_1.shape[0]), -d_1[:, 0] - d_1[:, 2],
  51. np.zeros(d_1.shape[0]), np.zeros(d_1.shape[0]),
  52. -d_1[:, 1] - d_1[:, 3], np.zeros(d_1.shape[0]),
  53. -d_1[:, 1], -d_1[:, 2]])
  54. p = p.transpose((1, 0)).reshape((-1, 5, 2)) # N*5*2
  55. rotate_matrix_x = np.array([np.cos(-angle_1), -np.sin(-angle_1)]).transpose((1, 0))
  56. rotate_matrix_x = np.repeat(rotate_matrix_x, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1)) # N*5*2
  57. rotate_matrix_y = np.array([np.sin(-angle_1), np.cos(-angle_1)]).transpose((1, 0))
  58. rotate_matrix_y = np.repeat(rotate_matrix_y, 5, axis=1).reshape(-1, 2, 5).transpose((0, 2, 1))
  59. p_rotate_x = np.sum(rotate_matrix_x * p, axis=2)[:, :, np.newaxis] # N*5*1
  60. p_rotate_y = np.sum(rotate_matrix_y * p, axis=2)[:, :, np.newaxis] # N*5*1
  61. p_rotate = np.concatenate([p_rotate_x, p_rotate_y], axis=2) # N*5*2
  62. p3_in_origin = origin_1 - p_rotate[:, 4, :]
  63. new_p0 = p_rotate[:, 0, :] + p3_in_origin # N*2
  64. new_p1 = p_rotate[:, 1, :] + p3_in_origin
  65. new_p2 = p_rotate[:, 2, :] + p3_in_origin
  66. new_p3 = p_rotate[:, 3, :] + p3_in_origin
  67. new_p_1 = np.concatenate([new_p0[:, np.newaxis, :], new_p1[:, np.newaxis, :],
  68. new_p2[:, np.newaxis, :], new_p3[:, np.newaxis, :]], axis=1) # N*4*2
  69. else:
  70. new_p_1 = np.zeros((0, 4, 2))
  71. return np.concatenate([new_p_0, new_p_1])
  72. def restore_rectangle(origin, geometry):
  73. return restore_rectangle_rbox(origin, geometry)
  74. if __name__ == '__main__':
  75. pass