result.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 math
  15. import random
  16. import numpy as np
  17. import cv2
  18. import PIL
  19. import os
  20. from PIL import Image, ImageDraw, ImageFont
  21. from ....utils.fonts import PINGFANG_FONT_FILE_PATH
  22. from ..components import CVResult, HtmlMixin, XlsxMixin
  23. class TableRecognitionResult(CVResult, HtmlMixin, XlsxMixin):
  24. def __init__(self, data):
  25. super().__init__(data)
  26. HtmlMixin.__init__(self)
  27. XlsxMixin.__init__(self)
  28. def save_to_html(self, save_path, *args, **kwargs):
  29. if not str(save_path).lower().endswith(".html"):
  30. save_path = save_path + "/res_table_%d.html" % self["table_region_id"]
  31. super().save_to_html(save_path, *args, **kwargs)
  32. def _to_html(self):
  33. return self["pred_html"]
  34. def save_to_xlsx(self, save_path, *args, **kwargs):
  35. if not str(save_path).lower().endswith(".xlsx"):
  36. save_path = save_path + "/res_table_%d.xlsx" % self["table_region_id"]
  37. super().save_to_xlsx(save_path, *args, **kwargs)
  38. def _to_xlsx(self):
  39. return self["pred_html"]
  40. def save_to_img(self, save_path, *args, **kwargs):
  41. if not str(save_path).lower().endswith((".jpg", ".png")):
  42. ocr_save_path = (
  43. save_path + "/res_table_ocr_%d.jpg" % self["table_region_id"]
  44. )
  45. save_path = save_path + "/res_table_cell_%d.jpg" % self["table_region_id"]
  46. self["table_ocr_pred"].save_to_img(ocr_save_path)
  47. super().save_to_img(save_path, *args, **kwargs)
  48. def _to_img(self):
  49. input_img = self["table_ocr_pred"]["input_img"].copy()
  50. cell_box_list = self["cell_box_list"]
  51. for box in cell_box_list:
  52. x1, y1, x2, y2 = [int(pos) for pos in box]
  53. cv2.rectangle(input_img, (x1, y1), (x2, y2), (255, 0, 0), 2)
  54. return input_img
  55. class LayoutParsingResult(dict):
  56. def __init__(self, data):
  57. super().__init__(data)
  58. def save_results(self, save_path):
  59. if not os.path.isdir(save_path):
  60. raise ValueError("The save path should be a dir.")
  61. layout_det_res = self["layout_det_res"]
  62. save_img_path = save_path + "/layout_det_result.jpg"
  63. layout_det_res.save_to_img(save_img_path)
  64. input_params = self["input_params"]
  65. if input_params["use_doc_preprocessor"]:
  66. save_img_path = save_path + "/doc_preprocessor_result.jpg"
  67. self["doc_preprocessor_res"].save_to_img(save_img_path)
  68. if input_params["use_common_ocr"]:
  69. save_img_path = save_path + "/text_paragraphs_ocr_result.jpg"
  70. self["text_paragraphs_ocr_res"].save_to_img(save_img_path)
  71. if input_params["use_table_recognition"]:
  72. for tno in range(len(self["table_res_list"])):
  73. table_res = self["table_res_list"][tno]
  74. table_res.save_to_img(save_path)
  75. table_res.save_to_html(save_path)
  76. table_res.save_to_xlsx(save_path)
  77. if input_params["use_seal_recognition"]:
  78. for sno in range(len(self["seal_res_list"])):
  79. seal_res = self["seal_res_list"][sno]
  80. save_img_path = (
  81. save_path
  82. + "/seal_%d_recognition_result.jpg" % seal_res["seal_region_id"]
  83. )
  84. seal_res.save_to_img(save_img_path)
  85. return