Skip to content

Commit

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


def decode_chars(char_list):
polygon_list = []
illegibility_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'])
text_list.append(char_dict['transcription'])
return polygon_list, illegibility_list, text_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=['.json'])):
content = load(file_path)
file_path = pathlib.Path(file_path)
img_name = file_path.stem + '.jpg'
cur_gt = {'img_name': img_name, 'annotations': []}
char_polygon_list, char_illegibility_list, char_text_list = decode_chars(content['chars'])
for line in content['lines']:
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'] = np.array(line['points']).reshape(-1, 2).tolist()
cur_line_gt['text'] = line['transcription']
cur_line_gt['illegibility'] = True if line['ignore'] == 1 else False
str_len = len(line['transcription'])
# 字符信息
flag = False
for char_idx in range(len(char_polygon_list)):
for str_idx in range(1, str_len + 1):
if ''.join(char_text_list[char_idx:char_idx + str_idx]) == line['transcription']:
chars_gt = []
for j in range(char_idx, char_idx + str_idx):
chars_gt.append({'polygon': char_polygon_list[j], 'char': char_text_list[j],
'illegibility': char_illegibility_list[j], 'language': 'Latin'})
cur_line_gt['chars'] = chars_gt
char_polygon_list = char_polygon_list[char_idx + str_len:]
char_text_list = char_text_list[char_idx + str_len:]
char_illegibility_list = char_illegibility_list[char_idx + str_len:]
flag = True
break
if flag:
break
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\ReCTS\detection\gt'
img_folder = r'D:\dataset\ReCTS\detection\img'
save_path = r'D:\dataset\ReCTS\detection\train.json'
cvt(gt_path, save_path, img_folder)
2 changes: 1 addition & 1 deletion convert/check_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from convert.utils import show_bbox_on_image, load_gt

if __name__ == '__main__':
json_path = r'D:\dataset\mlt2019\detection\gt.json'
json_path = r'D:\dataset\ReCTS\detection\train.json'
data = load_gt(json_path)
for img_path, gt in data.items():
img = Image.open(img_path)
Expand Down
3 changes: 2 additions & 1 deletion convert/convert2jpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
for img_path in tqdm(get_file_list(img_folder, p_postfix=['.*'])):
img_path = pathlib.Path(img_path)
save_path = img_path.parent / (img_path.stem + '.jpg')
os.rename(img_path, save_path)
if img_path != save_path:
os.rename(img_path, save_path)
6 changes: 3 additions & 3 deletions convert/crop_rec.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def four_point_transform(image, pts):


if __name__ == '__main__':
json_path = r'D:\dataset\COCO_Text\detection\train.json'
save_path = r'D:\dataset\COCO_Text\recognition\train'
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'
if os.path.exists(save_path):
shutil.rmtree(save_path, ignore_errors=True)
Expand All @@ -79,7 +79,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:
if illegibility or '###' in text or '*' in text:
continue
polygon = np.array(polygon)
roi_img_save_path = os.path.join(save_path, '{}_{}.jpg'.format(img_name, i))
Expand Down
7 changes: 4 additions & 3 deletions convert/icdar2015tojson.py → convert/icdar20152json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from convert.utils import load, save, get_file_list


def cvt(gt_path, save_path):
def cvt(gt_path, save_path, img_folder):
"""
将icdar2015格式的gt转换为json格式
:param gt_path:
:param save_path:
:return:
"""
gt_dict = {'data_root': gt_path}
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)
Expand All @@ -41,5 +41,6 @@ def cvt(gt_path, save_path):

if __name__ == '__main__':
gt_path = r'D:\dataset\icdar2015\detection\test\gt'
img_folder = r'D:\dataset\icdar2015\detection\test\imgs'
save_path = r'D:\dataset\icdar2015\detection\test.json'
cvt(gt_path, save_path)
cvt(gt_path, save_path, img_folder)
7 changes: 4 additions & 3 deletions convert/mlt2019tojson.py → convert/mlt20192json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from convert.utils import load, save, get_file_list


def cvt(gt_path, save_path):
def cvt(gt_path, save_path, img_folder):
"""
将icdar2015格式的gt转换为json格式
:param gt_path:
:param save_path:
:return:
"""
gt_dict = {'data_root': gt_path}
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)
Expand Down Expand Up @@ -45,5 +45,6 @@ def cvt(gt_path, save_path):

if __name__ == '__main__':
gt_path = r'D:\dataset\mlt2019\detection\gt'
img_folder = r'D:\dataset\mlt2019\detection\imgs'
save_path = r'D:\dataset\mlt2019\detection\gt.json'
cvt(gt_path, save_path)
cvt(gt_path, save_path, img_folder)
4 changes: 2 additions & 2 deletions convert/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def load_gt(json_path):
polygons.append(char_annotation['polygon'])
texts.append(char_annotation['char'])
illegibility_list.append(char_annotation['illegibility'])
language_list.append(char_annotation['language_'])
language_list.append(char_annotation['language'])
d[img_path] = {'polygons': polygons, 'texts': texts, 'illegibility_list': illegibility_list,
'language_list': language_list}
return d
return d

0 comments on commit 5ba9f29

Please sign in to comment.