table_line.py 106 KB

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