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

Method __getitem__

tools/data/simple_dataset.py:223–266  ·  view source on GitHub ↗
(self, properties)

Source from the content-addressed store, hash-verified

221 return data
222
223 def __getitem__(self, properties):
224 # properites is a tuple, contains (width, height, index)
225 img_height = properties[1]
226 idx = properties[2]
227 if self.ds_width and properties[3] is not None:
228 wh_ratio = properties[3]
229 img_width = img_height * (1 if int(round(wh_ratio)) == 0 else int(
230 round(wh_ratio)))
231 file_idx = self.wh_ratio_sort[idx]
232 else:
233 file_idx = self.data_idx_order_list[idx]
234 img_width = properties[0]
235 wh_ratio = None
236
237 data_line = self.data_lines[file_idx]
238 try:
239 data_line = data_line.decode('utf-8')
240 substr = data_line.strip('\n').split(self.delimiter)
241 file_name = substr[0]
242 file_name = self._try_parse_filename_list(file_name)
243 label = substr[1]
244 img_path = os.path.join(self.data_dir, file_name)
245 data = {'img_path': img_path, 'label': label}
246 if not os.path.exists(img_path):
247 raise Exception('{} does not exist!'.format(img_path))
248 with open(data['img_path'], 'rb') as f:
249 img = f.read()
250 data['image'] = img
251 data['ext_data'] = self.get_ext_data()
252 outs = transform(data, self.ops[:-1])
253 if outs is not None:
254 outs = self.resize_norm_img(outs, img_width, img_height)
255 outs = transform(outs, self.ops[-1:])
256 except:
257 self.logger.error(
258 'When parsing line {}, error happened with msg: {}'.format(
259 data_line, traceback.format_exc()))
260 outs = None
261 if outs is None:
262 # during evaluation, we should fix the idx to get same results for many times of evaluation.
263 rnd_idx = np.random.randint(self.__len__(
264 )) if self.mode == 'train' else (idx + 1) % self.__len__()
265 return self.__getitem__([img_width, img_height, rnd_idx, wh_ratio])
266 return outs

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected