neighbor.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # -*- coding:utf-8 -*-
  2. import numpy as np
  3. import pandas as pd
  4. from copy import deepcopy
  5. from numba import njit
  6. def neighbor_change(tex_box, im_box):
  7. """
  8. 后期处理 临近合并
  9. :param tex_box: 文字区域
  10. :param im_box: 未知区域
  11. :return:
  12. """
  13. # return tex_box, im_box
  14. text_box, image_box, unknow_box, small_text = [], [], [], []
  15. tex_box_df = pd.DataFrame(tex_box)
  16. height = tex_box_df[3] - tex_box_df[1]
  17. mix = height.median()
  18. min_xs = 1.2*mix ** 2
  19. img_w = int(tex_box_df[2].max())
  20. for i in tex_box:
  21. if (i[2] - i[0]) * (i[3] - i[1]) < min_xs and False:
  22. small_text.append(np.array(i))
  23. unknow_box.append(np.array(i))
  24. else:
  25. text_box.append(i)
  26. # 分开大图和小图
  27. for i in im_box:
  28. if (i[2] - i[0]) * (i[3] - i[1]) < min_xs:
  29. unknow_box.append(i)
  30. else:
  31. image_box.append(i)
  32. if len(image_box):
  33. image_box = pd.DataFrame(image_box)
  34. image_box[4] = 1
  35. image_box = image_box.sort_values(by=0).astype(np.int).values
  36. # print(unknow_box)
  37. if len(unknow_box) > 0:
  38. unknow_box = pd.DataFrame(unknow_box)
  39. unknow_box[4] = 2
  40. unknow_box = unknow_box.sort_values(by=0).astype(np.int).values
  41. tex_box = pd.DataFrame(text_box)
  42. tex_box[4] = 1
  43. tex_box = tex_box.sort_values(by=0).astype(np.int).values
  44. # @njit
  45. def _find_right(boxes, i, j, find_range):
  46. for xs in range(int(boxes[i][2]), int(boxes[i][2] + find_range)):
  47. for ys in range(boxes[i][1]+1, boxes[i][3]-1):
  48. if boxes[j][0] < xs < boxes[j][2] and boxes[j][1] < ys < boxes[j][3]:
  49. boxes[i][0] = min(boxes[i][0], boxes[j][0])
  50. boxes[i][1] = min(boxes[i][1], boxes[j][1])
  51. boxes[i][2] = max(boxes[i][2], boxes[j][2])
  52. boxes[i][3] = max(boxes[i][3], boxes[j][3])
  53. boxes[j] = np.array([0, 0, 0, 0, 10])
  54. return xs - boxes[i][2]
  55. # @njit
  56. def __find_in(in_box, out_box, i, j, find_range=0):
  57. # if all(in_box[i] == out_box[j] ):
  58. #
  59. # return
  60. # x_min, y_min, x_max, y_max = 0, 1, 2, 3
  61. # if in_box[i][x_max] <= out_box[j][x_max] and in_box[i][x_min] >= out_box[j][x_min]:
  62. # overlap_w = in_box[i]
  63. # elif in_box[i][x_max] >= out_box[j][x_max] and in_box[i][x_min] >= out_box[j][x_min]:
  64. # overlap_w = in_box[i]
  65. #
  66. #
  67. # if in_box[i][x_max] <= out_box[j][x_max] and \
  68. # in_box[i][y_max] <= out_box[j][y_max] and \
  69. # in_box[i][x_min] > out_box[j][x_min] and \
  70. # in_box[i][y_min] < out_box[j][y_min]+11:
  71. # if find_range==888:
  72. # print('********************')
  73. # print(in_box[i])
  74. # out_box[j][0] = min(out_box[j][0], in_box[i][0])
  75. # out_box[j][1] = min(out_box[j][1], in_box[i][1])
  76. # out_box[j][2] = max(out_box[j][2], in_box[i][2])
  77. # out_box[j][3] = max(out_box[j][3], in_box[i][3])
  78. # in_box[i] = np.array([0, 0, 0, 0, 10])
  79. # return 1
  80. for xs in range(in_box[i][0]-find_range, in_box[i][2]+find_range):
  81. for ys in range(in_box[i][1]-find_range, in_box[i][3]+find_range):
  82. if out_box[j][0] <= xs <= out_box[j][2] and out_box[j][1] <= ys <= out_box[j][3]:
  83. out_box[j][0] = min(out_box[j][0], in_box[i][0])
  84. out_box[j][1] = min(out_box[j][1], in_box[i][1])
  85. out_box[j][2] = max(out_box[j][2], in_box[i][2])
  86. out_box[j][3] = max(out_box[j][3], in_box[i][3])
  87. in_box[i] = np.array([0, 0, 0, 0, 10])
  88. return 1
  89. # @njit
  90. def __find_right(the_boxes, unknow_box, i, j, find_range):
  91. for xs in range(the_boxes[i][2], the_boxes[i][2] + int(find_range)):
  92. for ys in range(the_boxes[i][1], the_boxes[i][3]):
  93. if unknow_box[j][0] < xs < unknow_box[j][2] and unknow_box[j][1] < ys < unknow_box[j][3]:
  94. the_boxes[i][0] = min(the_boxes[i][0], unknow_box[j][0])
  95. the_boxes[i][1] = min(the_boxes[i][1], unknow_box[j][1])
  96. the_boxes[i][2] = max(the_boxes[i][2], unknow_box[j][2])
  97. the_boxes[i][3] = max(the_boxes[i][3], unknow_box[j][3])
  98. unknow_box[j] = np.array([0, 0, 0, 0, 10])
  99. return xs - the_boxes[i][2]
  100. # @njit
  101. def __find_left(the_boxes, unknow_box, i, j, find_range):
  102. for xs in range(the_boxes[i][0], max(int(the_boxes[i][0] - find_range), 0), -1):
  103. for ys in range(the_boxes[i][1], the_boxes[i][3]):
  104. if unknow_box[j][0] < xs < unknow_box[j][2] and unknow_box[j][1] < ys < unknow_box[j][3]:
  105. the_boxes[i][0] = min(the_boxes[i][0], unknow_box[j][0])
  106. the_boxes[i][1] = min(the_boxes[i][1], unknow_box[j][1])
  107. the_boxes[i][2] = max(the_boxes[i][2], unknow_box[j][2])
  108. the_boxes[i][3] = max(the_boxes[i][3], unknow_box[j][3])
  109. unknow_box[j] = np.array([0, 0, 0, 0, 10])
  110. return 1
  111. # @njit
  112. def __find_down(the_boxes, unknow_box, i, j, find_range):
  113. for xs in range(int(unknow_box[j][0]), int(unknow_box[j][2])):
  114. for ys in range(int(unknow_box[j][3]), int(unknow_box[j][3] + find_range)):
  115. if the_boxes[i][0] < xs < the_boxes[i][2] and the_boxes[i][1] < ys < the_boxes[i][3]:
  116. the_boxes[i][0] = min(the_boxes[i][0], unknow_box[j][0])
  117. the_boxes[i][1] = min(the_boxes[i][1], unknow_box[j][1])
  118. the_boxes[i][2] = max(the_boxes[i][2], unknow_box[j][2])
  119. the_boxes[i][3] = max(the_boxes[i][3], unknow_box[j][3])
  120. unknow_box[j] = np.array([0, 0, 0, 0, 10])
  121. return ys - unknow_box[j][3]
  122. # @njit
  123. def __find_top(the_boxes, unknow_box, i, j, find_range):
  124. for xs in range(int(unknow_box[j][0]), int(unknow_box[j][2])):
  125. for ys in range(int(unknow_box[j][1]), int(unknow_box[j][1] - find_range), -1):
  126. if the_boxes[i][0] < xs < the_boxes[i][2] and the_boxes[i][1] < ys < the_boxes[i][3]:
  127. the_boxes[i][0] = min(the_boxes[i][0], unknow_box[j][0])
  128. the_boxes[i][1] = min(the_boxes[i][1], unknow_box[j][1])
  129. the_boxes[i][2] = max(the_boxes[i][2], unknow_box[j][2])
  130. the_boxes[i][3] = max(the_boxes[i][3], unknow_box[j][3])
  131. unknow_box[j] = np.array([0, 0, 0, 0, 10])
  132. return unknow_box[j][3] - ys
  133. # @njit
  134. def _find_down(boxes, y_max, i, j):
  135. for xs in range(boxes[i][0], boxes[i][2]):
  136. for ys in range(boxes[i][3], y_max):
  137. if boxes[j][0] < xs < boxes[j][2] and boxes[j][1] < ys < boxes[j][2]:
  138. boxes[i][0] = min(boxes[i][0], boxes[j][0])
  139. boxes[i][1] = min(boxes[i][1], boxes[j][1])
  140. boxes[i][2] = max(boxes[i][2], boxes[j][2])
  141. boxes[i][3] = max(boxes[i][3], boxes[j][3])
  142. return 1
  143. # 小图片向上粘贴
  144. for i in range(len(image_box)):
  145. for j in range(len(unknow_box)):
  146. __find_top(image_box, unknow_box, i, j, mix//2)
  147. # 小图片向下粘贴
  148. for i in range(len(image_box)):
  149. for j in range(len(unknow_box)):
  150. __find_down(image_box, unknow_box, i, j, mix//2)
  151. # 小图片向左粘贴
  152. for i in range(len(image_box)):
  153. for j in range(len(unknow_box)):
  154. __find_left(image_box, unknow_box, i, j, mix//2 )
  155. # 小图片向右粘贴
  156. for i in range(len(image_box)):
  157. for j in range(len(unknow_box)):
  158. __find_right(image_box, unknow_box, i, j, mix//2)
  159. # 消除在imagebox里的textbox
  160. # for i in range(len(tex_box)):
  161. # for j in range(len(image_box)):
  162. # __find_in(tex_box, image_box, i, j)
  163. for i in range(len(small_text)):
  164. for j in range(len(image_box)):
  165. if not all(small_text[i] == image_box[j]):
  166. __find_in(small_text, image_box, i, j)
  167. # textbox向右寻找
  168. for i in range(len(tex_box)):
  169. for j in range(i + 1, len(tex_box)):
  170. _find_right(tex_box, i, j, mix)
  171. text_box = []
  172. for i in tex_box:
  173. if 1 < (i[2] - i[0]) * (i[3] - i[1]) < min_xs :
  174. small_text.append(np.array(i))
  175. else:
  176. text_box.append(i)
  177. tex_box = pd.DataFrame(text_box)
  178. tex_box[4] = 1
  179. tex_box = tex_box.sort_values(by=0).astype(np.int).values
  180. if len(small_text) > 0:
  181. small_text = pd.DataFrame(small_text)
  182. small_text[4] = 3
  183. small_text = small_text.sort_values(by=0).astype(np.int).values
  184. # textbox向左合并小图
  185. for i in range(len(tex_box)):
  186. if (tex_box[i][2] - tex_box[i][0]) * (tex_box[i][3] - tex_box[i][1]) > 5 * min_xs:
  187. for j in range(len(unknow_box)):
  188. __find_left(tex_box, unknow_box, i, j, mix)
  189. for i in range(len(tex_box)):
  190. if (tex_box[i][2] - tex_box[i][0]) * (tex_box[i][3] - tex_box[i][1]) > 5 * min_xs:
  191. for j in range(len(small_text)):
  192. __find_left(tex_box, small_text, i, j, 5 * mix)
  193. # textbox向下寻找
  194. for i in range(len(tex_box)):
  195. for j in range(len(unknow_box)):
  196. __find_down(tex_box, unknow_box, i, j, mix // 3)
  197. for i in range(len(tex_box)):
  198. for j in range(len(small_text)):
  199. __find_down(tex_box, small_text, i, j, mix // 3)
  200. # textbox向上寻找
  201. for i in range(len(tex_box)):
  202. for j in range(len(unknow_box)):
  203. __find_top(tex_box, unknow_box, i, j, mix // 3)
  204. for i in range(len(tex_box)):
  205. for j in range(len(small_text)):
  206. __find_top(tex_box, small_text, i, j, mix // 3)
  207. # image_box_bk = deepcopy(image_box)
  208. # 消除内部imagebox
  209. for i in range(len(image_box)):
  210. for j in range(i+1,len(image_box)):
  211. if not all(image_box[i] == image_box[j]):
  212. __find_in(image_box, image_box, i, j, 0)
  213. # 消除内部textbox
  214. for i in range(len(tex_box)):
  215. for j in range(i+1,len(tex_box)):
  216. if not all(tex_box[i] == tex_box[j]):
  217. __find_in(tex_box, tex_box, i, j, -int(mix//5))
  218. # text_box_p = [i[:4] for i in tex_box]# + [i[:4] for i in small_text]
  219. # image_box_p = [i[:4] for i in image_box]
  220. #
  221. # return text_box_p, image_box_p
  222. #
  223. # for i in range(len(tex_box)):
  224. # for j in range(i+1,len(tex_box)):
  225. # __find_in(tex_box, tex_box, i, j, -int(mix//10))
  226. for i in range(len(small_text)):
  227. for j in range(len(tex_box)):
  228. if not all(small_text[i] == tex_box[j]):
  229. __find_in(small_text, tex_box, i, j, 0)
  230. for i in range(len(tex_box)):
  231. for j in range(len(image_box)):
  232. if not all(tex_box[i] == image_box[j]):
  233. __find_in(tex_box, image_box, i, j, 0)
  234. for i in range(len(small_text)):
  235. for j in range(len(image_box)):
  236. if not all(small_text[i] == image_box[j]):
  237. __find_in(small_text, image_box, i, j, 0)
  238. text_box_p = [i[:4] for i in tex_box if np.sum(i[:4])] + [i[:4] for i in small_text if np.sum(i[:4])]
  239. image_box_p = [i[:4] for i in image_box if np.sum(i[:4])]
  240. return text_box_p, image_box_p