Apply transform to boxes Copy from nanodet/data/transform/warp.py
(boxes, M, width, height)
| 54 | |
| 55 | |
| 56 | def warp_boxes(boxes, M, width, height): |
| 57 | """Apply transform to boxes |
| 58 | Copy from nanodet/data/transform/warp.py |
| 59 | """ |
| 60 | n = len(boxes) |
| 61 | if n: |
| 62 | # warp points |
| 63 | xy = np.ones((n * 4, 3)) |
| 64 | xy[:, :2] = boxes[:, [0, 1, 2, 3, 0, 3, 2, 1]].reshape( |
| 65 | n * 4, 2 |
| 66 | ) # x1y1, x2y2, x1y2, x2y1 |
| 67 | xy = xy @ M.T # transform |
| 68 | xy = (xy[:, :2] / xy[:, 2:3]).reshape(n, 8) # rescale |
| 69 | # create new boxes |
| 70 | x = xy[:, [0, 2, 4, 6]] |
| 71 | y = xy[:, [1, 3, 5, 7]] |
| 72 | xy = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T |
| 73 | # clip boxes |
| 74 | xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width) |
| 75 | xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height) |
| 76 | return xy.astype(np.float32) |
| 77 | else: |
| 78 | return boxes |
| 79 | |
| 80 | |
| 81 | def overlay_bbox_cv(img, all_box, class_names): |