convert_tree.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 == [-16]:
  104. self.error_code = None
  105. return "<div>#idc error#<div>"
  106. if self.error_code is not None:
  107. return ""
  108. html_text = ""
  109. self.children = sort_object(self.children)
  110. for child in self.children:
  111. # 先调用get_html才能更新error_code
  112. child_html_text = child.get_html()
  113. if child.error_code is not None:
  114. self.error_code = child.error_code
  115. return ""
  116. else:
  117. html_text += child_html_text
  118. return html_text
  119. def get_text(self):
  120. return
  121. def convert(self):
  122. image_np = cv2.imread(self.path)
  123. obj_list = image_process(image_np, self.path, self.is_from_pdf, self.is_from_docx,
  124. self.b_table_from_text, self.b_table_text_obj_list,
  125. self.b_table_layout_size)
  126. if judge_error_code(obj_list):
  127. self.error_code = obj_list
  128. return
  129. if self.b_table_from_text:
  130. temp_list = []
  131. for obj in obj_list:
  132. if isinstance(obj, _Table):
  133. temp_list.append(obj)
  134. obj_list = temp_list
  135. for obj in obj_list:
  136. self.add_child(obj)
  137. class _Table:
  138. def __init__(self, content, bbox, is_html=False):
  139. self.content = content
  140. self.is_html = is_html
  141. self.bbox = bbox
  142. self.x = bbox[0]
  143. self.y = bbox[1]
  144. self.shape = (len(content), len(content[0]))
  145. self.error_code = None
  146. def get_html(self):
  147. if self.error_code is not None:
  148. return ""
  149. if self.is_html:
  150. return self.content
  151. else:
  152. # 将二维数组转为html table
  153. html_text = get_table_html(self.content)
  154. return html_text
  155. class _Sentence:
  156. def __init__(self, content, bbox, is_html=False):
  157. self.content = content
  158. self.is_html = is_html
  159. # 位置
  160. self.bbox = bbox
  161. self.x = bbox[0]
  162. self.y = bbox[1]
  163. self.error_code = None
  164. # 合并接近句子
  165. self.combine = True
  166. def get_html(self):
  167. if self.error_code is not None:
  168. return ""
  169. # print("_Sentence", self.content, self.bbox)
  170. if self.is_html:
  171. return self.content
  172. else:
  173. return add_div(self.content)
  174. class TextBox:
  175. def __init__(self, bbox, text):
  176. self.bbox = bbox
  177. self.text = text
  178. def get_text(self):
  179. return self.text
  180. def __str__(self):
  181. return '(%s@#@%s)' % (str(self.text), '@'.join([str(x) for x in self.bbox]))
  182. class TableLine:
  183. def __init__(self, bbox):
  184. self.bbox = bbox