table_line.py 144 KB

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