_360cc.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from __future__ import print_function, absolute_import
  2. import torch.utils.data as data
  3. import os
  4. import numpy as np
  5. import cv2
  6. class _360CC(data.Dataset):
  7. def __init__(self, config, is_train=True):
  8. self.root = config.DATASET.ROOT
  9. self.is_train = is_train
  10. self.inp_h = config.MODEL.IMAGE_SIZE.H
  11. self.inp_w = config.MODEL.IMAGE_SIZE.W
  12. self.dataset_name = config.DATASET.DATASET
  13. self.mean = np.array(config.DATASET.MEAN, dtype=np.float32)
  14. self.std = np.array(config.DATASET.STD, dtype=np.float32)
  15. char_file = config.DATASET.CHAR_FILE
  16. with open(char_file, 'rb') as file:
  17. char_dict = {num: char.strip().decode('gbk', 'ignore') for num, char in enumerate(file.readlines())}
  18. txt_file = config.DATASET.JSON_FILE['train'] if is_train else config.DATASET.JSON_FILE['val']
  19. # convert name:indices to name:string
  20. self.labels = []
  21. with open(txt_file, 'r', encoding='utf-8') as file:
  22. contents = file.readlines()
  23. for c in contents:
  24. imgname = c.split(' ')[0]
  25. indices = c.split(' ')[1:]
  26. string = ''.join([char_dict[int(idx)] for idx in indices])
  27. self.labels.append({imgname: string})
  28. print("load {} images!".format(self.__len__()))
  29. def __len__(self):
  30. return len(self.labels)
  31. def __getitem__(self, idx):
  32. img_name = list(self.labels[idx].keys())[0]
  33. img = cv2.imread(os.path.join(self.root, img_name))
  34. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  35. img_h, img_w = img.shape
  36. img = cv2.resize(img, (0,0), fx=self.inp_w / img_w, fy=self.inp_h / img_h, interpolation=cv2.INTER_CUBIC)
  37. img = np.reshape(img, (self.inp_h, self.inp_w, 1))
  38. img = img.astype(np.float32)
  39. img = (img/255. - self.mean) / self.std
  40. img = img.transpose([2, 0, 1])
  41. return img, idx