polygon_fast.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import numpy as np
  15. from shapely.geometry import Polygon
  16. """
  17. :param det_x: [1, N] Xs of detection's vertices
  18. :param det_y: [1, N] Ys of detection's vertices
  19. :param gt_x: [1, N] Xs of groundtruth's vertices
  20. :param gt_y: [1, N] Ys of groundtruth's vertices
  21. ##############
  22. All the calculation of 'AREA' in this script is handled by:
  23. 1) First generating a binary mask with the polygon area filled up with 1's
  24. 2) Summing up all the 1's
  25. """
  26. def area(x, y):
  27. polygon = Polygon(np.stack([x, y], axis=1))
  28. return float(polygon.area)
  29. def approx_area_of_intersection(det_x, det_y, gt_x, gt_y):
  30. """
  31. This helper determine if both polygons are intersecting with each others with an approximation method.
  32. Area of intersection represented by the minimum bounding rectangular [xmin, ymin, xmax, ymax]
  33. """
  34. det_ymax = np.max(det_y)
  35. det_xmax = np.max(det_x)
  36. det_ymin = np.min(det_y)
  37. det_xmin = np.min(det_x)
  38. gt_ymax = np.max(gt_y)
  39. gt_xmax = np.max(gt_x)
  40. gt_ymin = np.min(gt_y)
  41. gt_xmin = np.min(gt_x)
  42. all_min_ymax = np.minimum(det_ymax, gt_ymax)
  43. all_max_ymin = np.maximum(det_ymin, gt_ymin)
  44. intersect_heights = np.maximum(0.0, (all_min_ymax - all_max_ymin))
  45. all_min_xmax = np.minimum(det_xmax, gt_xmax)
  46. all_max_xmin = np.maximum(det_xmin, gt_xmin)
  47. intersect_widths = np.maximum(0.0, (all_min_xmax - all_max_xmin))
  48. return intersect_heights * intersect_widths
  49. def area_of_intersection(det_x, det_y, gt_x, gt_y):
  50. p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0)
  51. p2 = Polygon(np.stack([gt_x, gt_y], axis=1)).buffer(0)
  52. return float(p1.intersection(p2).area)
  53. def area_of_union(det_x, det_y, gt_x, gt_y):
  54. p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0)
  55. p2 = Polygon(np.stack([gt_x, gt_y], axis=1)).buffer(0)
  56. return float(p1.union(p2).area)
  57. def iou(det_x, det_y, gt_x, gt_y):
  58. return area_of_intersection(det_x, det_y, gt_x, gt_y) / (
  59. area_of_union(det_x, det_y, gt_x, gt_y) + 1.0)
  60. def iod(det_x, det_y, gt_x, gt_y):
  61. """
  62. This helper determine the fraction of intersection area over detection area
  63. """
  64. return area_of_intersection(det_x, det_y, gt_x, gt_y) / (
  65. area(det_x, det_y) + 1.0)