forked from yizt/abcnet_custom_dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trans_ic15.py
292 lines (254 loc) · 9.83 KB
/
trans_ic15.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- coding: utf-8 -*-
"""
@File : trans_ic15.py
@Time : 2020/7/20 上午9:51
@Author : yizuotian
@Description : 转换icdar15数据集
"""
import argparse
import codecs
import glob
import json
import os
import sys
import cv2
import numpy as np
from scipy import interpolate
def load_ic15(annotation_path, image_dir):
"""
加载标注信息
:param annotation_path:
:param image_dir:
:return:
"""
image_annotation = {}
# 文件名称,路径
base_name = os.path.basename(annotation_path)
image_name = base_name[3:-3] + '*' # 通配符 gt_img_3.txt,img_3.jpg or png
image_annotation["annotation_path"] = annotation_path
image_annotation["image_path"] = glob.glob(os.path.join(image_dir, image_name))[0]
image_annotation["file_name"] = os.path.basename(image_annotation["image_path"]) # 图像文件名
# 读取边框标注
bbox = []
quadrilateral = [] # 四边形
text_list = [] # gt text
with open(annotation_path, "r", encoding='utf-8') as f:
lines = f.read().encode('utf-8').decode('utf-8-sig').splitlines()
# lines = f.readlines()
# print(lines)
for line in lines:
line = line.strip().split(",")
# 左上、右上、右下、左下 四个坐标 如:377,117,463,117,465,130,378,130
lt_x, lt_y, rt_x, rt_y, rb_x, rb_y, lb_x, lb_y = map(float, line[:8])
text = line[8]
# 模糊标记不要
# if text == '###':
# continue
x_min, y_min, x_max, y_max = min(lt_x, lb_x), min(lt_y, rt_y), max(rt_x, rb_x), max(lb_y, rb_y)
bbox.append([y_min, x_min, y_max, x_max])
quadrilateral.append([lt_x, lt_y, rt_x, rt_y, rb_x, rb_y, lb_x, lb_y])
text_list.append(text)
image_annotation["boxes"] = np.asarray(bbox, np.float32).reshape((-1, 4))
image_annotation["quadrilaterals"] = np.asarray(quadrilateral, np.float32).reshape((-1, 8))
image_annotation["labels"] = text_list
return image_annotation
def interp(x1, y1, x2, y2):
"""
在(x1,y1)和(x2,y2)上均匀插值两个点
:param x1:
:param y1:
:param x2:
:param y2:
:return:
"""
xs = np.linspace(x1, x2, 4)
f = interpolate.interp1d((x1, x2), (y1, y2))
ys = f(xs)
return xs, ys
def save_to_abc(txt_path, image_annotation):
"""
将ic15标注数据保存为ABCNet的标注格式
:param txt_path: 保存路径
:param image_annotation:
:return:
"""
quads = image_annotation['quadrilaterals']
labels = image_annotation['labels']
with codecs.open(txt_path, mode='w', encoding='utf-8') as w:
for (lt_x, lt_y, rt_x, rt_y, rb_x, rb_y, lb_x, lb_y), text in zip(quads, labels):
xs_top, ys_top = interp(lt_x, lt_y, rt_x, rt_y)
xs_bottom, ys_bottom = interp(rb_x, rb_y, lb_x, lb_y)
xs = np.concatenate([xs_top, xs_bottom])
ys = np.concatenate([ys_top, ys_bottom])
points = np.array([xs, ys]).T # [8,(x,y)]
points = points.flatten() # 打平
# 保留两位小数
f = np.vectorize(lambda x: round(x, 2))
points = f(points)
# print(points.flatten().shape)
w.write('{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}||||{}\n'.format(*points, text))
def ic15_to_abc(annotation_dir, image_dir, abc_gt_dir):
"""
icdar15数据转为abcnet标注格式(带bezier控制点)
:param annotation_dir:
:param image_dir:
:param abc_gt_dir:
:return:
"""
for ann_name in os.listdir(annotation_dir):
ann_path = os.path.join(annotation_dir, ann_name)
ann_info = load_ic15(ann_path, image_dir)
dst_gt_path = os.path.join(abc_gt_dir,
'{}.txt'.format(os.path.splitext(ann_info['file_name'])[0]))
save_to_abc(dst_gt_path, ann_info)
def gen_abc_json(abc_gt_dir, abc_json_path, image_dir):
"""
根据abcnet的gt标注生成coco格式的json标注
:param abc_gt_dir:
:param abc_json_path:
:param image_dir:
:return:
"""
# Desktop Latin_embed.
cV2 = [' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~']
dataset = {
'licenses': [],
'info': {},
'categories': [],
'images': [],
'annotations': []
}
with open('./classes.txt') as f:
classes = f.read().strip().split()
for i, cls in enumerate(classes, 1):
dataset['categories'].append({
'id': i,
'name': cls,
'supercategory': 'beverage',
'keypoints': ['mean',
'xmin',
'x2',
'x3',
'xmax',
'ymin',
'y2',
'y3',
'ymax',
'cross'] # only for BDN
})
def get_category_id(cls):
for category in dataset['categories']:
if category['name'] == cls:
return category['id']
# 遍历abcnet txt 标注
indexes = sorted([f.split('.')[0]
for f in os.listdir(abc_gt_dir)])
print(indexes)
j = 1 # 标注边框id号
for index in indexes:
# if int(index) >3: continue
print('Processing: ' + index)
im = cv2.imread(os.path.join(image_dir, '{}.jpg'.format(index)))
height, width, _ = im.shape
dataset['images'].append({
'coco_url': '',
'date_captured': '',
'file_name': index + '.jpg',
'flickr_url': '',
'id': int(index.split('_')[-1]), # img_1
'license': 0,
'width': width,
'height': height
})
anno_file = os.path.join(abc_gt_dir, '{}.txt'.format(index))
with open(anno_file) as f:
lines = [line for line in f.readlines() if line.strip()]
# 没有清晰的标注,跳过
if len(lines) == 0:
continue
for i, line in enumerate(lines):
pttt = line.strip().split('||||')
parts = pttt[0].split(',')
ct = pttt[-1].strip()
cls = 'text'
segs = [float(kkpart) for kkpart in parts[:16]]
xt = [segs[ikpart] for ikpart in range(0, len(segs), 2)]
yt = [segs[ikpart] for ikpart in range(1, len(segs), 2)]
xmin = min([xt[0], xt[3], xt[4], xt[7]])
ymin = min([yt[0], yt[3], yt[4], yt[7]])
xmax = max([xt[0], xt[3], xt[4], xt[7]])
ymax = max([yt[0], yt[3], yt[4], yt[7]])
width = max(0, xmax - xmin + 1)
height = max(0, ymax - ymin + 1)
if width == 0 or height == 0:
continue
max_len = 100
recs = [len(cV2) + 1 for ir in range(max_len)]
ct = str(ct)
print('rec', ct)
for ix, ict in enumerate(ct):
if ix >= max_len: continue
if ict in cV2:
recs[ix] = cV2.index(ict)
else:
recs[ix] = len(cV2)
dataset['annotations'].append({
'area': width * height,
'bbox': [xmin, ymin, width, height],
'category_id': get_category_id(cls),
'id': j,
'image_id': int(index.split('_')[-1]), # img_1
'iscrowd': 0,
'bezier_pts': segs,
'rec': recs
})
j += 1
# 写入json文件
folder = os.path.dirname(abc_json_path)
if not os.path.exists(folder):
os.makedirs(folder)
with open(abc_json_path, 'w') as f:
json.dump(dataset, f)
def test_load_ic15():
anno_path = '/Users/yizuotian/dataset/IC15/ch4_training_localization_transcription_gt/gt_img_1.txt'
img_dir = '/Users/yizuotian/dataset/IC15/ch4_training_images'
txt_path = './001.txt'
save_to_abc(txt_path, load_ic15(anno_path, img_dir))
def test_ic15_to_abc():
ann_dir = '/Users/yizuotian/dataset/IC15/ch4_training_localization_transcription_gt'
img_dir = '/Users/yizuotian/dataset/IC15/ch4_training_images'
txt_dir = '/Users/yizuotian/dataset/IC15/abcnet_gt_train'
json_path = '/Users/yizuotian/dataset/IC15/annotations/train.json'
os.makedirs(txt_dir, exist_ok=True)
ic15_to_abc(ann_dir, img_dir, txt_dir)
gen_abc_json(txt_dir, json_path, img_dir)
def main(args):
os.makedirs(args.abc_gt_dir, exist_ok=True)
ic15_to_abc(args.ann_dir, args.image_dir, args.abc_gt_dir)
gen_abc_json(args.abc_gt_dir, args.json_path, args.image_dir)
if __name__ == '__main__':
# test_load_ic15()
# test_ic15_to_abc()
"""
Usage:
ic_root=/Users/yizuotian/dataset/IC15
python trans_ic15.py --ann-dir $ic_root/ch4_training_localization_transcription_gt \
--image-dir $ic_root/ch4_training_images \
--abc-gt-dir $ic_root/abcnet_gt_train \
--json-path $ic_root/annotations/train.json
python trans_ic15.py --ann-dir $ic_root/Challenge4_Test_Task1_GT \
--image-dir $ic_root/ch4_test_images \
--abc-gt-dir $ic_root/abcnet_gt_test \
--json-path $ic_root/annotations/test.json
"""
parse = argparse.ArgumentParser()
parse.add_argument("--ann-dir", type=str, default=None)
parse.add_argument("--image-dir", type=str, default=None)
parse.add_argument("--abc-gt-dir", type=str, default=None)
parse.add_argument("--json-path", type=str, default=None)
arguments = parse.parse_args(sys.argv[1:])
main(arguments)