json_results.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import six
  15. import numpy as np
  16. def get_det_res(bboxes, bbox_nums, image_id, label_to_cat_id_map, bias=0):
  17. det_res = []
  18. k = 0
  19. for i in range(len(bbox_nums)):
  20. cur_image_id = int(image_id[i][0])
  21. det_nums = bbox_nums[i]
  22. for j in range(det_nums):
  23. dt = bboxes[k]
  24. k = k + 1
  25. num_id, score, xmin, ymin, xmax, ymax = dt.tolist()
  26. if int(num_id) < 0:
  27. continue
  28. category_id = label_to_cat_id_map[int(num_id)]
  29. w = xmax - xmin + bias
  30. h = ymax - ymin + bias
  31. bbox = [xmin, ymin, w, h]
  32. dt_res = {
  33. 'image_id': cur_image_id,
  34. 'category_id': category_id,
  35. 'bbox': bbox,
  36. 'score': score
  37. }
  38. det_res.append(dt_res)
  39. return det_res
  40. def get_det_poly_res(bboxes, bbox_nums, image_id, label_to_cat_id_map, bias=0):
  41. det_res = []
  42. k = 0
  43. for i in range(len(bbox_nums)):
  44. cur_image_id = int(image_id[i][0])
  45. det_nums = bbox_nums[i]
  46. for j in range(det_nums):
  47. dt = bboxes[k]
  48. k = k + 1
  49. num_id, score, x1, y1, x2, y2, x3, y3, x4, y4 = dt.tolist()
  50. if int(num_id) < 0:
  51. continue
  52. category_id = label_to_cat_id_map[int(num_id)]
  53. rbox = [x1, y1, x2, y2, x3, y3, x4, y4]
  54. dt_res = {
  55. 'image_id': cur_image_id,
  56. 'category_id': category_id,
  57. 'bbox': rbox,
  58. 'score': score
  59. }
  60. det_res.append(dt_res)
  61. return det_res
  62. def get_seg_res(masks, bboxes, mask_nums, image_id, label_to_cat_id_map):
  63. import pycocotools.mask as mask_util
  64. seg_res = []
  65. k = 0
  66. for i in range(len(mask_nums)):
  67. cur_image_id = int(image_id[i][0])
  68. det_nums = mask_nums[i]
  69. for j in range(det_nums):
  70. mask = masks[k].astype(np.uint8)
  71. score = float(bboxes[k][1])
  72. label = int(bboxes[k][0])
  73. k = k + 1
  74. if label == -1:
  75. continue
  76. cat_id = label_to_cat_id_map[label]
  77. rle = mask_util.encode(
  78. np.array(
  79. mask[:, :, None], order="F", dtype="uint8"))[0]
  80. if six.PY3:
  81. if 'counts' in rle:
  82. rle['counts'] = rle['counts'].decode("utf8")
  83. sg_res = {
  84. 'image_id': cur_image_id,
  85. 'category_id': cat_id,
  86. 'segmentation': rle,
  87. 'score': score
  88. }
  89. seg_res.append(sg_res)
  90. return seg_res
  91. def get_solov2_segm_res(results, image_id, num_id_to_cat_id_map):
  92. import pycocotools.mask as mask_util
  93. segm_res = []
  94. # for each batch
  95. segms = results['segm'].astype(np.uint8)
  96. clsid_labels = results['cate_label']
  97. clsid_scores = results['cate_score']
  98. lengths = segms.shape[0]
  99. im_id = int(image_id[0][0])
  100. if lengths == 0 or segms is None:
  101. return None
  102. # for each sample
  103. for i in range(lengths - 1):
  104. clsid = int(clsid_labels[i])
  105. catid = num_id_to_cat_id_map[clsid]
  106. score = float(clsid_scores[i])
  107. mask = segms[i]
  108. segm = mask_util.encode(np.array(mask[:, :, np.newaxis], order='F'))[0]
  109. segm['counts'] = segm['counts'].decode('utf8')
  110. coco_res = {
  111. 'image_id': im_id,
  112. 'category_id': catid,
  113. 'segmentation': segm,
  114. 'score': score
  115. }
  116. segm_res.append(coco_res)
  117. return segm_res
  118. def get_keypoint_res(results, im_id):
  119. anns = []
  120. preds = results['keypoint']
  121. for idx in range(im_id.shape[0]):
  122. image_id = im_id[idx].item()
  123. kpts, scores = preds[idx]
  124. for kpt, score in zip(kpts, scores):
  125. kpt = kpt.flatten()
  126. ann = {
  127. 'image_id': image_id,
  128. 'category_id': 1, # XXX hard code
  129. 'keypoints': kpt.tolist(),
  130. 'score': float(score)
  131. }
  132. x = kpt[0::3]
  133. y = kpt[1::3]
  134. x0, x1, y0, y1 = np.min(x).item(), np.max(x).item(), np.min(y).item(
  135. ), np.max(y).item()
  136. ann['area'] = (x1 - x0) * (y1 - y0)
  137. ann['bbox'] = [x0, y0, x1 - x0, y1 - y0]
  138. anns.append(ann)
  139. return anns