voc_split.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (c) 2020 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. import os.path as osp
  15. import random
  16. import xml.etree.ElementTree as ET
  17. from .utils import list_files, is_pic, replace_ext
  18. import paddlex.utils.logging as logging
  19. def split_voc_dataset(dataset_dir, val_percent, test_percent, save_dir):
  20. if not osp.exists(osp.join(dataset_dir, "JPEGImages")):
  21. logging.error("\'JPEGImages\' is not found in {}!".format(dataset_dir))
  22. if not osp.exists(osp.join(dataset_dir, "Annotations")):
  23. logging.error("\'Annotations\' is not found in {}!".format(
  24. dataset_dir))
  25. all_image_files = list_files(osp.join(dataset_dir, "JPEGImages"))
  26. image_anno_list = list()
  27. label_list = list()
  28. for image_file in all_image_files:
  29. if not is_pic(image_file):
  30. continue
  31. anno_name = replace_ext(image_file, "xml")
  32. if osp.exists(osp.join(dataset_dir, "Annotations", anno_name)):
  33. image_anno_list.append([image_file, anno_name])
  34. try:
  35. tree = ET.parse(
  36. osp.join(dataset_dir, "Annotations", anno_name))
  37. except:
  38. raise Exception("文件{}不是一个良构的xml文件,请检查标注文件".format(
  39. osp.join(dataset_dir, "Annotations", anno_name)))
  40. objs = tree.findall("object")
  41. for i, obj in enumerate(objs):
  42. cname = obj.find('name').text
  43. if not cname in label_list:
  44. label_list.append(cname)
  45. random.shuffle(image_anno_list)
  46. image_num = len(image_anno_list)
  47. val_num = int(image_num * val_percent)
  48. test_num = int(image_num * test_percent)
  49. train_num = image_num - val_num - test_num
  50. train_image_anno_list = image_anno_list[:train_num]
  51. val_image_anno_list = image_anno_list[train_num:train_num + val_num]
  52. test_image_anno_list = image_anno_list[train_num + val_num:]
  53. with open(
  54. osp.join(save_dir, 'train_list.txt'), mode='w',
  55. encoding='utf-8') as f:
  56. for x in train_image_anno_list:
  57. file = osp.join("JPEGImages", x[0])
  58. label = osp.join("Annotations", x[1])
  59. f.write('{} {}\n'.format(file, label))
  60. with open(
  61. osp.join(save_dir, 'val_list.txt'), mode='w',
  62. encoding='utf-8') as f:
  63. for x in val_image_anno_list:
  64. file = osp.join("JPEGImages", x[0])
  65. label = osp.join("Annotations", x[1])
  66. f.write('{} {}\n'.format(file, label))
  67. if len(test_image_anno_list):
  68. with open(
  69. osp.join(save_dir, 'test_list.txt'), mode='w',
  70. encoding='utf-8') as f:
  71. for x in test_image_anno_list:
  72. file = osp.join("JPEGImages", x[0])
  73. label = osp.join("Annotations", x[1])
  74. f.write('{} {}\n'.format(file, label))
  75. with open(
  76. osp.join(save_dir, 'labels.txt'), mode='w', encoding='utf-8') as f:
  77. for l in sorted(label_list):
  78. f.write('{}\n'.format(l))
  79. return train_num, val_num, test_num