table_line.py 107 KB

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