MCPcopy
hub / github.com/roboflow/supervision / _create_detections

Function _create_detections

tests/helpers.py:19–76  ·  view source on GitHub ↗

Create a Detections object from list-based inputs. This is a helper function primarily used for testing purposes to quickly instantiate a Detections object without manually converting lists to numpy arrays. Args: xyxy: Bounding boxes in `(x_min, y_min, x_max, y_max)`

(
    xyxy: list[list[float]],
    mask: list[np.ndarray] | None = None,
    confidence: list[float] | None = None,
    class_id: list[int] | None = None,
    tracker_id: list[int] | None = None,
    data: dict[str, list[Any]] | None = None,
)

Source from the content-addressed store, hash-verified

17
18
19def _create_detections(
20 xyxy: list[list[float]],
21 mask: list[np.ndarray] | None = None,
22 confidence: list[float] | None = None,
23 class_id: list[int] | None = None,
24 tracker_id: list[int] | None = None,
25 data: dict[str, list[Any]] | None = None,
26) -> Detections:
27 """
28 Create a Detections object from list-based inputs.
29
30 This is a helper function primarily used for testing purposes to quickly
31 instantiate a Detections object without manually converting lists to numpy arrays.
32
33 Args:
34 xyxy: Bounding boxes in `(x_min, y_min, x_max, y_max)`
35 format.
36 mask: Binary masks for each detection.
37 confidence: Confidence scores for each detection.
38 class_id: Class identifiers for each detection.
39 tracker_id: Tracker identifiers for each detection.
40 data: Additional data to be associated with
41 each detection.
42
43 Returns:
44 A Detections object containing the provided data.
45
46 Examples:
47 >>> import numpy as np
48 >>> detections = _create_detections(
49 ... xyxy=[[0, 0, 10, 10], [20, 20, 30, 30]],
50 ... confidence=[0.5, 0.8],
51 ... class_id=[0, 1]
52 ... )
53 >>> detections.xyxy
54 array([[ 0., 0., 10., 10.],
55 [20., 20., 30., 30.]], dtype=float32)
56 >>> detections.confidence
57 array([0.5, 0.8], dtype=float32)
58 >>> detections.class_id
59 array([0, 1])
60 """
61
62 def convert_data(data: dict[str, list[Any]]):
63 return {k: np.array(v) for k, v in data.items()}
64
65 return Detections(
66 xyxy=np.array(xyxy, dtype=np.float32),
67 mask=(mask if mask is None else np.array(mask, dtype=bool)),
68 confidence=(
69 confidence if confidence is None else np.array(confidence, dtype=np.float32)
70 ),
71 class_id=(class_id if class_id is None else np.array(class_id, dtype=int)),
72 tracker_id=(
73 tracker_id if tracker_id is None else np.array(tracker_id, dtype=int)
74 ),
75 data=convert_data(data) if data else {},
76 )

Calls 2

DetectionsClass · 0.90
convert_dataFunction · 0.85