coco_eval.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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
  15. import sys
  16. import argparse
  17. import numpy as np
  18. from pycocotools.coco import COCO
  19. from pycocotools.cocoeval import COCOeval
  20. def parse_args():
  21. """ Parse input arguments """
  22. parser = argparse.ArgumentParser()
  23. parser.add_argument(
  24. '--prediction_json_path', type=str, default='./bbox.json')
  25. parser.add_argument(
  26. '--gt_json_path', type=str, default='./instance_val.json')
  27. args = parser.parse_args()
  28. return args
  29. def json_eval_results(args):
  30. """
  31. cocoapi eval with already exists bbox.json
  32. """
  33. prediction_json_path = args.prediction_json_path
  34. gt_json_path = args.gt_json_path
  35. assert os.path.exists(
  36. prediction_json_path), "The json directory:{} does not exist".format(
  37. prediction_json_path)
  38. cocoapi_eval(prediction_json_path, "bbox", anno_file=gt_json_path)
  39. def cocoapi_eval(jsonfile,
  40. style,
  41. coco_gt=None,
  42. anno_file=None,
  43. max_dets=(100, 300, 1000),
  44. sigmas=None,
  45. use_area=True):
  46. """
  47. Args:
  48. jsonfile (str): Evaluation json file, eg: bbox.json
  49. style (str): COCOeval style, can be `bbox`
  50. coco_gt (str): Whether to load COCOAPI through anno_file,
  51. eg: coco_gt = COCO(anno_file)
  52. anno_file (str): COCO annotations file.
  53. max_dets (tuple): COCO evaluation maxDets.
  54. sigmas (nparray): keypoint labelling sigmas.
  55. use_area (bool): If gt annotations (eg. CrowdPose, AIC)
  56. do not have 'area', please set use_area=False.
  57. """
  58. assert coco_gt is not None or anno_file is not None
  59. if coco_gt is None:
  60. coco_gt = COCO(anno_file)
  61. coco_dt = coco_gt.loadRes(jsonfile)
  62. if style == 'proposal':
  63. coco_eval = COCOeval(coco_gt, coco_dt, 'bbox')
  64. coco_eval.params.useCats = 0
  65. coco_eval.params.maxDets = list(max_dets)
  66. elif style == 'keypoints_crowd':
  67. coco_eval = COCOeval(coco_gt, coco_dt, style, sigmas, use_area)
  68. else:
  69. coco_eval = COCOeval(coco_gt, coco_dt, style)
  70. coco_eval.evaluate()
  71. coco_eval.accumulate()
  72. coco_eval.summarize()
  73. # flush coco evaluation result
  74. sys.stdout.flush()
  75. return coco_eval.stats
  76. if __name__ == "__main__":
  77. args = parse_args()
  78. json_eval_results(args)