ultralytics-opencv-headless 8.3.242__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 (298) hide show
  1. tests/__init__.py +23 -0
  2. tests/conftest.py +59 -0
  3. tests/test_cli.py +131 -0
  4. tests/test_cuda.py +216 -0
  5. tests/test_engine.py +157 -0
  6. tests/test_exports.py +309 -0
  7. tests/test_integrations.py +151 -0
  8. tests/test_python.py +777 -0
  9. tests/test_solutions.py +371 -0
  10. ultralytics/__init__.py +48 -0
  11. ultralytics/assets/bus.jpg +0 -0
  12. ultralytics/assets/zidane.jpg +0 -0
  13. ultralytics/cfg/__init__.py +1026 -0
  14. ultralytics/cfg/datasets/Argoverse.yaml +78 -0
  15. ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
  16. ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
  17. ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
  18. ultralytics/cfg/datasets/HomeObjects-3K.yaml +32 -0
  19. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  20. ultralytics/cfg/datasets/Objects365.yaml +447 -0
  21. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  22. ultralytics/cfg/datasets/VOC.yaml +102 -0
  23. ultralytics/cfg/datasets/VisDrone.yaml +87 -0
  24. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  25. ultralytics/cfg/datasets/brain-tumor.yaml +22 -0
  26. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  27. ultralytics/cfg/datasets/coco-pose.yaml +64 -0
  28. ultralytics/cfg/datasets/coco.yaml +118 -0
  29. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  30. ultralytics/cfg/datasets/coco128.yaml +101 -0
  31. ultralytics/cfg/datasets/coco8-grayscale.yaml +103 -0
  32. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  33. ultralytics/cfg/datasets/coco8-pose.yaml +47 -0
  34. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  35. ultralytics/cfg/datasets/coco8.yaml +101 -0
  36. ultralytics/cfg/datasets/construction-ppe.yaml +32 -0
  37. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  38. ultralytics/cfg/datasets/dog-pose.yaml +52 -0
  39. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  40. ultralytics/cfg/datasets/dota8.yaml +35 -0
  41. ultralytics/cfg/datasets/hand-keypoints.yaml +50 -0
  42. ultralytics/cfg/datasets/kitti.yaml +27 -0
  43. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  44. ultralytics/cfg/datasets/medical-pills.yaml +21 -0
  45. ultralytics/cfg/datasets/open-images-v7.yaml +663 -0
  46. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  47. ultralytics/cfg/datasets/signature.yaml +21 -0
  48. ultralytics/cfg/datasets/tiger-pose.yaml +41 -0
  49. ultralytics/cfg/datasets/xView.yaml +155 -0
  50. ultralytics/cfg/default.yaml +130 -0
  51. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  52. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  53. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  54. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  55. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  56. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  57. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  58. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  59. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  60. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  61. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  62. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  63. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  64. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  65. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  66. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  67. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  68. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  69. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  70. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  71. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  72. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  73. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  74. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  75. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  76. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  77. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  78. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  79. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  80. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +48 -0
  81. ultralytics/cfg/models/v8/yoloe-v8.yaml +48 -0
  82. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  83. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  84. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  85. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  86. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  87. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  88. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  89. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  90. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  91. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  92. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  93. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  94. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  95. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  96. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  97. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  98. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  99. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  100. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  101. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  102. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  103. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  104. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  105. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  106. ultralytics/cfg/trackers/botsort.yaml +21 -0
  107. ultralytics/cfg/trackers/bytetrack.yaml +12 -0
  108. ultralytics/data/__init__.py +26 -0
  109. ultralytics/data/annotator.py +66 -0
  110. ultralytics/data/augment.py +2801 -0
  111. ultralytics/data/base.py +435 -0
  112. ultralytics/data/build.py +437 -0
  113. ultralytics/data/converter.py +855 -0
  114. ultralytics/data/dataset.py +834 -0
  115. ultralytics/data/loaders.py +704 -0
  116. ultralytics/data/scripts/download_weights.sh +18 -0
  117. ultralytics/data/scripts/get_coco.sh +61 -0
  118. ultralytics/data/scripts/get_coco128.sh +18 -0
  119. ultralytics/data/scripts/get_imagenet.sh +52 -0
  120. ultralytics/data/split.py +138 -0
  121. ultralytics/data/split_dota.py +344 -0
  122. ultralytics/data/utils.py +798 -0
  123. ultralytics/engine/__init__.py +1 -0
  124. ultralytics/engine/exporter.py +1574 -0
  125. ultralytics/engine/model.py +1124 -0
  126. ultralytics/engine/predictor.py +508 -0
  127. ultralytics/engine/results.py +1522 -0
  128. ultralytics/engine/trainer.py +974 -0
  129. ultralytics/engine/tuner.py +448 -0
  130. ultralytics/engine/validator.py +384 -0
  131. ultralytics/hub/__init__.py +166 -0
  132. ultralytics/hub/auth.py +151 -0
  133. ultralytics/hub/google/__init__.py +174 -0
  134. ultralytics/hub/session.py +422 -0
  135. ultralytics/hub/utils.py +162 -0
  136. ultralytics/models/__init__.py +9 -0
  137. ultralytics/models/fastsam/__init__.py +7 -0
  138. ultralytics/models/fastsam/model.py +79 -0
  139. ultralytics/models/fastsam/predict.py +169 -0
  140. ultralytics/models/fastsam/utils.py +23 -0
  141. ultralytics/models/fastsam/val.py +38 -0
  142. ultralytics/models/nas/__init__.py +7 -0
  143. ultralytics/models/nas/model.py +98 -0
  144. ultralytics/models/nas/predict.py +56 -0
  145. ultralytics/models/nas/val.py +38 -0
  146. ultralytics/models/rtdetr/__init__.py +7 -0
  147. ultralytics/models/rtdetr/model.py +63 -0
  148. ultralytics/models/rtdetr/predict.py +88 -0
  149. ultralytics/models/rtdetr/train.py +89 -0
  150. ultralytics/models/rtdetr/val.py +216 -0
  151. ultralytics/models/sam/__init__.py +25 -0
  152. ultralytics/models/sam/amg.py +275 -0
  153. ultralytics/models/sam/build.py +365 -0
  154. ultralytics/models/sam/build_sam3.py +377 -0
  155. ultralytics/models/sam/model.py +169 -0
  156. ultralytics/models/sam/modules/__init__.py +1 -0
  157. ultralytics/models/sam/modules/blocks.py +1067 -0
  158. ultralytics/models/sam/modules/decoders.py +495 -0
  159. ultralytics/models/sam/modules/encoders.py +794 -0
  160. ultralytics/models/sam/modules/memory_attention.py +298 -0
  161. ultralytics/models/sam/modules/sam.py +1160 -0
  162. ultralytics/models/sam/modules/tiny_encoder.py +979 -0
  163. ultralytics/models/sam/modules/transformer.py +344 -0
  164. ultralytics/models/sam/modules/utils.py +512 -0
  165. ultralytics/models/sam/predict.py +3940 -0
  166. ultralytics/models/sam/sam3/__init__.py +3 -0
  167. ultralytics/models/sam/sam3/decoder.py +546 -0
  168. ultralytics/models/sam/sam3/encoder.py +529 -0
  169. ultralytics/models/sam/sam3/geometry_encoders.py +415 -0
  170. ultralytics/models/sam/sam3/maskformer_segmentation.py +286 -0
  171. ultralytics/models/sam/sam3/model_misc.py +199 -0
  172. ultralytics/models/sam/sam3/necks.py +129 -0
  173. ultralytics/models/sam/sam3/sam3_image.py +339 -0
  174. ultralytics/models/sam/sam3/text_encoder_ve.py +307 -0
  175. ultralytics/models/sam/sam3/vitdet.py +547 -0
  176. ultralytics/models/sam/sam3/vl_combiner.py +160 -0
  177. ultralytics/models/utils/__init__.py +1 -0
  178. ultralytics/models/utils/loss.py +466 -0
  179. ultralytics/models/utils/ops.py +315 -0
  180. ultralytics/models/yolo/__init__.py +7 -0
  181. ultralytics/models/yolo/classify/__init__.py +7 -0
  182. ultralytics/models/yolo/classify/predict.py +90 -0
  183. ultralytics/models/yolo/classify/train.py +202 -0
  184. ultralytics/models/yolo/classify/val.py +216 -0
  185. ultralytics/models/yolo/detect/__init__.py +7 -0
  186. ultralytics/models/yolo/detect/predict.py +122 -0
  187. ultralytics/models/yolo/detect/train.py +227 -0
  188. ultralytics/models/yolo/detect/val.py +507 -0
  189. ultralytics/models/yolo/model.py +430 -0
  190. ultralytics/models/yolo/obb/__init__.py +7 -0
  191. ultralytics/models/yolo/obb/predict.py +56 -0
  192. ultralytics/models/yolo/obb/train.py +79 -0
  193. ultralytics/models/yolo/obb/val.py +302 -0
  194. ultralytics/models/yolo/pose/__init__.py +7 -0
  195. ultralytics/models/yolo/pose/predict.py +65 -0
  196. ultralytics/models/yolo/pose/train.py +110 -0
  197. ultralytics/models/yolo/pose/val.py +248 -0
  198. ultralytics/models/yolo/segment/__init__.py +7 -0
  199. ultralytics/models/yolo/segment/predict.py +109 -0
  200. ultralytics/models/yolo/segment/train.py +69 -0
  201. ultralytics/models/yolo/segment/val.py +307 -0
  202. ultralytics/models/yolo/world/__init__.py +5 -0
  203. ultralytics/models/yolo/world/train.py +173 -0
  204. ultralytics/models/yolo/world/train_world.py +178 -0
  205. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  206. ultralytics/models/yolo/yoloe/predict.py +162 -0
  207. ultralytics/models/yolo/yoloe/train.py +287 -0
  208. ultralytics/models/yolo/yoloe/train_seg.py +122 -0
  209. ultralytics/models/yolo/yoloe/val.py +206 -0
  210. ultralytics/nn/__init__.py +27 -0
  211. ultralytics/nn/autobackend.py +958 -0
  212. ultralytics/nn/modules/__init__.py +182 -0
  213. ultralytics/nn/modules/activation.py +54 -0
  214. ultralytics/nn/modules/block.py +1947 -0
  215. ultralytics/nn/modules/conv.py +669 -0
  216. ultralytics/nn/modules/head.py +1183 -0
  217. ultralytics/nn/modules/transformer.py +793 -0
  218. ultralytics/nn/modules/utils.py +159 -0
  219. ultralytics/nn/tasks.py +1768 -0
  220. ultralytics/nn/text_model.py +356 -0
  221. ultralytics/py.typed +1 -0
  222. ultralytics/solutions/__init__.py +41 -0
  223. ultralytics/solutions/ai_gym.py +108 -0
  224. ultralytics/solutions/analytics.py +264 -0
  225. ultralytics/solutions/config.py +107 -0
  226. ultralytics/solutions/distance_calculation.py +123 -0
  227. ultralytics/solutions/heatmap.py +125 -0
  228. ultralytics/solutions/instance_segmentation.py +86 -0
  229. ultralytics/solutions/object_blurrer.py +89 -0
  230. ultralytics/solutions/object_counter.py +190 -0
  231. ultralytics/solutions/object_cropper.py +87 -0
  232. ultralytics/solutions/parking_management.py +280 -0
  233. ultralytics/solutions/queue_management.py +93 -0
  234. ultralytics/solutions/region_counter.py +133 -0
  235. ultralytics/solutions/security_alarm.py +151 -0
  236. ultralytics/solutions/similarity_search.py +219 -0
  237. ultralytics/solutions/solutions.py +828 -0
  238. ultralytics/solutions/speed_estimation.py +114 -0
  239. ultralytics/solutions/streamlit_inference.py +260 -0
  240. ultralytics/solutions/templates/similarity-search.html +156 -0
  241. ultralytics/solutions/trackzone.py +88 -0
  242. ultralytics/solutions/vision_eye.py +67 -0
  243. ultralytics/trackers/__init__.py +7 -0
  244. ultralytics/trackers/basetrack.py +115 -0
  245. ultralytics/trackers/bot_sort.py +257 -0
  246. ultralytics/trackers/byte_tracker.py +469 -0
  247. ultralytics/trackers/track.py +116 -0
  248. ultralytics/trackers/utils/__init__.py +1 -0
  249. ultralytics/trackers/utils/gmc.py +339 -0
  250. ultralytics/trackers/utils/kalman_filter.py +482 -0
  251. ultralytics/trackers/utils/matching.py +154 -0
  252. ultralytics/utils/__init__.py +1450 -0
  253. ultralytics/utils/autobatch.py +118 -0
  254. ultralytics/utils/autodevice.py +205 -0
  255. ultralytics/utils/benchmarks.py +728 -0
  256. ultralytics/utils/callbacks/__init__.py +5 -0
  257. ultralytics/utils/callbacks/base.py +233 -0
  258. ultralytics/utils/callbacks/clearml.py +146 -0
  259. ultralytics/utils/callbacks/comet.py +625 -0
  260. ultralytics/utils/callbacks/dvc.py +197 -0
  261. ultralytics/utils/callbacks/hub.py +110 -0
  262. ultralytics/utils/callbacks/mlflow.py +134 -0
  263. ultralytics/utils/callbacks/neptune.py +126 -0
  264. ultralytics/utils/callbacks/platform.py +73 -0
  265. ultralytics/utils/callbacks/raytune.py +42 -0
  266. ultralytics/utils/callbacks/tensorboard.py +123 -0
  267. ultralytics/utils/callbacks/wb.py +188 -0
  268. ultralytics/utils/checks.py +998 -0
  269. ultralytics/utils/cpu.py +85 -0
  270. ultralytics/utils/dist.py +123 -0
  271. ultralytics/utils/downloads.py +529 -0
  272. ultralytics/utils/errors.py +35 -0
  273. ultralytics/utils/events.py +113 -0
  274. ultralytics/utils/export/__init__.py +7 -0
  275. ultralytics/utils/export/engine.py +237 -0
  276. ultralytics/utils/export/imx.py +315 -0
  277. ultralytics/utils/export/tensorflow.py +231 -0
  278. ultralytics/utils/files.py +219 -0
  279. ultralytics/utils/git.py +137 -0
  280. ultralytics/utils/instance.py +484 -0
  281. ultralytics/utils/logger.py +444 -0
  282. ultralytics/utils/loss.py +849 -0
  283. ultralytics/utils/metrics.py +1560 -0
  284. ultralytics/utils/nms.py +337 -0
  285. ultralytics/utils/ops.py +664 -0
  286. ultralytics/utils/patches.py +201 -0
  287. ultralytics/utils/plotting.py +1045 -0
  288. ultralytics/utils/tal.py +403 -0
  289. ultralytics/utils/torch_utils.py +984 -0
  290. ultralytics/utils/tqdm.py +440 -0
  291. ultralytics/utils/triton.py +112 -0
  292. ultralytics/utils/tuner.py +160 -0
  293. ultralytics_opencv_headless-8.3.242.dist-info/METADATA +374 -0
  294. ultralytics_opencv_headless-8.3.242.dist-info/RECORD +298 -0
  295. ultralytics_opencv_headless-8.3.242.dist-info/WHEEL +5 -0
  296. ultralytics_opencv_headless-8.3.242.dist-info/entry_points.txt +3 -0
  297. ultralytics_opencv_headless-8.3.242.dist-info/licenses/LICENSE +661 -0
  298. ultralytics_opencv_headless-8.3.242.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1522 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+ """
3
+ Ultralytics Results, Boxes and Masks classes for handling inference results.
4
+
5
+ Usage: See https://docs.ultralytics.com/modes/predict/
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from copy import deepcopy
11
+ from functools import lru_cache
12
+ from pathlib import Path
13
+ from typing import Any
14
+
15
+ import numpy as np
16
+ import torch
17
+
18
+ from ultralytics.data.augment import LetterBox
19
+ from ultralytics.utils import LOGGER, DataExportMixin, SimpleClass, ops
20
+ from ultralytics.utils.plotting import Annotator, colors, save_one_box
21
+
22
+
23
+ class BaseTensor(SimpleClass):
24
+ """Base tensor class with additional methods for easy manipulation and device handling.
25
+
26
+ This class provides a foundation for tensor-like objects with device management capabilities, supporting both
27
+ PyTorch tensors and NumPy arrays. It includes methods for moving data between devices and converting between tensor
28
+ types.
29
+
30
+ Attributes:
31
+ data (torch.Tensor | np.ndarray): Prediction data such as bounding boxes, masks, or keypoints.
32
+ orig_shape (tuple[int, int]): Original shape of the image, typically in the format (height, width).
33
+
34
+ Methods:
35
+ cpu: Return a copy of the tensor stored in CPU memory.
36
+ numpy: Return a copy of the tensor as a numpy array.
37
+ cuda: Move the tensor to GPU memory, returning a new instance if necessary.
38
+ to: Return a copy of the tensor with the specified device and dtype.
39
+
40
+ Examples:
41
+ >>> import torch
42
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]])
43
+ >>> orig_shape = (720, 1280)
44
+ >>> base_tensor = BaseTensor(data, orig_shape)
45
+ >>> cpu_tensor = base_tensor.cpu()
46
+ >>> numpy_array = base_tensor.numpy()
47
+ >>> gpu_tensor = base_tensor.cuda()
48
+ """
49
+
50
+ def __init__(self, data: torch.Tensor | np.ndarray, orig_shape: tuple[int, int]) -> None:
51
+ """Initialize BaseTensor with prediction data and the original shape of the image.
52
+
53
+ Args:
54
+ data (torch.Tensor | np.ndarray): Prediction data such as bounding boxes, masks, or keypoints.
55
+ orig_shape (tuple[int, int]): Original shape of the image in (height, width) format.
56
+ """
57
+ assert isinstance(data, (torch.Tensor, np.ndarray)), "data must be torch.Tensor or np.ndarray"
58
+ self.data = data
59
+ self.orig_shape = orig_shape
60
+
61
+ @property
62
+ def shape(self) -> tuple[int, ...]:
63
+ """Return the shape of the underlying data tensor.
64
+
65
+ Returns:
66
+ (tuple[int, ...]): The shape of the data tensor.
67
+
68
+ Examples:
69
+ >>> data = torch.rand(100, 4)
70
+ >>> base_tensor = BaseTensor(data, orig_shape=(720, 1280))
71
+ >>> print(base_tensor.shape)
72
+ (100, 4)
73
+ """
74
+ return self.data.shape
75
+
76
+ def cpu(self):
77
+ """Return a copy of the tensor stored in CPU memory.
78
+
79
+ Returns:
80
+ (BaseTensor): A new BaseTensor object with the data tensor moved to CPU memory.
81
+
82
+ Examples:
83
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]]).cuda()
84
+ >>> base_tensor = BaseTensor(data, orig_shape=(720, 1280))
85
+ >>> cpu_tensor = base_tensor.cpu()
86
+ >>> isinstance(cpu_tensor, BaseTensor)
87
+ True
88
+ >>> cpu_tensor.data.device
89
+ device(type='cpu')
90
+ """
91
+ return self if isinstance(self.data, np.ndarray) else self.__class__(self.data.cpu(), self.orig_shape)
92
+
93
+ def numpy(self):
94
+ """Return a copy of this object with its data converted to a NumPy array.
95
+
96
+ Returns:
97
+ (BaseTensor): A new instance with `data` as a NumPy array.
98
+
99
+ Examples:
100
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]])
101
+ >>> orig_shape = (720, 1280)
102
+ >>> base_tensor = BaseTensor(data, orig_shape)
103
+ >>> numpy_tensor = base_tensor.numpy()
104
+ >>> print(type(numpy_tensor.data))
105
+ <class 'numpy.ndarray'>
106
+ """
107
+ return self if isinstance(self.data, np.ndarray) else self.__class__(self.data.numpy(), self.orig_shape)
108
+
109
+ def cuda(self):
110
+ """Move the tensor to GPU memory.
111
+
112
+ Returns:
113
+ (BaseTensor): A new BaseTensor instance with the data moved to GPU memory.
114
+
115
+ Examples:
116
+ >>> import torch
117
+ >>> from ultralytics.engine.results import BaseTensor
118
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]])
119
+ >>> base_tensor = BaseTensor(data, orig_shape=(720, 1280))
120
+ >>> gpu_tensor = base_tensor.cuda()
121
+ >>> print(gpu_tensor.data.device)
122
+ cuda:0
123
+ """
124
+ return self.__class__(torch.as_tensor(self.data).cuda(), self.orig_shape)
125
+
126
+ def to(self, *args, **kwargs):
127
+ """Return a copy of the tensor with the specified device and dtype.
128
+
129
+ Args:
130
+ *args (Any): Variable length argument list to be passed to torch.Tensor.to().
131
+ **kwargs (Any): Arbitrary keyword arguments to be passed to torch.Tensor.to().
132
+
133
+ Returns:
134
+ (BaseTensor): A new BaseTensor instance with the data moved to the specified device and/or dtype.
135
+
136
+ Examples:
137
+ >>> base_tensor = BaseTensor(torch.randn(3, 4), orig_shape=(480, 640))
138
+ >>> cuda_tensor = base_tensor.to("cuda")
139
+ >>> float16_tensor = base_tensor.to(dtype=torch.float16)
140
+ """
141
+ return self.__class__(torch.as_tensor(self.data).to(*args, **kwargs), self.orig_shape)
142
+
143
+ def __len__(self) -> int:
144
+ """Return the length of the underlying data tensor.
145
+
146
+ Returns:
147
+ (int): The number of elements in the first dimension of the data tensor.
148
+
149
+ Examples:
150
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]])
151
+ >>> base_tensor = BaseTensor(data, orig_shape=(720, 1280))
152
+ >>> len(base_tensor)
153
+ 2
154
+ """
155
+ return len(self.data)
156
+
157
+ def __getitem__(self, idx):
158
+ """Return a new BaseTensor instance containing the specified indexed elements of the data tensor.
159
+
160
+ Args:
161
+ idx (int | list[int] | torch.Tensor): Index or indices to select from the data tensor.
162
+
163
+ Returns:
164
+ (BaseTensor): A new BaseTensor instance containing the indexed data.
165
+
166
+ Examples:
167
+ >>> data = torch.tensor([[1, 2, 3], [4, 5, 6]])
168
+ >>> base_tensor = BaseTensor(data, orig_shape=(720, 1280))
169
+ >>> result = base_tensor[0] # Select the first row
170
+ >>> print(result.data)
171
+ tensor([1, 2, 3])
172
+ """
173
+ return self.__class__(self.data[idx], self.orig_shape)
174
+
175
+
176
+ class Results(SimpleClass, DataExportMixin):
177
+ """A class for storing and manipulating inference results.
178
+
179
+ This class provides comprehensive functionality for handling inference results from various Ultralytics models,
180
+ including detection, segmentation, classification, and pose estimation. It supports visualization, data export, and
181
+ various coordinate transformations.
182
+
183
+ Attributes:
184
+ orig_img (np.ndarray): The original image as a numpy array.
185
+ orig_shape (tuple[int, int]): Original image shape in (height, width) format.
186
+ boxes (Boxes | None): Detected bounding boxes.
187
+ masks (Masks | None): Segmentation masks.
188
+ probs (Probs | None): Classification probabilities.
189
+ keypoints (Keypoints | None): Detected keypoints.
190
+ obb (OBB | None): Oriented bounding boxes.
191
+ speed (dict): Dictionary containing inference speed information.
192
+ names (dict): Dictionary mapping class indices to class names.
193
+ path (str): Path to the input image file.
194
+ save_dir (str | None): Directory to save results.
195
+
196
+ Methods:
197
+ update: Update the Results object with new detection data.
198
+ cpu: Return a copy of the Results object with all tensors moved to CPU memory.
199
+ numpy: Convert all tensors in the Results object to numpy arrays.
200
+ cuda: Move all tensors in the Results object to GPU memory.
201
+ to: Move all tensors to the specified device and dtype.
202
+ new: Create a new Results object with the same image, path, names, and speed attributes.
203
+ plot: Plot detection results on an input BGR image.
204
+ show: Display the image with annotated inference results.
205
+ save: Save annotated inference results image to file.
206
+ verbose: Return a log string for each task in the results.
207
+ save_txt: Save detection results to a text file.
208
+ save_crop: Save cropped detection images to specified directory.
209
+ summary: Convert inference results to a summarized dictionary.
210
+ to_df: Convert detection results to a Polars DataFrame.
211
+ to_json: Convert detection results to JSON format.
212
+ to_csv: Convert detection results to a CSV format.
213
+
214
+ Examples:
215
+ >>> results = model("path/to/image.jpg")
216
+ >>> result = results[0] # Get the first result
217
+ >>> boxes = result.boxes # Get the boxes for the first result
218
+ >>> masks = result.masks # Get the masks for the first result
219
+ >>> for result in results:
220
+ >>> result.plot() # Plot detection results
221
+ """
222
+
223
+ def __init__(
224
+ self,
225
+ orig_img: np.ndarray,
226
+ path: str,
227
+ names: dict[int, str],
228
+ boxes: torch.Tensor | None = None,
229
+ masks: torch.Tensor | None = None,
230
+ probs: torch.Tensor | None = None,
231
+ keypoints: torch.Tensor | None = None,
232
+ obb: torch.Tensor | None = None,
233
+ speed: dict[str, float] | None = None,
234
+ ) -> None:
235
+ """Initialize the Results class for storing and manipulating inference results.
236
+
237
+ Args:
238
+ orig_img (np.ndarray): The original image as a numpy array.
239
+ path (str): The path to the image file.
240
+ names (dict): A dictionary of class names.
241
+ boxes (torch.Tensor | None): A 2D tensor of bounding box coordinates for each detection.
242
+ masks (torch.Tensor | None): A 3D tensor of detection masks, where each mask is a binary image.
243
+ probs (torch.Tensor | None): A 1D tensor of probabilities of each class for classification task.
244
+ keypoints (torch.Tensor | None): A 2D tensor of keypoint coordinates for each detection.
245
+ obb (torch.Tensor | None): A 2D tensor of oriented bounding box coordinates for each detection.
246
+ speed (dict | None): A dictionary containing preprocess, inference, and postprocess speeds (ms/image).
247
+
248
+ Notes:
249
+ For the default pose model, keypoint indices for human body pose estimation are:
250
+ 0: Nose, 1: Left Eye, 2: Right Eye, 3: Left Ear, 4: Right Ear
251
+ 5: Left Shoulder, 6: Right Shoulder, 7: Left Elbow, 8: Right Elbow
252
+ 9: Left Wrist, 10: Right Wrist, 11: Left Hip, 12: Right Hip
253
+ 13: Left Knee, 14: Right Knee, 15: Left Ankle, 16: Right Ankle
254
+ """
255
+ self.orig_img = orig_img
256
+ self.orig_shape = orig_img.shape[:2]
257
+ self.boxes = Boxes(boxes, self.orig_shape) if boxes is not None else None # native size boxes
258
+ self.masks = Masks(masks, self.orig_shape) if masks is not None else None # native size or imgsz masks
259
+ self.probs = Probs(probs) if probs is not None else None
260
+ self.keypoints = Keypoints(keypoints, self.orig_shape) if keypoints is not None else None
261
+ self.obb = OBB(obb, self.orig_shape) if obb is not None else None
262
+ self.speed = speed if speed is not None else {"preprocess": None, "inference": None, "postprocess": None}
263
+ self.names = names
264
+ self.path = path
265
+ self.save_dir = None
266
+ self._keys = "boxes", "masks", "probs", "keypoints", "obb"
267
+
268
+ def __getitem__(self, idx):
269
+ """Return a Results object for a specific index of inference results.
270
+
271
+ Args:
272
+ idx (int | slice): Index or slice to retrieve from the Results object.
273
+
274
+ Returns:
275
+ (Results): A new Results object containing the specified subset of inference results.
276
+
277
+ Examples:
278
+ >>> results = model("path/to/image.jpg") # Perform inference
279
+ >>> single_result = results[0] # Get the first result
280
+ >>> subset_results = results[1:4] # Get a slice of results
281
+ """
282
+ return self._apply("__getitem__", idx)
283
+
284
+ def __len__(self) -> int:
285
+ """Return the number of detections in the Results object.
286
+
287
+ Returns:
288
+ (int): The number of detections, determined by the length of the first non-empty attribute in (masks, probs,
289
+ keypoints, or obb).
290
+
291
+ Examples:
292
+ >>> results = Results(orig_img, path, names, boxes=torch.rand(5, 4))
293
+ >>> len(results)
294
+ 5
295
+ """
296
+ for k in self._keys:
297
+ v = getattr(self, k)
298
+ if v is not None:
299
+ return len(v)
300
+
301
+ def update(
302
+ self,
303
+ boxes: torch.Tensor | None = None,
304
+ masks: torch.Tensor | None = None,
305
+ probs: torch.Tensor | None = None,
306
+ obb: torch.Tensor | None = None,
307
+ keypoints: torch.Tensor | None = None,
308
+ ):
309
+ """Update the Results object with new detection data.
310
+
311
+ This method allows updating the boxes, masks, probabilities, and oriented bounding boxes (OBB) of the Results
312
+ object. It ensures that boxes are clipped to the original image shape.
313
+
314
+ Args:
315
+ boxes (torch.Tensor | None): A tensor of shape (N, 6) containing bounding box coordinates and confidence
316
+ scores. The format is (x1, y1, x2, y2, conf, class).
317
+ masks (torch.Tensor | None): A tensor of shape (N, H, W) containing segmentation masks.
318
+ probs (torch.Tensor | None): A tensor of shape (num_classes,) containing class probabilities.
319
+ obb (torch.Tensor | None): A tensor of shape (N, 5) containing oriented bounding box coordinates.
320
+ keypoints (torch.Tensor | None): A tensor of shape (N, 17, 3) containing keypoints.
321
+
322
+ Examples:
323
+ >>> results = model("image.jpg")
324
+ >>> new_boxes = torch.tensor([[100, 100, 200, 200, 0.9, 0]])
325
+ >>> results[0].update(boxes=new_boxes)
326
+ """
327
+ if boxes is not None:
328
+ self.boxes = Boxes(ops.clip_boxes(boxes, self.orig_shape), self.orig_shape)
329
+ if masks is not None:
330
+ self.masks = Masks(masks, self.orig_shape)
331
+ if probs is not None:
332
+ self.probs = probs
333
+ if obb is not None:
334
+ self.obb = OBB(obb, self.orig_shape)
335
+ if keypoints is not None:
336
+ self.keypoints = Keypoints(keypoints, self.orig_shape)
337
+
338
+ def _apply(self, fn: str, *args, **kwargs):
339
+ """Apply a function to all non-empty attributes and return a new Results object with modified attributes.
340
+
341
+ This method is internally called by methods like .to(), .cuda(), .cpu(), etc.
342
+
343
+ Args:
344
+ fn (str): The name of the function to apply.
345
+ *args (Any): Variable length argument list to pass to the function.
346
+ **kwargs (Any): Arbitrary keyword arguments to pass to the function.
347
+
348
+ Returns:
349
+ (Results): A new Results object with attributes modified by the applied function.
350
+
351
+ Examples:
352
+ >>> results = model("path/to/image.jpg")
353
+ >>> for result in results:
354
+ ... result_cuda = result.cuda()
355
+ ... result_cpu = result.cpu()
356
+ """
357
+ r = self.new()
358
+ for k in self._keys:
359
+ v = getattr(self, k)
360
+ if v is not None:
361
+ setattr(r, k, getattr(v, fn)(*args, **kwargs))
362
+ return r
363
+
364
+ def cpu(self):
365
+ """Return a copy of the Results object with all its tensors moved to CPU memory.
366
+
367
+ This method creates a new Results object with all tensor attributes (boxes, masks, probs, keypoints, obb)
368
+ transferred to CPU memory. It's useful for moving data from GPU to CPU for further processing or saving.
369
+
370
+ Returns:
371
+ (Results): A new Results object with all tensor attributes on CPU memory.
372
+
373
+ Examples:
374
+ >>> results = model("path/to/image.jpg") # Perform inference
375
+ >>> cpu_result = results[0].cpu() # Move the first result to CPU
376
+ >>> print(cpu_result.boxes.device) # Output: cpu
377
+ """
378
+ return self._apply("cpu")
379
+
380
+ def numpy(self):
381
+ """Convert all tensors in the Results object to numpy arrays.
382
+
383
+ Returns:
384
+ (Results): A new Results object with all tensors converted to numpy arrays.
385
+
386
+ Examples:
387
+ >>> results = model("path/to/image.jpg")
388
+ >>> numpy_result = results[0].numpy()
389
+ >>> type(numpy_result.boxes.data)
390
+ <class 'numpy.ndarray'>
391
+
392
+ Notes:
393
+ This method creates a new Results object, leaving the original unchanged. It's useful for
394
+ interoperability with numpy-based libraries or when CPU-based operations are required.
395
+ """
396
+ return self._apply("numpy")
397
+
398
+ def cuda(self):
399
+ """Move all tensors in the Results object to GPU memory.
400
+
401
+ Returns:
402
+ (Results): A new Results object with all tensors moved to CUDA device.
403
+
404
+ Examples:
405
+ >>> results = model("path/to/image.jpg")
406
+ >>> cuda_results = results[0].cuda() # Move first result to GPU
407
+ >>> for result in results:
408
+ ... result_cuda = result.cuda() # Move each result to GPU
409
+ """
410
+ return self._apply("cuda")
411
+
412
+ def to(self, *args, **kwargs):
413
+ """Move all tensors in the Results object to the specified device and dtype.
414
+
415
+ Args:
416
+ *args (Any): Variable length argument list to be passed to torch.Tensor.to().
417
+ **kwargs (Any): Arbitrary keyword arguments to be passed to torch.Tensor.to().
418
+
419
+ Returns:
420
+ (Results): A new Results object with all tensors moved to the specified device and dtype.
421
+
422
+ Examples:
423
+ >>> results = model("path/to/image.jpg")
424
+ >>> result_cuda = results[0].to("cuda") # Move first result to GPU
425
+ >>> result_cpu = results[0].to("cpu") # Move first result to CPU
426
+ >>> result_half = results[0].to(dtype=torch.float16) # Convert first result to half precision
427
+ """
428
+ return self._apply("to", *args, **kwargs)
429
+
430
+ def new(self):
431
+ """Create a new Results object with the same image, path, names, and speed attributes.
432
+
433
+ Returns:
434
+ (Results): A new Results object with copied attributes from the original instance.
435
+
436
+ Examples:
437
+ >>> results = model("path/to/image.jpg")
438
+ >>> new_result = results[0].new()
439
+ """
440
+ return Results(orig_img=self.orig_img, path=self.path, names=self.names, speed=self.speed)
441
+
442
+ def plot(
443
+ self,
444
+ conf: bool = True,
445
+ line_width: float | None = None,
446
+ font_size: float | None = None,
447
+ font: str = "Arial.ttf",
448
+ pil: bool = False,
449
+ img: np.ndarray | None = None,
450
+ im_gpu: torch.Tensor | None = None,
451
+ kpt_radius: int = 5,
452
+ kpt_line: bool = True,
453
+ labels: bool = True,
454
+ boxes: bool = True,
455
+ masks: bool = True,
456
+ probs: bool = True,
457
+ show: bool = False,
458
+ save: bool = False,
459
+ filename: str | None = None,
460
+ color_mode: str = "class",
461
+ txt_color: tuple[int, int, int] = (255, 255, 255),
462
+ ) -> np.ndarray:
463
+ """Plot detection results on an input BGR image.
464
+
465
+ Args:
466
+ conf (bool): Whether to plot detection confidence scores.
467
+ line_width (float | None): Line width of bounding boxes. If None, scaled to image size.
468
+ font_size (float | None): Font size for text. If None, scaled to image size.
469
+ font (str): Font to use for text.
470
+ pil (bool): Whether to return the image as a PIL Image.
471
+ img (np.ndarray | None): Image to plot on. If None, uses original image.
472
+ im_gpu (torch.Tensor | None): Normalized image on GPU for faster mask plotting.
473
+ kpt_radius (int): Radius of drawn keypoints.
474
+ kpt_line (bool): Whether to draw lines connecting keypoints.
475
+ labels (bool): Whether to plot labels of bounding boxes.
476
+ boxes (bool): Whether to plot bounding boxes.
477
+ masks (bool): Whether to plot masks.
478
+ probs (bool): Whether to plot classification probabilities.
479
+ show (bool): Whether to display the annotated image.
480
+ save (bool): Whether to save the annotated image.
481
+ filename (str | None): Filename to save image if save is True.
482
+ color_mode (str): Specify the color mode, e.g., 'instance' or 'class'.
483
+ txt_color (tuple[int, int, int]): Text color in BGR format for classification output.
484
+
485
+ Returns:
486
+ (np.ndarray | PIL.Image.Image): Annotated image as a NumPy array (BGR) or PIL image (RGB) if `pil=True`.
487
+
488
+ Examples:
489
+ >>> results = model("image.jpg")
490
+ >>> for result in results:
491
+ >>> im = result.plot()
492
+ >>> im.show()
493
+ """
494
+ assert color_mode in {"instance", "class"}, f"Expected color_mode='instance' or 'class', not {color_mode}."
495
+ if img is None and isinstance(self.orig_img, torch.Tensor):
496
+ img = (self.orig_img[0].detach().permute(1, 2, 0).contiguous() * 255).byte().cpu().numpy()
497
+
498
+ names = self.names
499
+ is_obb = self.obb is not None
500
+ pred_boxes, show_boxes = self.obb if is_obb else self.boxes, boxes
501
+ pred_masks, show_masks = self.masks, masks
502
+ pred_probs, show_probs = self.probs, probs
503
+ annotator = Annotator(
504
+ deepcopy(self.orig_img if img is None else img),
505
+ line_width,
506
+ font_size,
507
+ font,
508
+ pil or (pred_probs is not None and show_probs), # Classify tasks default to pil=True
509
+ example=names,
510
+ )
511
+
512
+ # Plot Segment results
513
+ if pred_masks and show_masks:
514
+ if im_gpu is None:
515
+ img = LetterBox(pred_masks.shape[1:])(image=annotator.result())
516
+ im_gpu = (
517
+ torch.as_tensor(img, dtype=torch.float16, device=pred_masks.data.device)
518
+ .permute(2, 0, 1)
519
+ .flip(0)
520
+ .contiguous()
521
+ / 255
522
+ )
523
+ idx = (
524
+ pred_boxes.id
525
+ if pred_boxes.is_track and color_mode == "instance"
526
+ else pred_boxes.cls
527
+ if pred_boxes and color_mode == "class"
528
+ else reversed(range(len(pred_masks)))
529
+ )
530
+ annotator.masks(pred_masks.data, colors=[colors(x, True) for x in idx], im_gpu=im_gpu)
531
+
532
+ # Plot Detect results
533
+ if pred_boxes is not None and show_boxes:
534
+ for i, d in enumerate(reversed(pred_boxes)):
535
+ c, d_conf, id = int(d.cls), float(d.conf) if conf else None, int(d.id.item()) if d.is_track else None
536
+ name = ("" if id is None else f"id:{id} ") + names[c]
537
+ label = (f"{name} {d_conf:.2f}" if conf else name) if labels else None
538
+ box = d.xyxyxyxy.squeeze() if is_obb else d.xyxy.squeeze()
539
+ annotator.box_label(
540
+ box,
541
+ label,
542
+ color=colors(
543
+ c
544
+ if color_mode == "class"
545
+ else id
546
+ if id is not None
547
+ else i
548
+ if color_mode == "instance"
549
+ else None,
550
+ True,
551
+ ),
552
+ )
553
+
554
+ # Plot Classify results
555
+ if pred_probs is not None and show_probs:
556
+ text = "\n".join(f"{names[j] if names else j} {pred_probs.data[j]:.2f}" for j in pred_probs.top5)
557
+ x = round(self.orig_shape[0] * 0.03)
558
+ annotator.text([x, x], text, txt_color=txt_color, box_color=(64, 64, 64, 128)) # RGBA box
559
+
560
+ # Plot Pose results
561
+ if self.keypoints is not None:
562
+ for i, k in enumerate(reversed(self.keypoints.data)):
563
+ annotator.kpts(
564
+ k,
565
+ self.orig_shape,
566
+ radius=kpt_radius,
567
+ kpt_line=kpt_line,
568
+ kpt_color=colors(i, True) if color_mode == "instance" else None,
569
+ )
570
+
571
+ # Show results
572
+ if show:
573
+ annotator.show(self.path)
574
+
575
+ # Save results
576
+ if save:
577
+ annotator.save(filename or f"results_{Path(self.path).name}")
578
+
579
+ return annotator.result(pil)
580
+
581
+ def show(self, *args, **kwargs):
582
+ """Display the image with annotated inference results.
583
+
584
+ This method plots the detection results on the original image and displays it. It's a convenient way to
585
+ visualize the model's predictions directly.
586
+
587
+ Args:
588
+ *args (Any): Variable length argument list to be passed to the `plot()` method.
589
+ **kwargs (Any): Arbitrary keyword arguments to be passed to the `plot()` method.
590
+
591
+ Examples:
592
+ >>> results = model("path/to/image.jpg")
593
+ >>> results[0].show() # Display the first result
594
+ >>> for result in results:
595
+ >>> result.show() # Display all results
596
+ """
597
+ self.plot(show=True, *args, **kwargs)
598
+
599
+ def save(self, filename: str | None = None, *args, **kwargs) -> str:
600
+ """Save annotated inference results image to file.
601
+
602
+ This method plots the detection results on the original image and saves the annotated image to a file. It
603
+ utilizes the `plot` method to generate the annotated image and then saves it to the specified filename.
604
+
605
+ Args:
606
+ filename (str | Path | None): The filename to save the annotated image. If None, a default filename is
607
+ generated based on the original image path.
608
+ *args (Any): Variable length argument list to be passed to the `plot` method.
609
+ **kwargs (Any): Arbitrary keyword arguments to be passed to the `plot` method.
610
+
611
+ Returns:
612
+ (str): The filename where the image was saved.
613
+
614
+ Examples:
615
+ >>> results = model("path/to/image.jpg")
616
+ >>> for result in results:
617
+ >>> result.save("annotated_image.jpg")
618
+ >>> # Or with custom plot arguments
619
+ >>> for result in results:
620
+ >>> result.save("annotated_image.jpg", conf=False, line_width=2)
621
+ """
622
+ if not filename:
623
+ filename = f"results_{Path(self.path).name}"
624
+ self.plot(save=True, filename=filename, *args, **kwargs)
625
+ return filename
626
+
627
+ def verbose(self) -> str:
628
+ """Return a log string for each task in the results, detailing detection and classification outcomes.
629
+
630
+ This method generates a human-readable string summarizing the detection and classification results. It includes
631
+ the number of detections for each class and the top probabilities for classification tasks.
632
+
633
+ Returns:
634
+ (str): A formatted string containing a summary of the results. For detection tasks, it includes the number
635
+ of detections per class. For classification tasks, it includes the top 5 class probabilities.
636
+
637
+ Examples:
638
+ >>> results = model("path/to/image.jpg")
639
+ >>> for result in results:
640
+ >>> print(result.verbose())
641
+ 2 persons, 1 car, 3 traffic lights,
642
+ dog 0.92, cat 0.78, horse 0.64,
643
+
644
+ Notes:
645
+ - If there are no detections, the method returns "(no detections), " for detection tasks.
646
+ - For classification tasks, it returns the top 5 class probabilities and their corresponding class names.
647
+ - The returned string is comma-separated and ends with a comma and a space.
648
+ """
649
+ boxes = self.obb if self.obb is not None else self.boxes
650
+ if len(self) == 0:
651
+ return "" if self.probs is not None else "(no detections), "
652
+ if self.probs is not None:
653
+ return f"{', '.join(f'{self.names[j]} {self.probs.data[j]:.2f}' for j in self.probs.top5)}, "
654
+ if boxes:
655
+ counts = boxes.cls.int().bincount()
656
+ return "".join(f"{n} {self.names[i]}{'s' * (n > 1)}, " for i, n in enumerate(counts) if n > 0)
657
+
658
+ def save_txt(self, txt_file: str | Path, save_conf: bool = False) -> str:
659
+ """Save detection results to a text file.
660
+
661
+ Args:
662
+ txt_file (str | Path): Path to the output text file.
663
+ save_conf (bool): Whether to include confidence scores in the output.
664
+
665
+ Returns:
666
+ (str): Path to the saved text file.
667
+
668
+ Examples:
669
+ >>> from ultralytics import YOLO
670
+ >>> model = YOLO("yolo11n.pt")
671
+ >>> results = model("path/to/image.jpg")
672
+ >>> for result in results:
673
+ >>> result.save_txt("output.txt")
674
+
675
+ Notes:
676
+ - The file will contain one line per detection or classification with the following structure:
677
+ - For detections: `class confidence x_center y_center width height`
678
+ - For classifications: `confidence class_name`
679
+ - For masks and keypoints, the specific formats will vary accordingly.
680
+ - The function will create the output directory if it does not exist.
681
+ - If save_conf is False, the confidence scores will be excluded from the output.
682
+ - Existing contents of the file will not be overwritten; new results will be appended.
683
+ """
684
+ is_obb = self.obb is not None
685
+ boxes = self.obb if is_obb else self.boxes
686
+ masks = self.masks
687
+ probs = self.probs
688
+ kpts = self.keypoints
689
+ texts = []
690
+ if probs is not None:
691
+ # Classify
692
+ [texts.append(f"{probs.data[j]:.2f} {self.names[j]}") for j in probs.top5]
693
+ elif boxes:
694
+ # Detect/segment/pose
695
+ for j, d in enumerate(boxes):
696
+ c, conf, id = int(d.cls), float(d.conf), int(d.id.item()) if d.is_track else None
697
+ line = (c, *(d.xyxyxyxyn.view(-1) if is_obb else d.xywhn.view(-1)))
698
+ if masks:
699
+ seg = masks[j].xyn[0].copy().reshape(-1) # reversed mask.xyn, (n,2) to (n*2)
700
+ line = (c, *seg)
701
+ if kpts is not None:
702
+ kpt = torch.cat((kpts[j].xyn, kpts[j].conf[..., None]), 2) if kpts[j].has_visible else kpts[j].xyn
703
+ line += (*kpt.reshape(-1).tolist(),)
704
+ line += (conf,) * save_conf + (() if id is None else (id,))
705
+ texts.append(("%g " * len(line)).rstrip() % line)
706
+
707
+ if texts:
708
+ Path(txt_file).parent.mkdir(parents=True, exist_ok=True) # make directory
709
+ with open(txt_file, "a", encoding="utf-8") as f:
710
+ f.writelines(text + "\n" for text in texts)
711
+
712
+ return str(txt_file)
713
+
714
+ def save_crop(self, save_dir: str | Path, file_name: str | Path = Path("im.jpg")):
715
+ """Save cropped detection images to specified directory.
716
+
717
+ This method saves cropped images of detected objects to a specified directory. Each crop is saved in a
718
+ subdirectory named after the object's class, with the filename based on the input file_name.
719
+
720
+ Args:
721
+ save_dir (str | Path): Directory path where cropped images will be saved.
722
+ file_name (str | Path): Base filename for the saved cropped images.
723
+
724
+ Examples:
725
+ >>> results = model("path/to/image.jpg")
726
+ >>> for result in results:
727
+ >>> result.save_crop(save_dir="path/to/crops", file_name="detection")
728
+
729
+ Notes:
730
+ - This method does not support Classify or Oriented Bounding Box (OBB) tasks.
731
+ - Crops are saved as 'save_dir/class_name/file_name.jpg'.
732
+ - The method will create necessary subdirectories if they don't exist.
733
+ - Original image is copied before cropping to avoid modifying the original.
734
+ """
735
+ if self.probs is not None:
736
+ LOGGER.warning("Classify task does not support `save_crop`.")
737
+ return
738
+ if self.obb is not None:
739
+ LOGGER.warning("OBB task does not support `save_crop`.")
740
+ return
741
+ for d in self.boxes:
742
+ save_one_box(
743
+ d.xyxy,
744
+ self.orig_img.copy(),
745
+ file=Path(save_dir) / self.names[int(d.cls)] / Path(file_name).with_suffix(".jpg"),
746
+ BGR=True,
747
+ )
748
+
749
+ def summary(self, normalize: bool = False, decimals: int = 5) -> list[dict[str, Any]]:
750
+ """Convert inference results to a summarized dictionary with optional normalization for box coordinates.
751
+
752
+ This method creates a list of detection dictionaries, each containing information about a single detection or
753
+ classification result. For classification tasks, it returns the top class and its
754
+ confidence. For detection tasks, it includes class information, bounding box coordinates, and
755
+ optionally mask segments and keypoints.
756
+
757
+ Args:
758
+ normalize (bool): Whether to normalize bounding box coordinates by image dimensions.
759
+ decimals (int): Number of decimal places to round the output values to.
760
+
761
+ Returns:
762
+ (list[dict[str, Any]]): A list of dictionaries, each containing summarized information for a single
763
+ detection or classification result. The structure of each dictionary varies based on the task type
764
+ (classification or detection) and available information (boxes, masks, keypoints).
765
+
766
+ Examples:
767
+ >>> results = model("image.jpg")
768
+ >>> for result in results:
769
+ >>> summary = result.summary()
770
+ >>> print(summary)
771
+ """
772
+ # Create list of detection dictionaries
773
+ results = []
774
+ if self.probs is not None:
775
+ class_id = self.probs.top1
776
+ results.append(
777
+ {
778
+ "name": self.names[class_id],
779
+ "class": class_id,
780
+ "confidence": round(self.probs.top1conf.item(), decimals),
781
+ }
782
+ )
783
+ return results
784
+
785
+ is_obb = self.obb is not None
786
+ data = self.obb if is_obb else self.boxes
787
+ h, w = self.orig_shape if normalize else (1, 1)
788
+ for i, row in enumerate(data): # xyxy, track_id if tracking, conf, class_id
789
+ class_id, conf = int(row.cls), round(row.conf.item(), decimals)
790
+ box = (row.xyxyxyxy if is_obb else row.xyxy).squeeze().reshape(-1, 2).tolist()
791
+ xy = {}
792
+ for j, b in enumerate(box):
793
+ xy[f"x{j + 1}"] = round(b[0] / w, decimals)
794
+ xy[f"y{j + 1}"] = round(b[1] / h, decimals)
795
+ result = {"name": self.names[class_id], "class": class_id, "confidence": conf, "box": xy}
796
+ if data.is_track:
797
+ result["track_id"] = int(row.id.item()) # track ID
798
+ if self.masks:
799
+ result["segments"] = {
800
+ "x": (self.masks.xy[i][:, 0] / w).round(decimals).tolist(),
801
+ "y": (self.masks.xy[i][:, 1] / h).round(decimals).tolist(),
802
+ }
803
+ if self.keypoints is not None:
804
+ x, y, visible = self.keypoints[i].data[0].cpu().unbind(dim=1) # torch Tensor
805
+ result["keypoints"] = {
806
+ "x": (x / w).numpy().round(decimals).tolist(), # decimals named argument required
807
+ "y": (y / h).numpy().round(decimals).tolist(),
808
+ "visible": visible.numpy().round(decimals).tolist(),
809
+ }
810
+ results.append(result)
811
+
812
+ return results
813
+
814
+
815
+ class Boxes(BaseTensor):
816
+ """A class for managing and manipulating detection boxes.
817
+
818
+ This class provides comprehensive functionality for handling detection boxes, including their coordinates,
819
+ confidence scores, class labels, and optional tracking IDs. It supports various box formats and offers methods for
820
+ easy manipulation and conversion between different coordinate systems.
821
+
822
+ Attributes:
823
+ data (torch.Tensor | np.ndarray): The raw tensor containing detection boxes and associated data.
824
+ orig_shape (tuple[int, int]): The original image dimensions (height, width).
825
+ is_track (bool): Indicates whether tracking IDs are included in the box data.
826
+ xyxy (torch.Tensor | np.ndarray): Boxes in [x1, y1, x2, y2] format.
827
+ conf (torch.Tensor | np.ndarray): Confidence scores for each box.
828
+ cls (torch.Tensor | np.ndarray): Class labels for each box.
829
+ id (torch.Tensor | None): Tracking IDs for each box (if available).
830
+ xywh (torch.Tensor | np.ndarray): Boxes in [x, y, width, height] format.
831
+ xyxyn (torch.Tensor | np.ndarray): Normalized [x1, y1, x2, y2] boxes relative to orig_shape.
832
+ xywhn (torch.Tensor | np.ndarray): Normalized [x, y, width, height] boxes relative to orig_shape.
833
+
834
+ Methods:
835
+ cpu: Return a copy of the object with all tensors on CPU memory.
836
+ numpy: Return a copy of the object with all tensors as numpy arrays.
837
+ cuda: Return a copy of the object with all tensors on GPU memory.
838
+ to: Return a copy of the object with tensors on specified device and dtype.
839
+
840
+ Examples:
841
+ >>> import torch
842
+ >>> boxes_data = torch.tensor([[100, 50, 150, 100, 0.9, 0], [200, 150, 300, 250, 0.8, 1]])
843
+ >>> orig_shape = (480, 640) # height, width
844
+ >>> boxes = Boxes(boxes_data, orig_shape)
845
+ >>> print(boxes.xyxy)
846
+ >>> print(boxes.conf)
847
+ >>> print(boxes.cls)
848
+ >>> print(boxes.xywhn)
849
+ """
850
+
851
+ def __init__(self, boxes: torch.Tensor | np.ndarray, orig_shape: tuple[int, int]) -> None:
852
+ """Initialize the Boxes class with detection box data and the original image shape.
853
+
854
+ This class manages detection boxes, providing easy access and manipulation of box coordinates, confidence
855
+ scores, class identifiers, and optional tracking IDs. It supports multiple formats for box coordinates,
856
+ including both absolute and normalized forms.
857
+
858
+ Args:
859
+ boxes (torch.Tensor | np.ndarray): A tensor or numpy array with detection boxes of shape (num_boxes, 6) or
860
+ (num_boxes, 7). Columns should contain [x1, y1, x2, y2, (optional) track_id, confidence, class].
861
+ orig_shape (tuple[int, int]): The original image shape as (height, width). Used for normalization.
862
+ """
863
+ if boxes.ndim == 1:
864
+ boxes = boxes[None, :]
865
+ n = boxes.shape[-1]
866
+ assert n in {6, 7}, f"expected 6 or 7 values but got {n}" # xyxy, track_id, conf, cls
867
+ super().__init__(boxes, orig_shape)
868
+ self.is_track = n == 7
869
+ self.orig_shape = orig_shape
870
+
871
+ @property
872
+ def xyxy(self) -> torch.Tensor | np.ndarray:
873
+ """Return bounding boxes in [x1, y1, x2, y2] format.
874
+
875
+ Returns:
876
+ (torch.Tensor | np.ndarray): A tensor or numpy array of shape (n, 4) containing bounding box coordinates in
877
+ [x1, y1, x2, y2] format, where n is the number of boxes.
878
+
879
+ Examples:
880
+ >>> results = model("image.jpg")
881
+ >>> boxes = results[0].boxes
882
+ >>> xyxy = boxes.xyxy
883
+ >>> print(xyxy)
884
+ """
885
+ return self.data[:, :4]
886
+
887
+ @property
888
+ def conf(self) -> torch.Tensor | np.ndarray:
889
+ """Return the confidence scores for each detection box.
890
+
891
+ Returns:
892
+ (torch.Tensor | np.ndarray): A 1D tensor or array containing confidence scores for each detection, with
893
+ shape (N,) where N is the number of detections.
894
+
895
+ Examples:
896
+ >>> boxes = Boxes(torch.tensor([[10, 20, 30, 40, 0.9, 0]]), orig_shape=(100, 100))
897
+ >>> conf_scores = boxes.conf
898
+ >>> print(conf_scores)
899
+ tensor([0.9000])
900
+ """
901
+ return self.data[:, -2]
902
+
903
+ @property
904
+ def cls(self) -> torch.Tensor | np.ndarray:
905
+ """Return the class ID tensor representing category predictions for each bounding box.
906
+
907
+ Returns:
908
+ (torch.Tensor | np.ndarray): A tensor or numpy array containing the class IDs for each detection box. The
909
+ shape is (N,), where N is the number of boxes.
910
+
911
+ Examples:
912
+ >>> results = model("image.jpg")
913
+ >>> boxes = results[0].boxes
914
+ >>> class_ids = boxes.cls
915
+ >>> print(class_ids) # tensor([0., 2., 1.])
916
+ """
917
+ return self.data[:, -1]
918
+
919
+ @property
920
+ def id(self) -> torch.Tensor | np.ndarray | None:
921
+ """Return the tracking IDs for each detection box if available.
922
+
923
+ Returns:
924
+ (torch.Tensor | None): A tensor containing tracking IDs for each box if tracking is enabled, otherwise None.
925
+ Shape is (N,) where N is the number of boxes.
926
+
927
+ Examples:
928
+ >>> results = model.track("path/to/video.mp4")
929
+ >>> for result in results:
930
+ ... boxes = result.boxes
931
+ ... if boxes.is_track:
932
+ ... track_ids = boxes.id
933
+ ... print(f"Tracking IDs: {track_ids}")
934
+ ... else:
935
+ ... print("Tracking is not enabled for these boxes.")
936
+
937
+ Notes:
938
+ - This property is only available when tracking is enabled (i.e., when `is_track` is True).
939
+ - The tracking IDs are typically used to associate detections across multiple frames in video analysis.
940
+ """
941
+ return self.data[:, -3] if self.is_track else None
942
+
943
+ @property
944
+ @lru_cache(maxsize=2)
945
+ def xywh(self) -> torch.Tensor | np.ndarray:
946
+ """Convert bounding boxes from [x1, y1, x2, y2] format to [x, y, width, height] format.
947
+
948
+ Returns:
949
+ (torch.Tensor | np.ndarray): Boxes in [x_center, y_center, width, height] format, where x_center, y_center
950
+ are the coordinates of the center point of the bounding box, width, height are the dimensions of the
951
+ bounding box and the shape of the returned tensor is (N, 4), where N is the number of boxes.
952
+
953
+ Examples:
954
+ >>> boxes = Boxes(torch.tensor([[100, 50, 150, 100], [200, 150, 300, 250]]), orig_shape=(480, 640))
955
+ >>> xywh = boxes.xywh
956
+ >>> print(xywh)
957
+ tensor([[100.0000, 50.0000, 50.0000, 50.0000],
958
+ [200.0000, 150.0000, 100.0000, 100.0000]])
959
+ """
960
+ return ops.xyxy2xywh(self.xyxy)
961
+
962
+ @property
963
+ @lru_cache(maxsize=2)
964
+ def xyxyn(self) -> torch.Tensor | np.ndarray:
965
+ """Return normalized bounding box coordinates relative to the original image size.
966
+
967
+ This property calculates and returns the bounding box coordinates in [x1, y1, x2, y2] format, normalized to the
968
+ range [0, 1] based on the original image dimensions.
969
+
970
+ Returns:
971
+ (torch.Tensor | np.ndarray): Normalized bounding box coordinates with shape (N, 4), where N is the number of
972
+ boxes. Each row contains [x1, y1, x2, y2] values normalized to [0, 1].
973
+
974
+ Examples:
975
+ >>> boxes = Boxes(torch.tensor([[100, 50, 300, 400, 0.9, 0]]), orig_shape=(480, 640))
976
+ >>> normalized = boxes.xyxyn
977
+ >>> print(normalized)
978
+ tensor([[0.1562, 0.1042, 0.4688, 0.8333]])
979
+ """
980
+ xyxy = self.xyxy.clone() if isinstance(self.xyxy, torch.Tensor) else np.copy(self.xyxy)
981
+ xyxy[..., [0, 2]] /= self.orig_shape[1]
982
+ xyxy[..., [1, 3]] /= self.orig_shape[0]
983
+ return xyxy
984
+
985
+ @property
986
+ @lru_cache(maxsize=2)
987
+ def xywhn(self) -> torch.Tensor | np.ndarray:
988
+ """Return normalized bounding boxes in [x, y, width, height] format.
989
+
990
+ This property calculates and returns the normalized bounding box coordinates in the format [x_center, y_center,
991
+ width, height], where all values are relative to the original image dimensions.
992
+
993
+ Returns:
994
+ (torch.Tensor | np.ndarray): Normalized bounding boxes with shape (N, 4), where N is the number of boxes.
995
+ Each row contains [x_center, y_center, width, height] values normalized to [0, 1] based on the original
996
+ image dimensions.
997
+
998
+ Examples:
999
+ >>> boxes = Boxes(torch.tensor([[100, 50, 150, 100, 0.9, 0]]), orig_shape=(480, 640))
1000
+ >>> normalized = boxes.xywhn
1001
+ >>> print(normalized)
1002
+ tensor([[0.1953, 0.1562, 0.0781, 0.1042]])
1003
+ """
1004
+ xywh = ops.xyxy2xywh(self.xyxy)
1005
+ xywh[..., [0, 2]] /= self.orig_shape[1]
1006
+ xywh[..., [1, 3]] /= self.orig_shape[0]
1007
+ return xywh
1008
+
1009
+
1010
+ class Masks(BaseTensor):
1011
+ """A class for storing and manipulating detection masks.
1012
+
1013
+ This class extends BaseTensor and provides functionality for handling segmentation masks, including methods for
1014
+ converting between pixel and normalized coordinates.
1015
+
1016
+ Attributes:
1017
+ data (torch.Tensor | np.ndarray): The raw tensor or array containing mask data.
1018
+ orig_shape (tuple): Original image shape in (height, width) format.
1019
+ xy (list[np.ndarray]): A list of segments in pixel coordinates.
1020
+ xyn (list[np.ndarray]): A list of normalized segments.
1021
+
1022
+ Methods:
1023
+ cpu: Return a copy of the Masks object with the mask tensor on CPU memory.
1024
+ numpy: Return a copy of the Masks object with the mask tensor as a numpy array.
1025
+ cuda: Return a copy of the Masks object with the mask tensor on GPU memory.
1026
+ to: Return a copy of the Masks object with the mask tensor on specified device and dtype.
1027
+
1028
+ Examples:
1029
+ >>> masks_data = torch.rand(1, 160, 160)
1030
+ >>> orig_shape = (720, 1280)
1031
+ >>> masks = Masks(masks_data, orig_shape)
1032
+ >>> pixel_coords = masks.xy
1033
+ >>> normalized_coords = masks.xyn
1034
+ """
1035
+
1036
+ def __init__(self, masks: torch.Tensor | np.ndarray, orig_shape: tuple[int, int]) -> None:
1037
+ """Initialize the Masks class with detection mask data and the original image shape.
1038
+
1039
+ Args:
1040
+ masks (torch.Tensor | np.ndarray): Detection masks with shape (num_masks, height, width).
1041
+ orig_shape (tuple): The original image shape as (height, width). Used for normalization.
1042
+ """
1043
+ if masks.ndim == 2:
1044
+ masks = masks[None, :]
1045
+ super().__init__(masks, orig_shape)
1046
+
1047
+ @property
1048
+ @lru_cache(maxsize=1)
1049
+ def xyn(self) -> list[np.ndarray]:
1050
+ """Return normalized xy-coordinates of the segmentation masks.
1051
+
1052
+ This property calculates and caches the normalized xy-coordinates of the segmentation masks. The coordinates are
1053
+ normalized relative to the original image shape.
1054
+
1055
+ Returns:
1056
+ (list[np.ndarray]): A list of numpy arrays, where each array contains the normalized xy-coordinates of a
1057
+ single segmentation mask. Each array has shape (N, 2), where N is the number of points in the
1058
+ mask contour.
1059
+
1060
+ Examples:
1061
+ >>> results = model("image.jpg")
1062
+ >>> masks = results[0].masks
1063
+ >>> normalized_coords = masks.xyn
1064
+ >>> print(normalized_coords[0]) # Normalized coordinates of the first mask
1065
+ """
1066
+ return [
1067
+ ops.scale_coords(self.data.shape[1:], x, self.orig_shape, normalize=True)
1068
+ for x in ops.masks2segments(self.data)
1069
+ ]
1070
+
1071
+ @property
1072
+ @lru_cache(maxsize=1)
1073
+ def xy(self) -> list[np.ndarray]:
1074
+ """Return the [x, y] pixel coordinates for each segment in the mask tensor.
1075
+
1076
+ This property calculates and returns a list of pixel coordinates for each segmentation mask in the Masks object.
1077
+ The coordinates are scaled to match the original image dimensions.
1078
+
1079
+ Returns:
1080
+ (list[np.ndarray]): A list of numpy arrays, where each array contains the [x, y] pixel coordinates for a
1081
+ single segmentation mask. Each array has shape (N, 2), where N is the number of points in the segment.
1082
+
1083
+ Examples:
1084
+ >>> results = model("image.jpg")
1085
+ >>> masks = results[0].masks
1086
+ >>> xy_coords = masks.xy
1087
+ >>> print(len(xy_coords)) # Number of masks
1088
+ >>> print(xy_coords[0].shape) # Shape of first mask's coordinates
1089
+ """
1090
+ return [
1091
+ ops.scale_coords(self.data.shape[1:], x, self.orig_shape, normalize=False)
1092
+ for x in ops.masks2segments(self.data)
1093
+ ]
1094
+
1095
+
1096
+ class Keypoints(BaseTensor):
1097
+ """A class for storing and manipulating detection keypoints.
1098
+
1099
+ This class encapsulates functionality for handling keypoint data, including coordinate manipulation, normalization,
1100
+ and confidence values. It supports keypoint detection results with optional visibility information.
1101
+
1102
+ Attributes:
1103
+ data (torch.Tensor): The raw tensor containing keypoint data.
1104
+ orig_shape (tuple[int, int]): The original image dimensions (height, width).
1105
+ has_visible (bool): Indicates whether visibility information is available for keypoints.
1106
+ xy (torch.Tensor): Keypoint coordinates in [x, y] format.
1107
+ xyn (torch.Tensor): Normalized keypoint coordinates in [x, y] format, relative to orig_shape.
1108
+ conf (torch.Tensor): Confidence values for each keypoint, if available.
1109
+
1110
+ Methods:
1111
+ cpu: Return a copy of the keypoints tensor on CPU memory.
1112
+ numpy: Return a copy of the keypoints tensor as a numpy array.
1113
+ cuda: Return a copy of the keypoints tensor on GPU memory.
1114
+ to: Return a copy of the keypoints tensor with specified device and dtype.
1115
+
1116
+ Examples:
1117
+ >>> import torch
1118
+ >>> from ultralytics.engine.results import Keypoints
1119
+ >>> keypoints_data = torch.rand(1, 17, 3) # 1 detection, 17 keypoints, (x, y, conf)
1120
+ >>> orig_shape = (480, 640) # Original image shape (height, width)
1121
+ >>> keypoints = Keypoints(keypoints_data, orig_shape)
1122
+ >>> print(keypoints.xy.shape) # Access xy coordinates
1123
+ >>> print(keypoints.conf) # Access confidence values
1124
+ >>> keypoints_cpu = keypoints.cpu() # Move keypoints to CPU
1125
+ """
1126
+
1127
+ def __init__(self, keypoints: torch.Tensor | np.ndarray, orig_shape: tuple[int, int]) -> None:
1128
+ """Initialize the Keypoints object with detection keypoints and original image dimensions.
1129
+
1130
+ This method processes the input keypoints tensor, handling both 2D and 3D formats. For 3D tensors (x, y,
1131
+ confidence), it masks out low-confidence keypoints by setting their coordinates to zero.
1132
+
1133
+ Args:
1134
+ keypoints (torch.Tensor): A tensor containing keypoint data. Shape can be either:
1135
+ - (num_objects, num_keypoints, 2) for x, y coordinates only
1136
+ - (num_objects, num_keypoints, 3) for x, y coordinates and confidence scores
1137
+ orig_shape (tuple[int, int]): The original image dimensions (height, width).
1138
+ """
1139
+ if keypoints.ndim == 2:
1140
+ keypoints = keypoints[None, :]
1141
+ super().__init__(keypoints, orig_shape)
1142
+ self.has_visible = self.data.shape[-1] == 3
1143
+
1144
+ @property
1145
+ @lru_cache(maxsize=1)
1146
+ def xy(self) -> torch.Tensor | np.ndarray:
1147
+ """Return x, y coordinates of keypoints.
1148
+
1149
+ Returns:
1150
+ (torch.Tensor): A tensor containing the x, y coordinates of keypoints with shape (N, K, 2), where N is the
1151
+ number of detections and K is the number of keypoints per detection.
1152
+
1153
+ Examples:
1154
+ >>> results = model("image.jpg")
1155
+ >>> keypoints = results[0].keypoints
1156
+ >>> xy = keypoints.xy
1157
+ >>> print(xy.shape) # (N, K, 2)
1158
+ >>> print(xy[0]) # x, y coordinates of keypoints for first detection
1159
+
1160
+ Notes:
1161
+ - The returned coordinates are in pixel units relative to the original image dimensions.
1162
+ - If keypoints were initialized with confidence values, only keypoints with confidence >= 0.5 are returned.
1163
+ - This property uses LRU caching to improve performance on repeated access.
1164
+ """
1165
+ return self.data[..., :2]
1166
+
1167
+ @property
1168
+ @lru_cache(maxsize=1)
1169
+ def xyn(self) -> torch.Tensor | np.ndarray:
1170
+ """Return normalized coordinates (x, y) of keypoints relative to the original image size.
1171
+
1172
+ Returns:
1173
+ (torch.Tensor | np.ndarray): A tensor or array of shape (N, K, 2) containing normalized keypoint
1174
+ coordinates, where N is the number of instances, K is the number of keypoints, and the last dimension
1175
+ contains [x, y] values in the range [0, 1].
1176
+
1177
+ Examples:
1178
+ >>> keypoints = Keypoints(torch.rand(1, 17, 2), orig_shape=(480, 640))
1179
+ >>> normalized_kpts = keypoints.xyn
1180
+ >>> print(normalized_kpts.shape)
1181
+ torch.Size([1, 17, 2])
1182
+ """
1183
+ xy = self.xy.clone() if isinstance(self.xy, torch.Tensor) else np.copy(self.xy)
1184
+ xy[..., 0] /= self.orig_shape[1]
1185
+ xy[..., 1] /= self.orig_shape[0]
1186
+ return xy
1187
+
1188
+ @property
1189
+ @lru_cache(maxsize=1)
1190
+ def conf(self) -> torch.Tensor | np.ndarray | None:
1191
+ """Return confidence values for each keypoint.
1192
+
1193
+ Returns:
1194
+ (torch.Tensor | None): A tensor containing confidence scores for each keypoint if available, otherwise None.
1195
+ Shape is (num_detections, num_keypoints) for batched data or (num_keypoints,) for single detection.
1196
+
1197
+ Examples:
1198
+ >>> keypoints = Keypoints(torch.rand(1, 17, 3), orig_shape=(640, 640)) # 1 detection, 17 keypoints
1199
+ >>> conf = keypoints.conf
1200
+ >>> print(conf.shape) # torch.Size([1, 17])
1201
+ """
1202
+ return self.data[..., 2] if self.has_visible else None
1203
+
1204
+
1205
+ class Probs(BaseTensor):
1206
+ """A class for storing and manipulating classification probabilities.
1207
+
1208
+ This class extends BaseTensor and provides methods for accessing and manipulating classification probabilities,
1209
+ including top-1 and top-5 predictions.
1210
+
1211
+ Attributes:
1212
+ data (torch.Tensor | np.ndarray): The raw tensor or array containing classification probabilities.
1213
+ orig_shape (tuple | None): The original image shape as (height, width). Not used in this class.
1214
+ top1 (int): Index of the class with the highest probability.
1215
+ top5 (list[int]): Indices of the top 5 classes by probability.
1216
+ top1conf (torch.Tensor | np.ndarray): Confidence score of the top 1 class.
1217
+ top5conf (torch.Tensor | np.ndarray): Confidence scores of the top 5 classes.
1218
+
1219
+ Methods:
1220
+ cpu: Return a copy of the probabilities tensor on CPU memory.
1221
+ numpy: Return a copy of the probabilities tensor as a numpy array.
1222
+ cuda: Return a copy of the probabilities tensor on GPU memory.
1223
+ to: Return a copy of the probabilities tensor with specified device and dtype.
1224
+
1225
+ Examples:
1226
+ >>> probs = torch.tensor([0.1, 0.3, 0.6])
1227
+ >>> p = Probs(probs)
1228
+ >>> print(p.top1)
1229
+ 2
1230
+ >>> print(p.top5)
1231
+ [2, 1, 0]
1232
+ >>> print(p.top1conf)
1233
+ tensor(0.6000)
1234
+ >>> print(p.top5conf)
1235
+ tensor([0.6000, 0.3000, 0.1000])
1236
+ """
1237
+
1238
+ def __init__(self, probs: torch.Tensor | np.ndarray, orig_shape: tuple[int, int] | None = None) -> None:
1239
+ """Initialize the Probs class with classification probabilities.
1240
+
1241
+ This class stores and manages classification probabilities, providing easy access to top predictions and their
1242
+ confidences.
1243
+
1244
+ Args:
1245
+ probs (torch.Tensor | np.ndarray): A 1D tensor or array of classification probabilities.
1246
+ orig_shape (tuple | None): The original image shape as (height, width). Not used in this class but kept for
1247
+ consistency with other result classes.
1248
+ """
1249
+ super().__init__(probs, orig_shape)
1250
+
1251
+ @property
1252
+ @lru_cache(maxsize=1)
1253
+ def top1(self) -> int:
1254
+ """Return the index of the class with the highest probability.
1255
+
1256
+ Returns:
1257
+ (int): Index of the class with the highest probability.
1258
+
1259
+ Examples:
1260
+ >>> probs = Probs(torch.tensor([0.1, 0.3, 0.6]))
1261
+ >>> probs.top1
1262
+ 2
1263
+ """
1264
+ return int(self.data.argmax())
1265
+
1266
+ @property
1267
+ @lru_cache(maxsize=1)
1268
+ def top5(self) -> list[int]:
1269
+ """Return the indices of the top 5 class probabilities.
1270
+
1271
+ Returns:
1272
+ (list[int]): A list containing the indices of the top 5 class probabilities, sorted in descending order.
1273
+
1274
+ Examples:
1275
+ >>> probs = Probs(torch.tensor([0.1, 0.2, 0.3, 0.4, 0.5]))
1276
+ >>> print(probs.top5)
1277
+ [4, 3, 2, 1, 0]
1278
+ """
1279
+ return (-self.data).argsort(0)[:5].tolist() # this way works with both torch and numpy.
1280
+
1281
+ @property
1282
+ @lru_cache(maxsize=1)
1283
+ def top1conf(self) -> torch.Tensor | np.ndarray:
1284
+ """Return the confidence score of the highest probability class.
1285
+
1286
+ This property retrieves the confidence score (probability) of the class with the highest predicted probability
1287
+ from the classification results.
1288
+
1289
+ Returns:
1290
+ (torch.Tensor | np.ndarray): A tensor containing the confidence score of the top 1 class.
1291
+
1292
+ Examples:
1293
+ >>> results = model("image.jpg") # classify an image
1294
+ >>> probs = results[0].probs # get classification probabilities
1295
+ >>> top1_confidence = probs.top1conf # get confidence of top 1 class
1296
+ >>> print(f"Top 1 class confidence: {top1_confidence.item():.4f}")
1297
+ """
1298
+ return self.data[self.top1]
1299
+
1300
+ @property
1301
+ @lru_cache(maxsize=1)
1302
+ def top5conf(self) -> torch.Tensor | np.ndarray:
1303
+ """Return confidence scores for the top 5 classification predictions.
1304
+
1305
+ This property retrieves the confidence scores corresponding to the top 5 class probabilities predicted by the
1306
+ model. It provides a quick way to access the most likely class predictions along with their associated
1307
+ confidence levels.
1308
+
1309
+ Returns:
1310
+ (torch.Tensor | np.ndarray): A tensor or array containing the confidence scores for the top 5 predicted
1311
+ classes, sorted in descending order of probability.
1312
+
1313
+ Examples:
1314
+ >>> results = model("image.jpg")
1315
+ >>> probs = results[0].probs
1316
+ >>> top5_conf = probs.top5conf
1317
+ >>> print(top5_conf) # Prints confidence scores for top 5 classes
1318
+ """
1319
+ return self.data[self.top5]
1320
+
1321
+
1322
+ class OBB(BaseTensor):
1323
+ """A class for storing and manipulating Oriented Bounding Boxes (OBB).
1324
+
1325
+ This class provides functionality to handle oriented bounding boxes, including conversion between different formats,
1326
+ normalization, and access to various properties of the boxes. It supports both tracking and non-tracking scenarios.
1327
+
1328
+ Attributes:
1329
+ data (torch.Tensor): The raw OBB tensor containing box coordinates and associated data.
1330
+ orig_shape (tuple): Original image size as (height, width).
1331
+ is_track (bool): Indicates whether tracking IDs are included in the box data.
1332
+ xywhr (torch.Tensor | np.ndarray): Boxes in [x_center, y_center, width, height, rotation] format.
1333
+ conf (torch.Tensor | np.ndarray): Confidence scores for each box.
1334
+ cls (torch.Tensor | np.ndarray): Class labels for each box.
1335
+ id (torch.Tensor | np.ndarray): Tracking IDs for each box, if available.
1336
+ xyxyxyxy (torch.Tensor | np.ndarray): Boxes in 8-point [x1, y1, x2, y2, x3, y3, x4, y4] format.
1337
+ xyxyxyxyn (torch.Tensor | np.ndarray): Normalized 8-point coordinates relative to orig_shape.
1338
+ xyxy (torch.Tensor | np.ndarray): Axis-aligned bounding boxes in [x1, y1, x2, y2] format.
1339
+
1340
+ Methods:
1341
+ cpu: Return a copy of the OBB object with all tensors on CPU memory.
1342
+ numpy: Return a copy of the OBB object with all tensors as numpy arrays.
1343
+ cuda: Return a copy of the OBB object with all tensors on GPU memory.
1344
+ to: Return a copy of the OBB object with tensors on specified device and dtype.
1345
+
1346
+ Examples:
1347
+ >>> boxes = torch.tensor([[100, 50, 150, 100, 30, 0.9, 0]]) # xywhr, conf, cls
1348
+ >>> obb = OBB(boxes, orig_shape=(480, 640))
1349
+ >>> print(obb.xyxyxyxy)
1350
+ >>> print(obb.conf)
1351
+ >>> print(obb.cls)
1352
+ """
1353
+
1354
+ def __init__(self, boxes: torch.Tensor | np.ndarray, orig_shape: tuple[int, int]) -> None:
1355
+ """Initialize an OBB (Oriented Bounding Box) instance with oriented bounding box data and original image shape.
1356
+
1357
+ This class stores and manipulates Oriented Bounding Boxes (OBB) for object detection tasks. It provides various
1358
+ properties and methods to access and transform the OBB data.
1359
+
1360
+ Args:
1361
+ boxes (torch.Tensor | np.ndarray): A tensor or numpy array containing the detection boxes, with shape
1362
+ (num_boxes, 7) or (num_boxes, 8). The last two columns contain confidence and class values. If present,
1363
+ the third last column contains track IDs, and the fifth column contains rotation.
1364
+ orig_shape (tuple[int, int]): Original image size, in the format (height, width).
1365
+
1366
+ Raises:
1367
+ AssertionError: If the number of values per box is not 7 or 8.
1368
+ """
1369
+ if boxes.ndim == 1:
1370
+ boxes = boxes[None, :]
1371
+ n = boxes.shape[-1]
1372
+ assert n in {7, 8}, f"expected 7 or 8 values but got {n}" # xywh, rotation, track_id, conf, cls
1373
+ super().__init__(boxes, orig_shape)
1374
+ self.is_track = n == 8
1375
+ self.orig_shape = orig_shape
1376
+
1377
+ @property
1378
+ def xywhr(self) -> torch.Tensor | np.ndarray:
1379
+ """Return boxes in [x_center, y_center, width, height, rotation] format.
1380
+
1381
+ Returns:
1382
+ (torch.Tensor | np.ndarray): A tensor or numpy array containing the oriented bounding boxes with format
1383
+ [x_center, y_center, width, height, rotation]. The shape is (N, 5) where N is the number of boxes.
1384
+
1385
+ Examples:
1386
+ >>> results = model("image.jpg")
1387
+ >>> obb = results[0].obb
1388
+ >>> xywhr = obb.xywhr
1389
+ >>> print(xywhr.shape)
1390
+ torch.Size([3, 5])
1391
+ """
1392
+ return self.data[:, :5]
1393
+
1394
+ @property
1395
+ def conf(self) -> torch.Tensor | np.ndarray:
1396
+ """Return the confidence scores for Oriented Bounding Boxes (OBBs).
1397
+
1398
+ This property retrieves the confidence values associated with each OBB detection. The confidence score
1399
+ represents the model's certainty in the detection.
1400
+
1401
+ Returns:
1402
+ (torch.Tensor | np.ndarray): A tensor or numpy array of shape (N,) containing confidence scores for N
1403
+ detections, where each score is in the range [0, 1].
1404
+
1405
+ Examples:
1406
+ >>> results = model("image.jpg")
1407
+ >>> obb_result = results[0].obb
1408
+ >>> confidence_scores = obb_result.conf
1409
+ >>> print(confidence_scores)
1410
+ """
1411
+ return self.data[:, -2]
1412
+
1413
+ @property
1414
+ def cls(self) -> torch.Tensor | np.ndarray:
1415
+ """Return the class values of the oriented bounding boxes.
1416
+
1417
+ Returns:
1418
+ (torch.Tensor | np.ndarray): A tensor or numpy array containing the class values for each oriented bounding
1419
+ box. The shape is (N,), where N is the number of boxes.
1420
+
1421
+ Examples:
1422
+ >>> results = model("image.jpg")
1423
+ >>> result = results[0]
1424
+ >>> obb = result.obb
1425
+ >>> class_values = obb.cls
1426
+ >>> print(class_values)
1427
+ """
1428
+ return self.data[:, -1]
1429
+
1430
+ @property
1431
+ def id(self) -> torch.Tensor | np.ndarray | None:
1432
+ """Return the tracking IDs of the oriented bounding boxes (if available).
1433
+
1434
+ Returns:
1435
+ (torch.Tensor | np.ndarray | None): A tensor or numpy array containing the tracking IDs for each oriented
1436
+ bounding box. Returns None if tracking IDs are not available.
1437
+
1438
+ Examples:
1439
+ >>> results = model("image.jpg", tracker=True) # Run inference with tracking
1440
+ >>> for result in results:
1441
+ ... if result.obb is not None:
1442
+ ... track_ids = result.obb.id
1443
+ ... if track_ids is not None:
1444
+ ... print(f"Tracking IDs: {track_ids}")
1445
+ """
1446
+ return self.data[:, -3] if self.is_track else None
1447
+
1448
+ @property
1449
+ @lru_cache(maxsize=2)
1450
+ def xyxyxyxy(self) -> torch.Tensor | np.ndarray:
1451
+ """Convert OBB format to 8-point (xyxyxyxy) coordinate format for rotated bounding boxes.
1452
+
1453
+ Returns:
1454
+ (torch.Tensor | np.ndarray): Rotated bounding boxes in xyxyxyxy format with shape (N, 4, 2), where N is the
1455
+ number of boxes. Each box is represented by 4 points (x, y), starting from the top-left corner and
1456
+ moving clockwise.
1457
+
1458
+ Examples:
1459
+ >>> obb = OBB(torch.tensor([[100, 100, 50, 30, 0.5, 0.9, 0]]), orig_shape=(640, 640))
1460
+ >>> xyxyxyxy = obb.xyxyxyxy
1461
+ >>> print(xyxyxyxy.shape)
1462
+ torch.Size([1, 4, 2])
1463
+ """
1464
+ return ops.xywhr2xyxyxyxy(self.xywhr)
1465
+
1466
+ @property
1467
+ @lru_cache(maxsize=2)
1468
+ def xyxyxyxyn(self) -> torch.Tensor | np.ndarray:
1469
+ """Convert rotated bounding boxes to normalized xyxyxyxy format.
1470
+
1471
+ Returns:
1472
+ (torch.Tensor | np.ndarray): Normalized rotated bounding boxes in xyxyxyxy format with shape (N, 4, 2),
1473
+ where N is the number of boxes. Each box is represented by 4 points (x, y), normalized relative to the
1474
+ original image dimensions.
1475
+
1476
+ Examples:
1477
+ >>> obb = OBB(torch.rand(10, 7), orig_shape=(640, 480)) # 10 random OBBs
1478
+ >>> normalized_boxes = obb.xyxyxyxyn
1479
+ >>> print(normalized_boxes.shape)
1480
+ torch.Size([10, 4, 2])
1481
+ """
1482
+ xyxyxyxyn = self.xyxyxyxy.clone() if isinstance(self.xyxyxyxy, torch.Tensor) else np.copy(self.xyxyxyxy)
1483
+ xyxyxyxyn[..., 0] /= self.orig_shape[1]
1484
+ xyxyxyxyn[..., 1] /= self.orig_shape[0]
1485
+ return xyxyxyxyn
1486
+
1487
+ @property
1488
+ @lru_cache(maxsize=2)
1489
+ def xyxy(self) -> torch.Tensor | np.ndarray:
1490
+ """Convert oriented bounding boxes (OBB) to axis-aligned bounding boxes in xyxy format.
1491
+
1492
+ This property calculates the minimal enclosing rectangle for each oriented bounding box and returns it in xyxy
1493
+ format (x1, y1, x2, y2). This is useful for operations that require axis-aligned bounding boxes, such as IoU
1494
+ calculation with non-rotated boxes.
1495
+
1496
+ Returns:
1497
+ (torch.Tensor | np.ndarray): Axis-aligned bounding boxes in xyxy format with shape (N, 4), where N is the
1498
+ number of boxes. Each row contains [x1, y1, x2, y2] coordinates.
1499
+
1500
+ Examples:
1501
+ >>> import torch
1502
+ >>> from ultralytics import YOLO
1503
+ >>> model = YOLO("yolo11n-obb.pt")
1504
+ >>> results = model("path/to/image.jpg")
1505
+ >>> for result in results:
1506
+ ... obb = result.obb
1507
+ ... if obb is not None:
1508
+ ... xyxy_boxes = obb.xyxy
1509
+ ... print(xyxy_boxes.shape) # (N, 4)
1510
+
1511
+ Notes:
1512
+ - This method approximates the OBB by its minimal enclosing rectangle.
1513
+ - The returned format is compatible with standard object detection metrics and visualization tools.
1514
+ - The property uses caching to improve performance for repeated access.
1515
+ """
1516
+ x = self.xyxyxyxy[..., 0]
1517
+ y = self.xyxyxyxy[..., 1]
1518
+ return (
1519
+ torch.stack([x.amin(1), y.amin(1), x.amax(1), y.amax(1)], -1)
1520
+ if isinstance(x, torch.Tensor)
1521
+ else np.stack([x.min(1), y.min(1), x.max(1), y.max(1)], -1)
1522
+ )