predict_det.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. import os
  15. import sys
  16. __dir__ = os.path.dirname(os.path.abspath(__file__))
  17. sys.path.append(__dir__)
  18. sys.path.insert(0, os.path.abspath(os.path.join(__dir__, '../..')))
  19. os.environ["FLAGS_allocator_strategy"] = 'auto_growth'
  20. import cv2
  21. import numpy as np
  22. import time
  23. import sys
  24. import tools.infer.utility as utility
  25. from ppocr.utils.logging import get_logger
  26. from ppocr.utils.utility import get_image_file_list, check_and_read_gif
  27. from ppocr.data import create_operators, transform
  28. from ppocr.postprocess import build_post_process
  29. import json
  30. logger = get_logger()
  31. class TextDetector(object):
  32. def __init__(self, args):
  33. self.args = args
  34. self.det_algorithm = args.det_algorithm
  35. self.use_onnx = args.use_onnx
  36. pre_process_list = [{
  37. 'DetResizeForTest': {
  38. # 'limit_side_len': args.det_limit_side_len,
  39. # 'limit_type': args.det_limit_type,
  40. 'resize_long': args.det_resize_long
  41. }
  42. }, {
  43. 'NormalizeImage': {
  44. 'std': [0.229, 0.224, 0.225],
  45. 'mean': [0.485, 0.456, 0.406],
  46. 'scale': '1./255.',
  47. 'order': 'hwc'
  48. }
  49. }, {
  50. 'ToCHWImage': None
  51. }, {
  52. 'KeepKeys': {
  53. 'keep_keys': ['image', 'shape']
  54. }
  55. }]
  56. postprocess_params = {}
  57. if self.det_algorithm == "DB":
  58. postprocess_params['name'] = 'DBPostProcess'
  59. postprocess_params["thresh"] = args.det_db_thresh
  60. postprocess_params["box_thresh"] = args.det_db_box_thresh
  61. postprocess_params["max_candidates"] = 1000
  62. postprocess_params["unclip_ratio"] = args.det_db_unclip_ratio
  63. postprocess_params["use_dilation"] = args.use_dilation
  64. postprocess_params["score_mode"] = args.det_db_score_mode
  65. elif self.det_algorithm == "EAST":
  66. postprocess_params['name'] = 'EASTPostProcess'
  67. postprocess_params["score_thresh"] = args.det_east_score_thresh
  68. postprocess_params["cover_thresh"] = args.det_east_cover_thresh
  69. postprocess_params["nms_thresh"] = args.det_east_nms_thresh
  70. elif self.det_algorithm == "SAST":
  71. pre_process_list[0] = {
  72. 'DetResizeForTest': {
  73. 'resize_long': args.det_limit_side_len
  74. }
  75. }
  76. postprocess_params['name'] = 'SASTPostProcess'
  77. postprocess_params["score_thresh"] = args.det_sast_score_thresh
  78. postprocess_params["nms_thresh"] = args.det_sast_nms_thresh
  79. self.det_sast_polygon = args.det_sast_polygon
  80. if self.det_sast_polygon:
  81. postprocess_params["sample_pts_num"] = 6
  82. postprocess_params["expand_scale"] = 1.2
  83. postprocess_params["shrink_ratio_of_width"] = 0.2
  84. else:
  85. postprocess_params["sample_pts_num"] = 2
  86. postprocess_params["expand_scale"] = 1.0
  87. postprocess_params["shrink_ratio_of_width"] = 0.3
  88. elif self.det_algorithm == "PSE":
  89. postprocess_params['name'] = 'PSEPostProcess'
  90. postprocess_params["thresh"] = args.det_pse_thresh
  91. postprocess_params["box_thresh"] = args.det_pse_box_thresh
  92. postprocess_params["min_area"] = args.det_pse_min_area
  93. postprocess_params["box_type"] = args.det_pse_box_type
  94. postprocess_params["scale"] = args.det_pse_scale
  95. self.det_pse_box_type = args.det_pse_box_type
  96. elif self.det_algorithm == "FCE":
  97. pre_process_list[0] = {
  98. 'DetResizeForTest': {
  99. 'rescale_img': [1080, 736]
  100. }
  101. }
  102. postprocess_params['name'] = 'FCEPostProcess'
  103. postprocess_params["scales"] = args.scales
  104. postprocess_params["alpha"] = args.alpha
  105. postprocess_params["beta"] = args.beta
  106. postprocess_params["fourier_degree"] = args.fourier_degree
  107. postprocess_params["box_type"] = args.det_fce_box_type
  108. else:
  109. logger.info("unknown det_algorithm:{}".format(self.det_algorithm))
  110. sys.exit(0)
  111. self.preprocess_op = create_operators(pre_process_list)
  112. self.postprocess_op = build_post_process(postprocess_params)
  113. self.predictor, self.input_tensor, self.output_tensors, self.config = utility.create_predictor(
  114. args, 'det', logger)
  115. if self.use_onnx:
  116. img_h, img_w = self.input_tensor.shape[2:]
  117. if img_h is not None and img_w is not None and img_h > 0 and img_w > 0:
  118. pre_process_list[0] = {
  119. 'DetResizeForTest': {
  120. 'image_shape': [img_h, img_w]
  121. }
  122. }
  123. self.preprocess_op = create_operators(pre_process_list)
  124. if args.benchmark:
  125. import auto_log
  126. pid = os.getpid()
  127. gpu_id = utility.get_infer_gpuid()
  128. self.autolog = auto_log.AutoLogger(
  129. model_name="det",
  130. model_precision=args.precision,
  131. batch_size=1,
  132. data_shape="dynamic",
  133. save_path=None,
  134. inference_config=self.config,
  135. pids=pid,
  136. process_name=None,
  137. gpu_ids=gpu_id if args.use_gpu else None,
  138. time_keys=[
  139. 'preprocess_time', 'inference_time', 'postprocess_time'
  140. ],
  141. warmup=2,
  142. logger=logger)
  143. def order_points_clockwise(self, pts):
  144. rect = np.zeros((4, 2), dtype="float32")
  145. s = pts.sum(axis=1)
  146. rect[0] = pts[np.argmin(s)]
  147. rect[2] = pts[np.argmax(s)]
  148. diff = np.diff(pts, axis=1)
  149. rect[1] = pts[np.argmin(diff)]
  150. rect[3] = pts[np.argmax(diff)]
  151. return rect
  152. def clip_det_res(self, points, img_height, img_width):
  153. for pno in range(points.shape[0]):
  154. points[pno, 0] = int(min(max(points[pno, 0], 0), img_width - 1))
  155. points[pno, 1] = int(min(max(points[pno, 1], 0), img_height - 1))
  156. return points
  157. def filter_tag_det_res(self, dt_boxes, image_shape):
  158. img_height, img_width = image_shape[0:2]
  159. dt_boxes_new = []
  160. for box in dt_boxes:
  161. box = self.order_points_clockwise(box)
  162. box = self.clip_det_res(box, img_height, img_width)
  163. rect_width = int(np.linalg.norm(box[0] - box[1]))
  164. rect_height = int(np.linalg.norm(box[0] - box[3]))
  165. if rect_width <= 3 or rect_height <= 3:
  166. continue
  167. dt_boxes_new.append(box)
  168. dt_boxes = np.array(dt_boxes_new)
  169. return dt_boxes
  170. def filter_tag_det_res_only_clip(self, dt_boxes, image_shape):
  171. img_height, img_width = image_shape[0:2]
  172. dt_boxes_new = []
  173. for box in dt_boxes:
  174. box = self.clip_det_res(box, img_height, img_width)
  175. dt_boxes_new.append(box)
  176. dt_boxes = np.array(dt_boxes_new)
  177. return dt_boxes
  178. def __call__(self, img):
  179. ori_im = img.copy()
  180. data = {'image': img}
  181. st = time.time()
  182. if self.args.benchmark:
  183. self.autolog.times.start()
  184. data = transform(data, self.preprocess_op)
  185. img, shape_list = data
  186. if img is None:
  187. return None, 0
  188. img = np.expand_dims(img, axis=0)
  189. shape_list = np.expand_dims(shape_list, axis=0)
  190. img = img.copy()
  191. if self.args.benchmark:
  192. self.autolog.times.stamp()
  193. if self.use_onnx:
  194. input_dict = {}
  195. input_dict[self.input_tensor.name] = img
  196. outputs = self.predictor.run(self.output_tensors, input_dict)
  197. else:
  198. self.input_tensor.copy_from_cpu(img)
  199. self.predictor.run()
  200. outputs = []
  201. for output_tensor in self.output_tensors:
  202. output = output_tensor.copy_to_cpu()
  203. outputs.append(output)
  204. if self.args.benchmark:
  205. self.autolog.times.stamp()
  206. preds = {}
  207. if self.det_algorithm == "EAST":
  208. preds['f_geo'] = outputs[0]
  209. preds['f_score'] = outputs[1]
  210. elif self.det_algorithm == 'SAST':
  211. preds['f_border'] = outputs[0]
  212. preds['f_score'] = outputs[1]
  213. preds['f_tco'] = outputs[2]
  214. preds['f_tvo'] = outputs[3]
  215. elif self.det_algorithm in ['DB', 'PSE']:
  216. preds['maps'] = outputs[0]
  217. elif self.det_algorithm == 'FCE':
  218. for i, output in enumerate(outputs):
  219. preds['level_{}'.format(i)] = output
  220. else:
  221. raise NotImplementedError
  222. #self.predictor.try_shrink_memory()
  223. post_result = self.postprocess_op(preds, shape_list)
  224. dt_boxes = post_result[0]['points']
  225. if (self.det_algorithm == "SAST" and self.det_sast_polygon) or (
  226. self.det_algorithm in ["PSE", "FCE"] and
  227. self.postprocess_op.box_type == 'poly'):
  228. dt_boxes = self.filter_tag_det_res_only_clip(dt_boxes, ori_im.shape)
  229. else:
  230. dt_boxes = self.filter_tag_det_res(dt_boxes, ori_im.shape)
  231. if self.args.benchmark:
  232. self.autolog.times.end(stamp=True)
  233. et = time.time()
  234. return dt_boxes, et - st
  235. if __name__ == "__main__":
  236. args = utility.parse_args()
  237. image_file_list = get_image_file_list(args.image_dir)
  238. text_detector = TextDetector(args)
  239. count = 0
  240. total_time = 0
  241. draw_img_save = "./inference_results"
  242. if args.warmup:
  243. img = np.random.uniform(0, 255, [640, 640, 3]).astype(np.uint8)
  244. for i in range(2):
  245. res = text_detector(img)
  246. if not os.path.exists(draw_img_save):
  247. os.makedirs(draw_img_save)
  248. save_results = []
  249. for image_file in image_file_list:
  250. img, flag = check_and_read_gif(image_file)
  251. if not flag:
  252. img = cv2.imread(image_file)
  253. if img is None:
  254. logger.info("error in loading image:{}".format(image_file))
  255. continue
  256. st = time.time()
  257. dt_boxes, _ = text_detector(img)
  258. elapse = time.time() - st
  259. if count > 0:
  260. total_time += elapse
  261. count += 1
  262. save_pred = os.path.basename(image_file) + "\t" + str(
  263. json.dumps([x.tolist() for x in dt_boxes])) + "\n"
  264. save_results.append(save_pred)
  265. logger.info(save_pred)
  266. logger.info("The predict time of {}: {}".format(image_file, elapse))
  267. src_im = utility.draw_text_det_res(dt_boxes, image_file)
  268. img_name_pure = os.path.split(image_file)[-1]
  269. img_path = os.path.join(draw_img_save,
  270. "det_res_{}".format(img_name_pure))
  271. cv2.imwrite(img_path, src_im)
  272. logger.info("The visualized image saved in {}".format(img_path))
  273. with open(os.path.join(draw_img_save, "det_results.txt"), 'w') as f:
  274. f.writelines(save_results)
  275. f.close()
  276. if args.benchmark:
  277. text_detector.autolog.report()