You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

convert_iiit5k.py 2.1 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import argparse
  16. from scipy import io
  17. ###############################################
  18. # load testdata
  19. # testdata.mat structure
  20. # test[:][0] : image name
  21. # test[:][1] : label
  22. # test[:][2] : 50 lexicon
  23. # test[:][3] : 1000 lexicon
  24. ##############################################
  25. def init_args():
  26. parser = argparse.ArgumentParser('')
  27. parser.add_argument('-m', '--mat_file', type=str, default='testdata.mat',
  28. help='Directory where character dictionaries for the dataset were stored')
  29. parser.add_argument('-o', '--output_dir', type=str, default='./processed',
  30. help='Directory where ord map dictionaries for the dataset were stored')
  31. parser.add_argument('-a', '--output_annotation', type=str, default='./annotation.txt',
  32. help='Directory where ord map dictionaries for the dataset were stored')
  33. return parser.parse_args()
  34. def mat_to_list(mat_file):
  35. ann_ori = io.loadmat(mat_file)
  36. testdata = ann_ori['testdata'][0]
  37. ann_output = []
  38. for elem in testdata:
  39. img_name = elem[0]
  40. label = elem[1]
  41. ann = img_name+',' +label
  42. ann_output.append(ann)
  43. return ann_output
  44. def convert():
  45. args = init_args()
  46. ann_list = mat_to_list(args.mat_file)
  47. ann_file = args.output_annotation
  48. with open(ann_file, 'w') as f:
  49. for line in ann_list:
  50. txt = line + '\n'
  51. f.write(txt)
  52. if __name__ == "__main__":
  53. convert()