json_Img2Json.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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, cat_keyname):
  27. print('Get Test'.center(100, '-'))
  28. print()
  29. print('json read...\n')
  30. data = {}
  31. with open(js_train_path, 'r') as load_f:
  32. data_train = json.load(load_f)
  33. file_list = os.listdir(test_image_path)
  34. # sort method
  35. # file_list.sort(key=lambda x: int(x.split('.')[0]))
  36. # file_list.sort()
  37. print('test image read...')
  38. with tqdm(file_list) as pbar:
  39. images = []
  40. for index, img_name in enumerate(pbar):
  41. img_path = os.path.join(test_image_path, img_name)
  42. img = cv2.imread(img_path)
  43. tmp = {}
  44. tmp['id'] = index
  45. tmp['width'] = img.shape[1]
  46. tmp['height'] = img.shape[0]
  47. tmp['file_name'] = img_name
  48. images.append(tmp)
  49. print('\n total test image:', len(file_list))
  50. data[image_keyname] = images
  51. data[cat_keyname] = data_train[cat_keyname]
  52. with open(js_test_path, 'w') as f:
  53. json.dump(data, f)
  54. def get_args():
  55. parser = argparse.ArgumentParser(description='Get Test Json')
  56. # parameters
  57. parser.add_argument('--test_image_path', type=str,
  58. help='test image path')
  59. parser.add_argument('--json_train_path', type=str,
  60. help='train json path, provide categories information')
  61. parser.add_argument('--json_test_path', type=str,
  62. help='test json path to save')
  63. parser.add_argument('--image_keyname', type=str, default='images',
  64. help='image key name in json, default images')
  65. parser.add_argument('--cat_keyname', type=str, default='categories',
  66. help='categories key name in json, default categories')
  67. parser.add_argument('-Args_show', '--Args_show', type=bool, default=True,
  68. help='Args_show(default: True), if True, show args info')
  69. args = parser.parse_args()
  70. if args.Args_show:
  71. print('Args'.center(100, '-'))
  72. for k, v in vars(args).items():
  73. print('%s = %s' % (k, v))
  74. print()
  75. return args
  76. if __name__ == '__main__':
  77. args = get_args()
  78. js_test(args.test_image_path, args.json_train_path, args.json_test_path, args.image_keyname, args.cat_keyname)