table_line_new.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. import copy
  2. import time
  3. import traceback
  4. import numpy as np
  5. import cv2
  6. import matplotlib.pyplot as plt
  7. from format_convert.utils import log, pil_resize
  8. def table_line(img, model, size=(512, 1024), prob=0.2, is_test=0):
  9. log("into table_line, prob is " + str(prob))
  10. # resize
  11. w, h = size
  12. img_new = pil_resize(img, h, w)
  13. img_show = copy.deepcopy(img_new)
  14. # predict
  15. start_time = time.time()
  16. pred = model.predict(np.array([img_new]))
  17. pred = pred[0]
  18. log("otr model predict time " + str(time.time() - start_time))
  19. # show
  20. show(pred, title='pred', prob=prob, mode=1, is_test=is_test)
  21. # 根据点获取线
  22. start_time = time.time()
  23. line_list = points2lines(pred, False, prob=prob)
  24. log("points2lines " + str(time.time() - start_time))
  25. if not line_list:
  26. return []
  27. show(line_list, title="points2lines", mode=2, is_test=is_test)
  28. # 清除短线
  29. start_time = time.time()
  30. line_list = delete_short_lines(line_list, img_new.shape)
  31. show(line_list, title="delete_short_lines", mode=2, is_test=is_test)
  32. log("delete_short_lines " + str(time.time() - start_time))
  33. # 分成横竖线
  34. start_time = time.time()
  35. row_line_list = []
  36. col_line_list = []
  37. for line in line_list:
  38. if line[0] == line[2]:
  39. col_line_list.append(line)
  40. elif line[1] == line[3]:
  41. row_line_list.append(line)
  42. log("divide rows and cols " + str(time.time() - start_time))
  43. # 两种线都需要存在,否则跳过
  44. if not row_line_list or not col_line_list:
  45. return []
  46. # 合并错开线
  47. start_time = time.time()
  48. row_line_list = merge_line(row_line_list, axis=0)
  49. col_line_list = merge_line(col_line_list, axis=1)
  50. show(row_line_list + col_line_list, title="merge_line", mode=2, is_test=is_test)
  51. log("merge_line " + str(time.time() - start_time))
  52. # 计算交点
  53. cross_points = get_points(row_line_list, col_line_list, (img_new.shape[0], img_new.shape[1]))
  54. if not cross_points:
  55. return []
  56. # 删除无交点线 需重复两次才删的干净
  57. row_line_list, col_line_list = delete_single_lines(row_line_list, col_line_list, cross_points)
  58. cross_points = get_points(row_line_list, col_line_list, (img_new.shape[0], img_new.shape[1]))
  59. row_line_list, col_line_list = delete_single_lines(row_line_list, col_line_list, cross_points)
  60. if not row_line_list or not col_line_list:
  61. return []
  62. # 多个表格分割线,获取多个表格区域
  63. start_time = time.time()
  64. split_lines, split_y = get_split_line(cross_points, col_line_list, img_new)
  65. area_row_line_list, area_col_line_list, area_point_list = get_split_area(split_y, row_line_list, col_line_list, cross_points)
  66. log("get_split_area " + str(time.time() - start_time))
  67. # 根据区域循环
  68. need_split_flag = False
  69. for i in range(len(area_point_list)):
  70. sub_row_line_list = area_row_line_list[i]
  71. sub_col_line_list = area_col_line_list[i]
  72. sub_point_list = area_point_list[i]
  73. # 修复边框
  74. start_time = time.time()
  75. new_rows, new_cols, long_rows, long_cols = fix_outline(img_new,
  76. sub_row_line_list,
  77. sub_col_line_list,
  78. sub_point_list)
  79. # 如有补线
  80. if new_rows or new_cols:
  81. # 连接至补线的延长线
  82. if long_rows:
  83. sub_row_line_list = long_rows
  84. if long_cols:
  85. sub_col_line_list = long_cols
  86. # 新的补线
  87. if new_rows:
  88. sub_row_line_list += new_rows
  89. if new_cols:
  90. sub_col_line_list += new_cols
  91. need_split_flag = True
  92. area_row_line_list[i] = sub_row_line_list
  93. area_col_line_list[i] = sub_col_line_list
  94. row_line_list = [y for x in area_row_line_list for y in x]
  95. col_line_list = [y for x in area_col_line_list for y in x]
  96. if need_split_flag:
  97. # 修复边框后重新计算交点
  98. cross_points = get_points(row_line_list, col_line_list, (img_new.shape[0], img_new.shape[1]))
  99. split_lines, split_y = get_split_line(cross_points, col_line_list, img_new)
  100. area_row_line_list, area_col_line_list, area_point_list = get_split_area(split_y, row_line_list, col_line_list, cross_points)
  101. show(cross_points, title="get_points", img=img_show, mode=4, is_test=is_test)
  102. show(split_lines, title="split_lines", img=img_show, mode=3, is_test=is_test)
  103. show(row_line_list + col_line_list, title="fix_outline", mode=2, is_test=is_test)
  104. log("fix_outline " + str(time.time() - start_time))
  105. # 根据区域循环
  106. for i in range(len(area_point_list)):
  107. sub_row_line_list = area_row_line_list[i]
  108. sub_col_line_list = area_col_line_list[i]
  109. sub_point_list = area_point_list[i]
  110. # 验证轮廓的4个交点
  111. sub_row_line_list, sub_col_line_list = fix_4_points(sub_point_list, sub_row_line_list, sub_col_line_list)
  112. # 把四个边线在加一次
  113. sub_point_list = get_points(sub_row_line_list, sub_col_line_list, (img_new.shape[0], img_new.shape[1]))
  114. sub_row_line_list, sub_col_line_list = add_outline(sub_point_list, sub_row_line_list, sub_col_line_list)
  115. # 修复内部缺线
  116. start_time = time.time()
  117. sub_row_line_list, sub_col_line_list = fix_inner(sub_row_line_list, sub_col_line_list, sub_point_list)
  118. log("fix_inner " + str(time.time() - start_time))
  119. show(sub_row_line_list + sub_col_line_list, title="fix_inner1", mode=2, is_test=is_test)
  120. # 合并错开
  121. start_time = time.time()
  122. sub_row_line_list = merge_line(sub_row_line_list, axis=0)
  123. sub_col_line_list = merge_line(sub_col_line_list, axis=1)
  124. log("merge_line " + str(time.time() - start_time))
  125. show(sub_row_line_list + sub_col_line_list, title="merge_line", mode=2, is_test=is_test)
  126. # 修复内部线后重新计算交点
  127. start_time = time.time()
  128. cross_points = get_points(sub_row_line_list, sub_col_line_list, (img_new.shape[0], img_new.shape[1]))
  129. show(cross_points, title="get_points3", img=img_show, mode=4, is_test=is_test)
  130. # 消除线突出,获取标准的线
  131. area_row_line_list[i], area_col_line_list[i] = get_standard_lines(sub_row_line_list, sub_col_line_list)
  132. show(area_row_line_list[i] + area_col_line_list[i], title="get_standard_lines", mode=2, is_test=is_test)
  133. row_line_list = [y for x in area_row_line_list for y in x]
  134. col_line_list = [y for x in area_col_line_list for y in x]
  135. line_list = row_line_list + col_line_list
  136. # 打印处理后线
  137. show(line_list, title="all", img=img_show, mode=5, is_test=is_test)
  138. log("otr postprocess table_line " + str(time.time() - start_time))
  139. return line_list
  140. def table_line_pdf_post_process(line_list, page_w, page_h, is_test=0):
  141. for i, line in enumerate(line_list):
  142. line_list[i] = [int(x) for x in line]
  143. img_new = np.full([int(page_h+1), int(page_w+1), 3], 255, dtype=np.uint8)
  144. img_show = copy.deepcopy(img_new)
  145. show(line_list, title="table_line_pdf start", mode=2, is_test=is_test)
  146. # 分成横竖线
  147. start_time = time.time()
  148. row_line_list = []
  149. col_line_list = []
  150. for line in line_list:
  151. # 可能有斜线
  152. if line[0] == line[2]:
  153. col_line_list.append(line)
  154. elif line[1] == line[3]:
  155. row_line_list.append(line)
  156. else:
  157. if is_test:
  158. print(line)
  159. # log("pdf divide rows and cols " + str(time.time() - start_time))
  160. show(row_line_list + col_line_list, title="divide", mode=2, is_test=is_test)
  161. # 两种线都需要存在,否则跳过
  162. if not row_line_list or not col_line_list:
  163. return []
  164. # 合并线
  165. row_line_list = merge_line(row_line_list, axis=0)
  166. col_line_list = merge_line(col_line_list, axis=1)
  167. show(row_line_list + col_line_list, title="merge", mode=2, is_test=is_test)
  168. # 计算交点
  169. # print('img_new.shape', img_new.shape)
  170. cross_points = get_points(row_line_list, col_line_list, (img_new.shape[0], img_new.shape[1]))
  171. if not cross_points:
  172. return []
  173. show(cross_points, title="get_points", img=img_show, mode=4, is_test=is_test)
  174. # 多个表格分割线,获取多个表格区域
  175. start_time = time.time()
  176. split_lines, split_y = get_split_line(cross_points, col_line_list, img_new)
  177. area_row_line_list, area_col_line_list, area_point_list = get_split_area(split_y, row_line_list, col_line_list, cross_points)
  178. log("pdf get_split_area " + str(time.time() - start_time))
  179. show(split_lines, title="split_lines", img=img_show, mode=3, is_test=is_test)
  180. # 根据区域循环
  181. need_split_flag = False
  182. for i in range(len(area_point_list)):
  183. sub_row_line_list = area_row_line_list[i]
  184. sub_col_line_list = area_col_line_list[i]
  185. sub_point_list = area_point_list[i]
  186. # 修复边框
  187. start_time = time.time()
  188. new_rows, new_cols, long_rows, long_cols = fix_outline(img_new,
  189. sub_row_line_list,
  190. sub_col_line_list,
  191. sub_point_list)
  192. # 如有补线
  193. if new_rows or new_cols:
  194. # 连接至补线的延长线
  195. if long_rows:
  196. sub_row_line_list = long_rows
  197. if long_cols:
  198. sub_col_line_list = long_cols
  199. # 新的补线
  200. if new_rows:
  201. sub_row_line_list += new_rows
  202. if new_cols:
  203. sub_col_line_list += new_cols
  204. need_split_flag = True
  205. area_row_line_list[i] = sub_row_line_list
  206. area_col_line_list[i] = sub_col_line_list
  207. row_line_list = [y for x in area_row_line_list for y in x]
  208. col_line_list = [y for x in area_col_line_list for y in x]
  209. if need_split_flag:
  210. # 修复边框后重新计算交点
  211. cross_points = get_points(row_line_list, col_line_list, (img_new.shape[0], img_new.shape[1]))
  212. split_lines, split_y = get_split_line(cross_points, col_line_list, img_new)
  213. area_row_line_list, area_col_line_list, area_point_list = get_split_area(split_y, row_line_list, col_line_list, cross_points)
  214. # log("pdf fix_outline " + str(time.time() - start_time))
  215. # 根据区域循环
  216. for i in range(len(area_point_list)):
  217. sub_row_line_list = area_row_line_list[i]
  218. sub_col_line_list = area_col_line_list[i]
  219. sub_point_list = area_point_list[i]
  220. # 验证轮廓的4个交点
  221. sub_row_line_list, sub_col_line_list = fix_4_points(sub_point_list, sub_row_line_list, sub_col_line_list)
  222. # 把四个边线在加一次
  223. sub_point_list = get_points(sub_row_line_list, sub_col_line_list, (img_new.shape[0], img_new.shape[1]))
  224. sub_row_line_list, sub_col_line_list = add_outline(sub_point_list, sub_row_line_list, sub_col_line_list)
  225. # 修复内部缺线
  226. start_time = time.time()
  227. sub_row_line_list, sub_col_line_list = fix_inner(sub_row_line_list, sub_col_line_list, sub_point_list)
  228. # log("pdf fix_inner " + str(time.time() - start_time))
  229. show(sub_row_line_list + sub_col_line_list, title="fix_inner1", mode=2, is_test=is_test)
  230. # 修复内部线后重新计算交点
  231. start_time = time.time()
  232. cross_points = get_points(sub_row_line_list, sub_col_line_list, (img_new.shape[0], img_new.shape[1]))
  233. show(cross_points, title="get_points3", img=img_show, mode=4, is_test=is_test)
  234. area_point_list[i] = cross_points
  235. # 合并线
  236. area_row_line_list[i] = merge_line(sub_row_line_list, axis=0)
  237. area_col_line_list[i] = merge_line(sub_col_line_list, axis=1)
  238. row_line_list = [y for x in area_row_line_list for y in x]
  239. col_line_list = [y for x in area_col_line_list for y in x]
  240. line_list = row_line_list + col_line_list
  241. # 打印处理后线
  242. show(line_list, title="all", img=img_show, mode=5, is_test=is_test)
  243. # log("table_line_pdf cost: " + str(time.time() - start_time))
  244. return line_list
  245. def show(pred_or_lines, title='', prob=0.2, img=None, mode=1, is_test=0):
  246. if not is_test:
  247. return
  248. if mode == 1:
  249. plt.figure()
  250. plt.title(title)
  251. _array = []
  252. for _h in range(len(pred_or_lines)):
  253. _line = []
  254. for _w in range(len(pred_or_lines[_h])):
  255. _prob = pred_or_lines[_h][_w]
  256. if _prob[0] > prob:
  257. _line.append((0, 0, 255))
  258. elif _prob[1] > prob:
  259. _line.append((255, 0, 0))
  260. else:
  261. _line.append((255, 255, 255))
  262. _array.append(_line)
  263. # plt.axis('off')
  264. plt.imshow(np.array(_array))
  265. plt.show()
  266. elif mode == 2:
  267. plt.figure()
  268. plt.title(title)
  269. for _line in pred_or_lines:
  270. x0, y0, x1, y1 = _line
  271. plt.plot([x0, x1], [y0, y1])
  272. plt.show()
  273. elif mode == 3:
  274. for _line in pred_or_lines:
  275. x0, y0 = _line[0]
  276. x1, y1 = _line[1]
  277. cv2.line(img, [int(x0), int(y0)], [int(x1), int(y1)], (0, 0, 255), 2)
  278. cv2.namedWindow(title, cv2.WINDOW_NORMAL)
  279. cv2.imshow(title, img)
  280. cv2.waitKey(0)
  281. elif mode == 4:
  282. for point in pred_or_lines:
  283. point = [int(x) for x in point]
  284. cv2.circle(img, (point[0], point[1]), 1, (0, 255, 0), 2)
  285. cv2.namedWindow(title, cv2.WINDOW_NORMAL)
  286. cv2.imshow(title, img)
  287. cv2.waitKey(0)
  288. elif mode == 5:
  289. for _line in pred_or_lines:
  290. x0, y0, x1, y1 = _line
  291. cv2.line(img, [int(x0), int(y0)], [int(x1), int(y1)], (0, 255, 0), 2)
  292. cv2.namedWindow(title, cv2.WINDOW_NORMAL)
  293. cv2.imshow(title, img)
  294. cv2.waitKey(0)
  295. def points2lines(pred, sourceP_LB=True, prob=0.2, line_width=8, padding=3, min_len=10,
  296. cell_width=13):
  297. _time = time.time()
  298. log("starting points2lines")
  299. height = len(pred)
  300. width = len(pred[0])
  301. _sum = list(np.sum(np.array((pred[..., 0] > prob)).astype(int), axis=1))
  302. h_index = -1
  303. h_lines = []
  304. v_lines = []
  305. _step = line_width
  306. while 1:
  307. h_index += 1
  308. if h_index >= height:
  309. break
  310. w_index = -1
  311. if sourceP_LB:
  312. h_i = height - 1 - h_index
  313. else:
  314. h_i = h_index
  315. _start = None
  316. if _sum[h_index] < min_len:
  317. continue
  318. last_back = 0
  319. while 1:
  320. if w_index >= width:
  321. if _start is not None:
  322. _end = w_index - 1
  323. _bbox = [_start, h_i, _end, h_i]
  324. _dict = {"bbox": _bbox}
  325. h_lines.append(_dict)
  326. _start = None
  327. break
  328. _h, _v = pred[h_i][w_index]
  329. if _h > prob:
  330. if _start is None:
  331. _start = w_index
  332. w_index += _step
  333. else:
  334. if _start is not None:
  335. _end = w_index - 1
  336. _bbox = [_start, h_i, _end, h_i]
  337. _dict = {"bbox": _bbox}
  338. h_lines.append(_dict)
  339. _start = None
  340. w_index -= _step // 2
  341. if w_index <= last_back:
  342. w_index = last_back + _step // 2
  343. last_back = w_index
  344. log("starting points2lines 1")
  345. w_index = -1
  346. _sum = list(np.sum(np.array((pred[..., 1] > prob)).astype(int), axis=0))
  347. _step = line_width
  348. while 1:
  349. w_index += 1
  350. if w_index >= width:
  351. break
  352. if _sum[w_index] < min_len:
  353. continue
  354. h_index = -1
  355. _start = None
  356. last_back = 0
  357. list_test = []
  358. list_lineprob = []
  359. while 1:
  360. if h_index >= height:
  361. if _start is not None:
  362. _end = last_h
  363. _bbox = [w_index, _start, w_index, _end]
  364. _dict = {"bbox": _bbox}
  365. v_lines.append(_dict)
  366. _start = None
  367. list_test.append(_dict)
  368. break
  369. if sourceP_LB:
  370. h_i = height - 1 - h_index
  371. else:
  372. h_i = h_index
  373. _h, _v = pred[h_index][w_index]
  374. list_lineprob.append((h_index, _v))
  375. if _v > prob:
  376. if _start is None:
  377. _start = h_i
  378. h_index += _step
  379. else:
  380. if _start is not None:
  381. _end = last_h
  382. _bbox = [w_index, _start, w_index, _end]
  383. _dict = {"bbox": _bbox}
  384. v_lines.append(_dict)
  385. _start = None
  386. list_test.append(_dict)
  387. h_index -= _step // 2
  388. if h_index <= last_back:
  389. h_index = last_back + _step // 2
  390. last_back = h_index
  391. last_h = h_i
  392. log("starting points2lines 2")
  393. for _line in h_lines:
  394. _bbox = _line["bbox"]
  395. _bbox = [max(_bbox[0] - 2, 0), (_bbox[1] + _bbox[3]) / 2, _bbox[2] + 2, (_bbox[1] + _bbox[3]) / 2]
  396. _line["bbox"] = _bbox
  397. for _line in v_lines:
  398. _bbox = _line["bbox"]
  399. _bbox = [(_bbox[0] + _bbox[2]) / 2, max(_bbox[1] - 2, 0), (_bbox[0] + _bbox[2]) / 2, _bbox[3] + 2]
  400. _line["bbox"] = _bbox
  401. h_lines = lines_cluster(h_lines, line_width=line_width)
  402. v_lines = lines_cluster(v_lines, line_width=line_width)
  403. list_line = []
  404. for _line in h_lines:
  405. _bbox = _line["bbox"]
  406. _bbox = [max(_bbox[0] - 1, 0), (_bbox[1] + _bbox[3]) / 2, _bbox[2] + 1, (_bbox[1] + _bbox[3]) / 2]
  407. list_line.append(_bbox)
  408. for _line in v_lines:
  409. _bbox = _line["bbox"]
  410. _bbox = [(_bbox[0] + _bbox[2]) / 2, max(_bbox[1] - 1, 0), (_bbox[0] + _bbox[2]) / 2, _bbox[3] + 1]
  411. list_line.append(_bbox)
  412. log("points2lines cost %.2fs" % (time.time() - _time))
  413. # import matplotlib.pyplot as plt
  414. # plt.figure()
  415. # for _line in list_line:
  416. # x0,y0,x1,y1 = _line
  417. # plt.plot([x0,x1],[y0,y1])
  418. # for _line in list_line:
  419. # x0,y0,x1,y1 = _line.bbox
  420. # plt.plot([x0,x1],[y0,y1])
  421. # for point in list_crosspoints:
  422. # plt.scatter(point.get("point")[0],point.get("point")[1])
  423. # plt.show()
  424. return list_line
  425. def lines_cluster(list_lines, line_width):
  426. after_len = 0
  427. prelength = len(list_lines)
  428. append_width = line_width // 2
  429. while 1:
  430. c_lines = []
  431. first_len = after_len
  432. for _line in list_lines:
  433. bbox = _line["bbox"]
  434. _find = False
  435. for c_l_i in range(len(c_lines)):
  436. c_l = c_lines[len(c_lines) - c_l_i - 1]
  437. bbox1 = c_l["bbox"]
  438. bboxa = [max(0, bbox[0] - append_width), max(0, bbox[1] - append_width), bbox[2] + append_width,
  439. bbox[3] + append_width]
  440. bboxb = [max(0, bbox1[0] - append_width), max(0, bbox1[1] - append_width), bbox1[2] + append_width,
  441. bbox1[3] + append_width]
  442. _iou = getIOU(bboxa, bboxb)
  443. if _iou > 0:
  444. new_bbox = [min(bbox[0], bbox[2], bbox1[0], bbox1[2]), min(bbox[1], bbox[3], bbox1[1], bbox1[3]),
  445. max(bbox[0], bbox[2], bbox1[0], bbox1[2]), max(bbox[1], bbox[3], bbox1[1], bbox1[3])]
  446. _find = True
  447. c_l["bbox"] = new_bbox
  448. break
  449. if not _find:
  450. c_lines.append(_line)
  451. after_len = len(c_lines)
  452. if first_len == after_len:
  453. break
  454. list_lines = c_lines
  455. log("cluster lines from %d to %d" % (prelength, len(list_lines)))
  456. return c_lines
  457. def getIOU(bbox0, bbox1):
  458. width = abs(max(bbox0[2], bbox1[2]) - min(bbox0[0], bbox1[0])) - (
  459. abs(bbox0[2] - bbox0[0]) + abs(bbox1[2] - bbox1[0]))
  460. height = abs(max(bbox0[3], bbox1[3]) - min(bbox0[1], bbox1[1])) - (
  461. abs(bbox0[3] - bbox0[1]) + abs(bbox1[3] - bbox1[1]))
  462. if width <= 0 and height <= 0:
  463. iou = abs(width * height / min(abs((bbox0[2] - bbox0[0]) * (bbox0[3] - bbox0[1])),
  464. abs((bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1]))))
  465. # print("getIOU", iou)
  466. return iou + 0.1
  467. return 0
  468. def delete_short_lines(list_lines, image_shape, scale=100):
  469. # 排除太短的线
  470. x_min_len = max(5, int(image_shape[0] / scale))
  471. y_min_len = max(5, int(image_shape[1] / scale))
  472. new_list_lines = []
  473. for line in list_lines:
  474. if line[0] == line[2]:
  475. if abs(line[3] - line[1]) >= y_min_len:
  476. # print("y_min_len", abs(line[3] - line[1]), y_min_len)
  477. new_list_lines.append(line)
  478. else:
  479. if abs(line[2] - line[0]) >= x_min_len:
  480. # print("x_min_len", abs(line[2] - line[0]), x_min_len)
  481. new_list_lines.append(line)
  482. return new_list_lines
  483. def delete_single_lines(row_line_list, col_line_list, point_list):
  484. new_col_line_list = []
  485. min_point_cnt = 2
  486. for line in col_line_list:
  487. p_cnt = 0
  488. for p in point_list:
  489. # if line[0] == p[0] and line[1] <= p[1] <= line[3]:
  490. if line[0] == p[0]:
  491. p_cnt += 1
  492. if p_cnt >= min_point_cnt:
  493. new_col_line_list.append(line)
  494. break
  495. new_row_line_list = []
  496. for line in row_line_list:
  497. p_cnt = 0
  498. for p in point_list:
  499. # if line[1] == p[1] and line[0] <= p[0] <= line[2]:
  500. if line[1] == p[1]:
  501. p_cnt += 1
  502. if p_cnt >= min_point_cnt:
  503. new_row_line_list.append(line)
  504. break
  505. return new_row_line_list, new_col_line_list
  506. def merge_line(lines, axis, threshold=5):
  507. """
  508. 解决模型预测一条直线错开成多条直线,合并成一条直线
  509. :param lines: 线条列表
  510. :param axis: 0:横线 1:竖线
  511. :param threshold: 两条线间像素差阈值
  512. :return: 合并后的线条列表
  513. """
  514. # 任意一条line获取该合并的line,横线往下找,竖线往右找
  515. lines.sort(key=lambda x: (x[axis], x[1 - axis]))
  516. merged_lines = []
  517. used_lines = []
  518. for line1 in lines:
  519. if line1 in used_lines:
  520. continue
  521. merged_line = [line1]
  522. used_lines.append(line1)
  523. for line2 in lines:
  524. if line2 in used_lines:
  525. continue
  526. if line1[1 - axis] - threshold <= line2[1 - axis] <= line1[1 - axis] + threshold:
  527. # 计算基准长度
  528. min_axis = 10000
  529. max_axis = 0
  530. for line3 in merged_line:
  531. if line3[axis] < min_axis:
  532. min_axis = line3[axis]
  533. if line3[axis + 2] > max_axis:
  534. max_axis = line3[axis + 2]
  535. # 判断两条线有无交集
  536. if min_axis <= line2[axis] <= max_axis \
  537. or min_axis <= line2[axis + 2] <= max_axis:
  538. merged_line.append(line2)
  539. used_lines.append(line2)
  540. if merged_line:
  541. merged_lines.append(merged_line)
  542. # 合并line
  543. result_lines = []
  544. for merged_line in merged_lines:
  545. # 获取line宽的平均值
  546. axis_average = 0
  547. for line in merged_line:
  548. axis_average += line[1 - axis]
  549. axis_average = int(axis_average / len(merged_line))
  550. # 获取最长line两端
  551. merged_line.sort(key=lambda x: (x[axis]))
  552. axis_start = merged_line[0][axis]
  553. merged_line.sort(key=lambda x: (x[axis + 2]))
  554. axis_end = merged_line[-1][axis + 2]
  555. if axis:
  556. result_lines.append([axis_average, axis_start, axis_average, axis_end])
  557. else:
  558. result_lines.append([axis_start, axis_average, axis_end, axis_average])
  559. return result_lines
  560. def get_points(row_lines, col_lines, image_size, threshold=5):
  561. # 创建空图
  562. row_img = np.zeros(image_size, np.uint8)
  563. col_img = np.zeros(image_size, np.uint8)
  564. # 画线
  565. # threshold = 5
  566. for row in row_lines:
  567. cv2.line(row_img, (int(row[0] - threshold), int(row[1])), (int(row[2] + threshold), int(row[3])), (255, 255, 255), 1)
  568. for col in col_lines:
  569. cv2.line(col_img, (int(col[0]), int(col[1] - threshold)), (int(col[2]), int(col[3] + threshold)), (255, 255, 255), 1)
  570. # cv2.imshow('get_points', row_img+col_img)
  571. # cv2.waitKey(0)
  572. # 求出交点
  573. point_img = np.bitwise_and(row_img, col_img)
  574. # cv2.imwrite("get_points.jpg", row_img+col_img)
  575. # cv2.imshow("get_points", row_img+col_img)
  576. # cv2.waitKey(0)
  577. # 识别黑白图中的白色交叉点,将横纵坐标取出
  578. ys, xs = np.where(point_img > 0)
  579. points = []
  580. for i in range(len(xs)):
  581. points.append((xs[i], ys[i]))
  582. points.sort(key=lambda x: (x[0], x[1]))
  583. return points
  584. def fix_outline(image, row_line_list, col_line_list, point_list, scale=25):
  585. log("into fix_outline")
  586. x_min_len = max(10, int(image.shape[0] / scale))
  587. y_min_len = max(10, int(image.shape[1] / scale))
  588. if len(row_line_list) <= 1 or len(col_line_list) <= 1:
  589. return [], [], row_line_list, col_line_list
  590. # 预测线取上下左右4个边(会有超出表格部分) [(), ()]
  591. row_line_list.sort(key=lambda x: (x[1], x[0]))
  592. up_line = row_line_list[0]
  593. bottom_line = row_line_list[-1]
  594. col_line_list.sort(key=lambda x: x[0])
  595. left_line = col_line_list[0]
  596. right_line = col_line_list[-1]
  597. # 计算单格高度宽度
  598. if len(row_line_list) > 1:
  599. height_dict = {}
  600. for j in range(len(row_line_list)):
  601. if j + 1 > len(row_line_list) - 1:
  602. break
  603. height = abs(int(row_line_list[j][3] - row_line_list[j + 1][3]))
  604. if height >= 10:
  605. if height in height_dict.keys():
  606. height_dict[height] = height_dict[height] + 1
  607. else:
  608. height_dict[height] = 1
  609. height_list = [[x, height_dict[x]] for x in height_dict.keys()]
  610. if height_list:
  611. height_list.sort(key=lambda x: (x[1], -x[0]), reverse=True)
  612. # print("box_height", height_list)
  613. box_height = height_list[0][0]
  614. else:
  615. box_height = y_min_len
  616. else:
  617. box_height = y_min_len
  618. if len(col_line_list) > 1:
  619. box_width = abs(col_line_list[1][2] - col_line_list[0][2])
  620. else:
  621. box_width = x_min_len
  622. # 设置轮廓线需超出阈值
  623. if box_height >= 2 * y_min_len:
  624. fix_h_len = y_min_len
  625. else:
  626. fix_h_len = box_height * 2 / 3
  627. if box_width >= 2 * x_min_len:
  628. fix_w_len = x_min_len
  629. else:
  630. fix_w_len = box_width * 2 / 3
  631. # 判断超出部分的长度,超出一定长度就补线
  632. new_row_lines = []
  633. new_col_lines = []
  634. all_longer_row_lines = []
  635. all_longer_col_lines = []
  636. # print('box_height, box_width, fix_h_len, fix_w_len', box_height, box_width, fix_h_len, fix_w_len)
  637. # print('bottom_line, left_line, right_line', bottom_line, left_line, right_line)
  638. # 补左右两条竖线超出来的线的row
  639. if up_line[1] - left_line[1] >= fix_h_len and up_line[1] - right_line[1] >= fix_h_len:
  640. if up_line[1] - left_line[1] >= up_line[1] - right_line[1]:
  641. new_row_lines.append([left_line[0], left_line[1], right_line[0], left_line[1]])
  642. new_col_y = left_line[1]
  643. # 补了row,要将其他短的col连到row上
  644. for j in range(len(col_line_list)):
  645. col = col_line_list[j]
  646. if abs(new_col_y - col[1]) <= box_height:
  647. col_line_list[j][1] = min([new_col_y, col[1]])
  648. else:
  649. new_row_lines.append([left_line[0], right_line[1], right_line[0], right_line[1]])
  650. new_col_y = right_line[1]
  651. # 补了row,要将其他短的col连到row上
  652. for j in range(len(col_line_list)):
  653. col = col_line_list[j]
  654. # 且距离不能相差太大
  655. if abs(new_col_y - col[1]) <= box_height:
  656. col_line_list[j][1] = min([new_col_y, col[1]])
  657. if left_line[3] - bottom_line[3] >= fix_h_len and right_line[3] - bottom_line[3] >= fix_h_len:
  658. if left_line[3] - bottom_line[3] >= right_line[3] - bottom_line[3]:
  659. new_row_lines.append([left_line[2], left_line[3], right_line[2], left_line[3]])
  660. new_col_y = left_line[3]
  661. # 补了row,要将其他短的col连到row上
  662. for j in range(len(col_line_list)):
  663. col = col_line_list[j]
  664. # 且距离不能相差太大
  665. if abs(new_col_y - col[3]) <= box_height:
  666. col_line_list[j][3] = max([new_col_y, col[3]])
  667. else:
  668. new_row_lines.append([left_line[2], right_line[3], right_line[2], right_line[3]])
  669. new_col_y = right_line[3]
  670. # 补了row,要将其他短的col连到row上
  671. for j in range(len(col_line_list)):
  672. col = col_line_list[j]
  673. # 且距离不能相差太大
  674. if abs(new_col_y - col[3]) <= box_height:
  675. col_line_list[j][3] = max([new_col_y, col[3]])
  676. # 补上下两条横线超出来的线的col
  677. if left_line[0] - up_line[0] >= fix_w_len and left_line[0] - bottom_line[0] >= fix_w_len:
  678. if left_line[0] - up_line[0] >= left_line[0] - bottom_line[0]:
  679. new_col_lines.append([up_line[0], up_line[1], up_line[0], bottom_line[1]])
  680. new_row_x = up_line[0]
  681. # 补了col,要将其他短的row连到col上
  682. for j in range(len(row_line_list)):
  683. row = row_line_list[j]
  684. # 且距离不能相差太大
  685. if abs(new_row_x - row[0]) <= box_width:
  686. row_line_list[j][0] = min([new_row_x, row[0]])
  687. else:
  688. new_col_lines.append([bottom_line[0], up_line[1], bottom_line[0], bottom_line[1]])
  689. new_row_x = bottom_line[0]
  690. # 补了col,要将其他短的row连到col上
  691. for j in range(len(row_line_list)):
  692. row = row_line_list[j]
  693. # 且距离不能相差太大
  694. if abs(new_row_x - row[0]) <= box_width:
  695. row_line_list[j][0] = min([new_row_x, row[0]])
  696. if up_line[2] - right_line[2] >= fix_w_len and bottom_line[2] - right_line[2] >= fix_w_len:
  697. if up_line[2] - right_line[2] >= bottom_line[2] - right_line[2]:
  698. new_col_lines.append([up_line[2], up_line[3], up_line[2], bottom_line[3]])
  699. new_row_x = up_line[2]
  700. # 补了col,要将其他短的row连到col上
  701. for j in range(len(row_line_list)):
  702. row = row_line_list[j]
  703. # 且距离不能相差太大
  704. if abs(new_row_x - row[2]) <= box_width:
  705. row_line_list[j][2] = max([new_row_x, row[2]])
  706. else:
  707. new_col_lines.append([bottom_line[2], up_line[3], bottom_line[2], bottom_line[3]])
  708. new_row_x = bottom_line[2]
  709. # 补了col,要将其他短的row连到col上
  710. for j in range(len(row_line_list)):
  711. row = row_line_list[j]
  712. # 且距离不能相差太大
  713. if abs(new_row_x - row[2]) <= box_width:
  714. row_line_list[j][2] = max([new_row_x, row[2]])
  715. all_longer_row_lines += row_line_list
  716. all_longer_col_lines += col_line_list
  717. # print('new_row_lines, new_col_lines', new_row_lines, new_col_lines)
  718. # print('all_longer_row_lines, all_longer_col_lines', all_longer_row_lines, all_longer_col_lines)
  719. return new_row_lines, new_col_lines, all_longer_row_lines, all_longer_col_lines
  720. def fix_inner(row_line_list, col_line_list, point_list):
  721. def fix(fix_lines, assist_lines, split_points, axis):
  722. new_line_point_list = []
  723. delete_line_point_list = []
  724. for line1 in fix_lines:
  725. min_assist_line = [[], []]
  726. min_distance = [1000, 1000]
  727. if_find = [0, 0]
  728. # 获取fix_line中的所有col point,里面可能不包括两个顶点,col point是交点,顶点可能不是交点
  729. fix_line_points = []
  730. for point in split_points:
  731. if abs(point[1 - axis] - line1[1 - axis]) <= 2:
  732. if line1[axis] <= point[axis] <= line1[axis + 2]:
  733. fix_line_points.append(point)
  734. # 找出离两个顶点最近的assist_line, 并且assist_line与fix_line不相交
  735. line1_point = [line1[:2], line1[2:]]
  736. for i in range(2):
  737. point = line1_point[i]
  738. for line2 in assist_lines:
  739. if not if_find[i] and abs(point[axis] - line2[axis]) <= 2:
  740. if line1[1 - axis] <= point[1 - axis] <= line2[1 - axis + 2]:
  741. # print("line1, match line2", line1, line2)
  742. if_find[i] = 1
  743. break
  744. else:
  745. if abs(point[axis] - line2[axis]) < min_distance[i] and line2[1 - axis] <= point[1 - axis] <= \
  746. line2[1 - axis + 2]:
  747. if line1[axis] <= line2[axis] <= line1[axis + 2]:
  748. continue
  749. min_distance[i] = abs(line1[axis] - line2[axis])
  750. min_assist_line[i] = line2
  751. if len(min_assist_line[0]) == 0 and len(min_assist_line[1]) == 0:
  752. continue
  753. # 找出离assist_line最近的交点
  754. min_distance = [1000, 1000]
  755. min_col_point = [[], []]
  756. for i in range(2):
  757. # print("顶点", i, line1_point[i])
  758. if min_assist_line[i]:
  759. for point in fix_line_points:
  760. if abs(point[axis] - min_assist_line[i][axis]) < min_distance[i]:
  761. min_distance[i] = abs(point[axis] - min_assist_line[i][axis])
  762. min_col_point[i] = point
  763. # print("min_col_point", min_col_point)
  764. # print("min_assist_line", min_assist_line)
  765. if len(min_col_point[0]) == 0 and len(min_col_point[1]) == 0:
  766. continue
  767. # 顶点到交点的距离(多出来的线)需大于assist_line到交点的距离(bbox的边)的1/3
  768. # print("line1_point", line1_point)
  769. if min_assist_line[0] and min_assist_line[0] == min_assist_line[1]:
  770. if min_assist_line[0][axis] < line1_point[0][axis]:
  771. bbox_len = abs(min_col_point[0][axis] - min_assist_line[0][axis])
  772. line_distance = abs(min_col_point[0][axis] - line1_point[0][axis])
  773. if bbox_len / 3 <= line_distance <= bbox_len:
  774. if axis == 1:
  775. add_point = (line1_point[0][1 - axis], min_assist_line[0][axis])
  776. else:
  777. add_point = (min_assist_line[0][axis], line1_point[0][1 - axis])
  778. new_line_point_list.append([line1, add_point])
  779. elif min_assist_line[1][axis] > line1_point[1][axis]:
  780. bbox_len = abs(min_col_point[1][axis] - min_assist_line[1][axis])
  781. line_distance = abs(min_col_point[1][axis] - line1_point[1][axis])
  782. if bbox_len / 3 <= line_distance <= bbox_len:
  783. if axis == 1:
  784. add_point = (line1_point[1][1 - axis], min_assist_line[1][axis])
  785. else:
  786. add_point = (min_assist_line[1][axis], line1_point[1][1 - axis])
  787. new_line_point_list.append([line1, add_point])
  788. else:
  789. for i in range(2):
  790. if min_col_point[i]:
  791. bbox_len = abs(min_col_point[i][axis] - min_assist_line[i][axis])
  792. line_distance = abs(min_col_point[i][axis] - line1_point[i][axis])
  793. # print("bbox_len, line_distance", bbox_len, line_distance)
  794. if bbox_len / 3 <= line_distance <= bbox_len:
  795. if axis == 1:
  796. add_point = (line1_point[i][1 - axis], min_assist_line[i][axis])
  797. else:
  798. add_point = (min_assist_line[i][axis], line1_point[i][1 - axis])
  799. new_line_point_list.append([line1, add_point])
  800. return new_line_point_list
  801. row_line_list_copy = copy.deepcopy(row_line_list)
  802. col_line_list_copy = copy.deepcopy(col_line_list)
  803. try:
  804. new_point_list = fix(col_line_list, row_line_list, point_list, axis=1)
  805. for line, new_point in new_point_list:
  806. if line in col_line_list:
  807. index = col_line_list.index(line)
  808. point1 = line[:2]
  809. point2 = line[2:]
  810. if new_point[1] >= point2[1]:
  811. col_line_list[index] = [point1[0], point1[1], new_point[0], new_point[1]]
  812. elif new_point[1] <= point1[1]:
  813. col_line_list[index] = [new_point[0], new_point[1], point2[0], point2[1]]
  814. new_point_list = fix(row_line_list, col_line_list, point_list, axis=0)
  815. for line, new_point in new_point_list:
  816. if line in row_line_list:
  817. index = row_line_list.index(line)
  818. point1 = line[:2]
  819. point2 = line[2:]
  820. if new_point[0] >= point2[0]:
  821. row_line_list[index] = [point1[0], point1[1], new_point[0], new_point[1]]
  822. elif new_point[0] <= point1[0]:
  823. row_line_list[index] = [new_point[0], new_point[1], point2[0], point2[1]]
  824. return row_line_list, col_line_list
  825. except:
  826. traceback.print_exc()
  827. return row_line_list_copy, col_line_list_copy
  828. def fix_4_points(cross_points, row_line_list, col_line_list):
  829. if not (len(row_line_list) >= 2 and len(col_line_list) >= 2):
  830. return row_line_list, col_line_list
  831. cross_points.sort(key=lambda x: (x[0], x[1]))
  832. left_up_p = cross_points[0]
  833. right_down_p = cross_points[-1]
  834. cross_points.sort(key=lambda x: (-x[0], x[1]))
  835. right_up_p = cross_points[0]
  836. left_down_p = cross_points[-1]
  837. # print('left_up_p', left_up_p, 'left_down_p', left_down_p)
  838. # print('right_up_p', right_up_p, 'right_down_p', right_down_p)
  839. min_x = min(left_up_p[0], left_down_p[0], right_down_p[0], right_up_p[0])
  840. max_x = max(left_up_p[0], left_down_p[0], right_down_p[0], right_up_p[0])
  841. min_y = min(left_up_p[1], left_down_p[1], right_down_p[1], right_up_p[1])
  842. max_y = max(left_up_p[1], left_down_p[1], right_down_p[1], right_up_p[1])
  843. if left_up_p[0] != min_x or left_up_p[1] != min_y:
  844. log('轮廓左上角交点有问题')
  845. row_line_list.append([min_x, min_y, max_x, min_y])
  846. col_line_list.append([min_x, min_y, min_x, max_y])
  847. if left_down_p[0] != min_x or left_down_p[1] != max_y:
  848. log('轮廓左下角交点有问题')
  849. row_line_list.append([min_x, max_y, max_x, max_y])
  850. col_line_list.append([min_x, min_y, min_x, max_y])
  851. if right_up_p[0] != max_x or right_up_p[1] != min_y:
  852. log('轮廓右上角交点有问题')
  853. row_line_list.append([min_x, max_y, max_x, max_y])
  854. col_line_list.append([max_x, min_y, max_x, max_y])
  855. if right_down_p[0] != max_x or right_down_p[1] != max_y:
  856. log('轮廓右下角交点有问题')
  857. row_line_list.append([min_x, max_y, max_x, max_y])
  858. col_line_list.append([max_x, min_y, max_x, max_y])
  859. return row_line_list, col_line_list
  860. def get_split_line(points, col_lines, image_np, threshold=5):
  861. # 线贴着边缘无法得到split_y,导致无法分区
  862. for _col in col_lines:
  863. if _col[3] >= image_np.shape[0] - 5:
  864. _col[3] = image_np.shape[0] - 6
  865. if _col[1] <= 0 + 5:
  866. _col[1] = 6
  867. # print("get_split_line", image_np.shape)
  868. points.sort(key=lambda x: (x[1], x[0]))
  869. # 遍历y坐标,并判断y坐标与上一个y坐标是否存在连接线
  870. i = 0
  871. split_line_y = []
  872. for point in points:
  873. # 从已分开的线下面开始判断
  874. if split_line_y:
  875. if point[1] <= split_line_y[-1] + threshold:
  876. last_y = point[1]
  877. continue
  878. if last_y <= split_line_y[-1] + threshold:
  879. last_y = point[1]
  880. continue
  881. if i == 0:
  882. last_y = point[1]
  883. i += 1
  884. continue
  885. current_line = (last_y, point[1])
  886. split_flag = 1
  887. for col in col_lines:
  888. # 只要找到一条col包含就不是分割线
  889. if current_line[0] >= col[1] - 3 and current_line[1] <= col[3] + 3:
  890. split_flag = 0
  891. break
  892. if split_flag:
  893. split_line_y.append(current_line[0] + 5)
  894. split_line_y.append(current_line[1] - 5)
  895. last_y = point[1]
  896. # 加上收尾分割线
  897. points.sort(key=lambda x: (x[1], x[0]))
  898. y_min = points[0][1]
  899. y_max = points[-1][1]
  900. if y_min - threshold < 0:
  901. split_line_y.append(0)
  902. else:
  903. split_line_y.append(y_min - threshold)
  904. if y_max + threshold > image_np.shape[0]:
  905. split_line_y.append(image_np.shape[0])
  906. else:
  907. split_line_y.append(y_max + threshold)
  908. split_line_y = list(set(split_line_y))
  909. # 剔除两条相隔太近分割线
  910. temp_split_line_y = []
  911. split_line_y.sort(key=lambda x: x)
  912. last_y = -20
  913. for y in split_line_y:
  914. if y - last_y >= 20:
  915. temp_split_line_y.append(y)
  916. last_y = y
  917. split_line_y = temp_split_line_y
  918. # 生成分割线
  919. split_line = []
  920. for y in split_line_y:
  921. split_line.append([(0, y), (image_np.shape[1], y)])
  922. split_line.append([(0, 0), (image_np.shape[1], 0)])
  923. split_line.append([(0, image_np.shape[0]), (image_np.shape[1], image_np.shape[0])])
  924. split_line.sort(key=lambda x: x[0][1])
  925. return split_line, split_line_y
  926. def get_split_area(split_y, row_line_list, col_line_list, cross_points):
  927. # 分割线纵坐标
  928. if len(split_y) < 2:
  929. return [], [], []
  930. split_y.sort(key=lambda x: x)
  931. # new_split_y = []
  932. # for i in range(1, len(split_y), 2):
  933. # new_split_y.append(int((split_y[i] + split_y[i - 1]) / 2))
  934. area_row_line_list = []
  935. area_col_line_list = []
  936. area_point_list = []
  937. for i in range(1, len(split_y)):
  938. y = split_y[i]
  939. last_y = split_y[i - 1]
  940. split_row = []
  941. for row in row_line_list:
  942. if last_y <= row[3] <= y:
  943. split_row.append(row)
  944. split_col = []
  945. for col in col_line_list:
  946. if last_y <= col[1] <= y or last_y <= col[3] <= y or col[1] < last_y < y < col[3]:
  947. split_col.append(col)
  948. split_point = []
  949. for point in cross_points:
  950. if last_y <= point[1] <= y:
  951. split_point.append(point)
  952. # 满足条件才能形成表格区域
  953. if len(split_row) >= 2 and len(split_col) >= 2 and len(split_point) >= 4:
  954. # print('len(split_row), len(split_col), len(split_point)', len(split_row), len(split_col), len(split_point))
  955. area_row_line_list.append(split_row)
  956. area_col_line_list.append(split_col)
  957. area_point_list.append(split_point)
  958. return area_row_line_list, area_col_line_list, area_point_list
  959. def get_standard_lines(row_line_list, col_line_list):
  960. new_row_line_list = []
  961. for row in row_line_list:
  962. w1 = row[0]
  963. w2 = row[2]
  964. # 横线的两个顶点分别找到最近的竖线
  965. min_distance = [10000, 10000]
  966. min_dis_w = [None, None]
  967. for col in col_line_list:
  968. if abs(col[0] - w1) < min_distance[0]:
  969. min_distance[0] = abs(col[0] - w1)
  970. min_dis_w[0] = col[0]
  971. if abs(col[0] - w2) < min_distance[1]:
  972. min_distance[1] = abs(col[0] - w2)
  973. min_dis_w[1] = col[0]
  974. if min_dis_w[0] is not None:
  975. row[0] = min_dis_w[0]
  976. if min_dis_w[1] is not None:
  977. row[2] = min_dis_w[1]
  978. new_row_line_list.append(row)
  979. new_col_line_list = []
  980. for col in col_line_list:
  981. h1 = col[1]
  982. h2 = col[3]
  983. # 横线的两个顶点分别找到最近的竖线
  984. min_distance = [10000, 10000]
  985. min_dis_w = [None, None]
  986. for row in row_line_list:
  987. if abs(row[1] - h1) < min_distance[0]:
  988. min_distance[0] = abs(row[1] - h1)
  989. min_dis_w[0] = row[1]
  990. if abs(row[1] - h2) < min_distance[1]:
  991. min_distance[1] = abs(row[1] - h2)
  992. min_dis_w[1] = row[1]
  993. if min_dis_w[0] is not None:
  994. col[1] = min_dis_w[0]
  995. if min_dis_w[1] is not None:
  996. col[3] = min_dis_w[1]
  997. new_col_line_list.append(col)
  998. # all_line_list = []
  999. # # 横线竖线两个维度
  1000. # for i in range(2):
  1001. # axis = i
  1002. # cross_points.sort(key=lambda x: (x[axis], x[1-axis]))
  1003. # current_axis = cross_points[0][axis]
  1004. # points = []
  1005. # line_list = []
  1006. # for p in cross_points:
  1007. # if p[axis] == current_axis:
  1008. # points.append(p)
  1009. # else:
  1010. # if points:
  1011. # line_list.append([points[0][0], points[0][1], points[-1][0], points[-1][1]])
  1012. # points = [p]
  1013. # current_axis = p[axis]
  1014. # if points:
  1015. # line_list.append([points[0][0], points[0][1], points[-1][0], points[-1][1]])
  1016. # all_line_list.append(line_list)
  1017. # new_col_line_list, new_row_line_list = all_line_list
  1018. return new_col_line_list, new_row_line_list
  1019. def add_outline(cross_points, row_line_list, col_line_list):
  1020. cross_points.sort(key=lambda x: (x[0], x[1]))
  1021. left_up_p = cross_points[0]
  1022. right_down_p = cross_points[-1]
  1023. row_line_list.append([left_up_p[0], left_up_p[1], right_down_p[0], left_up_p[1]])
  1024. row_line_list.append([left_up_p[0], right_down_p[1], right_down_p[0], right_down_p[1]])
  1025. col_line_list.append([left_up_p[0], left_up_p[1], left_up_p[0], right_down_p[1]])
  1026. col_line_list.append([right_down_p[0], left_up_p[1], right_down_p[0], right_down_p[1]])
  1027. return row_line_list, col_line_list