predict_det.py 12 KB

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