table_line.py 147 KB

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