| 67 | |
| 68 | |
| 69 | class Bbox: |
| 70 | def __init__(self, box, mode="whwh"): |
| 71 | |
| 72 | assert len(box) == 4 |
| 73 | assert mode in ["whwh", "xywh"] |
| 74 | self.box = box |
| 75 | self.mode = mode |
| 76 | |
| 77 | def to_xywh(self): |
| 78 | |
| 79 | if self.mode == "whwh": |
| 80 | |
| 81 | l, t, r, b = self.box |
| 82 | |
| 83 | center_x = (l + r) / 2 |
| 84 | center_y = (t + b) / 2 |
| 85 | width = r - l |
| 86 | height = b - t |
| 87 | return Bbox([center_x, center_y, width, height], mode="xywh") |
| 88 | else: |
| 89 | return self |
| 90 | |
| 91 | def to_whwh(self): |
| 92 | |
| 93 | if self.mode == "whwh": |
| 94 | return self |
| 95 | else: |
| 96 | |
| 97 | cx, cy, w, h = self.box |
| 98 | l = cx - w // 2 |
| 99 | t = cy - h // 2 |
| 100 | r = cx + w - (w // 2) |
| 101 | b = cy + h - (h // 2) |
| 102 | |
| 103 | return Bbox([l, t, r, b], mode="whwh") |
| 104 | |
| 105 | def area(self): |
| 106 | |
| 107 | box = self.to_xywh() |
| 108 | _, __, w, h = box.box |
| 109 | |
| 110 | return w * h |
| 111 | |
| 112 | def get_box(self): |
| 113 | return list(map(int, self.box)) |
| 114 | |
| 115 | def scale(self, scale, width, height): |
| 116 | new_box = self.to_xywh() |
| 117 | cx, cy, w, h = new_box.get_box() |
| 118 | w = w * scale |
| 119 | h = h * scale |
| 120 | |
| 121 | l = cx - w // 2 |
| 122 | t = cy - h // 2 |
| 123 | r = cx + w - (w // 2) |
| 124 | b = cy + h - (h // 2) |
| 125 | |
| 126 | l = int(max(l, 0)) |
no outgoing calls
no test coverage detected