You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

postprocess.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Evaluation for CTPN"""
  16. import os
  17. import argparse
  18. import numpy as np
  19. from src.text_connector.detector import detect
  20. parser = argparse.ArgumentParser(description="CTPN evaluation")
  21. parser.add_argument("--dataset_path", type=str, default="", help="Dataset path.")
  22. parser.add_argument("--result_path", type=str, default="", help="Image path.")
  23. parser.add_argument("--label_path", type=str, default="", help="label path.")
  24. args_opt = parser.parse_args()
  25. def get_pred(img_file, result_path):
  26. file_name = img_file.split('.')[0]
  27. proposal_file = os.path.join(result_path, file_name + "_0.bin")
  28. mask_file = os.path.join(result_path, file_name + "_1.bin")
  29. proposal = np.fromfile(proposal_file, dtype=np.float16).reshape(1000, 5)
  30. proposal_mask = np.fromfile(mask_file, dtype=np.int8).reshape(1000)
  31. return proposal, proposal_mask
  32. def get_img_metas(imgSize):
  33. org_width, org_height = imgSize
  34. h_scale = 576 / org_height
  35. w_scale = 960 / org_width
  36. return np.array([576, 960, h_scale, w_scale])
  37. def get_gt_box(img_file, label_path):
  38. label_file = os.path.join(label_path, img_file.replace("jpg", "txt"))
  39. file = open(label_file)
  40. lines = file.readlines()
  41. gt_boxs = []
  42. for line in lines:
  43. label_info = line.split(",")
  44. print(label_info)
  45. gt_boxs.append([int(label_info[0]), int(label_info[1]), int(label_info[2]), int(label_info[3])])
  46. #print(line)
  47. #print(gt_boxs)
  48. return gt_boxs
  49. def ctpn_infer_test(dataset_path='', result_path='', label_path=''):
  50. output_dir = "./output/"
  51. output_img_dir = "./output_img/"
  52. img_files = os.listdir(dataset_path)
  53. for file in img_files:
  54. print("processing image: ", file)
  55. from PIL import Image, ImageDraw
  56. img = Image.open(dataset_path + '/' + file)
  57. proposal, proposal_mask = get_pred(file, result_path)
  58. img_size = img.size
  59. img_metas = get_img_metas(img_size)
  60. all_box_tmp = proposal
  61. all_mask_tmp = np.expand_dims(proposal_mask, axis=1)
  62. using_boxes_mask = all_box_tmp * all_mask_tmp
  63. textsegs = using_boxes_mask[:, 0:4].astype(np.float32)
  64. scores = using_boxes_mask[:, 4].astype(np.float32)
  65. shape = img_metas[:2].astype(np.int32)
  66. bboxes = detect(textsegs, scores[:, np.newaxis], shape)
  67. draw = ImageDraw.Draw(img)
  68. image_h = img_metas[2]
  69. image_w = img_metas[3]
  70. gt_boxs = get_gt_box(file, label_path)
  71. for gt_box in gt_boxs:
  72. gt_x1 = gt_box[0]
  73. gt_y1 = gt_box[1]
  74. gt_x2 = gt_box[2]
  75. gt_y2 = gt_box[3]
  76. draw.line([(gt_x1, gt_y1), (gt_x1, gt_y2), (gt_x2, gt_y2), (gt_x2, gt_y1), (gt_x1, gt_y1)],\
  77. fill='green', width=2)
  78. file_name = "res_" + file.replace("jpg", "txt")
  79. output_file = os.path.join(output_dir, file_name)
  80. f = open(output_file, 'w')
  81. for bbox in bboxes:
  82. x1 = bbox[0] / image_w
  83. y1 = bbox[1] / image_h
  84. x2 = bbox[2] / image_w
  85. y2 = bbox[3] / image_h
  86. draw.line([(x1, y1), (x1, y2), (x2, y2), (x2, y1), (x1, y1)], fill='red', width=2)
  87. str_tmp = str(int(x1)) + "," + str(int(y1)) + "," + str(int(x2)) + "," + str(int(y2))
  88. f.write(str_tmp)
  89. f.write("\n")
  90. f.close()
  91. img.save(output_img_dir + file)
  92. if __name__ == '__main__':
  93. ctpn_infer_test(args_opt.dataset_path, args_opt.result_path, args_opt.label_path)