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.

images2coco.py 3.1 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. import mmcv
  5. from PIL import Image
  6. def parse_args():
  7. parser = argparse.ArgumentParser(
  8. description='Convert images to coco format without annotations')
  9. parser.add_argument('img_path', help='The root path of images')
  10. parser.add_argument(
  11. 'classes', type=str, help='The text file name of storage class list')
  12. parser.add_argument(
  13. 'out',
  14. type=str,
  15. help='The output annotation json file name, The save dir is in the '
  16. 'same directory as img_path')
  17. parser.add_argument(
  18. '-e',
  19. '--exclude-extensions',
  20. type=str,
  21. nargs='+',
  22. help='The suffix of images to be excluded, such as "png" and "bmp"')
  23. args = parser.parse_args()
  24. return args
  25. def collect_image_infos(path, exclude_extensions=None):
  26. img_infos = []
  27. images_generator = mmcv.scandir(path, recursive=True)
  28. for image_path in mmcv.track_iter_progress(list(images_generator)):
  29. if exclude_extensions is None or (
  30. exclude_extensions is not None
  31. and not image_path.lower().endswith(exclude_extensions)):
  32. image_path = os.path.join(path, image_path)
  33. img_pillow = Image.open(image_path)
  34. img_info = {
  35. 'filename': image_path,
  36. 'width': img_pillow.width,
  37. 'height': img_pillow.height,
  38. }
  39. img_infos.append(img_info)
  40. return img_infos
  41. def cvt_to_coco_json(img_infos, classes):
  42. image_id = 0
  43. coco = dict()
  44. coco['images'] = []
  45. coco['type'] = 'instance'
  46. coco['categories'] = []
  47. coco['annotations'] = []
  48. image_set = set()
  49. for category_id, name in enumerate(classes):
  50. category_item = dict()
  51. category_item['supercategory'] = str('none')
  52. category_item['id'] = int(category_id)
  53. category_item['name'] = str(name)
  54. coco['categories'].append(category_item)
  55. for img_dict in img_infos:
  56. file_name = img_dict['filename']
  57. assert file_name not in image_set
  58. image_item = dict()
  59. image_item['id'] = int(image_id)
  60. image_item['file_name'] = str(file_name)
  61. image_item['height'] = int(img_dict['height'])
  62. image_item['width'] = int(img_dict['width'])
  63. coco['images'].append(image_item)
  64. image_set.add(file_name)
  65. image_id += 1
  66. return coco
  67. def main():
  68. args = parse_args()
  69. assert args.out.endswith(
  70. 'json'), 'The output file name must be json suffix'
  71. # 1 load image list info
  72. img_infos = collect_image_infos(args.img_path, args.exclude_extensions)
  73. # 2 convert to coco format data
  74. classes = mmcv.list_from_file(args.classes)
  75. coco_info = cvt_to_coco_json(img_infos, classes)
  76. # 3 dump
  77. save_dir = os.path.join(args.img_path, '..', 'annotations')
  78. mmcv.mkdir_or_exist(save_dir)
  79. save_path = os.path.join(save_dir, args.out)
  80. mmcv.dump(coco_info, save_path)
  81. print(f'save json file: {save_path}')
  82. if __name__ == '__main__':
  83. main()

No Description

Contributors (3)