voc_utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import os.path as osp
  19. import re
  20. import random
  21. import shutil
  22. __all__ = ['create_list']
  23. def create_list(devkit_dir, years, output_dir):
  24. """
  25. create following list:
  26. 1. trainval.txt
  27. 2. test.txt
  28. """
  29. trainval_list = []
  30. test_list = []
  31. for year in years:
  32. trainval, test = _walk_voc_dir(devkit_dir, year, output_dir)
  33. trainval_list.extend(trainval)
  34. test_list.extend(test)
  35. random.shuffle(trainval_list)
  36. with open(osp.join(output_dir, 'trainval.txt'), 'w') as ftrainval:
  37. for item in trainval_list:
  38. ftrainval.write(item[0] + ' ' + item[1] + '\n')
  39. with open(osp.join(output_dir, 'test.txt'), 'w') as fval:
  40. ct = 0
  41. for item in test_list:
  42. ct += 1
  43. fval.write(item[0] + ' ' + item[1] + '\n')
  44. def _get_voc_dir(devkit_dir, year, type):
  45. return osp.join(devkit_dir, 'VOC' + year, type)
  46. def _walk_voc_dir(devkit_dir, year, output_dir):
  47. filelist_dir = _get_voc_dir(devkit_dir, year, 'ImageSets/Main')
  48. annotation_dir = _get_voc_dir(devkit_dir, year, 'Annotations')
  49. img_dir = _get_voc_dir(devkit_dir, year, 'JPEGImages')
  50. trainval_list = []
  51. test_list = []
  52. added = set()
  53. for _, _, files in os.walk(filelist_dir):
  54. for fname in files:
  55. img_ann_list = []
  56. if re.match(r'[a-z]+_trainval\.txt', fname):
  57. img_ann_list = trainval_list
  58. elif re.match(r'[a-z]+_test\.txt', fname):
  59. img_ann_list = test_list
  60. else:
  61. continue
  62. fpath = osp.join(filelist_dir, fname)
  63. for line in open(fpath):
  64. name_prefix = line.strip().split()[0]
  65. if name_prefix in added:
  66. continue
  67. added.add(name_prefix)
  68. ann_path = osp.join(
  69. osp.relpath(annotation_dir, output_dir),
  70. name_prefix + '.xml')
  71. img_path = osp.join(
  72. osp.relpath(img_dir, output_dir), name_prefix + '.jpg')
  73. img_ann_list.append((img_path, ann_path))
  74. return trainval_list, test_list