coco.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # This file is made availabel under the Apache license
  2. # This file is based on code availabel under Simplified BSD Licens:
  3. # https://github.com/cocodataset/cocoapi/blob/8c9bcc3cf640524c4c20a9c40e89cb6a2f2fa0e9/PythonAPI/pycocotools/coco.py#L305
  4. #
  5. # Copyright (c) 2014, Piotr Dollar and Tsung-Yi Lin
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are met:
  10. #
  11. # 1. Redistributions of source code must retain the above copyright notice, this
  12. # list of conditions and the following disclaimer.
  13. # 2. Redistributions in binary form must reproduce the above copyright notice,
  14. # this list of conditions and the following disclaimer in the documentation
  15. # and/or other materials provided with the distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #
  28. # The views and conclusions contained in the software and documentation are those
  29. # of the authors and should not be interpreted as representing official policies,
  30. # either expressed or implied, of the FreeBSD Project.
  31. def loadRes(coco_obj, anns):
  32. """
  33. Load result file and return a result api object.
  34. :param resFile (str) : file name of result file
  35. :return: res (obj) : result api object
  36. """
  37. # This function has the same functionality as pycocotools.COCO.loadRes,
  38. # except that the input anns is list of results rather than a json file.
  39. # Refer to
  40. # https://github.com/cocodataset/cocoapi/blob/8c9bcc3cf640524c4c20a9c40e89cb6a2f2fa0e9/PythonAPI/pycocotools/coco.py#L305,
  41. # matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
  42. # or matplotlib.backends is imported for the first time
  43. # pycocotools import matplotlib
  44. import matplotlib
  45. matplotlib.use('Agg')
  46. from pycocotools.coco import COCO
  47. import pycocotools.mask as maskUtils
  48. import time
  49. res = COCO()
  50. res.dataset['images'] = [img for img in coco_obj.dataset['images']]
  51. tic = time.time()
  52. assert type(anns) == list, 'results in not an array of objects'
  53. annsImgIds = [ann['image_id'] for ann in anns]
  54. assert set(annsImgIds) == (set(annsImgIds) & set(coco_obj.getImgIds())), \
  55. 'Results do not correspond to current coco set'
  56. if 'caption' in anns[0]:
  57. imgIds = set([img['id'] for img in res.dataset['images']]) & set(
  58. [ann['image_id'] for ann in anns])
  59. res.dataset['images'] = [
  60. img for img in res.dataset['images'] if img['id'] in imgIds
  61. ]
  62. for id, ann in enumerate(anns):
  63. ann['id'] = id + 1
  64. elif 'bbox' in anns[0] and not anns[0]['bbox'] == []:
  65. res.dataset['categories'] = copy.deepcopy(coco_obj.dataset[
  66. 'categories'])
  67. for id, ann in enumerate(anns):
  68. bb = ann['bbox']
  69. x1, x2, y1, y2 = [bb[0], bb[0] + bb[2], bb[1], bb[1] + bb[3]]
  70. if not 'segmentation' in ann:
  71. ann['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]]
  72. ann['area'] = bb[2] * bb[3]
  73. ann['id'] = id + 1
  74. ann['iscrowd'] = 0
  75. elif 'segmentation' in anns[0]:
  76. res.dataset['categories'] = copy.deepcopy(coco_obj.dataset[
  77. 'categories'])
  78. for id, ann in enumerate(anns):
  79. # now only support compressed RLE format as segmentation results
  80. ann['area'] = maskUtils.area(ann['segmentation'])
  81. if not 'bbox' in ann:
  82. ann['bbox'] = maskUtils.toBbox(ann['segmentation'])
  83. ann['id'] = id + 1
  84. ann['iscrowd'] = 0
  85. elif 'keypoints' in anns[0]:
  86. res.dataset['categories'] = copy.deepcopy(coco_obj.dataset[
  87. 'categories'])
  88. for id, ann in enumerate(anns):
  89. s = ann['keypoints']
  90. x = s[0::3]
  91. y = s[1::3]
  92. x0, x1, y0, y1 = np.min(x), np.max(x), np.min(y), np.max(y)
  93. ann['area'] = (x1 - x0) * (y1 - y0)
  94. ann['id'] = id + 1
  95. ann['bbox'] = [x0, y0, x1 - x0, y1 - y0]
  96. res.dataset['annotations'] = anns
  97. res.createIndex()
  98. return res