@@ -2088,6 +2088,43 @@ def box_area(self) -> np.ndarray:
20882088 """
20892089 return (self .xyxy [:, 3 ] - self .xyxy [:, 1 ]) * (self .xyxy [:, 2 ] - self .xyxy [:, 0 ])
20902090
2091+ @property
2092+ def box_aspect_ratio (self ) -> np .ndarray :
2093+ """
2094+ Compute the aspect ratio (width divided by height) for each bounding box.
2095+
2096+ Returns:
2097+ np.ndarray: Array of shape `(N,)` containing aspect ratios, where `N` is the
2098+ number of boxes (width / height for each box).
2099+
2100+ Examples:
2101+ ```python
2102+ import numpy as np
2103+ import supervision as sv
2104+
2105+ xyxy = np.array([
2106+ [10, 10, 50, 50],
2107+ [60, 10, 180, 50],
2108+ [10, 60, 50, 180],
2109+ ])
2110+
2111+ detections = sv.Detections(xyxy=xyxy)
2112+
2113+ detections.box_aspect_ratio
2114+ # array([1.0, 3.0, 0.33333333])
2115+
2116+ ar = detections.box_aspect_ratio
2117+ detections[(ar < 2.0) & (ar > 0.5)].xyxy
2118+ # array([[10., 10., 50., 50.]])
2119+ ```
2120+ """
2121+ widths = self .xyxy [:, 2 ] - self .xyxy [:, 0 ]
2122+ heights = self .xyxy [:, 3 ] - self .xyxy [:, 1 ]
2123+
2124+ aspect_ratios = np .full_like (widths , np .nan , dtype = np .float64 )
2125+ np .divide (widths , heights , out = aspect_ratios , where = heights != 0 )
2126+ return aspect_ratios
2127+
20912128 def with_nms (
20922129 self ,
20932130 threshold : float = 0.5 ,
0 commit comments