Skip to content

Commit

Permalink
完成SROIE,ART,LSVT转换
Browse files Browse the repository at this point in the history
  • Loading branch information
WenmuZhou committed Mar 22, 2020
1 parent 5ba9f29 commit 58532f3
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
- [x] MLT2019
- [x] COCO-Text_v2
- [x] ReCTS
- [ ] SROIE
- [ ] ArT
- [ ] LSVT
- [x] SROIE
- [x] ArT
- [x] LSVT
- [ ] Synth800k
- [ ] icdar2017rctw
- [ ] Synth800k
Expand Down
54 changes: 54 additions & 0 deletions convert/ArtS2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
# @Time : 2020/3/18 14:12
# @Author : zhoujun
"""
将icdar2015数据集转换为统一格式
"""
import os
from tqdm import tqdm
from convert.utils import load, save


def cvt_det(gt_path, save_path, img_folder):
"""
将icdar2015格式的gt转换为json格式
:param gt_path:
:param save_path:
:return:
"""
gt_dict = {'data_root': img_folder}
data_list = []
origin_gt = load(gt_path)
for img_name, gt in tqdm(origin_gt.items()):
cur_gt = {'img_name': img_name + '.jpg', 'annotations': []}
for line in gt:
cur_line_gt = {'polygon': [], 'text': '', 'illegibility': False, 'language': 'Latin'}
chars_gt = [{'polygon': [], 'char': '', 'illegibility': False, 'language': 'Latin'}]
cur_line_gt['chars'] = chars_gt
# 字符串级别的信息
cur_line_gt['polygon'] = line['points']
cur_line_gt['text'] = line['transcription']
cur_line_gt['illegibility'] = line['illegibility']
cur_line_gt['language'] = line['language']
cur_gt['annotations'].append(cur_line_gt)
data_list.append(cur_gt)
gt_dict['data_list'] = data_list
save(gt_dict, save_path)


def cvt_rec(gt_path, save_path, img_folder):
origin_gt = load(gt_path)
file_list = []
for img_name, gt in tqdm(origin_gt.items()):
assert len(gt) == 1
gt = gt[0]
img_path = os.path.join(img_folder, img_name + '.jpg')
file_list.append(img_path + '\t' + gt['transcription'] + '\t' + gt['language'])
save(file_list, save_path)


if __name__ == '__main__':
gt_path = r'D:\dataset\Art\detection\gt\train_labels.json'
img_folder = r'D:\dataset\Art\detection\train_images'
save_path = r'D:\dataset\Art\detection\train.json'
cvt_det(gt_path, save_path, img_folder)
42 changes: 42 additions & 0 deletions convert/LSVT2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
# @Time : 2020/3/18 14:12
# @Author : zhoujun
"""
将icdar2015数据集转换为统一格式
"""
import os
from tqdm import tqdm
from convert.utils import load, save


def cvt(gt_path, save_path, img_folder):
"""
将icdar2015格式的gt转换为json格式
:param gt_path:
:param save_path:
:return:
"""
gt_dict = {'data_root': img_folder}
data_list = []
origin_gt = load(gt_path)
for img_name, gt in tqdm(origin_gt.items()):
cur_gt = {'img_name': img_name + '.jpg', 'annotations': []}
for line in gt:
cur_line_gt = {'polygon': [], 'text': '', 'illegibility': False, 'language': 'Latin'}
chars_gt = [{'polygon': [], 'char': '', 'illegibility': False, 'language': 'Latin'}]
cur_line_gt['chars'] = chars_gt
# 字符串级别的信息
cur_line_gt['polygon'] = line['points']
cur_line_gt['text'] = line['transcription']
cur_line_gt['illegibility'] = line['illegibility']
cur_gt['annotations'].append(cur_line_gt)
data_list.append(cur_gt)
gt_dict['data_list'] = data_list
save(gt_dict, save_path)


if __name__ == '__main__':
gt_path = r'D:\dataset\LSVT\detection\train_full_labels.json'
img_folder = r'D:\dataset\LSVT\detection\imgs'
save_path = r'D:\dataset\LSVT\detection\train.json'
cvt(gt_path, save_path, img_folder)
2 changes: 1 addition & 1 deletion convert/RcCTS2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def decode_chars(char_list):
text_list = []
for char_dict in char_list:
polygon_list.append(np.array(char_dict['points']).reshape(-1, 2).tolist())
illegibility_list.append(char_dict['ignore'])
illegibility_list.append(True if char_dict['ignore'] == 1 else False)
text_list.append(char_dict['transcription'])
return polygon_list, illegibility_list, text_list

Expand Down
4 changes: 3 additions & 1 deletion convert/check_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from convert.utils import show_bbox_on_image, load_gt

if __name__ == '__main__':
json_path = r'D:\dataset\ReCTS\detection\train.json'
json_path = r'D:\dataset\LSVT\detection\train.json'
data = load_gt(json_path)
for img_path, gt in data.items():
print(gt['illegibility_list'])
print(gt['texts'])
img = Image.open(img_path)
img = show_bbox_on_image(img, gt['polygons'], gt['texts'])
plt.imshow(img)
Expand Down
16 changes: 10 additions & 6 deletions convert/crop_rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ def four_point_transform(image, pts):
return warped


if __name__ == '__main__':
json_path = r'D:\dataset\ReCTS\detection\train.json'
save_path = r'D:\dataset\ReCTS\recognition\train'
gt_path = pathlib.Path(save_path).parent / 'train.txt'
def crop(save_gt_path, json_path, save_path):
if os.path.exists(save_path):
shutil.rmtree(save_path, ignore_errors=True)
os.makedirs(save_path, exist_ok=True)
Expand All @@ -79,7 +76,7 @@ def four_point_transform(image, pts):
img_name = pathlib.Path(img_path).stem
for i, (polygon, text, illegibility, language) in enumerate(
zip(gt['polygons'], gt['texts'], gt['illegibility_list'], gt['language_list'])):
if illegibility or '###' in text or '*' in text:
if illegibility:
continue
polygon = np.array(polygon)
roi_img_save_path = os.path.join(save_path, '{}_{}.jpg'.format(img_name, i))
Expand All @@ -99,4 +96,11 @@ def four_point_transform(image, pts):
# plt.title(text)
# plt.imshow(roi_img)
# plt.show()
save(file_list, gt_path)
save(file_list, save_gt_path)


if __name__ == '__main__':
json_path = r'D:\dataset\LSVT\detection\train.json'
save_path = r'D:\dataset\LSVT\recognition\train'
gt_path = pathlib.Path(save_path).parent / 'train.txt'
crop(gt_path, json_path, save_path)
2 changes: 1 addition & 1 deletion convert/mlt20192json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def cvt(gt_path, save_path, img_folder):
for file_path in tqdm(get_file_list(gt_path, p_postfix=['.txt'])):
content = load(file_path)
file_path = pathlib.Path(file_path)
img_name = file_path.name.replace('gt_', '').replace('.txt', '.jpg')
img_name = file_path.name.replace('.txt', '.jpg')
cur_gt = {'img_name': img_name, 'annotations': []}
for line in content:
cur_line_gt = {'polygon': [], 'text': '', 'illegibility': False, 'language': 'Latin'}
Expand Down
48 changes: 48 additions & 0 deletions convert/sroie2json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# @Time : 2020/3/18 14:12
# @Author : zhoujun
"""
将icdar2015数据集转换为统一格式
"""
import pathlib
from tqdm import tqdm
from convert.utils import load, save, get_file_list


def cvt(gt_path, save_path, img_folder):
"""
将icdar2015格式的gt转换为json格式
:param gt_path:
:param save_path:
:return:
"""
gt_dict = {'data_root': img_folder}
data_list = []
for file_path in tqdm(get_file_list(gt_path, p_postfix=['.txt'])):
content = load(file_path)
file_path = pathlib.Path(file_path)
img_name = file_path.name.replace('.txt', '.jpg')
cur_gt = {'img_name': img_name, 'annotations': []}
for line in content:
cur_line_gt = {'polygon': [], 'text': '', 'illegibility': False, 'language': 'Latin'}
chars_gt = [{'polygon': [], 'char': '', 'illegibility': False, 'language': 'Latin'}]
cur_line_gt['chars'] = chars_gt
line = line.split(',')
if len(line) < 9:
continue
# 字符串级别的信息
x1, y1, x2, y2, x3, y3, x4, y4 = list(map(float, line[:8]))
cur_line_gt['polygon'] = [[x1, y1], [x2, y2], [x3, y3], [x4, y4]]
cur_line_gt['text'] = line[-1]
cur_line_gt['illegibility'] = True if cur_line_gt['text'] == '*' or cur_line_gt['text'] == '###' else False
cur_gt['annotations'].append(cur_line_gt)
data_list.append(cur_gt)
gt_dict['data_list'] = data_list
save(gt_dict, save_path)


if __name__ == '__main__':
gt_path = r'D:\dataset\SROIE2019\detection\test\gt'
img_folder = r'D:\dataset\SROIE2019\detection\test\imgs'
save_path = r'D:\dataset\SROIE2019\detection\test.json'
cvt(gt_path, save_path, img_folder)

0 comments on commit 58532f3

Please sign in to comment.