json_Img2Json.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) 2022 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. '''
  15. @File Description:
  16. # 根据test影像文件夹生成test.json
  17. python ./coco_tools/json_Img2Json.py \
  18. --test_image_path=./test2017 \
  19. --json_train_path=./annotations/instances_val2017.json \
  20. --json_test_path=./test.json
  21. '''
  22. import os, cv2
  23. import json
  24. import argparse
  25. from tqdm import tqdm
  26. def js_test(test_image_path, js_train_path, js_test_path, image_keyname,
  27. cat_keyname):
  28. print('Get Test'.center(100, '-'))
  29. print()
  30. print('json read...\n')
  31. data = {}
  32. with open(js_train_path, 'r') as load_f:
  33. data_train = json.load(load_f)
  34. file_list = os.listdir(test_image_path)
  35. # sort method
  36. # file_list.sort(key=lambda x: int(x.split('.')[0]))
  37. # file_list.sort()
  38. print('test image read...')
  39. with tqdm(file_list) as pbar:
  40. images = []
  41. for index, img_name in enumerate(pbar):
  42. img_path = os.path.join(test_image_path, img_name)
  43. img = cv2.imread(img_path)
  44. tmp = {}
  45. tmp['id'] = index
  46. tmp['width'] = img.shape[1]
  47. tmp['height'] = img.shape[0]
  48. tmp['file_name'] = img_name
  49. images.append(tmp)
  50. print('\n total test image:', len(file_list))
  51. data[image_keyname] = images
  52. data[cat_keyname] = data_train[cat_keyname]
  53. with open(js_test_path, 'w') as f:
  54. json.dump(data, f)
  55. def get_args():
  56. parser = argparse.ArgumentParser(description='Get Test Json')
  57. # parameters
  58. parser.add_argument('--test_image_path', type=str, help='test image path')
  59. parser.add_argument(
  60. '--json_train_path',
  61. type=str,
  62. help='train json path, provide categories information')
  63. parser.add_argument(
  64. '--json_test_path', type=str, help='test json path to save')
  65. parser.add_argument(
  66. '--image_keyname',
  67. type=str,
  68. default='images',
  69. help='image key name in json, default images')
  70. parser.add_argument(
  71. '--cat_keyname',
  72. type=str,
  73. default='categories',
  74. help='categories key name in json, default categories')
  75. parser.add_argument(
  76. '-Args_show',
  77. '--Args_show',
  78. type=bool,
  79. default=True,
  80. help='Args_show(default: True), if True, show args info')
  81. args = parser.parse_args()
  82. if args.Args_show:
  83. print('Args'.center(100, '-'))
  84. for k, v in vars(args).items():
  85. print('%s = %s' % (k, v))
  86. print()
  87. return args
  88. if __name__ == '__main__':
  89. args = get_args()
  90. js_test(args.test_image_path, args.json_train_path, args.json_test_path,
  91. args.image_keyname, args.cat_keyname)