table_line.py 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu Sep 9 23:11:51 2020
  5. table line detect
  6. @author: chineseocr
  7. """
  8. import copy
  9. import logging
  10. import tensorflow as tf
  11. import tensorflow.keras.backend as K
  12. from tensorflow.keras.models import Model
  13. from tensorflow.keras.layers import Input, concatenate, Conv2D, MaxPooling2D, BatchNormalization, UpSampling2D
  14. from tensorflow.keras.layers import LeakyReLU
  15. from otr.utils import letterbox_image, get_table_line, adjust_lines, line_to_line, draw_boxes
  16. import numpy as np
  17. import cv2
  18. import time
  19. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  20. def dice_coef(y_true, y_pred, smooth=1e-5):
  21. y_true_f = K.flatten(y_true)
  22. y_pred_f = K.flatten(y_pred)
  23. intersection = K.sum(y_true_f * y_pred_f)
  24. return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
  25. def dice_coef_loss():
  26. def dice_coef_loss_fixed(y_true, y_pred):
  27. return -dice_coef(y_true, y_pred)
  28. return dice_coef_loss_fixed
  29. def focal_loss(gamma=3., alpha=.5):
  30. # 3 0.85 2000e acc-0.6 p-0.99 r-0.99 val_acc-0.56 val_p-0.86 val_r-0.95
  31. # 2 0.85 double_gpu acc-
  32. # 3 0.25 gpu 50e acc-0.5 p-0.99 r-0.99 val_acc-0.45 val_p-0.96 val_r-0.88
  33. # 2 0.25 gpu acc-
  34. # 3 0.5 double_gpu acc-0.6 p-0.99 r-0.99 val_acc-0.60 val_p-0.93 val_r-0.93
  35. def focal_loss_fixed(y_true, y_pred):
  36. pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
  37. pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
  38. return -K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(K.epsilon()+pt_1))-K.sum((1-alpha) * K.pow( pt_0, gamma) * K.log(1. - pt_0 + K.epsilon()))
  39. return focal_loss_fixed
  40. def table_net(input_shape=(1152, 896, 3), num_classes=1):
  41. inputs = Input(shape=input_shape)
  42. # 512
  43. use_bias = False
  44. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
  45. down0a = BatchNormalization()(down0a)
  46. down0a = LeakyReLU(alpha=0.1)(down0a)
  47. down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down0a)
  48. down0a = BatchNormalization()(down0a)
  49. down0a = LeakyReLU(alpha=0.1)(down0a)
  50. down0a_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0a)
  51. # 256
  52. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0a_pool)
  53. down0 = BatchNormalization()(down0)
  54. down0 = LeakyReLU(alpha=0.1)(down0)
  55. down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0)
  56. down0 = BatchNormalization()(down0)
  57. down0 = LeakyReLU(alpha=0.1)(down0)
  58. down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
  59. # 128
  60. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down0_pool)
  61. down1 = BatchNormalization()(down1)
  62. down1 = LeakyReLU(alpha=0.1)(down1)
  63. down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down1)
  64. down1 = BatchNormalization()(down1)
  65. down1 = LeakyReLU(alpha=0.1)(down1)
  66. down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
  67. # 64
  68. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
  69. down2 = BatchNormalization()(down2)
  70. down2 = LeakyReLU(alpha=0.1)(down2)
  71. down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down2)
  72. down2 = BatchNormalization()(down2)
  73. down2 = LeakyReLU(alpha=0.1)(down2)
  74. down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
  75. # 32
  76. down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
  77. down3 = BatchNormalization()(down3)
  78. down3 = LeakyReLU(alpha=0.1)(down3)
  79. down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down3)
  80. down3 = BatchNormalization()(down3)
  81. down3 = LeakyReLU(alpha=0.1)(down3)
  82. down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
  83. # 16
  84. down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
  85. down4 = BatchNormalization()(down4)
  86. down4 = LeakyReLU(alpha=0.1)(down4)
  87. down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down4)
  88. down4 = BatchNormalization()(down4)
  89. down4 = LeakyReLU(alpha=0.1)(down4)
  90. down4_pool = MaxPooling2D((2, 2), strides=(2, 2))(down4)
  91. # 8
  92. center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(down4_pool)
  93. center = BatchNormalization()(center)
  94. center = LeakyReLU(alpha=0.1)(center)
  95. center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(center)
  96. center = BatchNormalization()(center)
  97. center = LeakyReLU(alpha=0.1)(center)
  98. # center
  99. up4 = UpSampling2D((2, 2))(center)
  100. up4 = concatenate([down4, up4], axis=3)
  101. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  102. up4 = BatchNormalization()(up4)
  103. up4 = LeakyReLU(alpha=0.1)(up4)
  104. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  105. up4 = BatchNormalization()(up4)
  106. up4 = LeakyReLU(alpha=0.1)(up4)
  107. up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
  108. up4 = BatchNormalization()(up4)
  109. up4 = LeakyReLU(alpha=0.1)(up4)
  110. # 16
  111. up3 = UpSampling2D((2, 2))(up4)
  112. up3 = concatenate([down3, up3], axis=3)
  113. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  114. up3 = BatchNormalization()(up3)
  115. up3 = LeakyReLU(alpha=0.1)(up3)
  116. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  117. up3 = BatchNormalization()(up3)
  118. up3 = LeakyReLU(alpha=0.1)(up3)
  119. up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
  120. up3 = BatchNormalization()(up3)
  121. up3 = LeakyReLU(alpha=0.1)(up3)
  122. # 32
  123. up2 = UpSampling2D((2, 2))(up3)
  124. up2 = concatenate([down2, up2], axis=3)
  125. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  126. up2 = BatchNormalization()(up2)
  127. up2 = LeakyReLU(alpha=0.1)(up2)
  128. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  129. up2 = BatchNormalization()(up2)
  130. up2 = LeakyReLU(alpha=0.1)(up2)
  131. up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
  132. up2 = BatchNormalization()(up2)
  133. up2 = LeakyReLU(alpha=0.1)(up2)
  134. # 64
  135. up1 = UpSampling2D((2, 2))(up2)
  136. up1 = concatenate([down1, up1], axis=3)
  137. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  138. up1 = BatchNormalization()(up1)
  139. up1 = LeakyReLU(alpha=0.1)(up1)
  140. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  141. up1 = BatchNormalization()(up1)
  142. up1 = LeakyReLU(alpha=0.1)(up1)
  143. up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
  144. up1 = BatchNormalization()(up1)
  145. up1 = LeakyReLU(alpha=0.1)(up1)
  146. # 128
  147. up0 = UpSampling2D((2, 2))(up1)
  148. up0 = concatenate([down0, up0], axis=3)
  149. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  150. up0 = BatchNormalization()(up0)
  151. up0 = LeakyReLU(alpha=0.1)(up0)
  152. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  153. up0 = BatchNormalization()(up0)
  154. up0 = LeakyReLU(alpha=0.1)(up0)
  155. up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
  156. up0 = BatchNormalization()(up0)
  157. up0 = LeakyReLU(alpha=0.1)(up0)
  158. # 256
  159. up0a = UpSampling2D((2, 2))(up0)
  160. up0a = concatenate([down0a, up0a], axis=3)
  161. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  162. up0a = BatchNormalization()(up0a)
  163. up0a = LeakyReLU(alpha=0.1)(up0a)
  164. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  165. up0a = BatchNormalization()(up0a)
  166. up0a = LeakyReLU(alpha=0.1)(up0a)
  167. up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
  168. up0a = BatchNormalization()(up0a)
  169. up0a = LeakyReLU(alpha=0.1)(up0a)
  170. # 512
  171. classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0a)
  172. model = Model(inputs=inputs, outputs=classify)
  173. return model
  174. model = table_net((None, None, 3), 2)
  175. def draw_pixel(pred):
  176. import matplotlib.pyplot as plt
  177. _array = []
  178. for _h in range(len(pred)):
  179. _line = []
  180. for _w in range(len(pred[_h])):
  181. _prob = pred[_h][_w]
  182. if _prob[0]>0.5:
  183. _line.append((0,255,255))
  184. elif _prob[1]>0.5:
  185. _line.append((255,255,0))
  186. else:
  187. _line.append((255,255,255))
  188. _array.append(_line)
  189. plt.imshow(np.array(_array))
  190. plt.show()
  191. def points2lines(pred, sourceP_LB=True, prob=0.2, line_width=7, padding=3, min_len=10,
  192. cell_width=13):
  193. def inBbox(bbox,point,line_width):
  194. x,y = point
  195. if x>=bbox[0]-line_width and x<=bbox[2]+line_width and y>=bbox[1]-line_width and y<=bbox[3]+line_width:
  196. return True,[min(x,bbox[0]),min(y,bbox[1]),max(x,bbox[2]),max(y,bbox[3])]
  197. return False,None
  198. height = len(pred)
  199. width = len(pred[0])
  200. clust_horizontal = []
  201. clust_vertical = []
  202. h_index = -1
  203. _step = line_width
  204. _sum = list(np.sum(np.array((pred[...,1]>prob)).astype(int),axis=1))
  205. _last = False
  206. _current = False
  207. while 1:
  208. h_index += 1
  209. if h_index>=height:
  210. break
  211. w_index = -1
  212. if sourceP_LB:
  213. h_i = height-1-h_index
  214. else:
  215. h_i = h_index
  216. if _sum[h_index]<min_len:
  217. continue
  218. while 1:
  219. w_index += 2
  220. if w_index>=width:
  221. break
  222. _v,_h = pred[h_index][w_index]
  223. if _h>prob:
  224. _find = False
  225. _point = (w_index,h_i)
  226. for l_h_i in range(len(clust_vertical)):
  227. l_h = clust_vertical[len(clust_vertical)-l_h_i-1]
  228. bbox = l_h.get("bbox")
  229. b_in,_bbox = inBbox(bbox,_point,line_width)
  230. if b_in:
  231. _find = True
  232. l_h.get("points").append(_point)
  233. l_h["bbox"] = _bbox
  234. break
  235. if not _find:
  236. clust_vertical.append({"points":[_point],"bbox":[w_index,h_i,w_index,h_i]})
  237. w_index = -1
  238. _sum = list(np.sum(np.array((pred[...,0]>prob)).astype(int),axis=0))
  239. while 1:
  240. w_index += 1
  241. if w_index>=width:
  242. break
  243. h_index = -1
  244. if _sum[w_index]<min_len:
  245. continue
  246. while 1:
  247. h_index += 2
  248. if h_index>=height:
  249. break
  250. if sourceP_LB:
  251. h_i = height-1-h_index
  252. else:
  253. h_i = h_index
  254. _v,_h = pred[h_index][w_index]
  255. if _v>prob:
  256. _find = False
  257. _point = (w_index,h_i)
  258. for l_h_i in range(len(clust_horizontal)):
  259. l_h = clust_horizontal[len(clust_horizontal)-l_h_i-1]
  260. bbox = l_h.get("bbox")
  261. b_in,_bbox = inBbox(bbox,_point,line_width)
  262. if b_in:
  263. _find = True
  264. l_h.get("points").append(_point)
  265. l_h["bbox"] = _bbox
  266. break
  267. if not _find:
  268. clust_horizontal.append({"points":[_point],"bbox":[w_index,h_i,w_index,h_i]})
  269. tmp_vertical = []
  270. for _dict in clust_vertical:
  271. _bbox = _dict.get("bbox")
  272. if _bbox[2]-_bbox[0]>=min_len or _bbox[3]-_bbox[1]>=min_len:
  273. tmp_vertical.append([(_bbox[0]+_bbox[2])/2,_bbox[1]-padding,(_bbox[0]+_bbox[2])/2,_bbox[3]+padding])
  274. tmp_horizontal = []
  275. for _dict in clust_horizontal:
  276. _bbox = _dict.get("bbox")
  277. if _bbox[2]-_bbox[0]>=min_len or _bbox[3]-_bbox[1]>=min_len:
  278. tmp_horizontal.append([_bbox[0]-padding,(_bbox[1]+_bbox[3])/2,_bbox[2]+padding,(_bbox[1]+_bbox[3])/2])
  279. #merge lines
  280. tmp_vertical.sort(key=lambda x:x[3],reverse=True)
  281. tmp_horizontal.sort(key=lambda x:x[0])
  282. pop_index = []
  283. final_vertical = []
  284. for _line in tmp_vertical:
  285. _find = False
  286. x0,y0,x1,y1 = _line
  287. for _line2 in final_vertical:
  288. x2,y2,x3,y3 = _line2
  289. if abs(x0-x2)<line_width and abs(y0-y3)<cell_width or abs(y1-y2)<cell_width:
  290. _find = True
  291. final_vertical.append([x0,min(y0,y2),x1,max(y1,y3)])
  292. break
  293. if not _find:
  294. final_vertical.append(_line)
  295. final_horizontal = []
  296. for _line in tmp_horizontal:
  297. _find = False
  298. x0,y0,x1,y1 = _line
  299. for _line2 in final_horizontal:
  300. x2,y2,x3,y3 = _line2
  301. if abs(y0-y2)<line_width and abs(x0-x3)<cell_width or abs(x1-x2)<cell_width:
  302. _find = True
  303. final_horizontal.append([min(x0,x2),y0,max(x1,x3),y1])
  304. break
  305. if not _find:
  306. final_horizontal.append(_line)
  307. list_line = []
  308. for _line in final_vertical:
  309. list_line.append(_line)
  310. for _line in final_horizontal:
  311. list_line.append(_line)
  312. import matplotlib.pyplot as plt
  313. plt.figure()
  314. for _line in list_line:
  315. x0,y0,x1,y1 = _line
  316. plt.plot([x0,x1],[y0,y1])
  317. # for _line in list_line:
  318. # x0,y0,x1,y1 = _line.bbox
  319. # plt.plot([x0,x1],[y0,y1])
  320. # for point in list_crosspoints:
  321. # plt.scatter(point.get("point")[0],point.get("point")[1])
  322. # plt.show()
  323. return list_line
  324. def table_line(img, model, size=(512, 1024), hprob=0.5, vprob=0.5, row=50, col=30, alph=15):
  325. sizew, sizeh = size
  326. img_new = cv2.resize(img, (sizew, sizeh), interpolation=cv2.INTER_AREA)
  327. pred = model.predict(np.array([img_new]))
  328. pred = pred[0]
  329. _time = time.time()
  330. list_line = points2lines(pred, False)
  331. return list_line
  332. def table_line2(img, model, size=(512, 1024), hprob=0.5, vprob=0.5, row=50, col=30, alph=15):
  333. sizew, sizeh = size
  334. # [..., ::-1] 最后一维内部反向输出
  335. # inputBlob, fx, fy = letterbox_image(img[..., ::-1], (sizew, sizeh))
  336. # pred = model.predict(np.array([np.array(inputBlob)]))
  337. # pred = model.predict(np.array([np.array(inputBlob)/255.0]))
  338. img_new = cv2.resize(img, (sizew, sizeh), interpolation=cv2.INTER_AREA)
  339. # logging.info("into table_line 1")
  340. pred = model.predict(np.array([img_new]))
  341. # logging.info("into table_line 2")
  342. pred = pred[0]
  343. draw_pixel(pred)
  344. _time = time.time()
  345. points2lines(pred)
  346. logging.info("points2lines takes %ds"%(time.time()-_time))
  347. vpred = pred[..., 1] > vprob # 横线
  348. hpred = pred[..., 0] > hprob # 竖线
  349. vpred = vpred.astype(int)
  350. hpred = hpred.astype(int)
  351. # print("vpred shape", vpred)
  352. # print("hpred shape", hpred)
  353. colboxes = get_table_line(vpred, axis=1, lineW=col)
  354. rowboxes = get_table_line(hpred, axis=0, lineW=row)
  355. # logging.info("into table_line 3")
  356. # if len(rowboxes) > 0:
  357. # rowboxes = np.array(rowboxes)
  358. # rowboxes[:, [0, 2]] = rowboxes[:, [0, 2]]/fx
  359. # rowboxes[:, [1, 3]] = rowboxes[:, [1, 3]]/fy
  360. # rowboxes = rowboxes.tolist()
  361. # if len(colboxes) > 0:
  362. # colboxes = np.array(colboxes)
  363. # colboxes[:, [0, 2]] = colboxes[:, [0, 2]]/fx
  364. # colboxes[:, [1, 3]] = colboxes[:, [1, 3]]/fy
  365. # colboxes = colboxes.tolist()
  366. nrow = len(rowboxes)
  367. ncol = len(colboxes)
  368. for i in range(nrow):
  369. for j in range(ncol):
  370. rowboxes[i] = line_to_line(rowboxes[i], colboxes[j], 10)
  371. colboxes[j] = line_to_line(colboxes[j], rowboxes[i], 10)
  372. # logging.info("into table_line 4")
  373. # 删掉贴着边框的line
  374. temp_list = []
  375. threshold = 5
  376. for line in rowboxes:
  377. if line[1]-0 <= threshold or size[1]-line[1] <= threshold:
  378. continue
  379. # 内部排序
  380. if line[0] > line[2]:
  381. line = [line[2], line[3], line[0], line[1]]
  382. temp_list.append(line)
  383. rowboxes = temp_list
  384. temp_list = []
  385. for line in colboxes:
  386. if line[0]-0 <= threshold or size[0]-line[0] <= threshold:
  387. continue
  388. # 内部排序
  389. if line[1] > line[3]:
  390. line = [line[2], line[3], line[0], line[1]]
  391. temp_list.append(line)
  392. colboxes = temp_list
  393. return rowboxes, colboxes, img_new
  394. def get_outline(points, image_np):
  395. # 取出x, y的最大值最小值
  396. x_min = points[0][0]
  397. x_max = points[-1][0]
  398. points.sort(key=lambda x: (x[1], x[0]))
  399. y_min = points[0][1]
  400. y_max = points[-1][1]
  401. # 创建空图
  402. # outline_img = np.zeros(image_size, np.uint8)
  403. outline_img = np.copy(image_np)
  404. cv2.rectangle(outline_img, (x_min-5, y_min-5), (x_max+5, y_max+5), (0, 0, 0), 2)
  405. # cv2.imshow("outline_img", outline_img)
  406. # cv2.waitKey(0)
  407. return outline_img
  408. def get_split_line(points, col_lines, image_np):
  409. # print("get_split_line", image_np.shape)
  410. points.sort(key=lambda x: (x[1], x[0]))
  411. # 遍历y坐标,并判断y坐标与上一个y坐标是否存在连接线
  412. i = 0
  413. split_line_y = []
  414. for point in points:
  415. # 从已分开的线下面开始判断
  416. if split_line_y:
  417. if point[1] <= split_line_y[-1] + 5:
  418. last_y = point[1]
  419. continue
  420. if last_y <= split_line_y[-1] + 5:
  421. last_y = point[1]
  422. continue
  423. if i == 0:
  424. last_y = point[1]
  425. i += 1
  426. continue
  427. current_line = (last_y, point[1])
  428. split_flag = 1
  429. for col in col_lines:
  430. # 只要找到一条col包含就不是分割线
  431. if current_line[0] >= col[1]-3 and current_line[1] <= col[3]+3:
  432. split_flag = 0
  433. # print("img", img.shape)
  434. # print("col", col)
  435. # print("current_line", current_line)
  436. break
  437. if split_flag:
  438. split_line_y.append(current_line[0]+5)
  439. split_line_y.append(current_line[1]-5)
  440. last_y = point[1]
  441. # 加上收尾分割线
  442. points.sort(key=lambda x: (x[1], x[0]))
  443. y_min = points[0][1]
  444. y_max = points[-1][1]
  445. # print("加上收尾分割线", y_min, y_max)
  446. if y_min-5 < 0:
  447. split_line_y.append(0)
  448. else:
  449. split_line_y.append(y_min-5)
  450. if y_max+5 > image_np.shape[0]:
  451. split_line_y.append(image_np.shape[0])
  452. else:
  453. split_line_y.append(y_max+5)
  454. split_line_y = list(set(split_line_y))
  455. # 剔除两条相隔太近分割线
  456. temp_split_line_y = []
  457. split_line_y.sort(key=lambda x: x)
  458. last_y = -20
  459. for y in split_line_y:
  460. # print(y)
  461. if y - last_y >= 20:
  462. # print(y, last_y)
  463. temp_split_line_y.append(y)
  464. last_y = y
  465. split_line_y = temp_split_line_y
  466. # print("split_line_y", split_line_y)
  467. # 生成分割线
  468. split_line = []
  469. last_y = 0
  470. for y in split_line_y:
  471. # if y - last_y <= 15:
  472. # continue
  473. split_line.append([(0, y), (image_np.shape[1], y)])
  474. last_y = y
  475. split_line.append([(0, 0), (image_np.shape[1], 0)])
  476. split_line.append([(0, image_np.shape[0]), (image_np.shape[1], image_np.shape[0])])
  477. split_line.sort(key=lambda x: x[0][1])
  478. # print("split_line", split_line)
  479. # 画图画线
  480. # split_line_img = np.copy(image_np)
  481. # for y in split_line_y:
  482. # cv2.line(split_line_img, (0, y), (image_np.shape[1], y), (0, 0, 0), 1)
  483. # cv2.imshow("split_line_img", split_line_img)
  484. # cv2.waitKey(0)
  485. return split_line, split_line_y
  486. def get_points(row_lines, col_lines, image_size):
  487. # 创建空图
  488. row_img = np.zeros(image_size, np.uint8)
  489. col_img = np.zeros(image_size, np.uint8)
  490. # 画线
  491. thresh = 3
  492. for row in row_lines:
  493. cv2.line(row_img, (int(row[0]-thresh), int(row[1])), (int(row[2]+thresh), int(row[3])), (255, 255, 255), 1)
  494. for col in col_lines:
  495. cv2.line(col_img, (int(col[0]), int(col[1]-thresh)), (int(col[2]), int(col[3]+thresh)), (255, 255, 255), 1)
  496. # 求出交点
  497. point_img = np.bitwise_and(row_img, col_img)
  498. # cv2.imshow("point_img", np.bitwise_not(point_img))
  499. # cv2.waitKey(0)
  500. # 识别黑白图中的白色交叉点,将横纵坐标取出
  501. ys, xs = np.where(point_img > 0)
  502. points = []
  503. for i in range(len(xs)):
  504. points.append((xs[i], ys[i]))
  505. points.sort(key=lambda x: (x[0], x[1]))
  506. return points
  507. def get_minAreaRect(image):
  508. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  509. gray = cv2.bitwise_not(gray)
  510. thresh = cv2.threshold(gray, 0, 255,
  511. cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  512. coords = np.column_stack(np.where(thresh > 0))
  513. return cv2.minAreaRect(coords)
  514. def get_contours(image):
  515. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  516. ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  517. contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  518. cv2.drawContours(image, contours, -1, (0, 0, 255), 3)
  519. cv2.imshow("get contours", image)
  520. cv2.waitKey(0)
  521. def merge_line(lines, axis, threshold=5):
  522. """
  523. 解决模型预测一条直线错开成多条直线,合并成一条直线
  524. :param lines: 线条列表
  525. :param axis: 0:横线 1:竖线
  526. :param threshold: 两条线间像素差阈值
  527. :return: 合并后的线条列表
  528. """
  529. # 任意一条line获取该合并的line,横线往下找,竖线往右找
  530. lines.sort(key=lambda x: (x[axis], x[1-axis]))
  531. merged_lines = []
  532. used_lines = []
  533. for line1 in lines:
  534. if line1 in used_lines:
  535. continue
  536. merged_line = [line1]
  537. used_lines.append(line1)
  538. for line2 in lines:
  539. if line2 in used_lines:
  540. continue
  541. if line1[1-axis]-threshold <= line2[1-axis] <= line1[1-axis]+threshold:
  542. # 计算基准长度
  543. min_axis = 10000
  544. max_axis = 0
  545. for line3 in merged_line:
  546. if line3[axis] < min_axis:
  547. min_axis = line3[axis]
  548. if line3[axis+2] > max_axis:
  549. max_axis = line3[axis+2]
  550. # 判断两条线有无交集
  551. if min_axis <= line2[axis] <= max_axis \
  552. or min_axis <= line2[axis+2] <= max_axis:
  553. merged_line.append(line2)
  554. used_lines.append(line2)
  555. if merged_line:
  556. merged_lines.append(merged_line)
  557. # 合并line
  558. result_lines = []
  559. for merged_line in merged_lines:
  560. # 获取line宽的平均值
  561. axis_average = 0
  562. for line in merged_line:
  563. axis_average += line[1-axis]
  564. axis_average = int(axis_average/len(merged_line))
  565. # 获取最长line两端
  566. merged_line.sort(key=lambda x: (x[axis]))
  567. axis_start = merged_line[0][axis]
  568. merged_line.sort(key=lambda x: (x[axis+2]))
  569. axis_end = merged_line[-1][axis+2]
  570. if axis:
  571. result_lines.append([axis_average, axis_start, axis_average, axis_end])
  572. else:
  573. result_lines.append([axis_start, axis_average, axis_end, axis_average])
  574. return result_lines
  575. def fix_inner2(row_points, col_points, row_lines, col_lines, threshold=3):
  576. for i in range(len(row_points)):
  577. row = row_points[i]
  578. row.sort(key=lambda x: (x[1], x[0]))
  579. for j in range(len(row)):
  580. # 当前点
  581. point = row[j]
  582. # 获取当前点在所在行的下个点
  583. if j >= len(row) - 1:
  584. next_row_point = []
  585. else:
  586. next_row_point = row[j+1]
  587. if next_row_point:
  588. for k in range(len(row_lines)):
  589. line = row_lines[k]
  590. if line[1] - threshold <= point[1] <= line[1] + threshold:
  591. if not line[0] <= point[0] <= next_row_point[0] <= line[2]:
  592. if point[0] <= line[2] < next_row_point[0]:
  593. if line[2] - point[0] >= 1/3 * (next_row_point[0] - point[0]):
  594. row_lines[k][2] = next_row_point[0]
  595. if point[0] < line[0] <= next_row_point[0]:
  596. if next_row_point[0] - line[0] >= 1/3 * (next_row_point[0] - point[0]):
  597. row_lines[k][0] = point[0]
  598. # 获取当前点所在列的下个点
  599. next_col_point = []
  600. for col in col_points:
  601. if point in col:
  602. col.sort(key=lambda x: (x[0], x[1]))
  603. if col.index(point) < len(col) - 1:
  604. next_col_point = col[col.index(point)+1]
  605. break
  606. # 获取当前点的对角线点,通过该列下个点所在行的下个点获得
  607. next_row_next_col_point = []
  608. if next_col_point:
  609. for row2 in row_points:
  610. if next_col_point in row2:
  611. row2.sort(key=lambda x: (x[1], x[0]))
  612. if row2.index(next_col_point) < len(row2) - 1:
  613. next_row_next_col_point = row2[row2.index(next_col_point)+1]
  614. break
  615. # 有该列下一点但没有该列下一点所在行的下个点
  616. if not next_row_next_col_point:
  617. # 如果有该行下个点
  618. if next_row_point:
  619. next_row_next_col_point = [next_row_point[0], next_col_point[1]]
  620. if next_col_point:
  621. for k in range(len(col_lines)):
  622. line = col_lines[k]
  623. if line[0] - threshold <= point[0] <= line[0] + threshold:
  624. if not line[1] <= point[1] <= next_col_point[1] <= line[3]:
  625. if point[1] <= line[3] < next_col_point[1]:
  626. if line[3] - point[1] >= 1/3 * (next_col_point[1] - point[1]):
  627. col_lines[k][3] = next_col_point[1]
  628. if point[1] < line[1] <= next_col_point[1]:
  629. if next_col_point[1] - line[1] >= 1/3 * (next_col_point[1] - point[1]):
  630. col_lines[k][1] = point[1]
  631. if next_row_next_col_point:
  632. for k in range(len(col_lines)):
  633. line = col_lines[k]
  634. if line[0] - threshold <= next_row_next_col_point[0] <= line[0] + threshold:
  635. if not line[1] <= point[1] <= next_row_next_col_point[1] <= line[3]:
  636. if point[1] < line[1] <= next_row_next_col_point[1]:
  637. if next_row_next_col_point[1] - line[1] >= 1/3 * (next_row_next_col_point[1] - point[1]):
  638. col_lines[k][1] = point[1]
  639. return row_lines, col_lines
  640. def fix_inner(row_lines, col_lines, points, split_y):
  641. def fix(fix_lines, assist_lines, split_points, axis):
  642. new_points = []
  643. for line1 in fix_lines:
  644. min_assist_line = [[], []]
  645. min_distance = [1000, 1000]
  646. if_find = [0, 0]
  647. # 获取fix_line中的所有col point,里面可能不包括两个顶点,col point是交点,顶点可能不是交点
  648. fix_line_points = []
  649. for point in split_points:
  650. if abs(point[1-axis] - line1[1-axis]) <= 2:
  651. if line1[axis] <= point[axis] <= line1[axis+2]:
  652. fix_line_points.append(point)
  653. # 找出离两个顶点最近的assist_line, 并且assist_line与fix_line不相交
  654. line1_point = [line1[:2], line1[2:]]
  655. for i in range(2):
  656. point = line1_point[i]
  657. for line2 in assist_lines:
  658. if not if_find[i] and abs(point[axis] - line2[axis]) <= 2:
  659. if line1[1-axis] <= point[1-axis] <= line2[1-axis+2]:
  660. # print("line1, match line2", line1, line2)
  661. if_find[i] = 1
  662. break
  663. else:
  664. if abs(point[axis] - line2[axis]) < min_distance[i] and line2[1-axis] <= point[1-axis] <= line2[1-axis+2]:
  665. if line1[axis] <= line2[axis] <= line1[axis+2]:
  666. continue
  667. min_distance[i] = abs(line1[axis] - line2[axis])
  668. min_assist_line[i] = line2
  669. # 找出离assist_line最近的交点
  670. # 顶点到交点的距离(多出来的线)需大于assist_line到交点的距离(bbox的边)的1/3
  671. min_distance = [1000, 1000]
  672. min_col_point = [[], []]
  673. for i in range(2):
  674. # print("顶点", i, line1_point[i])
  675. if not if_find[i]:
  676. if min_assist_line[i]:
  677. for point in fix_line_points:
  678. if abs(point[axis] - min_assist_line[i][axis]) < min_distance[i]:
  679. min_distance[i] = abs(point[axis] - min_assist_line[i][axis])
  680. min_col_point[i] = point
  681. if min_col_point[i]:
  682. if abs(min_col_point[i][axis] - line1_point[i][axis]) >= abs(min_col_point[i][axis] - min_assist_line[i][axis])/3:
  683. add_point = (line1_point[i][1-axis], min_assist_line[i][axis])
  684. # print("fix_inner add point", add_point)
  685. # print("line1, line2", line1, min_assist_line[i])
  686. new_points.append(add_point)
  687. return new_points
  688. new_points = []
  689. for i in range(1, len(split_y)):
  690. last_y = split_y[i-1]
  691. y = split_y[i]
  692. # 先对点线进行分区
  693. split_row_lines = []
  694. split_col_lines = []
  695. split_points = []
  696. for row in row_lines:
  697. if last_y <= row[1] <= y:
  698. split_row_lines.append(row)
  699. for col in col_lines:
  700. if last_y <= col[1] <= y:
  701. split_col_lines.append(col)
  702. for point in points:
  703. if last_y <= point[1] <= y:
  704. split_points.append(point)
  705. new_points += fix(split_col_lines, split_row_lines, split_points, axis=1)
  706. new_points += fix(split_row_lines, split_col_lines, split_points, axis=0)
  707. # 找出所有col的顶点不在row上的、row的顶点不在col上的
  708. # for col in split_col_lines:
  709. # print("*"*30)
  710. #
  711. # # 获取该line中的所有point
  712. # col_points = []
  713. # for point in split_points:
  714. # if abs(point[0] - col[0]) <= 2:
  715. # if col[1] <= point[1] <= col[3]:
  716. # col_points.append(point)
  717. #
  718. # # 比较顶点
  719. # min_row_1 = []
  720. # min_row_2 = []
  721. # min_distance_1 = 1000
  722. # min_distance_2 = 1000
  723. # if_find_1 = 0
  724. # if_find_2 = 0
  725. # for row in split_row_lines:
  726. # # 第一个顶点
  727. # if not if_find_1 and abs(col[1] - row[1]) <= 2:
  728. # if row[0] <= col[0] <= row[2]:
  729. # print("col, match row", col, row)
  730. # if_find_1 = 1
  731. # break
  732. # else:
  733. # if abs(col[1] - row[1]) < min_distance_1 and row[0] <= col[0] <= row[2]:
  734. # if col[1] <= row[1] <= col[3]:
  735. # continue
  736. # min_distance_1 = abs(col[1] - row[1])
  737. # min_row_1 = row
  738. #
  739. # # 第二个顶点
  740. # if not if_find_2 and abs(col[3] - row[1]) <= 2:
  741. # if row[0] <= col[2] <= row[2]:
  742. # if_find_2 = 1
  743. # break
  744. # else:
  745. # if abs(col[3] - row[1]) < min_distance_2 and row[0] <= col[2] <= row[2]:
  746. # min_distance_2 = abs(col[3] - row[1])
  747. # min_row_2 = row
  748. #
  749. # if not if_find_1:
  750. # print("col", col)
  751. # print("min_row_1", min_row_1)
  752. # if min_row_1:
  753. # min_distance_1 = 1000
  754. # min_col_point = []
  755. # for point in col_points:
  756. # if abs(point[1] - min_row_1[1]) < min_distance_1:
  757. # min_distance_1 = abs(point[1] - min_row_1[1])
  758. # min_col_point = point
  759. #
  760. # if abs(min_col_point[1] - col[1]) >= abs(min_col_point[1] - min_row_1[1])/3:
  761. #
  762. # add_point = (col[0], min_row_1[1])
  763. # print("fix_inner add point", add_point)
  764. # new_points.append(add_point)
  765. # else:
  766. # print("distance too long", min_col_point, min_row_1)
  767. # print(abs(min_col_point[1] - col[1]), abs(min_col_point[1] - min_row_1[1])/3)
  768. return points+new_points
  769. def fix_corner(row_lines, col_lines, split_y):
  770. new_row_lines = []
  771. new_col_lines = []
  772. last_y = split_y[0]
  773. for y in split_y:
  774. if y == last_y:
  775. continue
  776. split_row_lines = []
  777. split_col_lines = []
  778. for row in row_lines:
  779. if last_y <= row[1] <= y or last_y <= row[3] <= y:
  780. split_row_lines.append(row)
  781. for col in col_lines:
  782. if last_y <= col[1] <= y or last_y <= col[3] <= y:
  783. split_col_lines.append(col)
  784. if not split_row_lines or not split_col_lines:
  785. last_y = y
  786. continue
  787. split_row_lines.sort(key=lambda x: (x[1], x[0]))
  788. split_col_lines.sort(key=lambda x: (x[0], x[1]))
  789. up_line = split_row_lines[0]
  790. bottom_line = split_row_lines[-1]
  791. left_line = split_col_lines[0]
  792. right_line = split_col_lines[-1]
  793. # 左上角
  794. if up_line[0:2] != left_line[0:2]:
  795. # print("up_line, left_line", up_line, left_line)
  796. add_corner = [left_line[0], up_line[1]]
  797. split_row_lines[0][0] = add_corner[0]
  798. split_col_lines[0][1] = add_corner[1]
  799. # 右上角
  800. if up_line[2:] != right_line[:2]:
  801. # print("up_line, right_line", up_line, right_line)
  802. add_corner = [right_line[0], up_line[1]]
  803. split_row_lines[0][2] = add_corner[0]
  804. split_col_lines[-1][1] = add_corner[1]
  805. new_row_lines = new_row_lines + split_row_lines
  806. new_col_lines = new_col_lines + split_col_lines
  807. last_y = y
  808. return new_row_lines, new_col_lines
  809. def delete_outline(row_lines, col_lines, points):
  810. row_lines.sort(key=lambda x: (x[1], x[0]))
  811. col_lines.sort(key=lambda x: (x[0], x[1]))
  812. line = [row_lines[0], row_lines[-1], col_lines[0], col_lines[-1]]
  813. threshold = 2
  814. point_cnt = [0, 0, 0, 0]
  815. for point in points:
  816. for i in range(4):
  817. if i < 2:
  818. if line[i][1]-threshold <= point[1] <= line[i][1]+threshold:
  819. if line[i][0] <= point[0] <= line[i][2]:
  820. point_cnt[i] += 1
  821. else:
  822. if line[i][0]-threshold <= point[0] <= line[i][0]+threshold:
  823. if line[i][1] <= point[1] <= line[i][3]:
  824. point_cnt[i] += 1
  825. # if line[0][1]-threshold <= point[1] <= line[0][1]+threshold:
  826. # if line[0][0] <= point[0] <= line[0][2]:
  827. # point_cnt[0] += 1
  828. # elif line[1][1]-threshold <= point[1] <= line[1][1]+threshold:
  829. # if line[1][0] <= point[0] <= line[1][2]:
  830. # point_cnt[1] += 1
  831. # elif line[2][0]-threshold <= point[0] <= line[2][0]+threshold:
  832. # if line[2][1] <= point[1] <= line[2][3]:
  833. # point_cnt[2] += 1
  834. # elif line[3][0]-threshold <= point[0] <= line[3][0]+threshold:
  835. # if line[3][1] <= point[1] <= line[3][3]:
  836. # point_cnt[3] += 1
  837. # 轮廓line至少包含3个交点
  838. for i in range(4):
  839. if point_cnt[i] < 3:
  840. if i < 2:
  841. if line[i] in row_lines:
  842. row_lines.remove(line[i])
  843. else:
  844. if line[i] in col_lines:
  845. col_lines.remove(line[i])
  846. return row_lines, col_lines
  847. def fix_outline(image, row_lines, col_lines, points, split_y):
  848. print("split_y", split_y)
  849. # 分割线纵坐标
  850. if len(split_y) < 2:
  851. return [], [], [], []
  852. # elif len(split_y) == 2:
  853. # split_y = [2000., 2000., 2000., 2000.]
  854. split_y.sort(key=lambda x: x)
  855. new_split_y = []
  856. for i in range(1, len(split_y), 2):
  857. new_split_y.append(int((split_y[i]+split_y[i-1])/2))
  858. # # 查看是否正确输出区域分割线
  859. # for line in split_y:
  860. # cv2.line(image, (0, int(line)), (int(image.shape[1]), int(line)), (0, 0, 255), 2)
  861. # cv2.imshow("split_y", image)
  862. # cv2.waitKey(0)
  863. # 预测线根据分割线纵坐标分为多个分割区域
  864. # row_lines.sort(key=lambda x: (x[3], x[2], x[1], x[0]))
  865. # col_lines.sort(key=lambda x: (x[3], x[2], x[1], x[0]))
  866. # points.sort(key=lambda x: (x[1], x[0]))
  867. # row_count = 0
  868. # col_count = 0
  869. # point_count = 0
  870. split_row_list = []
  871. split_col_list = []
  872. split_point_list = []
  873. # for i in range(1, len(split_y)):
  874. # y = split_y[i]
  875. # last_y = split_y[i-1]
  876. # row_lines = row_lines[row_count:]
  877. # col_lines = col_lines[col_count:]
  878. # points = points[point_count:]
  879. # row_count = 0
  880. # col_count = 0
  881. # point_count = 0
  882. #
  883. # if not row_lines:
  884. # split_row_list.append([])
  885. # for row in row_lines:
  886. # if last_y <= row[3] <= y:
  887. # row_count += 1
  888. # else:
  889. # split_row_list.append(row_lines[:row_count])
  890. # break
  891. # if row_count == len(row_lines):
  892. # split_row_list.append(row_lines[:row_count])
  893. # break
  894. #
  895. # if not col_lines:
  896. # split_col_list.append([])
  897. #
  898. # for col in col_lines:
  899. # # if last_y <= col[3] <= y:
  900. # if col[1] <= last_y <= y <= col[3] or last_y <= col[3] <= y:
  901. # # if last_y <= col[1] <= y or last_y <= col[3] <= y:
  902. # col_count += 1
  903. # else:
  904. # split_col_list.append(col_lines[:col_count])
  905. # break
  906. # if col_count == len(col_lines):
  907. # split_col_list.append(col_lines[:col_count])
  908. # break
  909. #
  910. # if not points:
  911. # split_point_list.append([])
  912. # for point in points:
  913. # if last_y <= point[1] <= y:
  914. # point_count += 1
  915. # else:
  916. # split_point_list.append(points[:point_count])
  917. # break
  918. # if point_count == len(points):
  919. # split_point_list.append(points[:point_count])
  920. # break
  921. #
  922. # # print("len(split_row_list)", len(split_row_list))
  923. # # print("len(split_col_list)", len(split_col_list))
  924. # if row_count < len(row_lines) - 1 and col_count < len(col_lines) - 1:
  925. # row_lines = row_lines[row_count:]
  926. # split_row_list.append(row_lines)
  927. # col_lines = col_lines[col_count:]
  928. # split_col_list.append(col_lines)
  929. #
  930. # if point_count < len(points) - 1:
  931. # points = points[point_count:len(points)]
  932. # split_point_list.append(points)
  933. for i in range(1, len(split_y)):
  934. y = split_y[i]
  935. last_y = split_y[i-1]
  936. split_row = []
  937. for row in row_lines:
  938. if last_y <= row[3] <= y:
  939. split_row.append(row)
  940. split_row_list.append(split_row)
  941. split_col = []
  942. for col in col_lines:
  943. if last_y <= col[1] <= y or last_y <= col[3] <= y or col[1] < last_y < y < col[3]:
  944. split_col.append(col)
  945. split_col_list.append(split_col)
  946. split_point = []
  947. for point in points:
  948. if last_y <= point[1] <= y:
  949. split_point.append(point)
  950. split_point_list.append(split_point)
  951. # 预测线取上下左右4个边(会有超出表格部分) [(), ()]
  952. area_row_line = []
  953. area_col_line = []
  954. for area in split_row_list:
  955. if not area:
  956. area_row_line.append([])
  957. continue
  958. area.sort(key=lambda x: (x[1], x[0]))
  959. up_line = area[0]
  960. bottom_line = area[-1]
  961. area_row_line.append([up_line, bottom_line])
  962. for area in split_col_list:
  963. if not area:
  964. area_col_line.append([])
  965. continue
  966. area.sort(key=lambda x: x[0])
  967. left_line = area[0]
  968. right_line = area[-1]
  969. area_col_line.append([left_line, right_line])
  970. # 线交点根据分割线纵坐标分为多个分割区域
  971. # points.sort(key=lambda x: (x[1], x[0]))
  972. # point_count = 0
  973. # split_point_list = []
  974. # for y in new_split_y:
  975. # points = points[point_count:len(points)]
  976. # point_count = 0
  977. # for point in points:
  978. # if point[1] <= y:
  979. # point_count += 1
  980. # else:
  981. # split_point_list.append(points[:point_count])
  982. # break
  983. # if point_count == len(points):
  984. # split_point_list.append(points[:point_count])
  985. # break
  986. # if point_count < len(points) - 1:
  987. # points = points[point_count:len(points)]
  988. # split_point_list.append(points)
  989. # print("len(split_point_list)", len(split_point_list))
  990. # 取每个分割区域的4条线(无超出表格部分)
  991. area_row_line2 = []
  992. area_col_line2 = []
  993. for area in split_point_list:
  994. if not area:
  995. area_row_line2.append([])
  996. area_col_line2.append([])
  997. continue
  998. area.sort(key=lambda x: (x[0], x[1]))
  999. left_up = area[0]
  1000. right_bottom = area[-1]
  1001. up_line = [left_up[0], left_up[1], right_bottom[0], left_up[1]]
  1002. bottom_line = [left_up[0], right_bottom[1], right_bottom[0], right_bottom[1]]
  1003. left_line = [left_up[0], left_up[1], left_up[0], right_bottom[1]]
  1004. right_line = [right_bottom[0], left_up[1], right_bottom[0], right_bottom[1]]
  1005. area_row_line2.append([up_line, bottom_line])
  1006. area_col_line2.append([left_line, right_line])
  1007. # 判断超出部分的长度,超出一定长度就补线
  1008. new_row_lines = []
  1009. new_col_lines = []
  1010. longer_row_lines = []
  1011. longer_col_lines = []
  1012. all_longer_row_lines = []
  1013. all_longer_col_lines = []
  1014. # print("split_y", split_y)
  1015. # print("split_row_list", split_row_list, len(split_row_list))
  1016. # print("split_row_list", split_col_list, len(split_col_list))
  1017. # print("area_row_line", area_row_line, len(area_row_line))
  1018. # print("area_col_line", area_col_line, len(area_col_line))
  1019. for i in range(len(area_row_line)):
  1020. if not area_row_line[i] or not area_col_line[i]:
  1021. continue
  1022. up_line = area_row_line[i][0]
  1023. up_line2 = area_row_line2[i][0]
  1024. bottom_line = area_row_line[i][1]
  1025. bottom_line2 = area_row_line2[i][1]
  1026. left_line = area_col_line[i][0]
  1027. left_line2 = area_col_line2[i][0]
  1028. right_line = area_col_line[i][1]
  1029. right_line2 = area_col_line2[i][1]
  1030. # 计算单格高度宽度
  1031. if len(split_row_list[i]) > 1:
  1032. height_dict = {}
  1033. for j in range(len(split_row_list[i])):
  1034. if j + 1 > len(split_row_list[i]) - 1:
  1035. break
  1036. height = abs(int(split_row_list[i][j][3] - split_row_list[i][j+1][3]))
  1037. if height in height_dict.keys():
  1038. height_dict[height] = height_dict[height] + 1
  1039. else:
  1040. height_dict[height] = 1
  1041. height_list = [[x, height_dict[x]] for x in height_dict.keys()]
  1042. height_list.sort(key=lambda x: (x[1], -x[0]), reverse=True)
  1043. # print("height_list", height_list)
  1044. box_height = height_list[0][0]
  1045. else:
  1046. box_height = 10
  1047. if len(split_col_list[i]) > 1:
  1048. box_width = abs(split_col_list[i][1][2] - split_col_list[i][0][2])
  1049. else:
  1050. box_width = 10
  1051. print("box_height", box_height, "box_width", box_width)
  1052. # cv2.line(image, (int(up_line[0]), int(up_line[1])),
  1053. # (int(up_line[2]), int(up_line[3])),
  1054. # (255, 255, 0), 2)
  1055. # cv2.line(image, (int(right_line[0]), int(right_line[1])),
  1056. # (int(right_line[2]), int(right_line[3])),
  1057. # (0, 255, 255), 2)
  1058. # cv2.imshow("right_line", image)
  1059. # cv2.waitKey(0)
  1060. # 补左右两条竖线超出来的线的row
  1061. if (up_line[1] - left_line[1] >= 10 and up_line[1] - right_line[1] >= 2) or \
  1062. (up_line[1] - left_line[1] >= 2 and up_line[1] - right_line[1] >= 10):
  1063. if up_line[1] - left_line[1] >= up_line[1] - right_line[1]:
  1064. new_row_lines.append([left_line[0], left_line[1], right_line[0], left_line[1]])
  1065. new_col_y = left_line[1]
  1066. # 补了row,要将其他短的col连到row上
  1067. for j in range(len(split_col_list[i])):
  1068. col = split_col_list[i][j]
  1069. # 且距离不能相差大于一格
  1070. # print("abs(new_col_y - col[1])", abs(new_col_y - col[1]))
  1071. if abs(new_col_y - col[1]) <= box_height:
  1072. split_col_list[i][j][1] = min([new_col_y, col[1]])
  1073. longer_col_lines.append([col[0], min([new_col_y, col[1]]), col[2], col[3]])
  1074. else:
  1075. new_row_lines.append([left_line[0], right_line[1], right_line[0], right_line[1]])
  1076. new_col_y = right_line[1]
  1077. # 补了row,要将其他短的col连到row上
  1078. for j in range(len(split_col_list[i])):
  1079. # 需判断该线在这个区域中
  1080. # if up_line2[1]-3 <= col[1] <= col[3] <= bottom_line2[1]+3:
  1081. col = split_col_list[i][j]
  1082. # 且距离不能相差太大
  1083. # print("abs(new_col_y - col[1])", abs(new_col_y - col[1]))
  1084. if abs(new_col_y - col[1]) <= box_height:
  1085. split_col_list[i][j][1] = min([new_col_y, col[1]])
  1086. if (left_line[3] - bottom_line[3] >= 10 and right_line[3] - bottom_line[3] >= 2) or \
  1087. (left_line[3] - bottom_line[3] >= 2 and right_line[3] - bottom_line[3] >= 10):
  1088. if left_line[3] - bottom_line[3] >= right_line[3] - bottom_line[3]:
  1089. new_row_lines.append([left_line[2], left_line[3], right_line[2], left_line[3]])
  1090. new_col_y = left_line[3]
  1091. # 补了row,要将其他短的col连到row上
  1092. for j in range(len(split_col_list[i])):
  1093. col = split_col_list[i][j]
  1094. # 且距离不能相差太大
  1095. if abs(new_col_y - col[3]) <= box_height:
  1096. split_col_list[i][j][3] = max([new_col_y, col[3]])
  1097. else:
  1098. new_row_lines.append([left_line[2], right_line[3], right_line[2], right_line[3]])
  1099. new_col_y = right_line[3]
  1100. # 补了row,要将其他短的col连到row上
  1101. for j in range(len(split_col_list[i])):
  1102. col = split_col_list[i][j]
  1103. # 且距离不能相差太大
  1104. if abs(new_col_y - col[3]) <= box_height:
  1105. split_col_list[i][j][3] = max([new_col_y, col[3]])
  1106. # 补上下两条横线超出来的线的col
  1107. if (left_line[0] - up_line[0] >= 10 and left_line[0] - bottom_line[0] >= 2) or \
  1108. (left_line[0] - up_line[0] >= 2 and left_line[0] - bottom_line[0] >= 10):
  1109. if left_line[0] - up_line[0] >= left_line[0] - bottom_line[0]:
  1110. new_col_lines.append([up_line[0], up_line[1], up_line[0], bottom_line[1]])
  1111. new_row_x = up_line[0]
  1112. # 补了col,要将其他短的row连到col上
  1113. for j in range(len(split_row_list[i])):
  1114. row = split_row_list[i][j]
  1115. # 且距离不能相差太大
  1116. if abs(new_row_x - row[0]) <= box_width:
  1117. split_row_list[i][j][0] = min([new_row_x, row[0]])
  1118. else:
  1119. new_col_lines.append([bottom_line[0], up_line[1], bottom_line[0], bottom_line[1]])
  1120. new_row_x = bottom_line[0]
  1121. # 补了col,要将其他短的row连到col上
  1122. for j in range(len(split_row_list[i])):
  1123. row = split_row_list[i][j]
  1124. # 且距离不能相差太大
  1125. if abs(new_row_x - row[0]) <= box_width:
  1126. split_row_list[i][j][0] = min([new_row_x, row[0]])
  1127. if (up_line[2] - right_line[2] >= 10 and bottom_line[2] - right_line[2] >= 2) or \
  1128. (up_line[2] - right_line[2] >= 2 and bottom_line[2] - right_line[2] >= 10):
  1129. if up_line[2] - right_line[2] >= bottom_line[2] - right_line[2]:
  1130. new_col_lines.append([up_line[2], up_line[3], up_line[2], bottom_line[3]])
  1131. new_row_x = up_line[2]
  1132. # 补了col,要将其他短的row连到col上
  1133. for j in range(len(split_row_list[i])):
  1134. row = split_row_list[i][j]
  1135. # 且距离不能相差太大
  1136. if abs(new_row_x - row[2]) <= box_width:
  1137. split_row_list[i][j][2] = max([new_row_x, row[2]])
  1138. else:
  1139. new_col_lines.append([bottom_line[2], up_line[3], bottom_line[2], bottom_line[3]])
  1140. new_row_x = bottom_line[2]
  1141. # 补了col,要将其他短的row连到col上
  1142. for j in range(len(split_row_list[i])):
  1143. # 需判断该线在这个区域中
  1144. # if up_line2[1]-3 <= row[1] <= bottom_line2[1]+3:
  1145. row = split_row_list[i][j]
  1146. # 且距离不能相差太大
  1147. if abs(new_row_x - row[2]) <= box_width:
  1148. split_row_list[i][j][2] = max([new_row_x, row[2]])
  1149. all_longer_row_lines += split_row_list[i]
  1150. all_longer_col_lines += split_col_list[i]
  1151. # print("all_longer_row_lines", len(all_longer_row_lines), i)
  1152. # print("all_longer_col_lines", len(all_longer_col_lines), i)
  1153. # print("new_row_lines", len(new_row_lines), i)
  1154. # print("new_col_lines", len(new_col_lines), i)
  1155. # 删除表格内部的补线
  1156. # temp_list = []
  1157. # for row in new_row_lines:
  1158. # if up_line[1]-5 <= row[1] <= bottom_line[1]+5:
  1159. # continue
  1160. # temp_list.append(row)
  1161. # print("fix_outline", new_row_lines)
  1162. # new_row_lines = temp_list
  1163. # print("fix_outline", new_row_lines)
  1164. # temp_list = []
  1165. # for col in new_col_lines:
  1166. # if left_line[0]-5 <= col[0] <= right_line[0]+5:
  1167. # continue
  1168. # temp_list.append(col)
  1169. #
  1170. # new_col_lines = temp_list
  1171. # print("fix_outline", new_col_lines)
  1172. # print("fix_outline", new_row_lines)
  1173. # 删除重复包含的补线
  1174. # temp_list = []
  1175. # for row in new_row_lines:
  1176. # if up_line[1]-5 <= row[1] <= bottom_line[1]+5:
  1177. # continue
  1178. # temp_list.append(row)
  1179. # new_row_lines = temp_list
  1180. # 展示上下左右边框线
  1181. # for i in range(len(area_row_line)):
  1182. # print("row1", area_row_line[i])
  1183. # print("row2", area_row_line2[i])
  1184. # print("col1", area_col_line[i])
  1185. # print("col2", area_col_line2[i])
  1186. # cv2.line(image, (int(area_row_line[i][0][0]), int(area_row_line[i][0][1])),
  1187. # (int(area_row_line[i][0][2]), int(area_row_line[i][0][3])), (0, 255, 0), 2)
  1188. # cv2.line(image, (int(area_row_line2[i][1][0]), int(area_row_line2[i][1][1])),
  1189. # (int(area_row_line2[i][1][2]), int(area_row_line2[i][1][3])), (0, 0, 255), 2)
  1190. # cv2.imshow("fix_outline", image)
  1191. # cv2.waitKey(0)
  1192. # 展示所有线
  1193. # for line in all_longer_col_lines:
  1194. # cv2.line(image, (int(line[0]), int(line[1])),
  1195. # (int(line[2]), int(line[3])),
  1196. # (0, 255, 0), 2)
  1197. # cv2.imshow("fix_outline", image)
  1198. # cv2.waitKey(0)
  1199. # for line in all_longer_row_lines:
  1200. # cv2.line(image, (int(line[0]), int(line[1])),
  1201. # (int(line[2]), int(line[3])),
  1202. # (0, 0, 255), 2)
  1203. # cv2.imshow("fix_outline", image)
  1204. # cv2.waitKey(0)
  1205. return new_row_lines, new_col_lines, all_longer_row_lines, all_longer_col_lines
  1206. def fix_table(row_point_list, col_point_list, split_y, row_lines, col_lines):
  1207. # 分割线纵坐标
  1208. if len(split_y) < 2:
  1209. return []
  1210. # 获取bbox
  1211. bbox = []
  1212. # 每个点获取与其x最相近和y最相近的点
  1213. for i in range(1, len(split_y)):
  1214. # 循环每行
  1215. for row in row_point_list:
  1216. row.sort(key=lambda x: (x[0], x[1]))
  1217. # 行不在该区域跳过
  1218. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  1219. continue
  1220. # print("len(row)", len(row))
  1221. # print("row", row)
  1222. # 循环行中的点
  1223. for j in range(len(row)):
  1224. if j == len(row) - 1:
  1225. break
  1226. current_point = row[j]
  1227. next_point_in_row_list = row[j+1:]
  1228. # 循环这一行的下一个点
  1229. for next_point_in_row in next_point_in_row_list:
  1230. # 是否在这一行点找到,找不到就这一行的下个点
  1231. not_found = 1
  1232. # 查询下个点所在列
  1233. next_col = []
  1234. for col in col_point_list:
  1235. col.sort(key=lambda x: (x[1], x[0]))
  1236. # 列不在该区域跳过
  1237. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  1238. continue
  1239. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  1240. next_col = col
  1241. break
  1242. # 循环匹配当前点和下一列点
  1243. next_col.sort(key=lambda x: (x[1], x[0]))
  1244. for point1 in next_col:
  1245. # 同一行的就跳过
  1246. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  1247. continue
  1248. if point1[1] <= current_point[1]-3:
  1249. continue
  1250. # 候选bbox
  1251. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  1252. # print("candidate_bbox", candidate_bbox)
  1253. # 判断该bbox是否存在,判断bbox的上下两条边是否有包含在row中
  1254. contain_flag1 = 0
  1255. contain_flag2 = 0
  1256. for row1 in row_lines:
  1257. # 行不在该区域跳过
  1258. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  1259. continue
  1260. # bbox上边框 y一样
  1261. if not contain_flag1:
  1262. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  1263. # 格子里的断开线段
  1264. row1_break = (max([row1[0], candidate_bbox[0]]),
  1265. row1[1],
  1266. min([row1[2], candidate_bbox[2]]),
  1267. row1[3])
  1268. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1269. contain_flag1 = 1
  1270. # bbox下边框 y一样
  1271. if not contain_flag2:
  1272. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  1273. # 格子里的断开线段
  1274. row1_break = (max([row1[0], candidate_bbox[0]]),
  1275. row1[1],
  1276. min([row1[2], candidate_bbox[2]]),
  1277. row1[3])
  1278. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1279. contain_flag2 = 1
  1280. # 判断该bbox是否存在,判断bbox的左右两条边是否有包含在col中
  1281. contain_flag3 = 0
  1282. contain_flag4 = 0
  1283. for col1 in col_lines:
  1284. # 列不在该区域跳过
  1285. if col1[1] <= split_y[i-1] or col1[3] >= split_y[i]:
  1286. continue
  1287. # bbox左边线 x一样
  1288. if not contain_flag3:
  1289. if col1[0]-3 <= candidate_bbox[0] <= col1[0]+3:
  1290. # 格子里的断开线段
  1291. col1_break = (col1[0],
  1292. max([col1[1], candidate_bbox[1]]),
  1293. col1[2],
  1294. min([col1[3], candidate_bbox[3]]))
  1295. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1296. contain_flag3 = 1
  1297. # bbox右边框 x一样
  1298. if not contain_flag4:
  1299. if col1[0]-3 <= candidate_bbox[2] <= col1[0]+3:
  1300. # 格子里的断开线段
  1301. col1_break = (col1[0],
  1302. max([col1[1], candidate_bbox[1]]),
  1303. col1[2],
  1304. min([col1[3], candidate_bbox[3]]))
  1305. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1306. contain_flag4 = 1
  1307. # 找到了该bbox,并且是存在的
  1308. if contain_flag1 and contain_flag2 and contain_flag3 and contain_flag4:
  1309. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  1310. (candidate_bbox[2], candidate_bbox[3])])
  1311. not_found = 0
  1312. break
  1313. if not not_found:
  1314. break
  1315. return bbox
  1316. def delete_close_points(point_list, row_point_list, col_point_list, threshold=5):
  1317. new_point_list = []
  1318. delete_point_list = []
  1319. point_list.sort(key=lambda x: (x[1], x[0]))
  1320. for i in range(len(point_list)):
  1321. point1 = point_list[i]
  1322. if point1 in delete_point_list:
  1323. continue
  1324. if i == len(point_list) - 1:
  1325. new_point_list.append(point1)
  1326. break
  1327. point2 = point_list[i+1]
  1328. # 判断坐标
  1329. if abs(point1[0] - point2[0]) > threshold or abs(point1[1] - point2[1]) > threshold:
  1330. new_point_list.append(point1)
  1331. else:
  1332. # 看两个点上的相同坐标点哪个多,就保留哪个
  1333. count1 = 0
  1334. count2 = 0
  1335. for col in col_point_list:
  1336. if point1[0] == col[0][0]:
  1337. count1 += len(col)
  1338. elif point2[0] == col[0][0]:
  1339. count2 += len(col)
  1340. if count1 >= count2:
  1341. new_point_list.append(point1)
  1342. delete_point_list.append(point2)
  1343. else:
  1344. new_point_list.append(point2)
  1345. delete_point_list.append(point1)
  1346. point_list = new_point_list
  1347. new_point_list = []
  1348. delete_point_list = []
  1349. point_list.sort(key=lambda x: (x[0], x[1]))
  1350. for i in range(len(point_list)):
  1351. point1 = point_list[i]
  1352. if point1 in delete_point_list:
  1353. continue
  1354. if i == len(point_list) - 1:
  1355. new_point_list.append(point1)
  1356. break
  1357. point2 = point_list[i+1]
  1358. # 判断坐标
  1359. if abs(point1[0] - point2[0]) > threshold or abs(point1[1] - point2[1]) > threshold:
  1360. new_point_list.append(point1)
  1361. else:
  1362. count1 = 0
  1363. count2 = 0
  1364. for row in row_point_list:
  1365. if point1[0] == row[0][0]:
  1366. count1 += len(row)
  1367. elif point2[0] == row[0][0]:
  1368. count2 += len(row)
  1369. if count1 >= count2:
  1370. new_point_list.append(point1)
  1371. delete_point_list.append(point2)
  1372. else:
  1373. new_point_list.append(point2)
  1374. delete_point_list.append(point1)
  1375. return new_point_list
  1376. def get_bbox2(image_np, points):
  1377. # # 坐标点按行分
  1378. # row_point_list = []
  1379. # row_point = []
  1380. # points.sort(key=lambda x: (x[0], x[1]))
  1381. # for p in points:
  1382. # if len(row_point) == 0:
  1383. # x = p[0]
  1384. # if x-5 <= p[0] <= x+5:
  1385. # row_point.append(p)
  1386. # else:
  1387. # row_point_list.append(row_point)
  1388. # row_point = []
  1389. # # 坐标点按列分
  1390. # col_point_list = []
  1391. # col_point = []
  1392. # points.sort(key=lambda x: (x[1], x[0]))
  1393. # for p in points:
  1394. # if len(col_point) == 0:
  1395. # y = p[1]
  1396. # if y-5 <= p[1] <= y+5:
  1397. # col_point.append(p)
  1398. # else:
  1399. # col_point_list.append(col_point)
  1400. # col_point = []
  1401. row_point_list = get_points_row(points)
  1402. col_point_list = get_points_col(points)
  1403. print("len(points)", len(points))
  1404. for point in points:
  1405. cv2.circle(image_np, point, 1, (0, 255, 0), 1)
  1406. cv2.imshow("points_deleted", image_np)
  1407. points = delete_close_points(points, row_point_list, col_point_list)
  1408. print("len(points)", len(points))
  1409. for point in points:
  1410. cv2.circle(image_np, point, 1, (255, 0, 0), 3)
  1411. cv2.imshow("points_deleted", image_np)
  1412. cv2.waitKey(0)
  1413. row_point_list = get_points_row(points, 5)
  1414. col_point_list = get_points_col(points, 5)
  1415. print("len(row_point_list)", len(row_point_list))
  1416. for row in row_point_list:
  1417. print("row", len(row))
  1418. print("col_point_list", len(col_point_list))
  1419. for col in col_point_list:
  1420. print("col", len(col))
  1421. bbox = []
  1422. for i in range(len(row_point_list)):
  1423. if i == len(row_point_list) - 1:
  1424. break
  1425. # 遍历每个row的point,找到其所在列的下一个点和所在行的下一个点
  1426. current_row = row_point_list[i]
  1427. for j in range(len(current_row)):
  1428. current_point = current_row[j]
  1429. if j == len(current_row) - 1:
  1430. break
  1431. next_row_point = current_row[j+1]
  1432. # 找出当前点所在的col,得到该列下一个point
  1433. current_col = col_point_list[j]
  1434. for k in range(len(current_col)):
  1435. if current_col[k][1] > current_point[1] + 10:
  1436. next_col_point = current_col[k]
  1437. break
  1438. next_row = row_point_list[k]
  1439. for k in range(len(next_row)):
  1440. if next_row[k][0] >= next_row_point[0] + 5:
  1441. next_point = next_row[k]
  1442. break
  1443. # 得到bbox
  1444. bbox.append([(current_point[0], current_point[1]), (next_point[0], next_point[1])])
  1445. # bbox = []
  1446. # for p in points:
  1447. # # print("p", p)
  1448. # p_row = []
  1449. # p_col = []
  1450. # for row in row_point_list:
  1451. # if p[0] == row[0][0]:
  1452. # for p1 in row:
  1453. # if abs(p[1]-p1[1]) <= 5:
  1454. # continue
  1455. # p_row.append([p1, abs(p[1]-p1[1])])
  1456. # p_row.sort(key=lambda x: x[1])
  1457. # for col in col_point_list:
  1458. # if p[1] == col[0][1]:
  1459. # for p2 in col:
  1460. # if abs(p[0]-p2[0]) <= 5:
  1461. # continue
  1462. # p_col.append([p2, abs(p[0]-p2[0])])
  1463. # p_col.sort(key=lambda x: x[1])
  1464. # if len(p_row) == 0 or len(p_col) == 0:
  1465. # continue
  1466. # break_flag = 0
  1467. # for i in range(len(p_row)):
  1468. # for j in range(len(p_col)):
  1469. # # print(p_row[i][0])
  1470. # # print(p_col[j][0])
  1471. # another_point = (p_col[j][0][0], p_row[i][0][1])
  1472. # # print("another_point", another_point)
  1473. # if abs(p[0]-another_point[0]) <= 5 or abs(p[1]-another_point[1]) <= 5:
  1474. # continue
  1475. # if p[0] >= another_point[0] or p[1] >= another_point[1]:
  1476. # continue
  1477. # if another_point in points:
  1478. # box = [p, another_point]
  1479. # box.sort(key=lambda x: x[0])
  1480. # if box not in bbox:
  1481. # bbox.append(box)
  1482. # break_flag = 1
  1483. # break
  1484. # if break_flag:
  1485. # break
  1486. #
  1487. # # delete duplicate
  1488. # delete_bbox = []
  1489. # for i in range(len(bbox)):
  1490. # for j in range(i+1, len(bbox)):
  1491. # if bbox[i][0] == bbox[j][0]:
  1492. # if bbox[i][1][0] - bbox[j][1][0] <= 3 \
  1493. # and bbox[i][1][1] - bbox[j][1][1] <= 3:
  1494. # delete_bbox.append(bbox[j])
  1495. # if bbox[i][1] == bbox[j][1]:
  1496. # if bbox[i][0][0] - bbox[j][0][0] <= 3 \
  1497. # and bbox[i][0][1] - bbox[j][0][1] <= 3:
  1498. # delete_bbox.append(bbox[j])
  1499. # # delete too small area
  1500. # # for box in bbox:
  1501. # # if box[1][0] - box[0][0] <=
  1502. # for d_box in delete_bbox:
  1503. # if d_box in bbox:
  1504. # bbox.remove(d_box)
  1505. # print bbox
  1506. bbox.sort(key=lambda x: (x[0][0], x[0][1], x[1][0], x[1][1]))
  1507. # origin bbox
  1508. # origin_bbox = []
  1509. # for box in bbox:
  1510. # origin_bbox.append([(box[0][0], box[0][1] - 40), (box[1][0], box[1][1] - 40)])
  1511. # for box in origin_bbox:
  1512. # cv2.rectangle(origin_image, box[0], box[1], (0, 0, 255), 2, 8)
  1513. # cv2.imshow('AlanWang', origin_image)
  1514. # cv2.waitKey(0)
  1515. for box in bbox:
  1516. cv2.rectangle(image_np, box[0], box[1], (0, 0, 255), 2, 8)
  1517. cv2.imshow('bboxes', image_np)
  1518. cv2.waitKey(0)
  1519. # for point in points:
  1520. # print(point)
  1521. # cv2.circle(image_np, point, 1, (0, 0, 255), 3)
  1522. # cv2.imshow('points', image_np)
  1523. # cv2.waitKey(0)
  1524. return bbox
  1525. def get_bbox1(image_np, points, split_y):
  1526. # 分割线纵坐标
  1527. # print("split_y", split_y)
  1528. if len(split_y) < 2:
  1529. return []
  1530. # 计算行列,剔除相近交点
  1531. row_point_list = get_points_row(points)
  1532. col_point_list = get_points_col(points)
  1533. print("len(row_point_list)", row_point_list)
  1534. print("len(col_point_list)", len(col_point_list))
  1535. # for point in points:
  1536. # cv2.circle(image_np, point, 1, (0, 255, 0), 1)
  1537. # cv2.imshow("points", image_np)
  1538. points = delete_close_points(points, row_point_list, col_point_list)
  1539. # print("len(points)", len(points))
  1540. # for point in points:
  1541. # cv2.circle(image_np, point, 1, (255, 0, 0), 3)
  1542. # cv2.imshow("points_deleted", image_np)
  1543. # cv2.waitKey(0)
  1544. # 获取bbox
  1545. bbox = []
  1546. # 每个点获取与其x最相近和y最相近的点
  1547. for i in range(1, len(split_y)):
  1548. for point1 in points:
  1549. if point1[1] <= split_y[i-1] or point1[1] >= split_y[i]:
  1550. continue
  1551. distance_x = 10000
  1552. distance_y = 10000
  1553. x = 0
  1554. y = 0
  1555. threshold = 10
  1556. for point2 in points:
  1557. if point2[1] <= split_y[i-1] or point2[1] >= split_y[i]:
  1558. continue
  1559. # 最近 x y
  1560. if 2 < point2[0] - point1[0] < distance_x and point2[1] - point1[1] <= threshold:
  1561. distance_x = point2[0] - point1[0]
  1562. x = point2[0]
  1563. if 2 < point2[1] - point1[1] < distance_y and point2[0] - point1[0] <= threshold:
  1564. distance_y = point2[1] - point1[1]
  1565. y = point2[1]
  1566. if not x or not y:
  1567. continue
  1568. bbox.append([(point1[0], point1[1]), (x, y)])
  1569. # 删除包含关系bbox
  1570. temp_list = []
  1571. for i in range(len(bbox)):
  1572. box1 = bbox[i]
  1573. for j in range(len(bbox)):
  1574. if i == j:
  1575. continue
  1576. box2 = bbox[j]
  1577. contain_flag = 0
  1578. if box2[0][0] <= box1[0][0] <= box1[1][0] <= box2[1][0] and \
  1579. box2[0][1] <= box1[0][1] <= box1[1][1] <= box2[1][1]:
  1580. contain_flag = 1
  1581. break
  1582. temp_list.append(box1)
  1583. bbox = temp_list
  1584. # 展示
  1585. for box in bbox:
  1586. # print(box[0], box[1])
  1587. # if abs(box[0][1] - box[1][1]) > abs(box[0][0] - box[1][0]):
  1588. # continue
  1589. cv2.rectangle(image_np, box[0], box[1], (0, 0, 255), 2, 8)
  1590. cv2.imshow('bboxes', image_np)
  1591. cv2.waitKey(0)
  1592. return bbox
  1593. def get_bbox0(image_np, row_point_list, col_point_list, split_y, row_lines, col_lines):
  1594. # 分割线纵坐标
  1595. if len(split_y) < 2:
  1596. return []
  1597. # 计算行列,剔除相近交点
  1598. # row_point_list = get_points_row(points)
  1599. # col_point_list = get_points_col(points)
  1600. # points = delete_close_points(points, row_point_list, col_point_list)
  1601. # row_point_list = get_points_row(points)
  1602. # col_point_list = get_points_col(points)
  1603. # 获取bbox
  1604. bbox = []
  1605. # print("get_bbox split_y", split_y)
  1606. # 每个点获取与其x最相近和y最相近的点
  1607. for i in range(1, len(split_y)):
  1608. # 循环每行
  1609. for row in row_point_list:
  1610. row.sort(key=lambda x: (x[0], x[1]))
  1611. # 行不在该区域跳过
  1612. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  1613. continue
  1614. # 循环行中的点
  1615. for j in range(len(row)):
  1616. if j == len(row) - 1:
  1617. break
  1618. current_point = row[j]
  1619. next_point_in_row = row[j+1]
  1620. # 查询下个点所在列
  1621. next_col = []
  1622. for col in col_point_list:
  1623. col.sort(key=lambda x: (x[1], x[0]))
  1624. # 列不在该区域跳过
  1625. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  1626. continue
  1627. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  1628. next_col = col
  1629. break
  1630. # 循环匹配当前点和下一列点
  1631. for point1 in next_col:
  1632. # 同一行的就跳过
  1633. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  1634. continue
  1635. if point1[1] <= current_point[1]-3:
  1636. continue
  1637. # 候选bbox
  1638. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  1639. # 判断该bbox是否存在,线条包含关系
  1640. contain_flag1 = 0
  1641. contain_flag2 = 0
  1642. for row1 in row_lines:
  1643. # 行不在该区域跳过
  1644. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  1645. continue
  1646. # bbox上边框 y一样
  1647. if not contain_flag1:
  1648. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  1649. # candidate的x1,x2需被包含在row线中
  1650. if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1651. contain_flag1 = 1
  1652. # bbox下边框 y一样
  1653. if not contain_flag2:
  1654. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  1655. # candidate的x1,x2需被包含在row线中
  1656. if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1657. contain_flag2 = 1
  1658. # 找到了该bbox,并且是存在的
  1659. if contain_flag1 and contain_flag2:
  1660. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  1661. (candidate_bbox[2], candidate_bbox[3])])
  1662. break
  1663. return bbox
  1664. def get_bbox3(image_np, row_point_list, col_point_list, split_y, row_lines, col_lines):
  1665. # 分割线纵坐标
  1666. if len(split_y) < 2:
  1667. return []
  1668. # 获取bbox
  1669. bbox = []
  1670. # 每个点获取与其x最相近和y最相近的点
  1671. for i in range(1, len(split_y)):
  1672. # 循环每行
  1673. for row in row_point_list:
  1674. row.sort(key=lambda x: (x[0], x[1]))
  1675. # 行不在该区域跳过
  1676. if row[0][1] <= split_y[i-1] or row[0][1] >= split_y[i]:
  1677. continue
  1678. # print("len(row)", len(row))
  1679. # print("row", row)
  1680. # 循环行中的点
  1681. for j in range(len(row)):
  1682. if j == len(row) - 1:
  1683. break
  1684. current_point = row[j]
  1685. # print("current_point", current_point)
  1686. next_point_in_row_list = row[j+1:]
  1687. # 循环这一行的下一个点
  1688. for next_point_in_row in next_point_in_row_list:
  1689. # 是否在这一行点找到,找不到就这一行的下个点
  1690. not_found = 1
  1691. # 查询下个点所在列
  1692. next_col = []
  1693. for col in col_point_list:
  1694. col.sort(key=lambda x: (x[1], x[0]))
  1695. # 列不在该区域跳过
  1696. if col[0][1] <= split_y[i-1] or col[-1][1] >= split_y[i]:
  1697. continue
  1698. if col[0][0]-3 <= next_point_in_row[0] <= col[0][0]+3:
  1699. next_col = col
  1700. break
  1701. # 循环匹配当前点和下一列点
  1702. next_col.sort(key=lambda x: (x[1], x[0]))
  1703. for point1 in next_col:
  1704. # 同一行的就跳过
  1705. if current_point[1]-3 <= point1[1] <= current_point[1]+3:
  1706. continue
  1707. if point1[1] <= current_point[1]-3:
  1708. continue
  1709. # 候选bbox
  1710. candidate_bbox = [current_point[0], current_point[1], point1[0], point1[1]]
  1711. # print("candidate_bbox", candidate_bbox)
  1712. # 判断该bbox是否存在,判断bbox的上下两条边是否有包含在row中
  1713. contain_flag1 = 0
  1714. contain_flag2 = 0
  1715. for row1 in row_lines:
  1716. # 行不在该区域跳过
  1717. if row1[1] <= split_y[i-1] or row1[1] >= split_y[i]:
  1718. continue
  1719. # bbox上边框 y一样
  1720. if not contain_flag1:
  1721. if row1[1]-3 <= candidate_bbox[1] <= row1[1]+3:
  1722. # 格子里的断开线段
  1723. row1_break = (max([row1[0], candidate_bbox[0]]),
  1724. row1[1],
  1725. min([row1[2], candidate_bbox[2]]),
  1726. row1[3])
  1727. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1728. contain_flag1 = 1
  1729. # # candidate的x1,x2需被包含在row线中
  1730. # if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1731. # contain_flag1 = 1
  1732. #
  1733. # # 判断线条有无端点在格子中
  1734. # elif candidate_bbox[0] < row1[0] < candidate_bbox[2] \
  1735. # or candidate_bbox[0] < row1[2] < candidate_bbox[2]:
  1736. # # 线条会有缺一点情况,判断长度超过格子一半
  1737. # if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1738. # contain_flag1 = 1
  1739. # bbox下边框 y一样
  1740. if not contain_flag2:
  1741. if row1[1]-3 <= candidate_bbox[3] <= row1[1]+3:
  1742. # 格子里的断开线段
  1743. row1_break = (max([row1[0], candidate_bbox[0]]),
  1744. row1[1],
  1745. min([row1[2], candidate_bbox[2]]),
  1746. row1[3])
  1747. if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1748. contain_flag2 = 1
  1749. # # candidate的x1,x2需被包含在row线中
  1750. # if row1[0]-3 <= candidate_bbox[0] <= candidate_bbox[2] <= row1[2]+3:
  1751. # contain_flag2 = 1
  1752. #
  1753. # # 判断线条有无端点在格子中
  1754. # elif candidate_bbox[0] < row1[0] < candidate_bbox[2] \
  1755. # or candidate_bbox[0] < row1[2] < candidate_bbox[2]:
  1756. # # 线条会有缺一点情况,判断长度超过格子一半
  1757. # if row1_break[2] - row1_break[0] >= (candidate_bbox[2] - candidate_bbox[0])/3:
  1758. # contain_flag2 = 1
  1759. # 判断该bbox是否存在,判断bbox的左右两条边是否有包含在col中
  1760. contain_flag3 = 0
  1761. contain_flag4 = 0
  1762. for col1 in col_lines:
  1763. # 列不在该区域跳过
  1764. if col1[1] <= split_y[i-1] or col1[3] >= split_y[i]:
  1765. continue
  1766. # bbox左边线 x一样
  1767. if not contain_flag3:
  1768. if col1[0]-3 <= candidate_bbox[0] <= col1[0]+3:
  1769. # 格子里的断开线段
  1770. col1_break = (col1[0],
  1771. max([col1[1], candidate_bbox[1]]),
  1772. col1[2],
  1773. min([col1[3], candidate_bbox[3]]))
  1774. if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1775. contain_flag3 = 1
  1776. # # candidate的y1,y2需被包含在col线中
  1777. # if col1[1]-3 <= candidate_bbox[1] <= candidate_bbox[3] <= col1[3]+3:
  1778. # contain_flag3 = 1
  1779. #
  1780. # # 判断线条有无端点在格子中
  1781. # elif candidate_bbox[1] < col1[1] < candidate_bbox[3] \
  1782. # or candidate_bbox[1] < col1[3] < candidate_bbox[3]:
  1783. # # 线条会有缺一点情况,判断长度超过格子一半
  1784. # if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1785. # contain_flag3 = 1
  1786. # bbox右边框 x一样
  1787. if not contain_flag4:
  1788. if col1[0]-3 <= candidate_bbox[2] <= col1[0]+3:
  1789. # 格子里的断开线段
  1790. # col1_break = (col1[0],
  1791. # max([col1[1], candidate_bbox[1]]),
  1792. # col1[2],
  1793. # min([col1[3], candidate_bbox[3]]))
  1794. # if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1795. # contain_flag4 = 1
  1796. # 如果候选bbox的边的上1/3或下1/3包含在col中
  1797. candidate_bbox_line1 = [candidate_bbox[1],
  1798. candidate_bbox[1] + (candidate_bbox[3]-candidate_bbox[1])/3]
  1799. candidate_bbox_line2 = [candidate_bbox[3] - (candidate_bbox[3]-candidate_bbox[1])/3,
  1800. candidate_bbox[3]]
  1801. if col1[1] <= candidate_bbox_line1[0] <= candidate_bbox_line1[1] <= col1[3] \
  1802. or col1[1] <= candidate_bbox_line2[0] <= candidate_bbox_line2[1] <= col1[3]:
  1803. # print("candidate_bbox", candidate_bbox)
  1804. # print("col1", col1)
  1805. contain_flag4 = 1
  1806. # # candidate的y1,y2需被包含在col线中
  1807. # if col1[1]-3 <= candidate_bbox[1] <= candidate_bbox[3] <= col1[3]+3:
  1808. # contain_flag4 = 1
  1809. #
  1810. # # 判断线条有无端点在格子中
  1811. # elif candidate_bbox[1] < col1[1] < candidate_bbox[3] \
  1812. # or candidate_bbox[1] < col1[3] < candidate_bbox[3]:
  1813. # # 线条会有缺一点情况,判断长度超过格子一半
  1814. # if col1_break[3] - col1_break[1] >= (candidate_bbox[3] - candidate_bbox[1])/3:
  1815. # contain_flag4 = 1
  1816. # 找到了该bbox,并且是存在的
  1817. if contain_flag1 and contain_flag2 and contain_flag3 and contain_flag4:
  1818. bbox.append([(candidate_bbox[0], candidate_bbox[1]),
  1819. (candidate_bbox[2], candidate_bbox[3])])
  1820. not_found = 0
  1821. # print("exist candidate_bbox", candidate_bbox)
  1822. # print(contain_flag1, contain_flag2, contain_flag3, contain_flag4)
  1823. break
  1824. # else:
  1825. # print("candidate_bbox", candidate_bbox)
  1826. # print(contain_flag1, contain_flag2, contain_flag3, contain_flag4)
  1827. if not not_found:
  1828. break
  1829. return bbox
  1830. def get_bbox(image_np, row_point_list, col_point_list, split_y, row_lines, col_lines):
  1831. # 分割线纵坐标
  1832. if len(split_y) < 2:
  1833. return []
  1834. # 获取bbox
  1835. bbox_list = []
  1836. for i in range(1, len(split_y)):
  1837. last_y = split_y[i-1]
  1838. y = split_y[i]
  1839. # 先对点线进行分区
  1840. split_row_point_list = []
  1841. split_col_point_list = []
  1842. split_row_lines = []
  1843. split_col_lines = []
  1844. for row in row_point_list:
  1845. if last_y <= row[0][1] <= y:
  1846. row.sort(key=lambda x: (x[1], x[0]))
  1847. split_row_point_list.append(row)
  1848. for col in col_point_list:
  1849. if last_y <= col[0][1] <= y:
  1850. split_col_point_list.append(col)
  1851. for row in row_lines:
  1852. if last_y <= row[1] <= y:
  1853. split_row_lines.append(row)
  1854. for col in col_lines:
  1855. if last_y <= col[1] <= y:
  1856. split_col_lines.append(col)
  1857. # 每个点获取其对角线点,以便形成bbox,按行循环
  1858. for i in range(len(split_row_point_list)-1):
  1859. row = split_row_point_list[i]
  1860. # 循环该行的点
  1861. for k in range(len(row)-1):
  1862. point1 = row[k]
  1863. next_point1 = row[k+1]
  1864. # print("*"*30)
  1865. # print("point1", point1)
  1866. # 有三种对角线点
  1867. # 1. 该点下一行的下一列的点
  1868. # 2. 该点下一列的下一行的点
  1869. # 3. 上述两个点是同一个点
  1870. # 下一行没找到就循环后面的行
  1871. if_find = 0
  1872. for j in range(i+1, len(split_row_point_list)):
  1873. if if_find:
  1874. break
  1875. next_row = split_row_point_list[j]
  1876. # print("next_row", next_row)
  1877. # 循环下一行的点
  1878. for point2 in next_row:
  1879. if abs(point1[0] - point2[0]) <= 2:
  1880. continue
  1881. if point2[0] < point1[0]:
  1882. continue
  1883. bbox = [point1[0], point1[1], point2[0], point2[1]]
  1884. if abs(bbox[0] - bbox[2]) <= 10:
  1885. continue
  1886. if abs(bbox[1] - bbox[3]) <= 10:
  1887. continue
  1888. # bbox的四条边都需要验证是否在line上
  1889. if check_bbox(bbox, split_row_lines, split_col_lines):
  1890. bbox_list.append([(bbox[0], bbox[1]), (bbox[2], bbox[3])])
  1891. if_find = 1
  1892. # print("check bbox", bbox)
  1893. break
  1894. return bbox_list
  1895. def check_bbox(bbox, rows, cols, threshold=5):
  1896. def check(check_line, lines, limit_axis, axis):
  1897. # 需检查的线的1/2段,1/3段,2/3段,1/4段,3/4段
  1898. line_1_2 = [check_line[0], (check_line[0]+check_line[1])/2]
  1899. line_2_2 = [(check_line[0]+check_line[1])/2, check_line[1]]
  1900. line_1_3 = [check_line[0], check_line[0]+(check_line[1]-check_line[0])/3]
  1901. line_2_3 = [check_line[1]-(check_line[1]-check_line[0])/3, check_line[1]]
  1902. line_1_4 = [check_line[0], check_line[0]+(check_line[1]-check_line[0])/4]
  1903. line_3_4 = [check_line[1]-(check_line[1]-check_line[0])/4, check_line[1]]
  1904. # 限制row相同y,col相同x
  1905. if_line = 0
  1906. for line1 in lines:
  1907. if not if_line and abs(line1[1-axis] - limit_axis) <= threshold:
  1908. # check_line完全包含在line中
  1909. if line1[axis] <= check_line[0] <= check_line[1] <= line1[axis+2]:
  1910. if_line = 1
  1911. # check_line的1/2包含在line
  1912. elif line1[axis] <= line_1_2[0] <= line_1_2[1] <= line1[axis+2] \
  1913. or line1[axis] <= line_2_2[0] <= line_2_2[1] <= line1[axis+2]:
  1914. if_line = 1
  1915. # check_line两个1/3段被包含在不同line中
  1916. elif line1[axis] <= line_1_3[0] <= line_1_3[1] <= line1[axis+2]:
  1917. # check_line另一边的1/4被包含
  1918. for line2 in lines:
  1919. if abs(line1[1-axis] - limit_axis) <= threshold:
  1920. if line2[axis] <= line_3_4[0] <= line_3_4[1] <= line2[axis+2]:
  1921. if_line = 1
  1922. break
  1923. elif line1[axis] <= line_2_3[0] <= line_2_3[1] <= line1[axis+2]:
  1924. # check_line另一边的1/4被包含
  1925. for line2 in lines:
  1926. if abs(line1[1-axis] - limit_axis) <= threshold:
  1927. if line2[axis] <= line_1_4[0] <= line_1_4[1] <= line2[axis+2]:
  1928. if_line = 1
  1929. break
  1930. return if_line
  1931. up_down_line = [bbox[0], bbox[2]]
  1932. up_y, down_y = bbox[1], bbox[3]
  1933. left_right_line = [bbox[1], bbox[3]]
  1934. left_x, right_x = bbox[0], bbox[2]
  1935. # 检查bbox四条边是否存在
  1936. if_up = check(up_down_line, rows, up_y, 0)
  1937. if_down = check(up_down_line, rows, down_y, 0)
  1938. if_left = check(left_right_line, cols, left_x, 1)
  1939. if_right = check(left_right_line, cols, right_x, 1)
  1940. # 检查bbox内部除了四条边,是否有其它line在bbox内部
  1941. if_col = 0
  1942. if_row = 0
  1943. if if_up and if_down and if_left and if_right:
  1944. for col in cols:
  1945. if not if_col and left_x+threshold <= col[0] <= right_x-threshold:
  1946. if col[1] <= left_right_line[0] <= left_right_line[1] <= col[3]:
  1947. if_col = 1
  1948. elif left_right_line[0] <= col[1] <= left_right_line[1]:
  1949. if left_right_line[1] - col[1] >= (left_right_line[1] + left_right_line[0])/2:
  1950. if_col = 1
  1951. elif left_right_line[0] <= col[3] <= left_right_line[1]:
  1952. if col[3] - left_right_line[0] >= (left_right_line[1] + left_right_line[0])/2:
  1953. if_col = 1
  1954. for row in rows:
  1955. if not if_row and up_y+threshold <= row[1] <= down_y-threshold:
  1956. if row[0] <= up_down_line[0] <= up_down_line[1] <= row[2]:
  1957. if_row = 1
  1958. elif up_down_line[0] <= row[0] <= up_down_line[1]:
  1959. if up_down_line[1] - row[0] >= (up_down_line[1] + up_down_line[0])/2:
  1960. if_row = 1
  1961. elif up_down_line[0] <= row[2] <= up_down_line[1]:
  1962. if row[2] - up_down_line[0] >= (up_down_line[1] + up_down_line[0])/2:
  1963. if_row = 1
  1964. if if_up and if_down and if_left and if_right and not if_col and not if_row:
  1965. return True
  1966. else:
  1967. return False
  1968. def add_continue_bbox(bboxes):
  1969. add_bbox_list = []
  1970. bboxes.sort(key=lambda x: (x[0][0], x[0][1]))
  1971. last_bbox = bboxes[0]
  1972. # 先对bbox分区
  1973. for i in range(1, len(split_y)):
  1974. y = split_y[i]
  1975. last_y = split_y[i-1]
  1976. split_bbox = []
  1977. for bbox in bboxes:
  1978. if last_y <= bbox[1][1] <= y:
  1979. split_bbox.append(bbox)
  1980. split_bbox.sort
  1981. for i in range(1, len(bboxes)):
  1982. bbox = bboxes[i]
  1983. if last_y <= bbox[1][1] <= y and last_y <= last_bbox[1][1] <= y:
  1984. if abs(last_bbox[1][1] - bbox[0][1]) <= 2:
  1985. last_bbox = bbox
  1986. else:
  1987. if last_bbox[1][1] > bbox[0][1]:
  1988. last_bbox = bbox
  1989. else:
  1990. add_bbox = [(last_bbox[0][0], last_bbox[1][1]),
  1991. (last_bbox[1][0], bbox[0][1])]
  1992. add_bbox_list.append(add_bbox)
  1993. last_y = y
  1994. print("add_bbox_list", add_bbox_list)
  1995. if add_bbox_list:
  1996. bboxes = [str(x) for x in bboxes + add_bbox_list]
  1997. bboxes = list(set(bboxes))
  1998. bboxes = [eval(x) for x in bboxes]
  1999. bboxes.sort(key=lambda x: (x[0][1], x[0][0]))
  2000. return bboxes
  2001. def points_to_line(points_lines, axis):
  2002. new_line_list = []
  2003. for line in points_lines:
  2004. average = 0
  2005. _min = _min = line[0][axis]
  2006. _max = line[-1][axis]
  2007. for point in line:
  2008. average += point[1-axis]
  2009. if point[axis] < _min:
  2010. _min = point[axis]
  2011. if point[axis] > _max:
  2012. _max = point[axis]
  2013. average = int(average / len(line))
  2014. if axis:
  2015. new_line = [average, _min, average, _max]
  2016. else:
  2017. new_line = [_min, average, _max, average]
  2018. new_line_list.append(new_line)
  2019. return new_line_list
  2020. def get_bbox_by_contours(image_np):
  2021. img_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)
  2022. ret, img_bin = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
  2023. # 3.连通域分析
  2024. img_bin, contours, hierarchy = cv2.findContours(img_bin,
  2025. cv2.RETR_LIST,
  2026. cv2.CHAIN_APPROX_SIMPLE)
  2027. # 4.获取最小外接圆 圆心 半径
  2028. center, radius = cv2.minEnclosingTriangle(contours[0])
  2029. center = np.int0(center)
  2030. # 5.绘制最小外接圆
  2031. img_result = image_np.copy()
  2032. cv2.circle(img_result, tuple(center), int(radius), (255, 255, 255), 2)
  2033. # # 读入图片
  2034. # img = image_np
  2035. # cv2.imshow("get_bbox_by_contours ", image_np)
  2036. # # 中值滤波,去噪
  2037. # img = cv2.medianBlur(img, 3)
  2038. # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  2039. # cv2.namedWindow('original', cv2.WINDOW_AUTOSIZE)
  2040. # cv2.imshow('original', gray)
  2041. #
  2042. # # 阈值分割得到二值化图片
  2043. # ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  2044. #
  2045. # # 膨胀操作
  2046. # kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  2047. # bin_clo = cv2.dilate(binary, kernel2, iterations=2)
  2048. #
  2049. # # 连通域分析
  2050. # num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(bin_clo, connectivity=8)
  2051. #
  2052. # # 查看各个返回值
  2053. # # 连通域数量
  2054. # print('num_labels = ',num_labels)
  2055. # # 连通域的信息:对应各个轮廓的x、y、width、height和面积
  2056. # print('stats = ',stats)
  2057. # # 连通域的中心点
  2058. # print('centroids = ',centroids)
  2059. # # 每一个像素的标签1、2、3.。。,同一个连通域的标签是一致的
  2060. # print('labels = ',labels)
  2061. #
  2062. # # 不同的连通域赋予不同的颜色
  2063. # output = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)
  2064. # for i in range(1, num_labels):
  2065. #
  2066. # mask = labels == i
  2067. # output[:, :, 0][mask] = np.random.randint(0, 255)
  2068. # output[:, :, 1][mask] = np.random.randint(0, 255)
  2069. # output[:, :, 2][mask] = np.random.randint(0, 255)
  2070. # cv2.imshow('oginal', output)
  2071. # cv2.waitKey()
  2072. # cv2.destroyAllWindows()
  2073. def get_points_col(points, split_y, threshold=5):
  2074. # 坐标点按行分
  2075. row_point_list = []
  2076. row_point = []
  2077. points.sort(key=lambda x: (x[0], x[1]))
  2078. # print("get_points_col points sort", points)
  2079. x = points[0][0]
  2080. for i in range(1, len(split_y)):
  2081. for p in points:
  2082. if p[1] <= split_y[i-1] or p[1] >= split_y[i]:
  2083. continue
  2084. if x-threshold <= p[0] <= x+threshold:
  2085. row_point.append(p)
  2086. else:
  2087. # print("row_point", row_point)
  2088. row_point.sort(key=lambda x: (x[1], x[0]))
  2089. if row_point:
  2090. row_point_list.append(row_point)
  2091. row_point = []
  2092. x = p[0]
  2093. row_point.append(p)
  2094. if row_point:
  2095. row_point_list.append(row_point)
  2096. return row_point_list
  2097. def get_points_row(points, split_y, threshold=5):
  2098. # 坐标点按列分
  2099. col_point_list = []
  2100. col_point = []
  2101. points.sort(key=lambda x: (x[1], x[0]))
  2102. y = points[0][1]
  2103. for i in range(len(split_y)):
  2104. for p in points:
  2105. if p[1] <= split_y[i-1] or p[1] >= split_y[i]:
  2106. continue
  2107. if y-threshold <= p[1] <= y+threshold:
  2108. col_point.append(p)
  2109. else:
  2110. col_point.sort(key=lambda x: (x[0], x[1]))
  2111. if col_point:
  2112. col_point_list.append(col_point)
  2113. col_point = []
  2114. y = p[1]
  2115. col_point.append(p)
  2116. if col_point:
  2117. col_point_list.append(col_point)
  2118. return col_point_list
  2119. def get_outline_point(points, split_y):
  2120. # 分割线纵坐标
  2121. # print("get_outline_point split_y", split_y)
  2122. if len(split_y) < 2:
  2123. return []
  2124. outline_2point = []
  2125. points.sort(key=lambda x: (x[1], x[0]))
  2126. for i in range(1, len(split_y)):
  2127. area_points = []
  2128. for point in points:
  2129. if point[1] <= split_y[i-1] or point[1] >= split_y[i]:
  2130. continue
  2131. area_points.append(point)
  2132. if area_points:
  2133. area_points.sort(key=lambda x: (x[1], x[0]))
  2134. outline_2point.append([area_points[0], area_points[-1]])
  2135. return outline_2point
  2136. # def merge_row(row_lines):
  2137. # for row in row_lines:
  2138. # for row1 in row_lines:
  2139. def get_best_predict_size(image_np):
  2140. sizes = [1280, 1152, 1024, 896, 768, 640, 512, 384, 256, 128]
  2141. min_len = 10000
  2142. best_height = sizes[0]
  2143. for height in sizes:
  2144. if abs(image_np.shape[0] - height) < min_len:
  2145. min_len = abs(image_np.shape[0] - height)
  2146. best_height = height
  2147. min_len = 10000
  2148. best_width = sizes[0]
  2149. for width in sizes:
  2150. if abs(image_np.shape[1] - width) < min_len:
  2151. min_len = abs(image_np.shape[1] - width)
  2152. best_width = width
  2153. return best_height, best_width
  2154. def choose_longer_row(lines):
  2155. new_row = []
  2156. jump_row = []
  2157. for i in range(len(lines)):
  2158. row1 = lines[i]
  2159. jump_flag = 0
  2160. if row1 in jump_row:
  2161. continue
  2162. for j in range(i+1, len(lines)):
  2163. row2 = lines[j]
  2164. if row2 in jump_row:
  2165. continue
  2166. if row2[1]-5 <= row1[1] <= row2[1]+5:
  2167. if row1[0] <= row2[0] and row1[2] >= row2[2]:
  2168. new_row.append(row1)
  2169. jump_row.append(row1)
  2170. jump_row.append(row2)
  2171. jump_flag = 1
  2172. break
  2173. elif row2[0] <= row1[0] and row2[2] >= row1[2]:
  2174. new_row.append(row2)
  2175. jump_row.append(row1)
  2176. jump_row.append(row2)
  2177. jump_flag = 1
  2178. break
  2179. if not jump_flag:
  2180. new_row.append(row1)
  2181. jump_row.append(row1)
  2182. return new_row
  2183. def choose_longer_col(lines):
  2184. new_col = []
  2185. jump_col = []
  2186. for i in range(len(lines)):
  2187. col1 = lines[i]
  2188. jump_flag = 0
  2189. if col1 in jump_col:
  2190. continue
  2191. for j in range(i+1, len(lines)):
  2192. col2 = lines[j]
  2193. if col2 in jump_col:
  2194. continue
  2195. if col2[0]-5 <= col1[0] <= col2[0]+5:
  2196. if col1[1] <= col2[1] and col1[3] >= col2[3]:
  2197. new_col.append(col1)
  2198. jump_col.append(col1)
  2199. jump_col.append(col2)
  2200. jump_flag = 1
  2201. break
  2202. elif col2[1] <= col1[1] and col2[3] >= col1[3]:
  2203. new_col.append(col2)
  2204. jump_col.append(col1)
  2205. jump_col.append(col2)
  2206. jump_flag = 1
  2207. break
  2208. if not jump_flag:
  2209. new_col.append(col1)
  2210. jump_col.append(col1)
  2211. return new_col
  2212. def delete_contain_bbox(bboxes):
  2213. # bbox互相包含,取小的bbox
  2214. delete_bbox = []
  2215. for i in range(len(bboxes)):
  2216. for j in range(i+1, len(bboxes)):
  2217. bbox1 = bboxes[i]
  2218. bbox2 = bboxes[j]
  2219. # 横坐标相等情况
  2220. if bbox1[0][0] == bbox2[0][0] and bbox1[1][0] == bbox2[1][0]:
  2221. if bbox1[0][1] <= bbox2[0][1] <= bbox2[1][1] <= bbox1[1][1]:
  2222. # print("1", bbox1, bbox2)
  2223. delete_bbox.append(bbox1)
  2224. elif bbox2[0][1] <= bbox1[0][1] <= bbox1[1][1] <= bbox2[1][1]:
  2225. # print("2", bbox1, bbox2)
  2226. delete_bbox.append(bbox2)
  2227. # 纵坐标相等情况
  2228. elif bbox1[0][1] == bbox2[0][1] and bbox1[1][1] == bbox2[1][1]:
  2229. if bbox1[0][0] <= bbox2[0][0] <= bbox2[1][0] <= bbox1[1][0]:
  2230. print("3", bbox1, bbox2)
  2231. delete_bbox.append(bbox1)
  2232. elif bbox2[0][0] <= bbox1[0][0] <= bbox1[1][0] <= bbox2[1][0]:
  2233. print("4", bbox1, bbox2)
  2234. delete_bbox.append(bbox2)
  2235. print("delete_contain_bbox len(bboxes)", len(bboxes))
  2236. print("delete_contain_bbox len(delete_bbox)", len(delete_bbox))
  2237. for bbox in delete_bbox:
  2238. if bbox in bboxes:
  2239. bboxes.remove(bbox)
  2240. print("delete_contain_bbox len(bboxes)", len(bboxes))
  2241. return bboxes
  2242. if __name__ == '__main__':
  2243. # p = "开标记录表3_page_0.png"
  2244. # p = "train_data/label_1.jpg"
  2245. # p = "test_files/train_463.jpg"
  2246. p = "test_files/8.png"
  2247. # p = "test_files/无边框3.jpg"
  2248. # p = "test_files/part1.png"
  2249. # p = "D:\\Project\\format_conversion\\appendix_test\\temp\\00e959a0bc9011ebaf5a00163e0ae709" + \
  2250. # "\\00e95f7cbc9011ebaf5a00163e0ae709_pdf_page0.png"
  2251. # p = "D:\\Project\\format_conversion\\appendix_test\\temp\\00fb3e52bc7e11eb836000163e0ae709" + \
  2252. # "\\00fb43acbc7e11eb836000163e0ae709.png"
  2253. # p = "test_files/table.jpg"
  2254. # p = "data_process/create_data/0.jpg"
  2255. # p = "../format_conversion/temp/f1fe9c4ac8e511eb81d700163e0857b6/f1fea1e0c8e511eb81d700163e0857b6.png"
  2256. # p = "../format_conversion/1.png"
  2257. img = cv2.imread(p)
  2258. t = time.time()
  2259. model.load_weights("")
  2260. best_h, best_w = get_best_predict_size(img)
  2261. print(img.shape)
  2262. print((best_h, best_w))
  2263. # row_boxes, col_boxes = table_line(img[..., ::-1], model, size=(512, 1024), hprob=0.5, vprob=0.5)
  2264. # row_boxes, col_boxes, img = table_line(img[..., ::-1], model, size=(best_w, best_h), hprob=0.5, vprob=0.5)
  2265. row_boxes, col_boxes, img = table_line(img, model, size=(best_w, best_h), hprob=0.5, vprob=0.5)
  2266. print("len(row_boxes)", len(row_boxes))
  2267. print("len(col_boxes)", col_boxes)
  2268. # 创建空图
  2269. test_img = np.zeros((img.shape), np.uint8)
  2270. test_img.fill(255)
  2271. for box in row_boxes+col_boxes:
  2272. cv2.line(test_img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 0, 0), 1)
  2273. cv2.imshow("test_image", test_img)
  2274. cv2.waitKey(0)
  2275. cv2.imwrite("temp.jpg", test_img)
  2276. # 计算交点、分割线
  2277. crossover_points = get_points(row_boxes, col_boxes, (img.shape[0], img.shape[1]))
  2278. print("len(col_boxes)", len(col_boxes))
  2279. split_lines, split_y = get_split_line(crossover_points, col_boxes, img)
  2280. print("split_y", split_y)
  2281. # for point in crossover_points:
  2282. # cv2.circle(test_img, point, 1, (0, 255, 0), 3)
  2283. # cv2.imshow("point image1", test_img)
  2284. # cv2.waitKey(0)
  2285. # 计算行列,剔除相近交点
  2286. row_point_list = get_points_row(crossover_points, split_y, 0)
  2287. col_point_list = get_points_col(crossover_points, split_y, 0)
  2288. crossover_points = delete_close_points(crossover_points, row_point_list, col_point_list)
  2289. row_point_list = get_points_row(crossover_points, split_y)
  2290. col_point_list = get_points_col(crossover_points, split_y)
  2291. for point in crossover_points:
  2292. cv2.circle(test_img, point, 1, (0, 0, 255), 3)
  2293. cv2.imshow("point image1", test_img)
  2294. cv2.waitKey(0)
  2295. print("len(row_boxes)", len(row_boxes))
  2296. print("len(col_boxes)", len(col_boxes))
  2297. # 修复边框
  2298. new_row_boxes, new_col_boxes, long_row_boxes, long_col_boxes = \
  2299. fix_outline(img, row_boxes, col_boxes, crossover_points, split_y)
  2300. if new_row_boxes or new_col_boxes:
  2301. if long_row_boxes:
  2302. print("long_row_boxes", long_row_boxes)
  2303. row_boxes = long_row_boxes
  2304. if long_col_boxes:
  2305. print("long_col_boxes", long_col_boxes)
  2306. col_boxes = long_col_boxes
  2307. if new_row_boxes:
  2308. row_boxes += new_row_boxes
  2309. print("new_row_boxes", new_row_boxes)
  2310. if new_col_boxes:
  2311. print("new_col_boxes", new_col_boxes)
  2312. col_boxes += new_col_boxes
  2313. # print("len(row_boxes)", len(row_boxes))
  2314. # print("len(col_boxes)", len(col_boxes))
  2315. # row_boxes += new_row_boxes
  2316. # col_boxes += new_col_boxes
  2317. # row_boxes = choose_longer_row(row_boxes)
  2318. # col_boxes = choose_longer_col(col_boxes)
  2319. # 创建空图
  2320. test_img = np.zeros((img.shape), np.uint8)
  2321. test_img.fill(255)
  2322. for box in row_boxes+col_boxes:
  2323. cv2.line(test_img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (0, 0, 0), 1)
  2324. cv2.imshow("test_image2", test_img)
  2325. cv2.waitKey(0)
  2326. # 展示补线
  2327. for row in new_row_boxes:
  2328. cv2.line(test_img, (int(row[0]), int(row[1])),
  2329. (int(row[2]), int(row[3])), (0, 0, 255), 1)
  2330. for col in new_col_boxes:
  2331. cv2.line(test_img, (int(col[0]), int(col[1])),
  2332. (int(col[2]), int(col[3])), (0, 0, 255), 1)
  2333. cv2.imshow("fix_outline", test_img)
  2334. cv2.waitKey(0)
  2335. cv2.imwrite("temp.jpg", test_img)
  2336. # 修复边框后重新计算交点、分割线
  2337. print("crossover_points", len(crossover_points))
  2338. crossover_points = get_points(row_boxes, col_boxes, (img.shape[0], img.shape[1]))
  2339. print("crossover_points new", len(crossover_points))
  2340. split_lines, split_y = get_split_line(crossover_points, col_boxes, img)
  2341. # 计算行列,剔除相近交点
  2342. row_point_list = get_points_row(crossover_points, split_y, 0)
  2343. col_point_list = get_points_col(crossover_points, split_y, 0)
  2344. print(len(crossover_points), len(row_point_list), len(col_point_list))
  2345. crossover_points = delete_close_points(crossover_points, row_point_list, col_point_list)
  2346. print(len(crossover_points), len(row_point_list), len(col_point_list))
  2347. row_point_list = get_points_row(crossover_points, split_y)
  2348. col_point_list = get_points_col(crossover_points, split_y)
  2349. for point in crossover_points:
  2350. cv2.circle(test_img, point, 1, (0, 255, 0), 3)
  2351. cv2.imshow("point image2", test_img)
  2352. cv2.waitKey(0)
  2353. # 获取每个表格的左上右下两个点
  2354. outline_point = get_outline_point(crossover_points, split_y)
  2355. # print(outline_point)
  2356. for outline in outline_point:
  2357. cv2.circle(test_img, outline[0], 1, (255, 0, 0), 5)
  2358. cv2.circle(test_img, outline[1], 1, (255, 0, 0), 5)
  2359. cv2.imshow("outline point", test_img)
  2360. cv2.waitKey(0)
  2361. # 获取bbox
  2362. # get_bbox(img, crossover_points, split_y)
  2363. # for point in crossover_points:
  2364. # cv2.circle(test_img, point, 1, (0, 255, 0), 3)
  2365. # cv2.imshow("point image3", test_img)
  2366. # cv2.waitKey(0)
  2367. # split_y = []
  2368. # for outline in outline_point:
  2369. # split_y.extend([outline[0][1]-5, outline[1][1]+5])
  2370. print("len(row_boxes)", len(row_boxes))
  2371. print("len(col_boxes)", len(col_boxes))
  2372. bboxes = get_bbox(img, row_point_list, col_point_list, split_y, row_boxes, col_boxes)
  2373. # 展示
  2374. for box in bboxes:
  2375. # print(box[0], box[1])
  2376. # if abs(box[0][1] - box[1][1]) > abs(box[0][0] - box[1][0]):
  2377. # continue
  2378. cv2.rectangle(test_img, box[0], box[1], (0, 0, 255), 2, 8)
  2379. cv2.imshow('bboxes', test_img)
  2380. cv2.waitKey(0)
  2381. # img = draw_lines(img, row_boxes+col_boxes, color=(255, 0, 0), lineW=2)
  2382. # img = draw_boxes(img, rowboxes+colboxes, color=(0, 0, 255))
  2383. print(time.time()-t, len(row_boxes), len(col_boxes))
  2384. cv2.imwrite('temp.jpg', test_img)
  2385. # cv2.imshow('main', img)
  2386. # cv2.waitKey(0)