PaddleOCR/ppocr/data/simple_dataset.py

95 lines
3.6 KiB
Python
Raw Normal View History

2020-11-04 20:43:27 +08:00
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import os
import random
from paddle.io import Dataset
from .imaug import transform, create_operators
2020-11-05 15:13:36 +08:00
2020-11-04 20:43:27 +08:00
class SimpleDataSet(Dataset):
2020-11-05 15:13:36 +08:00
def __init__(self, config, mode, logger):
2020-11-04 20:43:27 +08:00
super(SimpleDataSet, self).__init__()
2020-11-12 23:55:52 +08:00
self.logger = logger
2020-11-05 15:13:36 +08:00
2020-11-04 20:43:27 +08:00
global_config = config['Global']
dataset_config = config[mode]['dataset']
loader_config = config[mode]['loader']
2020-11-05 15:13:36 +08:00
2020-11-04 20:43:27 +08:00
self.delimiter = dataset_config.get('delimiter', '\t')
label_file_list = dataset_config.pop('label_file_list')
data_source_num = len(label_file_list)
2020-11-26 21:32:33 +08:00
ratio_list = dataset_config.get("ratio_list", [1.0])
if isinstance(ratio_list, (float, int)):
2020-12-10 10:19:39 +08:00
ratio_list = [float(ratio_list)] * int(data_source_num)
2020-11-05 15:13:36 +08:00
assert len(
ratio_list
) == data_source_num, "The length of ratio_list should be the same as the file_list."
2020-11-04 20:43:27 +08:00
self.data_dir = dataset_config['data_dir']
self.do_shuffle = loader_config['shuffle']
2020-11-05 15:13:36 +08:00
2020-11-04 20:43:27 +08:00
logger.info("Initialize indexs of datasets:%s" % label_file_list)
2020-12-10 10:19:39 +08:00
self.data_lines = self.get_image_info_list(label_file_list, ratio_list)
2020-11-26 21:32:33 +08:00
self.data_idx_order_list = list(range(len(self.data_lines)))
if mode.lower() == "train":
self.shuffle_data_random()
2020-11-04 20:43:27 +08:00
self.ops = create_operators(dataset_config['transforms'], global_config)
2020-12-10 10:19:39 +08:00
def get_image_info_list(self, file_list, ratio_list):
2020-11-04 20:43:27 +08:00
if isinstance(file_list, str):
file_list = [file_list]
2020-11-26 21:32:33 +08:00
data_lines = []
for idx, file in enumerate(file_list):
2020-11-04 20:43:27 +08:00
with open(file, "rb") as f:
lines = f.readlines()
2020-12-10 11:00:05 +08:00
lines = random.sample(lines,
round(len(lines) * ratio_list[idx]))
2020-11-26 21:32:33 +08:00
data_lines.extend(lines)
return data_lines
2020-11-04 20:43:27 +08:00
def shuffle_data_random(self):
if self.do_shuffle:
2020-11-26 21:32:33 +08:00
random.shuffle(self.data_lines)
2020-11-04 20:43:27 +08:00
return
2020-11-05 15:13:36 +08:00
2020-11-04 20:43:27 +08:00
def __getitem__(self, idx):
2020-11-26 21:32:33 +08:00
file_idx = self.data_idx_order_list[idx]
data_line = self.data_lines[file_idx]
2020-11-12 23:55:52 +08:00
try:
data_line = data_line.decode('utf-8')
substr = data_line.strip("\n").split(self.delimiter)
2020-11-21 23:02:18 +08:00
file_name = substr[0]
label = substr[1]
2020-11-12 23:55:52 +08:00
img_path = os.path.join(self.data_dir, file_name)
data = {'img_path': img_path, 'label': label}
2020-12-09 14:59:04 +08:00
if not os.path.exists(img_path):
raise Exception("{} does not exist!".format(img_path))
2020-11-12 23:55:52 +08:00
with open(data['img_path'], 'rb') as f:
img = f.read()
data['image'] = img
outs = transform(data, self.ops)
except Exception as e:
self.logger.error(
"When parsing line {}, error happened with msg: {}".format(
data_line, e))
outs = None
2020-11-04 20:43:27 +08:00
if outs is None:
return self.__getitem__(np.random.randint(self.__len__()))
return outs
def __len__(self):
return len(self.data_idx_order_list)