table_line.py 135 KB

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