forked from chineseocr/table-detect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtable_line.py
executable file
·262 lines (225 loc) · 9.71 KB
/
table_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from tensorflow.keras.layers import Input, concatenate, Conv2D, MaxPooling2D, BatchNormalization, UpSampling2D
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.models import Model
def table_net(input_shape=(512, 512, 3), num_classes=1):
inputs = Input(shape=input_shape)
# 512
use_bias = False
down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(inputs)
down0a = BatchNormalization()(down0a)
down0a = LeakyReLU(alpha=0.1)(down0a)
down0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(down0a)
down0a = BatchNormalization()(down0a)
down0a = LeakyReLU(alpha=0.1)(down0a)
down0a_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0a)
# 256
down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0a_pool)
down0 = BatchNormalization()(down0)
down0 = LeakyReLU(alpha=0.1)(down0)
down0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(down0)
down0 = BatchNormalization()(down0)
down0 = LeakyReLU(alpha=0.1)(down0)
down0_pool = MaxPooling2D((2, 2), strides=(2, 2))(down0)
# 128
down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down0_pool)
down1 = BatchNormalization()(down1)
down1 = LeakyReLU(alpha=0.1)(down1)
down1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(down1)
down1 = BatchNormalization()(down1)
down1 = LeakyReLU(alpha=0.1)(down1)
down1_pool = MaxPooling2D((2, 2), strides=(2, 2))(down1)
# 64
down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down1_pool)
down2 = BatchNormalization()(down2)
down2 = LeakyReLU(alpha=0.1)(down2)
down2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(down2)
down2 = BatchNormalization()(down2)
down2 = LeakyReLU(alpha=0.1)(down2)
down2_pool = MaxPooling2D((2, 2), strides=(2, 2))(down2)
# 32
down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down2_pool)
down3 = BatchNormalization()(down3)
down3 = LeakyReLU(alpha=0.1)(down3)
down3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(down3)
down3 = BatchNormalization()(down3)
down3 = LeakyReLU(alpha=0.1)(down3)
down3_pool = MaxPooling2D((2, 2), strides=(2, 2))(down3)
# 16
down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down3_pool)
down4 = BatchNormalization()(down4)
down4 = LeakyReLU(alpha=0.1)(down4)
down4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(down4)
down4 = BatchNormalization()(down4)
down4 = LeakyReLU(alpha=0.1)(down4)
down4_pool = MaxPooling2D((2, 2), strides=(2, 2))(down4)
# 8
center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(down4_pool)
center = BatchNormalization()(center)
center = LeakyReLU(alpha=0.1)(center)
center = Conv2D(1024, (3, 3), padding='same', use_bias=use_bias)(center)
center = BatchNormalization()(center)
center = LeakyReLU(alpha=0.1)(center)
# center
up4 = UpSampling2D((2, 2))(center)
up4 = concatenate([down4, up4], axis=3)
up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
up4 = BatchNormalization()(up4)
up4 = LeakyReLU(alpha=0.1)(up4)
up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
up4 = BatchNormalization()(up4)
up4 = LeakyReLU(alpha=0.1)(up4)
up4 = Conv2D(512, (3, 3), padding='same', use_bias=use_bias)(up4)
up4 = BatchNormalization()(up4)
up4 = LeakyReLU(alpha=0.1)(up4)
# 16
up3 = UpSampling2D((2, 2))(up4)
up3 = concatenate([down3, up3], axis=3)
up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
up3 = BatchNormalization()(up3)
up3 = LeakyReLU(alpha=0.1)(up3)
up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
up3 = BatchNormalization()(up3)
up3 = LeakyReLU(alpha=0.1)(up3)
up3 = Conv2D(256, (3, 3), padding='same', use_bias=use_bias)(up3)
up3 = BatchNormalization()(up3)
up3 = LeakyReLU(alpha=0.1)(up3)
# 32
up2 = UpSampling2D((2, 2))(up3)
up2 = concatenate([down2, up2], axis=3)
up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
up2 = BatchNormalization()(up2)
up2 = LeakyReLU(alpha=0.1)(up2)
up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
up2 = BatchNormalization()(up2)
up2 = LeakyReLU(alpha=0.1)(up2)
up2 = Conv2D(128, (3, 3), padding='same', use_bias=use_bias)(up2)
up2 = BatchNormalization()(up2)
up2 = LeakyReLU(alpha=0.1)(up2)
# 64
up1 = UpSampling2D((2, 2))(up2)
up1 = concatenate([down1, up1], axis=3)
up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
up1 = BatchNormalization()(up1)
up1 = LeakyReLU(alpha=0.1)(up1)
up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
up1 = BatchNormalization()(up1)
up1 = LeakyReLU(alpha=0.1)(up1)
up1 = Conv2D(64, (3, 3), padding='same', use_bias=use_bias)(up1)
up1 = BatchNormalization()(up1)
up1 = LeakyReLU(alpha=0.1)(up1)
# 128
up0 = UpSampling2D((2, 2))(up1)
up0 = concatenate([down0, up0], axis=3)
up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
up0 = BatchNormalization()(up0)
up0 = LeakyReLU(alpha=0.1)(up0)
up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
up0 = BatchNormalization()(up0)
up0 = LeakyReLU(alpha=0.1)(up0)
up0 = Conv2D(32, (3, 3), padding='same', use_bias=use_bias)(up0)
up0 = BatchNormalization()(up0)
up0 = LeakyReLU(alpha=0.1)(up0)
# 256
up0a = UpSampling2D((2, 2))(up0)
up0a = concatenate([down0a, up0a], axis=3)
up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
up0a = BatchNormalization()(up0a)
up0a = LeakyReLU(alpha=0.1)(up0a)
up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
up0a = BatchNormalization()(up0a)
up0a = LeakyReLU(alpha=0.1)(up0a)
up0a = Conv2D(16, (3, 3), padding='same', use_bias=use_bias)(up0a)
up0a = BatchNormalization()(up0a)
up0a = LeakyReLU(alpha=0.1)(up0a)
# 512
classify = Conv2D(num_classes, (1, 1), activation='sigmoid')(up0a)
model = Model(inputs=inputs, outputs=classify)
return model
from config import tableModeLinePath, gpu_id, mem_limit
from utils import letterbox_image, get_table_line, adjust_lines, line_to_line
import numpy as np
import cv2
import tensorflow as tf
# 设置TensorFlow会话配置
config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = mem_limit
# 创建TensorFlow会话
session = tf.compat.v1.Session(config=config)
# with tf.device(f'/GPU:{gpu_id}'):
# # 在此范围内的所有操作将在指定的GPU上运行
# print(f'loading tf on cuda:{gpu_id}')
# model = table_net((None, None, 3), 2)
# model.load_weights(tableModeLinePath)
# 如果没有可用的GPU,则使用CPU
if gpu_id == "cpu":
print("No GPU available, using CPU instead.")
with tf.device('/CPU:0'):
model = table_net((None, None, 3), 2)
model.load_weights(tableModeLinePath)
else:
# 指定要使用的GPU ID是否在可用的物理设备列表中
print(f'Loading tf on CUDA:{gpu_id}')
tf.config.set_visible_devices(physical_devices[int(gpu_id)], 'GPU')
with tf.device(f'/GPU:{gpu_id}'):
model = table_net((None, None, 3), 2)
model.load_weights(tableModeLinePath)
def table_line(img, size=(512, 512), hprob=0.5, vprob=0.5, row=50, col=30, alph=15):
sizew, sizeh = size
inputBlob, fx, fy = letterbox_image(img[..., ::-1], (sizew, sizeh))
if gpu_id == "cpu":
with tf.device('/CPU:0'):
pred = model.predict(np.array([np.array(inputBlob) / 255.0]))
else:
with tf.device(f'/GPU:{gpu_id}'):
pred = model.predict(np.array([np.array(inputBlob) / 255.0]))
pred = pred[0]
vpred = pred[..., 1] > vprob ##竖线
hpred = pred[..., 0] > hprob ##横线
vpred = vpred.astype(int)
hpred = hpred.astype(int)
colboxes = get_table_line(vpred, axis=1, lineW=col)
rowboxes = get_table_line(hpred, axis=0, lineW=row)
ccolbox = []
crowlbox = []
if len(rowboxes) > 0:
rowboxes = np.array(rowboxes)
rowboxes[:, [0, 2]] = rowboxes[:, [0, 2]] / fx
rowboxes[:, [1, 3]] = rowboxes[:, [1, 3]] / fy
xmin = rowboxes[:, [0, 2]].min()
xmax = rowboxes[:, [0, 2]].max()
ymin = rowboxes[:, [1, 3]].min()
ymax = rowboxes[:, [1, 3]].max()
ccolbox = [[xmin, ymin, xmin, ymax], [xmax, ymin, xmax, ymax]]
rowboxes = rowboxes.tolist()
if len(colboxes) > 0:
colboxes = np.array(colboxes)
colboxes[:, [0, 2]] = colboxes[:, [0, 2]] / fx
colboxes[:, [1, 3]] = colboxes[:, [1, 3]] / fy
xmin = colboxes[:, [0, 2]].min()
xmax = colboxes[:, [0, 2]].max()
ymin = colboxes[:, [1, 3]].min()
ymax = colboxes[:, [1, 3]].max()
colboxes = colboxes.tolist()
crowlbox = [[xmin, ymin, xmax, ymin], [xmin, ymax, xmax, ymax]]
rowboxes += crowlbox
colboxes += ccolbox
rboxes_row_, rboxes_col_ = adjust_lines(rowboxes, colboxes, alph=alph)
rowboxes += rboxes_row_
colboxes += rboxes_col_
nrow = len(rowboxes)
ncol = len(colboxes)
for i in range(nrow):
for j in range(ncol):
rowboxes[i] = line_to_line(rowboxes[i], colboxes[j], 10)
colboxes[j] = line_to_line(colboxes[j], rowboxes[i], 10)
return rowboxes, colboxes
if __name__ == '__main__':
import time
p = 'img/table-detect.jpg'
from utils import draw_lines
img = cv2.imread(p)
t = time.time()
rowboxes, colboxes = table_line(img[..., ::-1], size=(512, 512), hprob=0.5, vprob=0.5)
img = draw_lines(img, rowboxes + colboxes, color=(255, 0, 0), lineW=2)
print(time.time() - t, len(rowboxes), len(colboxes))
cv2.imwrite('img/table-line.png', img)