predict_det.py 14 KB

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