table_line_new.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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(point[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. if len(min_col_point[0]) == 0 and len(min_col_point[1]) == 0:
  764. continue
  765. # print('line1', line1)
  766. # print("min_col_point", min_col_point)
  767. # print("min_assist_line", min_assist_line)
  768. # 顶点到交点的距离(多出来的线)需大于assist_line到交点的距离(bbox的边)的1/3
  769. # print("line1_point", line1_point)
  770. if min_assist_line[0] and min_assist_line[0] == min_assist_line[1]:
  771. if min_assist_line[0][axis] < line1_point[0][axis]:
  772. bbox_len = abs(min_col_point[0][axis] - min_assist_line[0][axis])
  773. line_distance = abs(min_col_point[0][axis] - line1_point[0][axis])
  774. if bbox_len / 3 <= line_distance <= bbox_len:
  775. if axis == 1:
  776. add_point = (line1_point[0][1 - axis], min_assist_line[0][axis])
  777. else:
  778. add_point = (min_assist_line[0][axis], line1_point[0][1 - axis])
  779. new_line_point_list.append([line1, add_point])
  780. elif min_assist_line[1][axis] > line1_point[1][axis]:
  781. bbox_len = abs(min_col_point[1][axis] - min_assist_line[1][axis])
  782. line_distance = abs(min_col_point[1][axis] - line1_point[1][axis])
  783. if bbox_len / 3 <= line_distance <= bbox_len:
  784. if axis == 1:
  785. add_point = (line1_point[1][1 - axis], min_assist_line[1][axis])
  786. else:
  787. add_point = (min_assist_line[1][axis], line1_point[1][1 - axis])
  788. new_line_point_list.append([line1, add_point])
  789. else:
  790. for i in range(2):
  791. if min_col_point[i]:
  792. bbox_len = abs(min_col_point[i][axis] - min_assist_line[i][axis])
  793. line_distance = abs(min_col_point[i][axis] - line1_point[i][axis])
  794. # print("bbox_len, line_distance", bbox_len, line_distance)
  795. if bbox_len / 3 <= line_distance <= bbox_len:
  796. if axis == 1:
  797. add_point = (line1_point[i][1 - axis], min_assist_line[i][axis])
  798. else:
  799. add_point = (min_assist_line[i][axis], line1_point[i][1 - axis])
  800. new_line_point_list.append([line1, add_point])
  801. return new_line_point_list
  802. row_line_list_copy = copy.deepcopy(row_line_list)
  803. col_line_list_copy = copy.deepcopy(col_line_list)
  804. try:
  805. new_point_list = fix(col_line_list, row_line_list, point_list, axis=1)
  806. for line, new_point in new_point_list:
  807. if line in col_line_list:
  808. index = col_line_list.index(line)
  809. point1 = line[:2]
  810. point2 = line[2:]
  811. if new_point[1] >= point2[1]:
  812. col_line_list[index] = [point1[0], point1[1], new_point[0], new_point[1]]
  813. elif new_point[1] <= point1[1]:
  814. col_line_list[index] = [new_point[0], new_point[1], point2[0], point2[1]]
  815. new_point_list = fix(row_line_list, col_line_list, point_list, axis=0)
  816. for line, new_point in new_point_list:
  817. if line in row_line_list:
  818. index = row_line_list.index(line)
  819. point1 = line[:2]
  820. point2 = line[2:]
  821. if new_point[0] >= point2[0]:
  822. row_line_list[index] = [point1[0], point1[1], new_point[0], new_point[1]]
  823. elif new_point[0] <= point1[0]:
  824. row_line_list[index] = [new_point[0], new_point[1], point2[0], point2[1]]
  825. return row_line_list, col_line_list
  826. except:
  827. traceback.print_exc()
  828. return row_line_list_copy, col_line_list_copy
  829. def fix_4_points(cross_points, row_line_list, col_line_list):
  830. if not (len(row_line_list) >= 2 and len(col_line_list) >= 2):
  831. return row_line_list, col_line_list
  832. cross_points.sort(key=lambda x: (x[0], x[1]))
  833. left_up_p = cross_points[0]
  834. right_down_p = cross_points[-1]
  835. cross_points.sort(key=lambda x: (-x[0], x[1]))
  836. right_up_p = cross_points[0]
  837. left_down_p = cross_points[-1]
  838. # print('left_up_p', left_up_p, 'left_down_p', left_down_p)
  839. # print('right_up_p', right_up_p, 'right_down_p', right_down_p)
  840. min_x = min(left_up_p[0], left_down_p[0], right_down_p[0], right_up_p[0])
  841. max_x = max(left_up_p[0], left_down_p[0], right_down_p[0], right_up_p[0])
  842. min_y = min(left_up_p[1], left_down_p[1], right_down_p[1], right_up_p[1])
  843. max_y = max(left_up_p[1], left_down_p[1], right_down_p[1], right_up_p[1])
  844. if left_up_p[0] != min_x or left_up_p[1] != min_y:
  845. log('轮廓左上角交点有问题')
  846. row_line_list.append([min_x, min_y, max_x, min_y])
  847. col_line_list.append([min_x, min_y, min_x, max_y])
  848. if left_down_p[0] != min_x or left_down_p[1] != max_y:
  849. log('轮廓左下角交点有问题')
  850. row_line_list.append([min_x, max_y, max_x, max_y])
  851. col_line_list.append([min_x, min_y, min_x, max_y])
  852. if right_up_p[0] != max_x or right_up_p[1] != min_y:
  853. log('轮廓右上角交点有问题')
  854. row_line_list.append([min_x, max_y, max_x, max_y])
  855. col_line_list.append([max_x, min_y, max_x, max_y])
  856. if right_down_p[0] != max_x or right_down_p[1] != max_y:
  857. log('轮廓右下角交点有问题')
  858. row_line_list.append([min_x, max_y, max_x, max_y])
  859. col_line_list.append([max_x, min_y, max_x, max_y])
  860. return row_line_list, col_line_list
  861. def get_split_line(points, col_lines, image_np, threshold=5):
  862. # 线贴着边缘无法得到split_y,导致无法分区
  863. for _col in col_lines:
  864. if _col[3] >= image_np.shape[0] - 5:
  865. _col[3] = image_np.shape[0] - 6
  866. if _col[1] <= 0 + 5:
  867. _col[1] = 6
  868. # print("get_split_line", image_np.shape)
  869. points.sort(key=lambda x: (x[1], x[0]))
  870. # 遍历y坐标,并判断y坐标与上一个y坐标是否存在连接线
  871. i = 0
  872. split_line_y = []
  873. for point in points:
  874. # 从已分开的线下面开始判断
  875. if split_line_y:
  876. if point[1] <= split_line_y[-1] + threshold:
  877. last_y = point[1]
  878. continue
  879. if last_y <= split_line_y[-1] + threshold:
  880. last_y = point[1]
  881. continue
  882. if i == 0:
  883. last_y = point[1]
  884. i += 1
  885. continue
  886. current_line = (last_y, point[1])
  887. split_flag = 1
  888. for col in col_lines:
  889. # 只要找到一条col包含就不是分割线
  890. if current_line[0] >= col[1] - 3 and current_line[1] <= col[3] + 3:
  891. split_flag = 0
  892. break
  893. if split_flag:
  894. split_line_y.append(current_line[0] + 5)
  895. split_line_y.append(current_line[1] - 5)
  896. last_y = point[1]
  897. # 加上收尾分割线
  898. points.sort(key=lambda x: (x[1], x[0]))
  899. y_min = points[0][1]
  900. y_max = points[-1][1]
  901. if y_min - threshold < 0:
  902. split_line_y.append(0)
  903. else:
  904. split_line_y.append(y_min - threshold)
  905. if y_max + threshold > image_np.shape[0]:
  906. split_line_y.append(image_np.shape[0])
  907. else:
  908. split_line_y.append(y_max + threshold)
  909. split_line_y = list(set(split_line_y))
  910. # 剔除两条相隔太近分割线
  911. temp_split_line_y = []
  912. split_line_y.sort(key=lambda x: x)
  913. last_y = -20
  914. for y in split_line_y:
  915. if y - last_y >= 20:
  916. temp_split_line_y.append(y)
  917. last_y = y
  918. split_line_y = temp_split_line_y
  919. # 生成分割线
  920. split_line = []
  921. for y in split_line_y:
  922. split_line.append([(0, y), (image_np.shape[1], y)])
  923. split_line.append([(0, 0), (image_np.shape[1], 0)])
  924. split_line.append([(0, image_np.shape[0]), (image_np.shape[1], image_np.shape[0])])
  925. split_line.sort(key=lambda x: x[0][1])
  926. return split_line, split_line_y
  927. def get_split_area(split_y, row_line_list, col_line_list, cross_points):
  928. # 分割线纵坐标
  929. if len(split_y) < 2:
  930. return [], [], []
  931. split_y.sort(key=lambda x: x)
  932. # new_split_y = []
  933. # for i in range(1, len(split_y), 2):
  934. # new_split_y.append(int((split_y[i] + split_y[i - 1]) / 2))
  935. area_row_line_list = []
  936. area_col_line_list = []
  937. area_point_list = []
  938. for i in range(1, len(split_y)):
  939. y = split_y[i]
  940. last_y = split_y[i - 1]
  941. split_row = []
  942. for row in row_line_list:
  943. if last_y <= row[3] <= y:
  944. split_row.append(row)
  945. split_col = []
  946. for col in col_line_list:
  947. if last_y <= col[1] <= y or last_y <= col[3] <= y or col[1] < last_y < y < col[3]:
  948. split_col.append(col)
  949. split_point = []
  950. for point in cross_points:
  951. if last_y <= point[1] <= y:
  952. split_point.append(point)
  953. # 满足条件才能形成表格区域
  954. if len(split_row) >= 2 and len(split_col) >= 2 and len(split_point) >= 4:
  955. # print('len(split_row), len(split_col), len(split_point)', len(split_row), len(split_col), len(split_point))
  956. area_row_line_list.append(split_row)
  957. area_col_line_list.append(split_col)
  958. area_point_list.append(split_point)
  959. return area_row_line_list, area_col_line_list, area_point_list
  960. def get_standard_lines(row_line_list, col_line_list):
  961. new_row_line_list = []
  962. for row in row_line_list:
  963. w1 = row[0]
  964. w2 = row[2]
  965. # 横线的两个顶点分别找到最近的竖线
  966. min_distance = [10000, 10000]
  967. min_dis_w = [None, None]
  968. for col in col_line_list:
  969. if abs(col[0] - w1) < min_distance[0]:
  970. min_distance[0] = abs(col[0] - w1)
  971. min_dis_w[0] = col[0]
  972. if abs(col[0] - w2) < min_distance[1]:
  973. min_distance[1] = abs(col[0] - w2)
  974. min_dis_w[1] = col[0]
  975. if min_dis_w[0] is not None:
  976. row[0] = min_dis_w[0]
  977. if min_dis_w[1] is not None:
  978. row[2] = min_dis_w[1]
  979. new_row_line_list.append(row)
  980. new_col_line_list = []
  981. for col in col_line_list:
  982. h1 = col[1]
  983. h2 = col[3]
  984. # 横线的两个顶点分别找到最近的竖线
  985. min_distance = [10000, 10000]
  986. min_dis_w = [None, None]
  987. for row in row_line_list:
  988. if abs(row[1] - h1) < min_distance[0]:
  989. min_distance[0] = abs(row[1] - h1)
  990. min_dis_w[0] = row[1]
  991. if abs(row[1] - h2) < min_distance[1]:
  992. min_distance[1] = abs(row[1] - h2)
  993. min_dis_w[1] = row[1]
  994. if min_dis_w[0] is not None:
  995. col[1] = min_dis_w[0]
  996. if min_dis_w[1] is not None:
  997. col[3] = min_dis_w[1]
  998. new_col_line_list.append(col)
  999. # all_line_list = []
  1000. # # 横线竖线两个维度
  1001. # for i in range(2):
  1002. # axis = i
  1003. # cross_points.sort(key=lambda x: (x[axis], x[1-axis]))
  1004. # current_axis = cross_points[0][axis]
  1005. # points = []
  1006. # line_list = []
  1007. # for p in cross_points:
  1008. # if p[axis] == current_axis:
  1009. # points.append(p)
  1010. # else:
  1011. # if points:
  1012. # line_list.append([points[0][0], points[0][1], points[-1][0], points[-1][1]])
  1013. # points = [p]
  1014. # current_axis = p[axis]
  1015. # if points:
  1016. # line_list.append([points[0][0], points[0][1], points[-1][0], points[-1][1]])
  1017. # all_line_list.append(line_list)
  1018. # new_col_line_list, new_row_line_list = all_line_list
  1019. return new_col_line_list, new_row_line_list
  1020. def add_outline(cross_points, row_line_list, col_line_list):
  1021. cross_points.sort(key=lambda x: (x[0], x[1]))
  1022. left_up_p = cross_points[0]
  1023. right_down_p = cross_points[-1]
  1024. row_line_list.append([left_up_p[0], left_up_p[1], right_down_p[0], left_up_p[1]])
  1025. row_line_list.append([left_up_p[0], right_down_p[1], right_down_p[0], right_down_p[1]])
  1026. col_line_list.append([left_up_p[0], left_up_p[1], left_up_p[0], right_down_p[1]])
  1027. col_line_list.append([right_down_p[0], left_up_p[1], right_down_p[0], right_down_p[1]])
  1028. return row_line_list, col_line_list