json_results.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 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 strip_mask(mask):
  63. row = mask[0, 0, :]
  64. col = mask[0, :, 0]
  65. im_h = len(col) - np.count_nonzero(col == -1)
  66. im_w = len(row) - np.count_nonzero(row == -1)
  67. return mask[:, :im_h, :im_w]
  68. def get_seg_res(masks, bboxes, mask_nums, image_id, label_to_cat_id_map):
  69. import pycocotools.mask as mask_util
  70. seg_res = []
  71. k = 0
  72. for i in range(len(mask_nums)):
  73. cur_image_id = int(image_id[i][0])
  74. det_nums = mask_nums[i]
  75. mask_i = masks[k:k + det_nums]
  76. mask_i = strip_mask(mask_i)
  77. for j in range(det_nums):
  78. mask = mask_i[j].astype(np.uint8)
  79. score = float(bboxes[k][1])
  80. label = int(bboxes[k][0])
  81. k = k + 1
  82. if label == -1:
  83. continue
  84. cat_id = label_to_cat_id_map[label]
  85. rle = mask_util.encode(
  86. np.array(
  87. mask[:, :, None], order="F", dtype="uint8"))[0]
  88. if six.PY3:
  89. if 'counts' in rle:
  90. rle['counts'] = rle['counts'].decode("utf8")
  91. sg_res = {
  92. 'image_id': cur_image_id,
  93. 'category_id': cat_id,
  94. 'segmentation': rle,
  95. 'score': score
  96. }
  97. seg_res.append(sg_res)
  98. return seg_res
  99. def get_solov2_segm_res(results, image_id, num_id_to_cat_id_map):
  100. import pycocotools.mask as mask_util
  101. segm_res = []
  102. # for each batch
  103. segms = results['segm'].astype(np.uint8)
  104. clsid_labels = results['cate_label']
  105. clsid_scores = results['cate_score']
  106. lengths = segms.shape[0]
  107. im_id = int(image_id[0][0])
  108. if lengths == 0 or segms is None:
  109. return None
  110. # for each sample
  111. for i in range(lengths - 1):
  112. clsid = int(clsid_labels[i])
  113. catid = num_id_to_cat_id_map[clsid]
  114. score = float(clsid_scores[i])
  115. mask = segms[i]
  116. segm = mask_util.encode(np.array(mask[:, :, np.newaxis], order='F'))[0]
  117. segm['counts'] = segm['counts'].decode('utf8')
  118. coco_res = {
  119. 'image_id': im_id,
  120. 'category_id': catid,
  121. 'segmentation': segm,
  122. 'score': score
  123. }
  124. segm_res.append(coco_res)
  125. return segm_res
  126. def get_keypoint_res(results, im_id):
  127. anns = []
  128. preds = results['keypoint']
  129. for idx in range(im_id.shape[0]):
  130. image_id = im_id[idx].item()
  131. kpts, scores = preds[idx]
  132. for kpt, score in zip(kpts, scores):
  133. kpt = kpt.flatten()
  134. ann = {
  135. 'image_id': image_id,
  136. 'category_id': 1, # XXX hard code
  137. 'keypoints': kpt.tolist(),
  138. 'score': float(score)
  139. }
  140. x = kpt[0::3]
  141. y = kpt[1::3]
  142. x0, x1, y0, y1 = np.min(x).item(), np.max(x).item(), np.min(y).item(
  143. ), np.max(y).item()
  144. ann['area'] = (x1 - x0) * (y1 - y0)
  145. ann['bbox'] = [x0, y0, x1 - x0, y1 - y0]
  146. anns.append(ann)
  147. return anns