convert_tree.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. def add_child(self, child):
  90. if child.error_code is None:
  91. self.children.append(child)
  92. else:
  93. self.error_code = child.error_code
  94. def get_html(self):
  95. # 将Image转为Sentence,table
  96. self.convert()
  97. if self.error_code is not None:
  98. return ""
  99. html_text = ""
  100. self.children = sort_object(self.children)
  101. for child in self.children:
  102. # 先调用get_html才能更新error_code
  103. child_html_text = child.get_html()
  104. if child.error_code is not None:
  105. self.error_code = child.error_code
  106. return ""
  107. else:
  108. html_text += child_html_text
  109. return html_text
  110. def get_text(self):
  111. return
  112. def convert(self):
  113. # 二进制转numpy
  114. # image_np = Image.open(io.BytesIO(self.content))
  115. # image_np = cv2.cvtColor(np.asarray(image_np), cv2.COLOR_RGB2BGR)
  116. image_np = cv2.imread(self.path)
  117. obj_list = image_process(image_np, self.path, self.is_from_pdf, self.is_from_docx, use_ocr=True)
  118. if judge_error_code(obj_list):
  119. self.error_code = obj_list
  120. return
  121. for obj in obj_list:
  122. self.add_child(obj)
  123. class _Table:
  124. def __init__(self, content, bbox, is_html=False):
  125. self.content = content
  126. self.is_html = is_html
  127. self.bbox = bbox
  128. self.x = bbox[0]
  129. self.y = bbox[1]
  130. self.shape = (len(content), len(content[0]))
  131. self.error_code = None
  132. def get_html(self):
  133. if self.error_code is not None:
  134. return ""
  135. if self.is_html:
  136. return self.content
  137. else:
  138. # 将二维数组转为html table
  139. html_text = get_table_html(self.content)
  140. return html_text
  141. class _Sentence:
  142. def __init__(self, content, bbox, is_html=False):
  143. self.content = content
  144. self.is_html = is_html
  145. # 位置
  146. self.bbox = bbox
  147. self.x = bbox[0]
  148. self.y = bbox[1]
  149. self.error_code = None
  150. # 合并接近句子
  151. self.combine = True
  152. def get_html(self):
  153. if self.error_code is not None:
  154. return ""
  155. # print("_Sentence", self.content, self.bbox)
  156. if self.is_html:
  157. return self.content
  158. else:
  159. return add_div(self.content)
  160. class TextBox:
  161. def __init__(self, bbox, text):
  162. self.bbox = bbox
  163. self.text = text
  164. def get_text(self):
  165. return self.text
  166. class TableLine:
  167. def __init__(self, bbox):
  168. self.bbox = bbox