convert_tree.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import io
  2. import logging
  3. import cv2
  4. from PIL import Image
  5. import numpy as np
  6. from format_convert.convert_image import image_process
  7. from format_convert.utils import add_div, judge_error_code, get_table_html, sort_object
  8. class _Document:
  9. def __init__(self, doc_path):
  10. self.doc_path = doc_path
  11. # Document's child -> Page
  12. self.children = []
  13. self.error_code = None
  14. def add_child(self, child):
  15. if child.error_code is None:
  16. self.children.append(child)
  17. else:
  18. self.error_code = child.error_code
  19. def get_html(self, return_list=False):
  20. if self.error_code is not None:
  21. return self.error_code
  22. if return_list:
  23. html_text = []
  24. else:
  25. html_text = ""
  26. for child in self.children:
  27. # 先调用get_html才能更新error_code
  28. child_html_text = child.get_html()
  29. if child.error_code is not None:
  30. self.error_code = child.error_code
  31. return self.error_code
  32. else:
  33. if return_list:
  34. html_text += [child_html_text]
  35. else:
  36. html_text += child_html_text
  37. if not return_list:
  38. html_text = [html_text]
  39. return html_text
  40. class _Page:
  41. def __init__(self, page, page_no):
  42. self.page = page
  43. self.page_no = page_no
  44. # Page's child -> Image, Table, Sentence
  45. self.children = []
  46. self.error_code = None
  47. # pdf对象需反向排序
  48. self.is_reverse = False
  49. # objs in tables
  50. self.in_table_objs = set()
  51. def add_child(self, child):
  52. if child.error_code is None:
  53. self.children.append(child)
  54. else:
  55. self.error_code = child.error_code
  56. def get_html(self):
  57. if self.error_code is not None:
  58. return ""
  59. html_text = ""
  60. self.children = sort_object(self.children, self.is_reverse)
  61. for child in self.children:
  62. # 先调用get_html才能更新error_code
  63. child_html_text = child.get_html()
  64. if child.error_code is not None:
  65. self.error_code = child.error_code
  66. return ""
  67. else:
  68. html_text += child_html_text
  69. return html_text
  70. class _Image:
  71. def __init__(self, content, path, bbox=(0, 0, 0, 0)):
  72. self.content = content
  73. self.path = path
  74. # 来源
  75. self.is_from_pdf = False
  76. self.is_from_docx = False
  77. # 位置
  78. self.bbox = bbox
  79. self.x = bbox[0]
  80. self.y = bbox[1]
  81. # 识别结果
  82. self.otr_result = None
  83. self.ocr_result = None
  84. # Image's child -> Table, Sentence
  85. self.children = []
  86. self.error_code = None
  87. # objs in tables
  88. self.in_table_objs = set()
  89. # 是否是文本形成的无边框表格
  90. self.b_table_from_text = False
  91. # pdf读取的文本对象
  92. self.b_table_text_obj_list = []
  93. # pdf layout的尺寸
  94. self.b_table_layout_size = (0, 0)
  95. def add_child(self, child):
  96. if child.error_code is None:
  97. self.children.append(child)
  98. else:
  99. self.error_code = child.error_code
  100. def get_html(self):
  101. # 将Image转为Sentence,table
  102. self.convert()
  103. if self.error_code is not None:
  104. return ""
  105. html_text = ""
  106. self.children = sort_object(self.children)
  107. for child in self.children:
  108. # 先调用get_html才能更新error_code
  109. child_html_text = child.get_html()
  110. if child.error_code is not None:
  111. self.error_code = child.error_code
  112. return ""
  113. else:
  114. html_text += child_html_text
  115. return html_text
  116. def get_text(self):
  117. return
  118. def convert(self):
  119. image_np = cv2.imread(self.path)
  120. obj_list = image_process(image_np, self.path, self.is_from_pdf, self.is_from_docx,
  121. self.b_table_from_text, self.b_table_text_obj_list,
  122. self.b_table_layout_size)
  123. if self.b_table_from_text:
  124. temp_list = []
  125. for obj in obj_list:
  126. if isinstance(obj, _Table):
  127. temp_list.append(obj)
  128. obj_list = temp_list
  129. if judge_error_code(obj_list):
  130. self.error_code = obj_list
  131. return
  132. for obj in obj_list:
  133. self.add_child(obj)
  134. class _Table:
  135. def __init__(self, content, bbox, is_html=False):
  136. self.content = content
  137. self.is_html = is_html
  138. self.bbox = bbox
  139. self.x = bbox[0]
  140. self.y = bbox[1]
  141. self.shape = (len(content), len(content[0]))
  142. self.error_code = None
  143. def get_html(self):
  144. if self.error_code is not None:
  145. return ""
  146. if self.is_html:
  147. return self.content
  148. else:
  149. # 将二维数组转为html table
  150. html_text = get_table_html(self.content)
  151. return html_text
  152. class _Sentence:
  153. def __init__(self, content, bbox, is_html=False):
  154. self.content = content
  155. self.is_html = is_html
  156. # 位置
  157. self.bbox = bbox
  158. self.x = bbox[0]
  159. self.y = bbox[1]
  160. self.error_code = None
  161. # 合并接近句子
  162. self.combine = True
  163. def get_html(self):
  164. if self.error_code is not None:
  165. return ""
  166. # print("_Sentence", self.content, self.bbox)
  167. if self.is_html:
  168. return self.content
  169. else:
  170. return add_div(self.content)
  171. class TextBox:
  172. def __init__(self, bbox, text):
  173. self.bbox = bbox
  174. self.text = text
  175. def get_text(self):
  176. return self.text
  177. def __str__(self):
  178. return '(%s@#@%s)' % (str(self.text), '@'.join([str(x) for x in self.bbox]))
  179. class TableLine:
  180. def __init__(self, bbox):
  181. self.bbox = bbox