计算两个矩形框的 IoU
(box1: list, box2: list)
| 161 | |
| 162 | # ======================== IoU计算 ======================== |
| 163 | def calculate_iou(box1: list, box2: list) -> float: |
| 164 | """计算两个矩形框的 IoU""" |
| 165 | x_left = max(box1[0], box2[0]) |
| 166 | y_top = max(box1[1], box2[1]) |
| 167 | x_right = min(box1[2], box2[2]) |
| 168 | y_bottom = min(box1[3], box2[3]) |
| 169 | |
| 170 | if x_right < x_left or y_bottom < y_top: |
| 171 | return 0.0 |
| 172 | |
| 173 | intersection_area = (x_right - x_left) * (y_bottom - y_top) |
| 174 | box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1]) |
| 175 | box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1]) |
| 176 | union_area = box1_area + box2_area - intersection_area |
| 177 | |
| 178 | if union_area <= 0: |
| 179 | return 0.0 |
| 180 | |
| 181 | return intersection_area / union_area |
| 182 | |
| 183 | |
| 184 | # ======================== 边框宽度检测 ======================== |
no outgoing calls
no test coverage detected