json_results.py 5.1 KB

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