box_utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (c) 2021 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. def bbox_area(src_bbox):
  15. if src_bbox[2] < src_bbox[0] or src_bbox[3] < src_bbox[1]:
  16. return 0.
  17. else:
  18. width = src_bbox[2] - src_bbox[0]
  19. height = src_bbox[3] - src_bbox[1]
  20. return width * height
  21. def jaccard_overlap(sample_bbox, object_bbox):
  22. if sample_bbox[0] >= object_bbox[2] or \
  23. sample_bbox[2] <= object_bbox[0] or \
  24. sample_bbox[1] >= object_bbox[3] or \
  25. sample_bbox[3] <= object_bbox[1]:
  26. return 0
  27. intersect_xmin = max(sample_bbox[0], object_bbox[0])
  28. intersect_ymin = max(sample_bbox[1], object_bbox[1])
  29. intersect_xmax = min(sample_bbox[2], object_bbox[2])
  30. intersect_ymax = min(sample_bbox[3], object_bbox[3])
  31. intersect_size = (intersect_xmax - intersect_xmin) * (
  32. intersect_ymax - intersect_ymin)
  33. sample_bbox_size = bbox_area(sample_bbox)
  34. object_bbox_size = bbox_area(object_bbox)
  35. overlap = intersect_size / (
  36. sample_bbox_size + object_bbox_size - intersect_size)
  37. return overlap