det.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. from ...utils.io import ImageReader
  16. from ..base import BaseComponent
  17. def restructured_boxes(boxes, labels):
  18. return [
  19. {
  20. "cls_id": int(box[0]),
  21. "label": labels[int(box[0])],
  22. "score": float(box[1]),
  23. "coordinate": list(map(int, box[2:])),
  24. }
  25. for box in boxes
  26. ]
  27. class DetPostProcess(BaseComponent):
  28. """Save Result Transform"""
  29. INPUT_KEYS = ["img_path", "boxes"]
  30. OUTPUT_KEYS = ["boxes"]
  31. DEAULT_INPUTS = {"boxes": "boxes"}
  32. DEAULT_OUTPUTS = {"boxes": "boxes"}
  33. def __init__(self, threshold=0.5, labels=None):
  34. super().__init__()
  35. self.threshold = threshold
  36. self.labels = labels
  37. def apply(self, boxes):
  38. """apply"""
  39. expect_boxes = (boxes[:, 1] > self.threshold) & (boxes[:, 0] > -1)
  40. boxes = boxes[expect_boxes, :]
  41. boxes = restructured_boxes(boxes, self.labels)
  42. result = {"boxes": boxes}
  43. return result
  44. class CropByBoxes(BaseComponent):
  45. """Crop Image by Box"""
  46. INPUT_KEYS = ["img_path", "boxes", "labels"]
  47. OUTPUT_KEYS = ["img", "box", "label"]
  48. DEAULT_INPUTS = {"img_path": "img_path", "boxes": "boxes", "labels": "labels"}
  49. DEAULT_OUTPUTS = {"img": "img", "box": "box", "label": "label"}
  50. def __init__(self):
  51. super().__init__()
  52. self._reader = ImageReader(backend="opencv")
  53. def apply(self, img_path, boxes, labels=None):
  54. output_list = []
  55. img = self._reader.read(img_path)
  56. for bbox in boxes:
  57. label_id = int(bbox[0])
  58. box = bbox[2:]
  59. if labels is not None:
  60. label = labels[label_id]
  61. else:
  62. label = label_id
  63. xmin, ymin, xmax, ymax = [int(i) for i in box]
  64. img_crop = img[ymin:ymax, xmin:xmax]
  65. output_list.append({"img": img_crop, "box": box, "label": label})
  66. return output_list