predict_det.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import numpy as np
  2. from .imaug import transform, create_operators
  3. from .db_postprocess import DBPostProcess
  4. from .predict_base import PredictBase
  5. class TextDetector(PredictBase):
  6. def __init__(self, args):
  7. self.args = args
  8. self.det_algorithm = args.det_algorithm
  9. pre_process_list = [
  10. {
  11. "DetResizeForTest": {
  12. "limit_side_len": args.det_limit_side_len,
  13. "limit_type": args.det_limit_type,
  14. }
  15. },
  16. {
  17. "NormalizeImage": {
  18. "std": [0.229, 0.224, 0.225],
  19. "mean": [0.485, 0.456, 0.406],
  20. "scale": "1./255.",
  21. "order": "hwc",
  22. }
  23. },
  24. {"ToCHWImage": None},
  25. {"KeepKeys": {"keep_keys": ["image", "shape"]}},
  26. ]
  27. postprocess_params = {}
  28. postprocess_params["name"] = "DBPostProcess"
  29. postprocess_params["thresh"] = args.det_db_thresh
  30. postprocess_params["box_thresh"] = args.det_db_box_thresh
  31. postprocess_params["max_candidates"] = 1000
  32. postprocess_params["unclip_ratio"] = args.det_db_unclip_ratio
  33. postprocess_params["use_dilation"] = args.use_dilation
  34. postprocess_params["score_mode"] = args.det_db_score_mode
  35. postprocess_params["box_type"] = args.det_box_type
  36. # 实例化预处理操作类
  37. self.preprocess_op = create_operators(pre_process_list)
  38. # self.postprocess_op = build_post_process(postprocess_params)
  39. # 实例化后处理操作类
  40. self.postprocess_op = DBPostProcess(**postprocess_params)
  41. # 初始化模型
  42. self.det_onnx_session = self.get_onnx_session(args.det_model_dir, args.use_gpu, gpu_id = args.gpu_id)
  43. self.det_input_name = self.get_input_name(self.det_onnx_session)
  44. self.det_output_name = self.get_output_name(self.det_onnx_session)
  45. def order_points_clockwise(self, pts):
  46. rect = np.zeros((4, 2), dtype="float32")
  47. s = pts.sum(axis=1)
  48. rect[0] = pts[np.argmin(s)]
  49. rect[2] = pts[np.argmax(s)]
  50. tmp = np.delete(pts, (np.argmin(s), np.argmax(s)), axis=0)
  51. diff = np.diff(np.array(tmp), axis=1)
  52. rect[1] = tmp[np.argmin(diff)]
  53. rect[3] = tmp[np.argmax(diff)]
  54. return rect
  55. def clip_det_res(self, points, img_height, img_width):
  56. for pno in range(points.shape[0]):
  57. points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))
  58. points[pno, 1] = int(min(max(points[pno, 1], 0), img_height - 1))
  59. return points
  60. def filter_tag_det_res(self, dt_boxes, image_shape):
  61. img_height, img_width = image_shape[0:2]
  62. dt_boxes_new = []
  63. for box in dt_boxes:
  64. if type(box) is list:
  65. box = np.array(box)
  66. box = self.order_points_clockwise(box)
  67. box = self.clip_det_res(box, img_height, img_width)
  68. rect_width = int(np.linalg.norm(box[0] - box[1]))
  69. rect_height = int(np.linalg.norm(box[0] - box[3]))
  70. if rect_width <= 3 or rect_height <= 3:
  71. continue
  72. dt_boxes_new.append(box)
  73. dt_boxes = np.array(dt_boxes_new)
  74. return dt_boxes
  75. def filter_tag_det_res_only_clip(self, dt_boxes, image_shape):
  76. img_height, img_width = image_shape[0:2]
  77. dt_boxes_new = []
  78. for box in dt_boxes:
  79. if type(box) is list:
  80. box = np.array(box)
  81. box = self.clip_det_res(box, img_height, img_width)
  82. dt_boxes_new.append(box)
  83. dt_boxes = np.array(dt_boxes_new)
  84. return dt_boxes
  85. def __call__(self, img):
  86. ori_im = img.copy()
  87. data = {"image": img}
  88. data = transform(data, self.preprocess_op)
  89. img, shape_list = data
  90. if img is None:
  91. return None, 0
  92. img = np.expand_dims(img, axis=0)
  93. shape_list = np.expand_dims(shape_list, axis=0)
  94. img = img.copy()
  95. input_feed = self.get_input_feed(self.det_input_name, img)
  96. outputs = self.det_onnx_session.run(self.det_output_name, input_feed=input_feed)
  97. preds = {}
  98. preds["maps"] = outputs[0]
  99. post_result = self.postprocess_op(preds, shape_list)
  100. dt_boxes = post_result[0]["points"]
  101. if self.args.det_box_type == "poly":
  102. dt_boxes = self.filter_tag_det_res_only_clip(dt_boxes, ori_im.shape)
  103. else:
  104. dt_boxes = self.filter_tag_det_res(dt_boxes, ori_im.shape)
  105. return dt_boxes