MCPcopy Create free account
hub / github.com/Topdu/OpenOCR / __getitem__

Method __getitem__

tools/data/simple_dataset.py:139–168  ·  view source on GitHub ↗
(self, idx)

Source from the content-addressed store, hash-verified

137 return ext_data
138
139 def __getitem__(self, idx):
140 file_idx = self.data_idx_order_list[idx]
141 data_line = self.data_lines[file_idx]
142 try:
143 data_line = data_line.decode('utf-8')
144 substr = data_line.strip('\n').split(self.delimiter)
145 file_name = substr[0]
146 file_name = self._try_parse_filename_list(file_name)
147 label = substr[1]
148 img_path = os.path.join(self.data_dir, file_name)
149 data = {'img_path': img_path, 'label': label}
150
151 if not os.path.exists(img_path):
152 raise Exception('{} does not exist!'.format(img_path))
153 with open(data['img_path'], 'rb') as f:
154 img = f.read()
155 data['image'] = img
156 data['ext_data'] = self.get_ext_data()
157 outs = transform(data, self.ops)
158 except:
159 self.logger.error(
160 'When parsing line {}, error happened with msg: {}'.format(
161 data_line, traceback.format_exc()))
162 outs = None
163 if outs is None:
164 # during evaluation, we should fix the idx to get same results for many times of evaluation.
165 rnd_idx = np.random.randint(self.__len__(
166 )) if self.mode == 'train' else (idx + 1) % self.__len__()
167 return self.__getitem__(rnd_idx)
168 return outs
169
170 def __len__(self):
171 return len(self.data_idx_order_list)

Callers

nothing calls this directly

Calls 5

get_ext_dataMethod · 0.95
__len__Method · 0.95
transformFunction · 0.90
decodeMethod · 0.45

Tested by

no test coverage detected