json_results.py 4.9 KB

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