pipeline.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. # Copyright (c) 2024 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. from typing import Any, Dict, List, Optional, Union
  15. import numpy as np
  16. from ....utils import logging
  17. from ....utils.deps import pipeline_requires_extra
  18. from ...common.batch_sampler import ImageBatchSampler
  19. from ...common.reader import ReadImage
  20. from ...utils.hpi import HPIConfig
  21. from ...utils.pp_option import PaddlePredictorOption
  22. from .._parallel import AutoParallelImageSimpleInferencePipeline
  23. from ..base import BasePipeline
  24. from ..components import (
  25. CropByPolys,
  26. SortPolyBoxes,
  27. SortQuadBoxes,
  28. convert_points_to_boxes,
  29. rotate_image,
  30. )
  31. from .result import OCRResult
  32. class _OCRPipeline(BasePipeline):
  33. """OCR Pipeline"""
  34. def __init__(
  35. self,
  36. config: Dict,
  37. device: Optional[str] = None,
  38. pp_option: Optional[PaddlePredictorOption] = None,
  39. use_hpip: bool = False,
  40. hpi_config: Optional[Union[Dict[str, Any], HPIConfig]] = None,
  41. ) -> None:
  42. """
  43. Initializes the class with given configurations and options.
  44. Args:
  45. config (Dict): Configuration dictionary containing various settings.
  46. device (str, optional): Device to run the predictions on. Defaults to None.
  47. pp_option (PaddlePredictorOption, optional): PaddlePredictor options. Defaults to None.
  48. use_hpip (bool, optional): Whether to use the high-performance
  49. inference plugin (HPIP) by default. Defaults to False.
  50. hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
  51. The default high-performance inference configuration dictionary.
  52. Defaults to None.
  53. """
  54. super().__init__(
  55. device=device, pp_option=pp_option, use_hpip=use_hpip, hpi_config=hpi_config
  56. )
  57. self.use_doc_preprocessor = config.get("use_doc_preprocessor", True)
  58. if self.use_doc_preprocessor:
  59. doc_preprocessor_config = config.get("SubPipelines", {}).get(
  60. "DocPreprocessor",
  61. {
  62. "pipeline_config_error": "config error for doc_preprocessor_pipeline!"
  63. },
  64. )
  65. self.doc_preprocessor_pipeline = self.create_pipeline(
  66. doc_preprocessor_config
  67. )
  68. self.use_textline_orientation = config.get("use_textline_orientation", True)
  69. if self.use_textline_orientation:
  70. textline_orientation_config = config.get("SubModules", {}).get(
  71. "TextLineOrientation",
  72. {"model_config_error": "config error for textline_orientation_model!"},
  73. )
  74. self.textline_orientation_model = self.create_model(
  75. textline_orientation_config
  76. )
  77. text_det_config = config.get("SubModules", {}).get(
  78. "TextDetection", {"model_config_error": "config error for text_det_model!"}
  79. )
  80. self.text_type = config["text_type"]
  81. if self.text_type == "general":
  82. self.text_det_limit_side_len = text_det_config.get("limit_side_len", 960)
  83. self.text_det_limit_type = text_det_config.get("limit_type", "max")
  84. self.text_det_thresh = text_det_config.get("thresh", 0.3)
  85. self.text_det_box_thresh = text_det_config.get("box_thresh", 0.6)
  86. self.input_shape = text_det_config.get("input_shape", None)
  87. self.text_det_unclip_ratio = text_det_config.get("unclip_ratio", 2.0)
  88. self._sort_boxes = SortQuadBoxes()
  89. self._crop_by_polys = CropByPolys(det_box_type="quad")
  90. elif self.text_type == "seal":
  91. self.text_det_limit_side_len = text_det_config.get("limit_side_len", 736)
  92. self.text_det_limit_type = text_det_config.get("limit_type", "min")
  93. self.text_det_thresh = text_det_config.get("thresh", 0.2)
  94. self.text_det_box_thresh = text_det_config.get("box_thresh", 0.6)
  95. self.text_det_unclip_ratio = text_det_config.get("unclip_ratio", 0.5)
  96. self.input_shape = text_det_config.get("input_shape", None)
  97. self._sort_boxes = SortPolyBoxes()
  98. self._crop_by_polys = CropByPolys(det_box_type="poly")
  99. else:
  100. raise ValueError("Unsupported text type {}".format(self.text_type))
  101. self.text_det_model = self.create_model(
  102. text_det_config,
  103. limit_side_len=self.text_det_limit_side_len,
  104. limit_type=self.text_det_limit_type,
  105. thresh=self.text_det_thresh,
  106. box_thresh=self.text_det_box_thresh,
  107. unclip_ratio=self.text_det_unclip_ratio,
  108. input_shape=self.input_shape,
  109. )
  110. text_rec_config = config.get("SubModules", {}).get(
  111. "TextRecognition",
  112. {"model_config_error": "config error for text_rec_model!"},
  113. )
  114. self.text_rec_score_thresh = text_rec_config.get("score_thresh", 0)
  115. self.input_shape = text_rec_config.get("input_shape", None)
  116. self.text_rec_model = self.create_model(
  117. text_rec_config, input_shape=self.input_shape
  118. )
  119. self.batch_sampler = ImageBatchSampler(batch_size=config.get("batch_size", 1))
  120. self.img_reader = ReadImage(format="BGR")
  121. def rotate_image(
  122. self, image_array_list: List[np.ndarray], rotate_angle_list: List[int]
  123. ) -> List[np.ndarray]:
  124. """
  125. Rotate the given image arrays by their corresponding angles.
  126. 0 corresponds to 0 degrees, 1 corresponds to 180 degrees.
  127. Args:
  128. image_array_list (List[np.ndarray]): A list of input image arrays to be rotated.
  129. rotate_angle_list (List[int]): A list of rotation indicators (0 or 1).
  130. 0 means rotate by 0 degrees
  131. 1 means rotate by 180 degrees
  132. Returns:
  133. List[np.ndarray]: A list of rotated image arrays.
  134. Raises:
  135. AssertionError: If any rotate_angle is not 0 or 1.
  136. AssertionError: If the lengths of input lists don't match.
  137. """
  138. assert len(image_array_list) == len(
  139. rotate_angle_list
  140. ), f"Length of image_array_list ({len(image_array_list)}) must match length of rotate_angle_list ({len(rotate_angle_list)})"
  141. for angle in rotate_angle_list:
  142. assert angle in [0, 1], f"rotate_angle must be 0 or 1, now it's {angle}"
  143. rotated_images = []
  144. for image_array, rotate_indicator in zip(image_array_list, rotate_angle_list):
  145. # Convert 0/1 indicator to actual rotation angle
  146. rotate_angle = rotate_indicator * 180
  147. rotated_image = rotate_image(image_array, rotate_angle)
  148. rotated_images.append(rotated_image)
  149. return rotated_images
  150. def check_model_settings_valid(self, model_settings: Dict) -> bool:
  151. """
  152. Check if the input parameters are valid based on the initialized models.
  153. Args:
  154. model_info_params(Dict): A dictionary containing input parameters.
  155. Returns:
  156. bool: True if all required models are initialized according to input parameters, False otherwise.
  157. """
  158. if model_settings["use_doc_preprocessor"] and not self.use_doc_preprocessor:
  159. logging.error(
  160. "Set use_doc_preprocessor, but the models for doc preprocessor are not initialized."
  161. )
  162. return False
  163. if (
  164. model_settings["use_textline_orientation"]
  165. and not self.use_textline_orientation
  166. ):
  167. logging.error(
  168. "Set use_textline_orientation, but the models for use_textline_orientation are not initialized."
  169. )
  170. return False
  171. return True
  172. def get_model_settings(
  173. self,
  174. use_doc_orientation_classify: Optional[bool],
  175. use_doc_unwarping: Optional[bool],
  176. use_textline_orientation: Optional[bool],
  177. ) -> dict:
  178. """
  179. Get the model settings based on the provided parameters or default values.
  180. Args:
  181. use_doc_orientation_classify (Optional[bool]): Whether to use document orientation classification.
  182. use_doc_unwarping (Optional[bool]): Whether to use document unwarping.
  183. use_textline_orientation (Optional[bool]): Whether to use textline orientation.
  184. Returns:
  185. dict: A dictionary containing the model settings.
  186. """
  187. if use_doc_orientation_classify is None and use_doc_unwarping is None:
  188. use_doc_preprocessor = self.use_doc_preprocessor
  189. else:
  190. if use_doc_orientation_classify is True or use_doc_unwarping is True:
  191. use_doc_preprocessor = True
  192. else:
  193. use_doc_preprocessor = False
  194. if use_textline_orientation is None:
  195. use_textline_orientation = self.use_textline_orientation
  196. return dict(
  197. use_doc_preprocessor=use_doc_preprocessor,
  198. use_textline_orientation=use_textline_orientation,
  199. )
  200. def get_text_det_params(
  201. self,
  202. text_det_limit_side_len: Optional[int] = None,
  203. text_det_limit_type: Optional[str] = None,
  204. text_det_thresh: Optional[float] = None,
  205. text_det_box_thresh: Optional[float] = None,
  206. text_det_unclip_ratio: Optional[float] = None,
  207. ) -> dict:
  208. """
  209. Get text detection parameters.
  210. If a parameter is None, its default value from the instance will be used.
  211. Args:
  212. text_det_limit_side_len (Optional[int]): The maximum side length of the text box.
  213. text_det_limit_type (Optional[str]): The type of limit to apply to the text box.
  214. text_det_thresh (Optional[float]): The threshold for text detection.
  215. text_det_box_thresh (Optional[float]): The threshold for the bounding box.
  216. text_det_unclip_ratio (Optional[float]): The ratio for unclipping the text box.
  217. Returns:
  218. dict: A dictionary containing the text detection parameters.
  219. """
  220. if text_det_limit_side_len is None:
  221. text_det_limit_side_len = self.text_det_limit_side_len
  222. if text_det_limit_type is None:
  223. text_det_limit_type = self.text_det_limit_type
  224. if text_det_thresh is None:
  225. text_det_thresh = self.text_det_thresh
  226. if text_det_box_thresh is None:
  227. text_det_box_thresh = self.text_det_box_thresh
  228. if text_det_unclip_ratio is None:
  229. text_det_unclip_ratio = self.text_det_unclip_ratio
  230. return dict(
  231. limit_side_len=text_det_limit_side_len,
  232. limit_type=text_det_limit_type,
  233. thresh=text_det_thresh,
  234. box_thresh=text_det_box_thresh,
  235. unclip_ratio=text_det_unclip_ratio,
  236. )
  237. def predict(
  238. self,
  239. input: Union[str, List[str], np.ndarray, List[np.ndarray]],
  240. use_doc_orientation_classify: Optional[bool] = None,
  241. use_doc_unwarping: Optional[bool] = None,
  242. use_textline_orientation: Optional[bool] = None,
  243. text_det_limit_side_len: Optional[int] = None,
  244. text_det_limit_type: Optional[str] = None,
  245. text_det_thresh: Optional[float] = None,
  246. text_det_box_thresh: Optional[float] = None,
  247. text_det_unclip_ratio: Optional[float] = None,
  248. text_rec_score_thresh: Optional[float] = None,
  249. ) -> OCRResult:
  250. """
  251. Predict OCR results based on input images or arrays with optional preprocessing steps.
  252. Args:
  253. input (Union[str, list[str], np.ndarray, list[np.ndarray]]): Input image of pdf path(s) or numpy array(s).
  254. use_doc_orientation_classify (Optional[bool]): Whether to use document orientation classification.
  255. use_doc_unwarping (Optional[bool]): Whether to use document unwarping.
  256. use_textline_orientation (Optional[bool]): Whether to use textline orientation prediction.
  257. text_det_limit_side_len (Optional[int]): Maximum side length for text detection.
  258. text_det_limit_type (Optional[str]): Type of limit to apply for text detection.
  259. text_det_thresh (Optional[float]): Threshold for text detection.
  260. text_det_box_thresh (Optional[float]): Threshold for text detection boxes.
  261. text_det_unclip_ratio (Optional[float]): Ratio for unclipping text detection boxes.
  262. text_rec_score_thresh (Optional[float]): Score threshold for text recognition.
  263. Returns:
  264. OCRResult: Generator yielding OCR results for each input image.
  265. """
  266. model_settings = self.get_model_settings(
  267. use_doc_orientation_classify, use_doc_unwarping, use_textline_orientation
  268. )
  269. if not self.check_model_settings_valid(model_settings):
  270. yield {"error": "the input params for model settings are invalid!"}
  271. text_det_params = self.get_text_det_params(
  272. text_det_limit_side_len,
  273. text_det_limit_type,
  274. text_det_thresh,
  275. text_det_box_thresh,
  276. text_det_unclip_ratio,
  277. )
  278. if text_rec_score_thresh is None:
  279. text_rec_score_thresh = self.text_rec_score_thresh
  280. for _, batch_data in enumerate(self.batch_sampler(input)):
  281. image_arrays = self.img_reader(batch_data.instances)
  282. if model_settings["use_doc_preprocessor"]:
  283. doc_preprocessor_results = list(
  284. self.doc_preprocessor_pipeline(
  285. image_arrays,
  286. use_doc_orientation_classify=use_doc_orientation_classify,
  287. use_doc_unwarping=use_doc_unwarping,
  288. )
  289. )
  290. else:
  291. doc_preprocessor_results = [{"output_img": arr} for arr in image_arrays]
  292. doc_preprocessor_images = [
  293. item["output_img"] for item in doc_preprocessor_results
  294. ]
  295. det_results = list(
  296. self.text_det_model(doc_preprocessor_images, **text_det_params)
  297. )
  298. dt_polys_list = [item["dt_polys"] for item in det_results]
  299. dt_polys_list = [self._sort_boxes(item) for item in dt_polys_list]
  300. results = [
  301. {
  302. "input_path": input_path,
  303. "page_index": page_index,
  304. "doc_preprocessor_res": doc_preprocessor_res,
  305. "dt_polys": dt_polys,
  306. "model_settings": model_settings,
  307. "text_det_params": text_det_params,
  308. "text_type": self.text_type,
  309. "text_rec_score_thresh": text_rec_score_thresh,
  310. "rec_texts": [],
  311. "rec_scores": [],
  312. "rec_polys": [],
  313. }
  314. for input_path, page_index, doc_preprocessor_res, dt_polys in zip(
  315. batch_data.input_paths,
  316. batch_data.page_indexes,
  317. doc_preprocessor_results,
  318. dt_polys_list,
  319. )
  320. ]
  321. indices = list(range(len(doc_preprocessor_images)))
  322. indices = [idx for idx in indices if len(dt_polys_list[idx]) > 0]
  323. if indices:
  324. all_subs_of_imgs = []
  325. chunk_indices = [0]
  326. for idx in indices:
  327. all_subs_of_img = list(
  328. self._crop_by_polys(
  329. doc_preprocessor_images[idx], dt_polys_list[idx]
  330. )
  331. )
  332. all_subs_of_imgs.extend(all_subs_of_img)
  333. chunk_indices.append(chunk_indices[-1] + len(all_subs_of_img))
  334. # use textline orientation model
  335. if model_settings["use_textline_orientation"]:
  336. angles = [
  337. int(textline_angle_info["class_ids"][0])
  338. for textline_angle_info in self.textline_orientation_model(
  339. all_subs_of_imgs
  340. )
  341. ]
  342. all_subs_of_imgs = self.rotate_image(all_subs_of_imgs, angles)
  343. else:
  344. angles = [-1] * len(all_subs_of_imgs)
  345. for i, idx in enumerate(indices):
  346. res = results[idx]
  347. res["textline_orientation_angles"] = angles[
  348. chunk_indices[i] : chunk_indices[i + 1]
  349. ]
  350. # TODO: Process all sub-images in the batch together
  351. for i, idx in enumerate(indices):
  352. all_subs_of_img = all_subs_of_imgs[
  353. chunk_indices[i] : chunk_indices[i + 1]
  354. ]
  355. res = results[idx]
  356. dt_polys = dt_polys_list[idx]
  357. sub_img_info_list = [
  358. {
  359. "sub_img_id": img_id,
  360. "sub_img_ratio": sub_img.shape[1] / float(sub_img.shape[0]),
  361. }
  362. for img_id, sub_img in enumerate(all_subs_of_img)
  363. ]
  364. sorted_subs_info = sorted(
  365. sub_img_info_list, key=lambda x: x["sub_img_ratio"]
  366. )
  367. sorted_subs_of_img = [
  368. all_subs_of_img[x["sub_img_id"]] for x in sorted_subs_info
  369. ]
  370. for i, rec_res in enumerate(
  371. self.text_rec_model(sorted_subs_of_img)
  372. ):
  373. sub_img_id = sorted_subs_info[i]["sub_img_id"]
  374. sub_img_info_list[sub_img_id]["rec_res"] = rec_res
  375. for sno in range(len(sub_img_info_list)):
  376. rec_res = sub_img_info_list[sno]["rec_res"]
  377. if rec_res["rec_score"] >= text_rec_score_thresh:
  378. res["rec_texts"].append(rec_res["rec_text"])
  379. res["rec_scores"].append(rec_res["rec_score"])
  380. res["rec_polys"].append(dt_polys[sno])
  381. for res in results:
  382. if self.text_type == "general":
  383. rec_boxes = convert_points_to_boxes(res["rec_polys"])
  384. res["rec_boxes"] = rec_boxes
  385. else:
  386. res["rec_boxes"] = np.array([])
  387. yield OCRResult(res)
  388. @pipeline_requires_extra("ocr")
  389. class OCRPipeline(AutoParallelImageSimpleInferencePipeline):
  390. entities = "OCR"
  391. @property
  392. def _pipeline_cls(self):
  393. return _OCRPipeline
  394. def _get_batch_size(self, config):
  395. return config.get("batch_size", 1)