det.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 os
  15. import numpy as np
  16. from ...utils.io import ImageReader
  17. from ..base import BaseComponent
  18. def restructured_boxes(boxes, labels):
  19. return [
  20. {
  21. "cls_id": int(box[0]),
  22. "label": labels[int(box[0])],
  23. "score": float(box[1]),
  24. "coordinate": list(map(int, box[2:])),
  25. }
  26. for box in boxes
  27. ]
  28. class DetPostProcess(BaseComponent):
  29. """Save Result Transform"""
  30. INPUT_KEYS = ["img_path", "boxes"]
  31. OUTPUT_KEYS = ["boxes"]
  32. DEAULT_INPUTS = {"boxes": "boxes"}
  33. DEAULT_OUTPUTS = {"boxes": "boxes"}
  34. def __init__(self, threshold=0.5, labels=None):
  35. super().__init__()
  36. self.threshold = threshold
  37. self.labels = labels
  38. def apply(self, boxes):
  39. """apply"""
  40. expect_boxes = (boxes[:, 1] > self.threshold) & (boxes[:, 0] > -1)
  41. boxes = boxes[expect_boxes, :]
  42. boxes = restructured_boxes(boxes, self.labels)
  43. result = {"boxes": boxes}
  44. return result
  45. class CropByBoxes(BaseComponent):
  46. """Crop Image by Box"""
  47. INPUT_KEYS = ["img_path", "boxes", "labels"]
  48. OUTPUT_KEYS = ["img", "box", "label"]
  49. DEAULT_INPUTS = {"img_path": "img_path", "boxes": "boxes", "labels": "labels"}
  50. DEAULT_OUTPUTS = {"img": "img", "box": "box", "label": "label"}
  51. def __init__(self):
  52. super().__init__()
  53. self._reader = ImageReader(backend="opencv")
  54. def apply(self, img_path, boxes, labels=None):
  55. output_list = []
  56. img = self._reader.read(img_path)
  57. for bbox in boxes:
  58. label_id = int(bbox[0])
  59. box = bbox[2:]
  60. if labels is not None:
  61. label = labels[label_id]
  62. else:
  63. label = label_id
  64. xmin, ymin, xmax, ymax = [int(i) for i in box]
  65. img_crop = img[ymin:ymax, xmin:xmax]
  66. output_list.append({"img": img_crop, "box": box, "label": label})
  67. return output_list
  68. class DetPad(BaseComponent):
  69. INPUT_KEYS = "img"
  70. OUTPUT_KEYS = "img"
  71. DEAULT_INPUTS = {"img": "img"}
  72. DEAULT_OUTPUTS = {"img": "img"}
  73. def __init__(self, size, fill_value=[114.0, 114.0, 114.0]):
  74. """
  75. Pad image to a specified size.
  76. Args:
  77. size (list[int]): image target size
  78. fill_value (list[float]): rgb value of pad area, default (114.0, 114.0, 114.0)
  79. """
  80. super().__init__()
  81. if isinstance(size, int):
  82. size = [size, size]
  83. self.size = size
  84. self.fill_value = fill_value
  85. def apply(self, img):
  86. im = img
  87. im_h, im_w = im.shape[:2]
  88. h, w = self.size
  89. if h == im_h and w == im_w:
  90. return {"img": im}
  91. canvas = np.ones((h, w, 3), dtype=np.float32)
  92. canvas *= np.array(self.fill_value, dtype=np.float32)
  93. canvas[0:im_h, 0:im_w, :] = im.astype(np.float32)
  94. return {"img": canvas}