dgenerate-ultralytics-headless 8.3.134__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. dgenerate_ultralytics_headless-8.3.134.dist-info/METADATA +400 -0
  2. dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
  3. dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
  4. dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
  5. dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
  6. dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
  7. tests/__init__.py +22 -0
  8. tests/conftest.py +83 -0
  9. tests/test_cli.py +138 -0
  10. tests/test_cuda.py +215 -0
  11. tests/test_engine.py +131 -0
  12. tests/test_exports.py +236 -0
  13. tests/test_integrations.py +154 -0
  14. tests/test_python.py +694 -0
  15. tests/test_solutions.py +187 -0
  16. ultralytics/__init__.py +30 -0
  17. ultralytics/assets/bus.jpg +0 -0
  18. ultralytics/assets/zidane.jpg +0 -0
  19. ultralytics/cfg/__init__.py +1023 -0
  20. ultralytics/cfg/datasets/Argoverse.yaml +77 -0
  21. ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
  22. ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
  23. ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
  24. ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
  25. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  26. ultralytics/cfg/datasets/Objects365.yaml +443 -0
  27. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  28. ultralytics/cfg/datasets/VOC.yaml +106 -0
  29. ultralytics/cfg/datasets/VisDrone.yaml +77 -0
  30. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  31. ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
  32. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  33. ultralytics/cfg/datasets/coco-pose.yaml +42 -0
  34. ultralytics/cfg/datasets/coco.yaml +118 -0
  35. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  36. ultralytics/cfg/datasets/coco128.yaml +101 -0
  37. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  38. ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
  39. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  40. ultralytics/cfg/datasets/coco8.yaml +101 -0
  41. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  42. ultralytics/cfg/datasets/dog-pose.yaml +24 -0
  43. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  44. ultralytics/cfg/datasets/dota8.yaml +35 -0
  45. ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
  46. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  47. ultralytics/cfg/datasets/medical-pills.yaml +22 -0
  48. ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
  49. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  50. ultralytics/cfg/datasets/signature.yaml +21 -0
  51. ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
  52. ultralytics/cfg/datasets/xView.yaml +155 -0
  53. ultralytics/cfg/default.yaml +127 -0
  54. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  55. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  56. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  57. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  58. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  59. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  60. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  61. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  62. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  63. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  64. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  65. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  66. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  67. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  68. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  69. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  70. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  71. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  72. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  73. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  74. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  75. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  76. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  77. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  78. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  79. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  80. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  81. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  82. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  83. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
  84. ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
  85. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  86. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  87. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  88. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  89. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  90. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  91. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  92. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  93. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  94. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  95. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  96. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  97. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  98. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  99. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  100. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  101. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  102. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  103. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  104. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  105. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  106. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  107. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  108. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  109. ultralytics/cfg/trackers/botsort.yaml +22 -0
  110. ultralytics/cfg/trackers/bytetrack.yaml +14 -0
  111. ultralytics/data/__init__.py +26 -0
  112. ultralytics/data/annotator.py +66 -0
  113. ultralytics/data/augment.py +2945 -0
  114. ultralytics/data/base.py +438 -0
  115. ultralytics/data/build.py +258 -0
  116. ultralytics/data/converter.py +754 -0
  117. ultralytics/data/dataset.py +834 -0
  118. ultralytics/data/loaders.py +676 -0
  119. ultralytics/data/scripts/download_weights.sh +18 -0
  120. ultralytics/data/scripts/get_coco.sh +61 -0
  121. ultralytics/data/scripts/get_coco128.sh +18 -0
  122. ultralytics/data/scripts/get_imagenet.sh +52 -0
  123. ultralytics/data/split.py +125 -0
  124. ultralytics/data/split_dota.py +325 -0
  125. ultralytics/data/utils.py +777 -0
  126. ultralytics/engine/__init__.py +1 -0
  127. ultralytics/engine/exporter.py +1519 -0
  128. ultralytics/engine/model.py +1156 -0
  129. ultralytics/engine/predictor.py +502 -0
  130. ultralytics/engine/results.py +1840 -0
  131. ultralytics/engine/trainer.py +853 -0
  132. ultralytics/engine/tuner.py +243 -0
  133. ultralytics/engine/validator.py +377 -0
  134. ultralytics/hub/__init__.py +168 -0
  135. ultralytics/hub/auth.py +137 -0
  136. ultralytics/hub/google/__init__.py +176 -0
  137. ultralytics/hub/session.py +446 -0
  138. ultralytics/hub/utils.py +248 -0
  139. ultralytics/models/__init__.py +9 -0
  140. ultralytics/models/fastsam/__init__.py +7 -0
  141. ultralytics/models/fastsam/model.py +61 -0
  142. ultralytics/models/fastsam/predict.py +181 -0
  143. ultralytics/models/fastsam/utils.py +24 -0
  144. ultralytics/models/fastsam/val.py +40 -0
  145. ultralytics/models/nas/__init__.py +7 -0
  146. ultralytics/models/nas/model.py +102 -0
  147. ultralytics/models/nas/predict.py +58 -0
  148. ultralytics/models/nas/val.py +39 -0
  149. ultralytics/models/rtdetr/__init__.py +7 -0
  150. ultralytics/models/rtdetr/model.py +63 -0
  151. ultralytics/models/rtdetr/predict.py +84 -0
  152. ultralytics/models/rtdetr/train.py +85 -0
  153. ultralytics/models/rtdetr/val.py +191 -0
  154. ultralytics/models/sam/__init__.py +6 -0
  155. ultralytics/models/sam/amg.py +260 -0
  156. ultralytics/models/sam/build.py +358 -0
  157. ultralytics/models/sam/model.py +170 -0
  158. ultralytics/models/sam/modules/__init__.py +1 -0
  159. ultralytics/models/sam/modules/blocks.py +1129 -0
  160. ultralytics/models/sam/modules/decoders.py +515 -0
  161. ultralytics/models/sam/modules/encoders.py +854 -0
  162. ultralytics/models/sam/modules/memory_attention.py +299 -0
  163. ultralytics/models/sam/modules/sam.py +1006 -0
  164. ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
  165. ultralytics/models/sam/modules/transformer.py +351 -0
  166. ultralytics/models/sam/modules/utils.py +394 -0
  167. ultralytics/models/sam/predict.py +1605 -0
  168. ultralytics/models/utils/__init__.py +1 -0
  169. ultralytics/models/utils/loss.py +455 -0
  170. ultralytics/models/utils/ops.py +268 -0
  171. ultralytics/models/yolo/__init__.py +7 -0
  172. ultralytics/models/yolo/classify/__init__.py +7 -0
  173. ultralytics/models/yolo/classify/predict.py +88 -0
  174. ultralytics/models/yolo/classify/train.py +233 -0
  175. ultralytics/models/yolo/classify/val.py +215 -0
  176. ultralytics/models/yolo/detect/__init__.py +7 -0
  177. ultralytics/models/yolo/detect/predict.py +124 -0
  178. ultralytics/models/yolo/detect/train.py +217 -0
  179. ultralytics/models/yolo/detect/val.py +451 -0
  180. ultralytics/models/yolo/model.py +354 -0
  181. ultralytics/models/yolo/obb/__init__.py +7 -0
  182. ultralytics/models/yolo/obb/predict.py +66 -0
  183. ultralytics/models/yolo/obb/train.py +81 -0
  184. ultralytics/models/yolo/obb/val.py +283 -0
  185. ultralytics/models/yolo/pose/__init__.py +7 -0
  186. ultralytics/models/yolo/pose/predict.py +79 -0
  187. ultralytics/models/yolo/pose/train.py +154 -0
  188. ultralytics/models/yolo/pose/val.py +394 -0
  189. ultralytics/models/yolo/segment/__init__.py +7 -0
  190. ultralytics/models/yolo/segment/predict.py +113 -0
  191. ultralytics/models/yolo/segment/train.py +123 -0
  192. ultralytics/models/yolo/segment/val.py +428 -0
  193. ultralytics/models/yolo/world/__init__.py +5 -0
  194. ultralytics/models/yolo/world/train.py +119 -0
  195. ultralytics/models/yolo/world/train_world.py +176 -0
  196. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  197. ultralytics/models/yolo/yoloe/predict.py +169 -0
  198. ultralytics/models/yolo/yoloe/train.py +298 -0
  199. ultralytics/models/yolo/yoloe/train_seg.py +124 -0
  200. ultralytics/models/yolo/yoloe/val.py +191 -0
  201. ultralytics/nn/__init__.py +29 -0
  202. ultralytics/nn/autobackend.py +842 -0
  203. ultralytics/nn/modules/__init__.py +182 -0
  204. ultralytics/nn/modules/activation.py +53 -0
  205. ultralytics/nn/modules/block.py +1966 -0
  206. ultralytics/nn/modules/conv.py +712 -0
  207. ultralytics/nn/modules/head.py +880 -0
  208. ultralytics/nn/modules/transformer.py +713 -0
  209. ultralytics/nn/modules/utils.py +164 -0
  210. ultralytics/nn/tasks.py +1627 -0
  211. ultralytics/nn/text_model.py +351 -0
  212. ultralytics/solutions/__init__.py +41 -0
  213. ultralytics/solutions/ai_gym.py +116 -0
  214. ultralytics/solutions/analytics.py +252 -0
  215. ultralytics/solutions/config.py +106 -0
  216. ultralytics/solutions/distance_calculation.py +124 -0
  217. ultralytics/solutions/heatmap.py +127 -0
  218. ultralytics/solutions/instance_segmentation.py +84 -0
  219. ultralytics/solutions/object_blurrer.py +90 -0
  220. ultralytics/solutions/object_counter.py +195 -0
  221. ultralytics/solutions/object_cropper.py +84 -0
  222. ultralytics/solutions/parking_management.py +273 -0
  223. ultralytics/solutions/queue_management.py +93 -0
  224. ultralytics/solutions/region_counter.py +120 -0
  225. ultralytics/solutions/security_alarm.py +154 -0
  226. ultralytics/solutions/similarity_search.py +172 -0
  227. ultralytics/solutions/solutions.py +724 -0
  228. ultralytics/solutions/speed_estimation.py +110 -0
  229. ultralytics/solutions/streamlit_inference.py +196 -0
  230. ultralytics/solutions/templates/similarity-search.html +160 -0
  231. ultralytics/solutions/trackzone.py +88 -0
  232. ultralytics/solutions/vision_eye.py +68 -0
  233. ultralytics/trackers/__init__.py +7 -0
  234. ultralytics/trackers/basetrack.py +124 -0
  235. ultralytics/trackers/bot_sort.py +260 -0
  236. ultralytics/trackers/byte_tracker.py +480 -0
  237. ultralytics/trackers/track.py +125 -0
  238. ultralytics/trackers/utils/__init__.py +1 -0
  239. ultralytics/trackers/utils/gmc.py +376 -0
  240. ultralytics/trackers/utils/kalman_filter.py +493 -0
  241. ultralytics/trackers/utils/matching.py +157 -0
  242. ultralytics/utils/__init__.py +1435 -0
  243. ultralytics/utils/autobatch.py +106 -0
  244. ultralytics/utils/autodevice.py +174 -0
  245. ultralytics/utils/benchmarks.py +695 -0
  246. ultralytics/utils/callbacks/__init__.py +5 -0
  247. ultralytics/utils/callbacks/base.py +234 -0
  248. ultralytics/utils/callbacks/clearml.py +153 -0
  249. ultralytics/utils/callbacks/comet.py +552 -0
  250. ultralytics/utils/callbacks/dvc.py +205 -0
  251. ultralytics/utils/callbacks/hub.py +108 -0
  252. ultralytics/utils/callbacks/mlflow.py +138 -0
  253. ultralytics/utils/callbacks/neptune.py +140 -0
  254. ultralytics/utils/callbacks/raytune.py +43 -0
  255. ultralytics/utils/callbacks/tensorboard.py +132 -0
  256. ultralytics/utils/callbacks/wb.py +185 -0
  257. ultralytics/utils/checks.py +897 -0
  258. ultralytics/utils/dist.py +119 -0
  259. ultralytics/utils/downloads.py +499 -0
  260. ultralytics/utils/errors.py +43 -0
  261. ultralytics/utils/export.py +219 -0
  262. ultralytics/utils/files.py +221 -0
  263. ultralytics/utils/instance.py +499 -0
  264. ultralytics/utils/loss.py +813 -0
  265. ultralytics/utils/metrics.py +1356 -0
  266. ultralytics/utils/ops.py +885 -0
  267. ultralytics/utils/patches.py +143 -0
  268. ultralytics/utils/plotting.py +1011 -0
  269. ultralytics/utils/tal.py +416 -0
  270. ultralytics/utils/torch_utils.py +990 -0
  271. ultralytics/utils/triton.py +116 -0
  272. ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,499 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from collections import abc
4
+ from itertools import repeat
5
+ from numbers import Number
6
+ from typing import List
7
+
8
+ import numpy as np
9
+
10
+ from .ops import ltwh2xywh, ltwh2xyxy, resample_segments, xywh2ltwh, xywh2xyxy, xyxy2ltwh, xyxy2xywh
11
+
12
+
13
+ def _ntuple(n):
14
+ """From PyTorch internals."""
15
+
16
+ def parse(x):
17
+ """Parse input to return n-tuple by repeating singleton values n times."""
18
+ return x if isinstance(x, abc.Iterable) else tuple(repeat(x, n))
19
+
20
+ return parse
21
+
22
+
23
+ to_2tuple = _ntuple(2)
24
+ to_4tuple = _ntuple(4)
25
+
26
+ # `xyxy` means left top and right bottom
27
+ # `xywh` means center x, center y and width, height(YOLO format)
28
+ # `ltwh` means left top and width, height(COCO format)
29
+ _formats = ["xyxy", "xywh", "ltwh"]
30
+
31
+ __all__ = ("Bboxes", "Instances") # tuple or list
32
+
33
+
34
+ class Bboxes:
35
+ """
36
+ A class for handling bounding boxes.
37
+
38
+ The class supports various bounding box formats like 'xyxy', 'xywh', and 'ltwh'.
39
+ Bounding box data should be provided in numpy arrays.
40
+
41
+ Attributes:
42
+ bboxes (np.ndarray): The bounding boxes stored in a 2D numpy array with shape (N, 4).
43
+ format (str): The format of the bounding boxes ('xyxy', 'xywh', or 'ltwh').
44
+
45
+ Note:
46
+ This class does not handle normalization or denormalization of bounding boxes.
47
+ """
48
+
49
+ def __init__(self, bboxes, format="xyxy") -> None:
50
+ """
51
+ Initialize the Bboxes class with bounding box data in a specified format.
52
+
53
+ Args:
54
+ bboxes (np.ndarray): Array of bounding boxes with shape (N, 4) or (4,).
55
+ format (str): Format of the bounding boxes, one of 'xyxy', 'xywh', or 'ltwh'.
56
+ """
57
+ assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
58
+ bboxes = bboxes[None, :] if bboxes.ndim == 1 else bboxes
59
+ assert bboxes.ndim == 2
60
+ assert bboxes.shape[1] == 4
61
+ self.bboxes = bboxes
62
+ self.format = format
63
+ # self.normalized = normalized
64
+
65
+ def convert(self, format):
66
+ """
67
+ Convert bounding box format from one type to another.
68
+
69
+ Args:
70
+ format (str): Target format for conversion, one of 'xyxy', 'xywh', or 'ltwh'.
71
+ """
72
+ assert format in _formats, f"Invalid bounding box format: {format}, format must be one of {_formats}"
73
+ if self.format == format:
74
+ return
75
+ elif self.format == "xyxy":
76
+ func = xyxy2xywh if format == "xywh" else xyxy2ltwh
77
+ elif self.format == "xywh":
78
+ func = xywh2xyxy if format == "xyxy" else xywh2ltwh
79
+ else:
80
+ func = ltwh2xyxy if format == "xyxy" else ltwh2xywh
81
+ self.bboxes = func(self.bboxes)
82
+ self.format = format
83
+
84
+ def areas(self):
85
+ """Return box areas."""
86
+ return (
87
+ (self.bboxes[:, 2] - self.bboxes[:, 0]) * (self.bboxes[:, 3] - self.bboxes[:, 1]) # format xyxy
88
+ if self.format == "xyxy"
89
+ else self.bboxes[:, 3] * self.bboxes[:, 2] # format xywh or ltwh
90
+ )
91
+
92
+ # def denormalize(self, w, h):
93
+ # if not self.normalized:
94
+ # return
95
+ # assert (self.bboxes <= 1.0).all()
96
+ # self.bboxes[:, 0::2] *= w
97
+ # self.bboxes[:, 1::2] *= h
98
+ # self.normalized = False
99
+ #
100
+ # def normalize(self, w, h):
101
+ # if self.normalized:
102
+ # return
103
+ # assert (self.bboxes > 1.0).any()
104
+ # self.bboxes[:, 0::2] /= w
105
+ # self.bboxes[:, 1::2] /= h
106
+ # self.normalized = True
107
+
108
+ def mul(self, scale):
109
+ """
110
+ Multiply bounding box coordinates by scale factor(s).
111
+
112
+ Args:
113
+ scale (int | tuple | list): Scale factor(s) for four coordinates.
114
+ If int, the same scale is applied to all coordinates.
115
+ """
116
+ if isinstance(scale, Number):
117
+ scale = to_4tuple(scale)
118
+ assert isinstance(scale, (tuple, list))
119
+ assert len(scale) == 4
120
+ self.bboxes[:, 0] *= scale[0]
121
+ self.bboxes[:, 1] *= scale[1]
122
+ self.bboxes[:, 2] *= scale[2]
123
+ self.bboxes[:, 3] *= scale[3]
124
+
125
+ def add(self, offset):
126
+ """
127
+ Add offset to bounding box coordinates.
128
+
129
+ Args:
130
+ offset (int | tuple | list): Offset(s) for four coordinates.
131
+ If int, the same offset is applied to all coordinates.
132
+ """
133
+ if isinstance(offset, Number):
134
+ offset = to_4tuple(offset)
135
+ assert isinstance(offset, (tuple, list))
136
+ assert len(offset) == 4
137
+ self.bboxes[:, 0] += offset[0]
138
+ self.bboxes[:, 1] += offset[1]
139
+ self.bboxes[:, 2] += offset[2]
140
+ self.bboxes[:, 3] += offset[3]
141
+
142
+ def __len__(self):
143
+ """Return the number of boxes."""
144
+ return len(self.bboxes)
145
+
146
+ @classmethod
147
+ def concatenate(cls, boxes_list: List["Bboxes"], axis=0) -> "Bboxes":
148
+ """
149
+ Concatenate a list of Bboxes objects into a single Bboxes object.
150
+
151
+ Args:
152
+ boxes_list (List[Bboxes]): A list of Bboxes objects to concatenate.
153
+ axis (int, optional): The axis along which to concatenate the bounding boxes.
154
+
155
+ Returns:
156
+ (Bboxes): A new Bboxes object containing the concatenated bounding boxes.
157
+
158
+ Note:
159
+ The input should be a list or tuple of Bboxes objects.
160
+ """
161
+ assert isinstance(boxes_list, (list, tuple))
162
+ if not boxes_list:
163
+ return cls(np.empty(0))
164
+ assert all(isinstance(box, Bboxes) for box in boxes_list)
165
+
166
+ if len(boxes_list) == 1:
167
+ return boxes_list[0]
168
+ return cls(np.concatenate([b.bboxes for b in boxes_list], axis=axis))
169
+
170
+ def __getitem__(self, index) -> "Bboxes":
171
+ """
172
+ Retrieve a specific bounding box or a set of bounding boxes using indexing.
173
+
174
+ Args:
175
+ index (int | slice | np.ndarray): The index, slice, or boolean array to select
176
+ the desired bounding boxes.
177
+
178
+ Returns:
179
+ (Bboxes): A new Bboxes object containing the selected bounding boxes.
180
+
181
+ Raises:
182
+ AssertionError: If the indexed bounding boxes do not form a 2-dimensional matrix.
183
+
184
+ Note:
185
+ When using boolean indexing, make sure to provide a boolean array with the same
186
+ length as the number of bounding boxes.
187
+ """
188
+ if isinstance(index, int):
189
+ return Bboxes(self.bboxes[index].reshape(1, -1))
190
+ b = self.bboxes[index]
191
+ assert b.ndim == 2, f"Indexing on Bboxes with {index} failed to return a matrix!"
192
+ return Bboxes(b)
193
+
194
+
195
+ class Instances:
196
+ """
197
+ Container for bounding boxes, segments, and keypoints of detected objects in an image.
198
+
199
+ Attributes:
200
+ _bboxes (Bboxes): Internal object for handling bounding box operations.
201
+ keypoints (np.ndarray): Keypoints with shape (N, 17, 3) in format (x, y, visible).
202
+ normalized (bool): Flag indicating whether the bounding box coordinates are normalized.
203
+ segments (np.ndarray): Segments array with shape (N, M, 2) after resampling.
204
+
205
+ Methods:
206
+ convert_bbox: Convert bounding box format.
207
+ scale: Scale coordinates by given factors.
208
+ denormalize: Convert normalized coordinates to absolute coordinates.
209
+ normalize: Convert absolute coordinates to normalized coordinates.
210
+ add_padding: Add padding to coordinates.
211
+ flipud: Flip coordinates vertically.
212
+ fliplr: Flip coordinates horizontally.
213
+ clip: Clip coordinates to stay within image boundaries.
214
+ remove_zero_area_boxes: Remove boxes with zero area.
215
+ update: Update instance variables.
216
+ concatenate: Concatenate multiple Instances objects.
217
+
218
+ Examples:
219
+ >>> instances = Instances(
220
+ ... bboxes=np.array([[10, 10, 30, 30], [20, 20, 40, 40]]),
221
+ ... segments=[np.array([[5, 5], [10, 10]]), np.array([[15, 15], [20, 20]])],
222
+ ... keypoints=np.array([[[5, 5, 1], [10, 10, 1]], [[15, 15, 1], [20, 20, 1]]]),
223
+ ... )
224
+ """
225
+
226
+ def __init__(self, bboxes, segments=None, keypoints=None, bbox_format="xywh", normalized=True) -> None:
227
+ """
228
+ Initialize the object with bounding boxes, segments, and keypoints.
229
+
230
+ Args:
231
+ bboxes (np.ndarray): Bounding boxes, shape (N, 4).
232
+ segments (List | np.ndarray, optional): Segmentation masks.
233
+ keypoints (np.ndarray, optional): Keypoints, shape (N, 17, 3) in format (x, y, visible).
234
+ bbox_format (str, optional): Format of bboxes.
235
+ normalized (bool, optional): Whether the coordinates are normalized.
236
+ """
237
+ self._bboxes = Bboxes(bboxes=bboxes, format=bbox_format)
238
+ self.keypoints = keypoints
239
+ self.normalized = normalized
240
+ self.segments = segments
241
+
242
+ def convert_bbox(self, format):
243
+ """
244
+ Convert bounding box format.
245
+
246
+ Args:
247
+ format (str): Target format for conversion, one of 'xyxy', 'xywh', or 'ltwh'.
248
+ """
249
+ self._bboxes.convert(format=format)
250
+
251
+ @property
252
+ def bbox_areas(self):
253
+ """Calculate the area of bounding boxes."""
254
+ return self._bboxes.areas()
255
+
256
+ def scale(self, scale_w, scale_h, bbox_only=False):
257
+ """
258
+ Scale coordinates by given factors.
259
+
260
+ Args:
261
+ scale_w (float): Scale factor for width.
262
+ scale_h (float): Scale factor for height.
263
+ bbox_only (bool, optional): Whether to scale only bounding boxes.
264
+ """
265
+ self._bboxes.mul(scale=(scale_w, scale_h, scale_w, scale_h))
266
+ if bbox_only:
267
+ return
268
+ self.segments[..., 0] *= scale_w
269
+ self.segments[..., 1] *= scale_h
270
+ if self.keypoints is not None:
271
+ self.keypoints[..., 0] *= scale_w
272
+ self.keypoints[..., 1] *= scale_h
273
+
274
+ def denormalize(self, w, h):
275
+ """
276
+ Convert normalized coordinates to absolute coordinates.
277
+
278
+ Args:
279
+ w (int): Image width.
280
+ h (int): Image height.
281
+ """
282
+ if not self.normalized:
283
+ return
284
+ self._bboxes.mul(scale=(w, h, w, h))
285
+ self.segments[..., 0] *= w
286
+ self.segments[..., 1] *= h
287
+ if self.keypoints is not None:
288
+ self.keypoints[..., 0] *= w
289
+ self.keypoints[..., 1] *= h
290
+ self.normalized = False
291
+
292
+ def normalize(self, w, h):
293
+ """
294
+ Convert absolute coordinates to normalized coordinates.
295
+
296
+ Args:
297
+ w (int): Image width.
298
+ h (int): Image height.
299
+ """
300
+ if self.normalized:
301
+ return
302
+ self._bboxes.mul(scale=(1 / w, 1 / h, 1 / w, 1 / h))
303
+ self.segments[..., 0] /= w
304
+ self.segments[..., 1] /= h
305
+ if self.keypoints is not None:
306
+ self.keypoints[..., 0] /= w
307
+ self.keypoints[..., 1] /= h
308
+ self.normalized = True
309
+
310
+ def add_padding(self, padw, padh):
311
+ """
312
+ Add padding to coordinates.
313
+
314
+ Args:
315
+ padw (int): Padding width.
316
+ padh (int): Padding height.
317
+ """
318
+ assert not self.normalized, "you should add padding with absolute coordinates."
319
+ self._bboxes.add(offset=(padw, padh, padw, padh))
320
+ self.segments[..., 0] += padw
321
+ self.segments[..., 1] += padh
322
+ if self.keypoints is not None:
323
+ self.keypoints[..., 0] += padw
324
+ self.keypoints[..., 1] += padh
325
+
326
+ def __getitem__(self, index) -> "Instances":
327
+ """
328
+ Retrieve a specific instance or a set of instances using indexing.
329
+
330
+ Args:
331
+ index (int | slice | np.ndarray): The index, slice, or boolean array to select the desired instances.
332
+
333
+ Returns:
334
+ (Instances): A new Instances object containing the selected boxes, segments, and keypoints if present.
335
+
336
+ Note:
337
+ When using boolean indexing, make sure to provide a boolean array with the same
338
+ length as the number of instances.
339
+ """
340
+ segments = self.segments[index] if len(self.segments) else self.segments
341
+ keypoints = self.keypoints[index] if self.keypoints is not None else None
342
+ bboxes = self.bboxes[index]
343
+ bbox_format = self._bboxes.format
344
+ return Instances(
345
+ bboxes=bboxes,
346
+ segments=segments,
347
+ keypoints=keypoints,
348
+ bbox_format=bbox_format,
349
+ normalized=self.normalized,
350
+ )
351
+
352
+ def flipud(self, h):
353
+ """
354
+ Flip coordinates vertically.
355
+
356
+ Args:
357
+ h (int): Image height.
358
+ """
359
+ if self._bboxes.format == "xyxy":
360
+ y1 = self.bboxes[:, 1].copy()
361
+ y2 = self.bboxes[:, 3].copy()
362
+ self.bboxes[:, 1] = h - y2
363
+ self.bboxes[:, 3] = h - y1
364
+ else:
365
+ self.bboxes[:, 1] = h - self.bboxes[:, 1]
366
+ self.segments[..., 1] = h - self.segments[..., 1]
367
+ if self.keypoints is not None:
368
+ self.keypoints[..., 1] = h - self.keypoints[..., 1]
369
+
370
+ def fliplr(self, w):
371
+ """
372
+ Flip coordinates horizontally.
373
+
374
+ Args:
375
+ w (int): Image width.
376
+ """
377
+ if self._bboxes.format == "xyxy":
378
+ x1 = self.bboxes[:, 0].copy()
379
+ x2 = self.bboxes[:, 2].copy()
380
+ self.bboxes[:, 0] = w - x2
381
+ self.bboxes[:, 2] = w - x1
382
+ else:
383
+ self.bboxes[:, 0] = w - self.bboxes[:, 0]
384
+ self.segments[..., 0] = w - self.segments[..., 0]
385
+ if self.keypoints is not None:
386
+ self.keypoints[..., 0] = w - self.keypoints[..., 0]
387
+
388
+ def clip(self, w, h):
389
+ """
390
+ Clip coordinates to stay within image boundaries.
391
+
392
+ Args:
393
+ w (int): Image width.
394
+ h (int): Image height.
395
+ """
396
+ ori_format = self._bboxes.format
397
+ self.convert_bbox(format="xyxy")
398
+ self.bboxes[:, [0, 2]] = self.bboxes[:, [0, 2]].clip(0, w)
399
+ self.bboxes[:, [1, 3]] = self.bboxes[:, [1, 3]].clip(0, h)
400
+ if ori_format != "xyxy":
401
+ self.convert_bbox(format=ori_format)
402
+ self.segments[..., 0] = self.segments[..., 0].clip(0, w)
403
+ self.segments[..., 1] = self.segments[..., 1].clip(0, h)
404
+ if self.keypoints is not None:
405
+ # Set out of bounds visibility to zero
406
+ self.keypoints[..., 2][
407
+ (self.keypoints[..., 0] < 0)
408
+ | (self.keypoints[..., 0] > w)
409
+ | (self.keypoints[..., 1] < 0)
410
+ | (self.keypoints[..., 1] > h)
411
+ ] = 0.0
412
+
413
+ def remove_zero_area_boxes(self):
414
+ """
415
+ Remove zero-area boxes, i.e. after clipping some boxes may have zero width or height.
416
+
417
+ Returns:
418
+ (np.ndarray): Boolean array indicating which boxes were kept.
419
+ """
420
+ good = self.bbox_areas > 0
421
+ if not all(good):
422
+ self._bboxes = self._bboxes[good]
423
+ if len(self.segments):
424
+ self.segments = self.segments[good]
425
+ if self.keypoints is not None:
426
+ self.keypoints = self.keypoints[good]
427
+ return good
428
+
429
+ def update(self, bboxes, segments=None, keypoints=None):
430
+ """
431
+ Update instance variables.
432
+
433
+ Args:
434
+ bboxes (np.ndarray): New bounding boxes.
435
+ segments (np.ndarray, optional): New segments.
436
+ keypoints (np.ndarray, optional): New keypoints.
437
+ """
438
+ self._bboxes = Bboxes(bboxes, format=self._bboxes.format)
439
+ if segments is not None:
440
+ self.segments = segments
441
+ if keypoints is not None:
442
+ self.keypoints = keypoints
443
+
444
+ def __len__(self):
445
+ """Return the length of the instance list."""
446
+ return len(self.bboxes)
447
+
448
+ @classmethod
449
+ def concatenate(cls, instances_list: List["Instances"], axis=0) -> "Instances":
450
+ """
451
+ Concatenate a list of Instances objects into a single Instances object.
452
+
453
+ Args:
454
+ instances_list (List[Instances]): A list of Instances objects to concatenate.
455
+ axis (int, optional): The axis along which the arrays will be concatenated.
456
+
457
+ Returns:
458
+ (Instances): A new Instances object containing the concatenated bounding boxes,
459
+ segments, and keypoints if present.
460
+
461
+ Note:
462
+ The `Instances` objects in the list should have the same properties, such as
463
+ the format of the bounding boxes, whether keypoints are present, and if the
464
+ coordinates are normalized.
465
+ """
466
+ assert isinstance(instances_list, (list, tuple))
467
+ if not instances_list:
468
+ return cls(np.empty(0))
469
+ assert all(isinstance(instance, Instances) for instance in instances_list)
470
+
471
+ if len(instances_list) == 1:
472
+ return instances_list[0]
473
+
474
+ use_keypoint = instances_list[0].keypoints is not None
475
+ bbox_format = instances_list[0]._bboxes.format
476
+ normalized = instances_list[0].normalized
477
+
478
+ cat_boxes = np.concatenate([ins.bboxes for ins in instances_list], axis=axis)
479
+ seg_len = [b.segments.shape[1] for b in instances_list]
480
+ if len(frozenset(seg_len)) > 1: # resample segments if there's different length
481
+ max_len = max(seg_len)
482
+ cat_segments = np.concatenate(
483
+ [
484
+ resample_segments(list(b.segments), max_len)
485
+ if len(b.segments)
486
+ else np.zeros((0, max_len, 2), dtype=np.float32) # re-generating empty segments
487
+ for b in instances_list
488
+ ],
489
+ axis=axis,
490
+ )
491
+ else:
492
+ cat_segments = np.concatenate([b.segments for b in instances_list], axis=axis)
493
+ cat_keypoints = np.concatenate([b.keypoints for b in instances_list], axis=axis) if use_keypoint else None
494
+ return cls(cat_boxes, cat_segments, cat_keypoints, bbox_format, normalized)
495
+
496
+ @property
497
+ def bboxes(self):
498
+ """Return bounding boxes."""
499
+ return self._bboxes.bboxes