cv.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. from shapely.geometry import Polygon
  15. def compute_iou(box_or_poly1, box_or_poly2):
  16. if isinstance(box_or_poly1[0], list):
  17. poly1 = box_or_poly1
  18. poly2 = box_or_poly2
  19. poly1 = Polygon(poly1)
  20. poly2 = Polygon(poly2)
  21. inter_area = poly1.intersection(poly2).area
  22. union_area = poly1.union(poly2).area
  23. iou = inter_area / (union_area + 1e-9)
  24. return iou
  25. else:
  26. box1 = box_or_poly1
  27. box2 = box_or_poly2
  28. x11, y11, x12, y12 = box1
  29. x21, y21, x22, y22 = box2
  30. xi1 = max(x11, x21)
  31. yi1 = max(y11, y21)
  32. xi2 = min(x12, x22)
  33. yi2 = min(y12, y22)
  34. inter_area = max(0, xi2 - xi1 + 1) * max(0, yi2 - yi1 + 1)
  35. box1_area = (x12 - x11 + 1) * (y12 - y11 + 1)
  36. box2_area = (x22 - x21 + 1) * (y22 - y21 + 1)
  37. union_area = box1_area + box2_area - inter_area
  38. iou = inter_area / (union_area + 1e-9)
  39. return iou
  40. def compare_det_results(
  41. boxes_or_polys1,
  42. boxes_or_polys2,
  43. *,
  44. labels1=None,
  45. labels2=None,
  46. scores1=None,
  47. scores2=None,
  48. iou_tol=0.1,
  49. score_tol=1e-3,
  50. ):
  51. compare_labels = labels1 is not None
  52. compare_scores = scores1 is not None
  53. assert len(boxes_or_polys1) == len(boxes_or_polys2)
  54. if compare_labels:
  55. assert len(labels1) == len(labels2)
  56. if compare_scores:
  57. assert len(scores1) == len(scores2)
  58. boxes_or_polys2 = boxes_or_polys2.copy()
  59. if labels2 is not None:
  60. labels2 = labels2.copy()
  61. if scores2 is not None:
  62. scores2 = scores2.copy()
  63. for i, box_or_poly1 in enumerate(boxes_or_polys1):
  64. j = 0
  65. max_iou = 0
  66. for k, box_or_poly2 in enumerate(boxes_or_polys2):
  67. iou = compute_iou(box_or_poly1, box_or_poly2)
  68. if iou > max_iou:
  69. max_iou = iou
  70. j = k
  71. assert max_iou > 1 - iou_tol
  72. if compare_labels:
  73. assert labels1[i] == labels2[j]
  74. if compare_scores:
  75. assert abs(scores1[i] - scores2[j]) < score_tol
  76. del boxes_or_polys2[j]
  77. if compare_labels:
  78. del labels2[j]
  79. if compare_scores:
  80. del scores2[j]