dgenerate-ultralytics-headless 8.3.253__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 (299) hide show
  1. dgenerate_ultralytics_headless-8.3.253.dist-info/METADATA +405 -0
  2. dgenerate_ultralytics_headless-8.3.253.dist-info/RECORD +299 -0
  3. dgenerate_ultralytics_headless-8.3.253.dist-info/WHEEL +5 -0
  4. dgenerate_ultralytics_headless-8.3.253.dist-info/entry_points.txt +3 -0
  5. dgenerate_ultralytics_headless-8.3.253.dist-info/licenses/LICENSE +661 -0
  6. dgenerate_ultralytics_headless-8.3.253.dist-info/top_level.txt +1 -0
  7. tests/__init__.py +23 -0
  8. tests/conftest.py +59 -0
  9. tests/test_cli.py +131 -0
  10. tests/test_cuda.py +216 -0
  11. tests/test_engine.py +157 -0
  12. tests/test_exports.py +309 -0
  13. tests/test_integrations.py +151 -0
  14. tests/test_python.py +777 -0
  15. tests/test_solutions.py +371 -0
  16. ultralytics/__init__.py +48 -0
  17. ultralytics/assets/bus.jpg +0 -0
  18. ultralytics/assets/zidane.jpg +0 -0
  19. ultralytics/cfg/__init__.py +1028 -0
  20. ultralytics/cfg/datasets/Argoverse.yaml +78 -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 +32 -0
  25. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  26. ultralytics/cfg/datasets/Objects365.yaml +447 -0
  27. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  28. ultralytics/cfg/datasets/TT100K.yaml +346 -0
  29. ultralytics/cfg/datasets/VOC.yaml +102 -0
  30. ultralytics/cfg/datasets/VisDrone.yaml +87 -0
  31. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  32. ultralytics/cfg/datasets/brain-tumor.yaml +22 -0
  33. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  34. ultralytics/cfg/datasets/coco-pose.yaml +64 -0
  35. ultralytics/cfg/datasets/coco.yaml +118 -0
  36. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  37. ultralytics/cfg/datasets/coco128.yaml +101 -0
  38. ultralytics/cfg/datasets/coco8-grayscale.yaml +103 -0
  39. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  40. ultralytics/cfg/datasets/coco8-pose.yaml +47 -0
  41. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  42. ultralytics/cfg/datasets/coco8.yaml +101 -0
  43. ultralytics/cfg/datasets/construction-ppe.yaml +32 -0
  44. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  45. ultralytics/cfg/datasets/dog-pose.yaml +52 -0
  46. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  47. ultralytics/cfg/datasets/dota8.yaml +35 -0
  48. ultralytics/cfg/datasets/hand-keypoints.yaml +50 -0
  49. ultralytics/cfg/datasets/kitti.yaml +27 -0
  50. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  51. ultralytics/cfg/datasets/medical-pills.yaml +21 -0
  52. ultralytics/cfg/datasets/open-images-v7.yaml +663 -0
  53. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  54. ultralytics/cfg/datasets/signature.yaml +21 -0
  55. ultralytics/cfg/datasets/tiger-pose.yaml +41 -0
  56. ultralytics/cfg/datasets/xView.yaml +155 -0
  57. ultralytics/cfg/default.yaml +130 -0
  58. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  59. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  60. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  61. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  62. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  63. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  64. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  65. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  66. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  67. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  68. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  69. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  70. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  71. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  72. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  73. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  74. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  75. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  76. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  77. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  78. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  79. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  80. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  81. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  82. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  83. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  84. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  85. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  86. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  87. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +48 -0
  88. ultralytics/cfg/models/v8/yoloe-v8.yaml +48 -0
  89. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  90. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  91. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  92. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  93. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  94. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  95. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  96. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  97. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  98. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  99. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  100. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  101. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  102. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  103. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  104. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  105. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  106. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  107. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  108. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  109. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  110. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  111. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  112. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  113. ultralytics/cfg/trackers/botsort.yaml +21 -0
  114. ultralytics/cfg/trackers/bytetrack.yaml +12 -0
  115. ultralytics/data/__init__.py +26 -0
  116. ultralytics/data/annotator.py +66 -0
  117. ultralytics/data/augment.py +2801 -0
  118. ultralytics/data/base.py +435 -0
  119. ultralytics/data/build.py +437 -0
  120. ultralytics/data/converter.py +855 -0
  121. ultralytics/data/dataset.py +834 -0
  122. ultralytics/data/loaders.py +704 -0
  123. ultralytics/data/scripts/download_weights.sh +18 -0
  124. ultralytics/data/scripts/get_coco.sh +61 -0
  125. ultralytics/data/scripts/get_coco128.sh +18 -0
  126. ultralytics/data/scripts/get_imagenet.sh +52 -0
  127. ultralytics/data/split.py +138 -0
  128. ultralytics/data/split_dota.py +344 -0
  129. ultralytics/data/utils.py +798 -0
  130. ultralytics/engine/__init__.py +1 -0
  131. ultralytics/engine/exporter.py +1580 -0
  132. ultralytics/engine/model.py +1125 -0
  133. ultralytics/engine/predictor.py +508 -0
  134. ultralytics/engine/results.py +1522 -0
  135. ultralytics/engine/trainer.py +977 -0
  136. ultralytics/engine/tuner.py +449 -0
  137. ultralytics/engine/validator.py +387 -0
  138. ultralytics/hub/__init__.py +166 -0
  139. ultralytics/hub/auth.py +151 -0
  140. ultralytics/hub/google/__init__.py +174 -0
  141. ultralytics/hub/session.py +422 -0
  142. ultralytics/hub/utils.py +162 -0
  143. ultralytics/models/__init__.py +9 -0
  144. ultralytics/models/fastsam/__init__.py +7 -0
  145. ultralytics/models/fastsam/model.py +79 -0
  146. ultralytics/models/fastsam/predict.py +169 -0
  147. ultralytics/models/fastsam/utils.py +23 -0
  148. ultralytics/models/fastsam/val.py +38 -0
  149. ultralytics/models/nas/__init__.py +7 -0
  150. ultralytics/models/nas/model.py +98 -0
  151. ultralytics/models/nas/predict.py +56 -0
  152. ultralytics/models/nas/val.py +38 -0
  153. ultralytics/models/rtdetr/__init__.py +7 -0
  154. ultralytics/models/rtdetr/model.py +63 -0
  155. ultralytics/models/rtdetr/predict.py +88 -0
  156. ultralytics/models/rtdetr/train.py +89 -0
  157. ultralytics/models/rtdetr/val.py +216 -0
  158. ultralytics/models/sam/__init__.py +25 -0
  159. ultralytics/models/sam/amg.py +275 -0
  160. ultralytics/models/sam/build.py +365 -0
  161. ultralytics/models/sam/build_sam3.py +377 -0
  162. ultralytics/models/sam/model.py +169 -0
  163. ultralytics/models/sam/modules/__init__.py +1 -0
  164. ultralytics/models/sam/modules/blocks.py +1067 -0
  165. ultralytics/models/sam/modules/decoders.py +495 -0
  166. ultralytics/models/sam/modules/encoders.py +794 -0
  167. ultralytics/models/sam/modules/memory_attention.py +298 -0
  168. ultralytics/models/sam/modules/sam.py +1160 -0
  169. ultralytics/models/sam/modules/tiny_encoder.py +979 -0
  170. ultralytics/models/sam/modules/transformer.py +344 -0
  171. ultralytics/models/sam/modules/utils.py +512 -0
  172. ultralytics/models/sam/predict.py +3940 -0
  173. ultralytics/models/sam/sam3/__init__.py +3 -0
  174. ultralytics/models/sam/sam3/decoder.py +546 -0
  175. ultralytics/models/sam/sam3/encoder.py +529 -0
  176. ultralytics/models/sam/sam3/geometry_encoders.py +415 -0
  177. ultralytics/models/sam/sam3/maskformer_segmentation.py +286 -0
  178. ultralytics/models/sam/sam3/model_misc.py +199 -0
  179. ultralytics/models/sam/sam3/necks.py +129 -0
  180. ultralytics/models/sam/sam3/sam3_image.py +339 -0
  181. ultralytics/models/sam/sam3/text_encoder_ve.py +307 -0
  182. ultralytics/models/sam/sam3/vitdet.py +547 -0
  183. ultralytics/models/sam/sam3/vl_combiner.py +160 -0
  184. ultralytics/models/utils/__init__.py +1 -0
  185. ultralytics/models/utils/loss.py +466 -0
  186. ultralytics/models/utils/ops.py +315 -0
  187. ultralytics/models/yolo/__init__.py +7 -0
  188. ultralytics/models/yolo/classify/__init__.py +7 -0
  189. ultralytics/models/yolo/classify/predict.py +90 -0
  190. ultralytics/models/yolo/classify/train.py +202 -0
  191. ultralytics/models/yolo/classify/val.py +216 -0
  192. ultralytics/models/yolo/detect/__init__.py +7 -0
  193. ultralytics/models/yolo/detect/predict.py +122 -0
  194. ultralytics/models/yolo/detect/train.py +227 -0
  195. ultralytics/models/yolo/detect/val.py +507 -0
  196. ultralytics/models/yolo/model.py +430 -0
  197. ultralytics/models/yolo/obb/__init__.py +7 -0
  198. ultralytics/models/yolo/obb/predict.py +56 -0
  199. ultralytics/models/yolo/obb/train.py +79 -0
  200. ultralytics/models/yolo/obb/val.py +302 -0
  201. ultralytics/models/yolo/pose/__init__.py +7 -0
  202. ultralytics/models/yolo/pose/predict.py +65 -0
  203. ultralytics/models/yolo/pose/train.py +110 -0
  204. ultralytics/models/yolo/pose/val.py +248 -0
  205. ultralytics/models/yolo/segment/__init__.py +7 -0
  206. ultralytics/models/yolo/segment/predict.py +109 -0
  207. ultralytics/models/yolo/segment/train.py +69 -0
  208. ultralytics/models/yolo/segment/val.py +307 -0
  209. ultralytics/models/yolo/world/__init__.py +5 -0
  210. ultralytics/models/yolo/world/train.py +173 -0
  211. ultralytics/models/yolo/world/train_world.py +178 -0
  212. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  213. ultralytics/models/yolo/yoloe/predict.py +162 -0
  214. ultralytics/models/yolo/yoloe/train.py +287 -0
  215. ultralytics/models/yolo/yoloe/train_seg.py +122 -0
  216. ultralytics/models/yolo/yoloe/val.py +206 -0
  217. ultralytics/nn/__init__.py +27 -0
  218. ultralytics/nn/autobackend.py +964 -0
  219. ultralytics/nn/modules/__init__.py +182 -0
  220. ultralytics/nn/modules/activation.py +54 -0
  221. ultralytics/nn/modules/block.py +1947 -0
  222. ultralytics/nn/modules/conv.py +669 -0
  223. ultralytics/nn/modules/head.py +1183 -0
  224. ultralytics/nn/modules/transformer.py +793 -0
  225. ultralytics/nn/modules/utils.py +159 -0
  226. ultralytics/nn/tasks.py +1768 -0
  227. ultralytics/nn/text_model.py +356 -0
  228. ultralytics/py.typed +1 -0
  229. ultralytics/solutions/__init__.py +41 -0
  230. ultralytics/solutions/ai_gym.py +108 -0
  231. ultralytics/solutions/analytics.py +264 -0
  232. ultralytics/solutions/config.py +107 -0
  233. ultralytics/solutions/distance_calculation.py +123 -0
  234. ultralytics/solutions/heatmap.py +125 -0
  235. ultralytics/solutions/instance_segmentation.py +86 -0
  236. ultralytics/solutions/object_blurrer.py +89 -0
  237. ultralytics/solutions/object_counter.py +190 -0
  238. ultralytics/solutions/object_cropper.py +87 -0
  239. ultralytics/solutions/parking_management.py +280 -0
  240. ultralytics/solutions/queue_management.py +93 -0
  241. ultralytics/solutions/region_counter.py +133 -0
  242. ultralytics/solutions/security_alarm.py +151 -0
  243. ultralytics/solutions/similarity_search.py +219 -0
  244. ultralytics/solutions/solutions.py +828 -0
  245. ultralytics/solutions/speed_estimation.py +114 -0
  246. ultralytics/solutions/streamlit_inference.py +260 -0
  247. ultralytics/solutions/templates/similarity-search.html +156 -0
  248. ultralytics/solutions/trackzone.py +88 -0
  249. ultralytics/solutions/vision_eye.py +67 -0
  250. ultralytics/trackers/__init__.py +7 -0
  251. ultralytics/trackers/basetrack.py +115 -0
  252. ultralytics/trackers/bot_sort.py +257 -0
  253. ultralytics/trackers/byte_tracker.py +469 -0
  254. ultralytics/trackers/track.py +116 -0
  255. ultralytics/trackers/utils/__init__.py +1 -0
  256. ultralytics/trackers/utils/gmc.py +339 -0
  257. ultralytics/trackers/utils/kalman_filter.py +482 -0
  258. ultralytics/trackers/utils/matching.py +154 -0
  259. ultralytics/utils/__init__.py +1450 -0
  260. ultralytics/utils/autobatch.py +118 -0
  261. ultralytics/utils/autodevice.py +205 -0
  262. ultralytics/utils/benchmarks.py +728 -0
  263. ultralytics/utils/callbacks/__init__.py +5 -0
  264. ultralytics/utils/callbacks/base.py +233 -0
  265. ultralytics/utils/callbacks/clearml.py +146 -0
  266. ultralytics/utils/callbacks/comet.py +625 -0
  267. ultralytics/utils/callbacks/dvc.py +197 -0
  268. ultralytics/utils/callbacks/hub.py +110 -0
  269. ultralytics/utils/callbacks/mlflow.py +134 -0
  270. ultralytics/utils/callbacks/neptune.py +126 -0
  271. ultralytics/utils/callbacks/platform.py +453 -0
  272. ultralytics/utils/callbacks/raytune.py +42 -0
  273. ultralytics/utils/callbacks/tensorboard.py +123 -0
  274. ultralytics/utils/callbacks/wb.py +188 -0
  275. ultralytics/utils/checks.py +1020 -0
  276. ultralytics/utils/cpu.py +85 -0
  277. ultralytics/utils/dist.py +123 -0
  278. ultralytics/utils/downloads.py +529 -0
  279. ultralytics/utils/errors.py +35 -0
  280. ultralytics/utils/events.py +113 -0
  281. ultralytics/utils/export/__init__.py +7 -0
  282. ultralytics/utils/export/engine.py +237 -0
  283. ultralytics/utils/export/imx.py +325 -0
  284. ultralytics/utils/export/tensorflow.py +231 -0
  285. ultralytics/utils/files.py +219 -0
  286. ultralytics/utils/git.py +137 -0
  287. ultralytics/utils/instance.py +484 -0
  288. ultralytics/utils/logger.py +506 -0
  289. ultralytics/utils/loss.py +849 -0
  290. ultralytics/utils/metrics.py +1563 -0
  291. ultralytics/utils/nms.py +337 -0
  292. ultralytics/utils/ops.py +664 -0
  293. ultralytics/utils/patches.py +201 -0
  294. ultralytics/utils/plotting.py +1047 -0
  295. ultralytics/utils/tal.py +404 -0
  296. ultralytics/utils/torch_utils.py +984 -0
  297. ultralytics/utils/tqdm.py +443 -0
  298. ultralytics/utils/triton.py +112 -0
  299. ultralytics/utils/tuner.py +168 -0
@@ -0,0 +1,86 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from typing import Any
4
+
5
+ from ultralytics.engine.results import Results
6
+ from ultralytics.solutions.solutions import BaseSolution, SolutionResults
7
+
8
+
9
+ class InstanceSegmentation(BaseSolution):
10
+ """A class to manage instance segmentation in images or video streams.
11
+
12
+ This class extends the BaseSolution class and provides functionality for performing instance segmentation, including
13
+ drawing segmented masks with bounding boxes and labels.
14
+
15
+ Attributes:
16
+ model (str): The segmentation model to use for inference.
17
+ line_width (int): Width of the bounding box and text lines.
18
+ names (dict[int, str]): Dictionary mapping class indices to class names.
19
+ clss (list[int]): List of detected class indices.
20
+ track_ids (list[int]): List of track IDs for detected instances.
21
+ masks (list[np.ndarray]): List of segmentation masks for detected instances.
22
+ show_conf (bool): Whether to display confidence scores.
23
+ show_labels (bool): Whether to display class labels.
24
+ show_boxes (bool): Whether to display bounding boxes.
25
+
26
+ Methods:
27
+ process: Process the input image to perform instance segmentation and annotate results.
28
+ extract_tracks: Extract tracks including bounding boxes, classes, and masks from model predictions.
29
+
30
+ Examples:
31
+ >>> segmenter = InstanceSegmentation()
32
+ >>> frame = cv2.imread("frame.jpg")
33
+ >>> results = segmenter.process(frame)
34
+ >>> print(f"Total segmented instances: {results.total_tracks}")
35
+ """
36
+
37
+ def __init__(self, **kwargs: Any) -> None:
38
+ """Initialize the InstanceSegmentation class for detecting and annotating segmented instances.
39
+
40
+ Args:
41
+ **kwargs (Any): Keyword arguments passed to the BaseSolution parent class including:
42
+ - model (str): Model name or path, defaults to "yolo11n-seg.pt".
43
+ """
44
+ kwargs["model"] = kwargs.get("model", "yolo11n-seg.pt")
45
+ super().__init__(**kwargs)
46
+
47
+ self.show_conf = self.CFG.get("show_conf", True)
48
+ self.show_labels = self.CFG.get("show_labels", True)
49
+ self.show_boxes = self.CFG.get("show_boxes", True)
50
+
51
+ def process(self, im0) -> SolutionResults:
52
+ """Perform instance segmentation on the input image and annotate the results.
53
+
54
+ Args:
55
+ im0 (np.ndarray): The input image for segmentation.
56
+
57
+ Returns:
58
+ (SolutionResults): Object containing the annotated image and total number of tracked instances.
59
+
60
+ Examples:
61
+ >>> segmenter = InstanceSegmentation()
62
+ >>> frame = cv2.imread("image.jpg")
63
+ >>> summary = segmenter.process(frame)
64
+ >>> print(summary)
65
+ """
66
+ self.extract_tracks(im0) # Extract tracks (bounding boxes, classes, and masks)
67
+ self.masks = getattr(self.tracks, "masks", None)
68
+
69
+ # Iterate over detected classes, track IDs, and segmentation masks
70
+ if self.masks is None:
71
+ self.LOGGER.warning("No masks detected! Ensure you're using a supported Ultralytics segmentation model.")
72
+ plot_im = im0
73
+ else:
74
+ results = Results(im0, path=None, names=self.names, boxes=self.track_data.data, masks=self.masks.data)
75
+ plot_im = results.plot(
76
+ line_width=self.line_width,
77
+ boxes=self.show_boxes,
78
+ conf=self.show_conf,
79
+ labels=self.show_labels,
80
+ color_mode="instance",
81
+ )
82
+
83
+ self.display_output(plot_im) # Display the annotated output using the base class function
84
+
85
+ # Return SolutionResults
86
+ return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
@@ -0,0 +1,89 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from typing import Any
4
+
5
+ import cv2
6
+
7
+ from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
8
+ from ultralytics.utils import LOGGER
9
+ from ultralytics.utils.plotting import colors
10
+
11
+
12
+ class ObjectBlurrer(BaseSolution):
13
+ """A class to manage the blurring of detected objects in a real-time video stream.
14
+
15
+ This class extends the BaseSolution class and provides functionality for blurring objects based on detected bounding
16
+ boxes. The blurred areas are updated directly in the input image, allowing for privacy preservation or other effects.
17
+
18
+ Attributes:
19
+ blur_ratio (int): The intensity of the blur effect applied to detected objects (higher values create more blur).
20
+ iou (float): Intersection over Union threshold for object detection.
21
+ conf (float): Confidence threshold for object detection.
22
+
23
+ Methods:
24
+ process: Apply a blurring effect to detected objects in the input image.
25
+ extract_tracks: Extract tracking information from detected objects.
26
+ display_output: Display the processed output image.
27
+
28
+ Examples:
29
+ >>> blurrer = ObjectBlurrer()
30
+ >>> frame = cv2.imread("frame.jpg")
31
+ >>> processed_results = blurrer.process(frame)
32
+ >>> print(f"Total blurred objects: {processed_results.total_tracks}")
33
+ """
34
+
35
+ def __init__(self, **kwargs: Any) -> None:
36
+ """Initialize the ObjectBlurrer class for applying a blur effect to objects detected in video streams or images.
37
+
38
+ Args:
39
+ **kwargs (Any): Keyword arguments passed to the parent class and for configuration including:
40
+ - blur_ratio (float): Intensity of the blur effect (0.1-1.0, default=0.5).
41
+ """
42
+ super().__init__(**kwargs)
43
+ blur_ratio = self.CFG["blur_ratio"]
44
+ if blur_ratio < 0.1:
45
+ LOGGER.warning("blur ratio cannot be less than 0.1, updating it to default value 0.5")
46
+ blur_ratio = 0.5
47
+ self.blur_ratio = int(blur_ratio * 100)
48
+
49
+ def process(self, im0) -> SolutionResults:
50
+ """Apply a blurring effect to detected objects in the input image.
51
+
52
+ This method extracts tracking information, applies blur to regions corresponding to detected objects, and
53
+ annotates the image with bounding boxes.
54
+
55
+ Args:
56
+ im0 (np.ndarray): The input image containing detected objects.
57
+
58
+ Returns:
59
+ (SolutionResults): Object containing the processed image and number of tracked objects.
60
+ - plot_im (np.ndarray): The annotated output image with blurred objects.
61
+ - total_tracks (int): The total number of tracked objects in the frame.
62
+
63
+ Examples:
64
+ >>> blurrer = ObjectBlurrer()
65
+ >>> frame = cv2.imread("image.jpg")
66
+ >>> results = blurrer.process(frame)
67
+ >>> print(f"Blurred {results.total_tracks} objects")
68
+ """
69
+ self.extract_tracks(im0) # Extract tracks
70
+ annotator = SolutionAnnotator(im0, self.line_width)
71
+
72
+ # Iterate over bounding boxes and classes
73
+ for box, cls, conf in zip(self.boxes, self.clss, self.confs):
74
+ # Crop and blur the detected object
75
+ blur_obj = cv2.blur(
76
+ im0[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])],
77
+ (self.blur_ratio, self.blur_ratio),
78
+ )
79
+ # Update the blurred area in the original image
80
+ im0[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])] = blur_obj
81
+ annotator.box_label(
82
+ box, label=self.adjust_box_label(cls, conf), color=colors(cls, True)
83
+ ) # Annotate bounding box
84
+
85
+ plot_im = annotator.result()
86
+ self.display_output(plot_im) # Display the output using the base class function
87
+
88
+ # Return a SolutionResults
89
+ return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
@@ -0,0 +1,190 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import defaultdict
6
+ from typing import Any
7
+
8
+ from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
9
+ from ultralytics.utils.plotting import colors
10
+
11
+
12
+ class ObjectCounter(BaseSolution):
13
+ """A class to manage the counting of objects in a real-time video stream based on their tracks.
14
+
15
+ This class extends the BaseSolution class and provides functionality for counting objects moving in and out of a
16
+ specified region in a video stream. It supports both polygonal and linear regions for counting.
17
+
18
+ Attributes:
19
+ in_count (int): Counter for objects moving inward.
20
+ out_count (int): Counter for objects moving outward.
21
+ counted_ids (list[int]): List of IDs of objects that have been counted.
22
+ classwise_count (dict[str, dict[str, int]]): Dictionary for counts, categorized by object class.
23
+ region_initialized (bool): Flag indicating whether the counting region has been initialized.
24
+ show_in (bool): Flag to control display of inward count.
25
+ show_out (bool): Flag to control display of outward count.
26
+ margin (int): Margin for background rectangle size to display counts properly.
27
+
28
+ Methods:
29
+ count_objects: Count objects within a polygonal or linear region based on their tracks.
30
+ display_counts: Display object counts on the frame.
31
+ process: Process input data and update counts.
32
+
33
+ Examples:
34
+ >>> counter = ObjectCounter()
35
+ >>> frame = cv2.imread("frame.jpg")
36
+ >>> results = counter.process(frame)
37
+ >>> print(f"Inward count: {counter.in_count}, Outward count: {counter.out_count}")
38
+ """
39
+
40
+ def __init__(self, **kwargs: Any) -> None:
41
+ """Initialize the ObjectCounter class for real-time object counting in video streams."""
42
+ super().__init__(**kwargs)
43
+
44
+ self.in_count = 0 # Counter for objects moving inward
45
+ self.out_count = 0 # Counter for objects moving outward
46
+ self.counted_ids = [] # List of IDs of objects that have been counted
47
+ self.classwise_count = defaultdict(lambda: {"IN": 0, "OUT": 0}) # Dictionary for counts, categorized by class
48
+ self.region_initialized = False # Flag indicating whether the region has been initialized
49
+
50
+ self.show_in = self.CFG["show_in"]
51
+ self.show_out = self.CFG["show_out"]
52
+ self.margin = self.line_width * 2 # Scales the background rectangle size to display counts properly
53
+
54
+ def count_objects(
55
+ self,
56
+ current_centroid: tuple[float, float],
57
+ track_id: int,
58
+ prev_position: tuple[float, float] | None,
59
+ cls: int,
60
+ ) -> None:
61
+ """Count objects within a polygonal or linear region based on their tracks.
62
+
63
+ Args:
64
+ current_centroid (tuple[float, float]): Current centroid coordinates (x, y) in the current frame.
65
+ track_id (int): Unique identifier for the tracked object.
66
+ prev_position (tuple[float, float], optional): Last frame position coordinates (x, y) of the track.
67
+ cls (int): Class index for classwise count updates.
68
+
69
+ Examples:
70
+ >>> counter = ObjectCounter()
71
+ >>> track_line = {1: [100, 200], 2: [110, 210], 3: [120, 220]}
72
+ >>> box = [130, 230, 150, 250]
73
+ >>> track_id_num = 1
74
+ >>> previous_position = (120, 220)
75
+ >>> class_to_count = 0 # In COCO model, class 0 = person
76
+ >>> counter.count_objects((140, 240), track_id_num, previous_position, class_to_count)
77
+ """
78
+ if prev_position is None or track_id in self.counted_ids:
79
+ return
80
+
81
+ if len(self.region) == 2: # Linear region (defined as a line segment)
82
+ if self.r_s.intersects(self.LineString([prev_position, current_centroid])):
83
+ # Determine orientation of the region (vertical or horizontal)
84
+ if abs(self.region[0][0] - self.region[1][0]) < abs(self.region[0][1] - self.region[1][1]):
85
+ # Vertical region: Compare x-coordinates to determine direction
86
+ if current_centroid[0] > prev_position[0]: # Moving right
87
+ self.in_count += 1
88
+ self.classwise_count[self.names[cls]]["IN"] += 1
89
+ else: # Moving left
90
+ self.out_count += 1
91
+ self.classwise_count[self.names[cls]]["OUT"] += 1
92
+ # Horizontal region: Compare y-coordinates to determine direction
93
+ elif current_centroid[1] > prev_position[1]: # Moving downward
94
+ self.in_count += 1
95
+ self.classwise_count[self.names[cls]]["IN"] += 1
96
+ else: # Moving upward
97
+ self.out_count += 1
98
+ self.classwise_count[self.names[cls]]["OUT"] += 1
99
+ self.counted_ids.append(track_id)
100
+
101
+ elif len(self.region) > 2: # Polygonal region
102
+ if self.r_s.contains(self.Point(current_centroid)):
103
+ # Determine motion direction for vertical or horizontal polygons
104
+ region_width = max(p[0] for p in self.region) - min(p[0] for p in self.region)
105
+ region_height = max(p[1] for p in self.region) - min(p[1] for p in self.region)
106
+
107
+ if (region_width < region_height and current_centroid[0] > prev_position[0]) or (
108
+ region_width >= region_height and current_centroid[1] > prev_position[1]
109
+ ): # Moving right or downward
110
+ self.in_count += 1
111
+ self.classwise_count[self.names[cls]]["IN"] += 1
112
+ else: # Moving left or upward
113
+ self.out_count += 1
114
+ self.classwise_count[self.names[cls]]["OUT"] += 1
115
+ self.counted_ids.append(track_id)
116
+
117
+ def display_counts(self, plot_im) -> None:
118
+ """Display object counts on the input image or frame.
119
+
120
+ Args:
121
+ plot_im (np.ndarray): The image or frame to display counts on.
122
+
123
+ Examples:
124
+ >>> counter = ObjectCounter()
125
+ >>> frame = cv2.imread("image.jpg")
126
+ >>> counter.display_counts(frame)
127
+ """
128
+ labels_dict = {
129
+ str.capitalize(key): f"{'IN ' + str(value['IN']) if self.show_in else ''} "
130
+ f"{'OUT ' + str(value['OUT']) if self.show_out else ''}".strip()
131
+ for key, value in self.classwise_count.items()
132
+ if (value["IN"] != 0 and self.show_in) or (value["OUT"] != 0 and self.show_out)
133
+ }
134
+ if labels_dict:
135
+ self.annotator.display_analytics(plot_im, labels_dict, (104, 31, 17), (255, 255, 255), self.margin)
136
+
137
+ def process(self, im0) -> SolutionResults:
138
+ """Process input data (frames or object tracks) and update object counts.
139
+
140
+ This method initializes the counting region, extracts tracks, draws bounding boxes and regions, updates object
141
+ counts, and displays the results on the input image.
142
+
143
+ Args:
144
+ im0 (np.ndarray): The input image or frame to be processed.
145
+
146
+ Returns:
147
+ (SolutionResults): Contains processed image `im0`, 'in_count' (int, count of objects entering the region),
148
+ 'out_count' (int, count of objects exiting the region), 'classwise_count' (dict, per-class object
149
+ count), and 'total_tracks' (int, total number of tracked objects).
150
+
151
+ Examples:
152
+ >>> counter = ObjectCounter()
153
+ >>> frame = cv2.imread("path/to/image.jpg")
154
+ >>> results = counter.process(frame)
155
+ """
156
+ if not self.region_initialized:
157
+ self.initialize_region()
158
+ self.region_initialized = True
159
+
160
+ self.extract_tracks(im0) # Extract tracks
161
+ self.annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
162
+
163
+ self.annotator.draw_region(
164
+ reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2
165
+ ) # Draw region
166
+
167
+ # Iterate over bounding boxes, track ids and classes index
168
+ for box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):
169
+ # Draw bounding box and counting region
170
+ self.annotator.box_label(box, label=self.adjust_box_label(cls, conf, track_id), color=colors(cls, True))
171
+ self.store_tracking_history(track_id, box) # Store track history
172
+
173
+ # Store previous position of track for object counting
174
+ prev_position = None
175
+ if len(self.track_history[track_id]) > 1:
176
+ prev_position = self.track_history[track_id][-2]
177
+ self.count_objects(self.track_history[track_id][-1], track_id, prev_position, cls) # object counting
178
+
179
+ plot_im = self.annotator.result()
180
+ self.display_counts(plot_im) # Display the counts on the frame
181
+ self.display_output(plot_im) # Display output with base class function
182
+
183
+ # Return SolutionResults
184
+ return SolutionResults(
185
+ plot_im=plot_im,
186
+ in_count=self.in_count,
187
+ out_count=self.out_count,
188
+ classwise_count=dict(self.classwise_count),
189
+ total_tracks=len(self.track_ids),
190
+ )
@@ -0,0 +1,87 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+
6
+ from ultralytics.solutions.solutions import BaseSolution, SolutionResults
7
+ from ultralytics.utils.plotting import save_one_box
8
+
9
+
10
+ class ObjectCropper(BaseSolution):
11
+ """A class to manage the cropping of detected objects in a real-time video stream or images.
12
+
13
+ This class extends the BaseSolution class and provides functionality for cropping objects based on detected bounding
14
+ boxes. The cropped images are saved to a specified directory for further analysis or usage.
15
+
16
+ Attributes:
17
+ crop_dir (str): Directory where cropped object images are stored.
18
+ crop_idx (int): Counter for the total number of cropped objects.
19
+ iou (float): IoU (Intersection over Union) threshold for non-maximum suppression.
20
+ conf (float): Confidence threshold for filtering detections.
21
+
22
+ Methods:
23
+ process: Crop detected objects from the input image and save them to the output directory.
24
+
25
+ Examples:
26
+ >>> cropper = ObjectCropper()
27
+ >>> frame = cv2.imread("frame.jpg")
28
+ >>> processed_results = cropper.process(frame)
29
+ >>> print(f"Total cropped objects: {cropper.crop_idx}")
30
+ """
31
+
32
+ def __init__(self, **kwargs: Any) -> None:
33
+ """Initialize the ObjectCropper class for cropping objects from detected bounding boxes.
34
+
35
+ Args:
36
+ **kwargs (Any): Keyword arguments passed to the parent class and used for configuration including:
37
+ - crop_dir (str): Path to the directory for saving cropped object images.
38
+ """
39
+ super().__init__(**kwargs)
40
+
41
+ self.crop_dir = self.CFG["crop_dir"] # Directory for storing cropped detections
42
+ Path(self.crop_dir).mkdir(parents=True, exist_ok=True)
43
+ if self.CFG["show"]:
44
+ self.LOGGER.warning(f"show=True is not supported for ObjectCropper; saving crops to '{self.crop_dir}'.")
45
+ self.CFG["show"] = False
46
+ self.crop_idx = 0 # Initialize counter for total cropped objects
47
+ self.iou = self.CFG["iou"]
48
+ self.conf = self.CFG["conf"]
49
+
50
+ def process(self, im0) -> SolutionResults:
51
+ """Crop detected objects from the input image and save them as separate images.
52
+
53
+ Args:
54
+ im0 (np.ndarray): The input image containing detected objects.
55
+
56
+ Returns:
57
+ (SolutionResults): A SolutionResults object containing the total number of cropped objects and processed
58
+ image.
59
+
60
+ Examples:
61
+ >>> cropper = ObjectCropper()
62
+ >>> frame = cv2.imread("image.jpg")
63
+ >>> results = cropper.process(frame)
64
+ >>> print(f"Total cropped objects: {results.total_crop_objects}")
65
+ """
66
+ with self.profilers[0]:
67
+ results = self.model.predict(
68
+ im0,
69
+ classes=self.classes,
70
+ conf=self.conf,
71
+ iou=self.iou,
72
+ device=self.CFG["device"],
73
+ verbose=False,
74
+ )[0]
75
+ self.clss = results.boxes.cls.tolist() # required for logging only.
76
+
77
+ for box in results.boxes:
78
+ self.crop_idx += 1
79
+ save_one_box(
80
+ box.xyxy,
81
+ im0,
82
+ file=Path(self.crop_dir) / f"crop_{self.crop_idx}.jpg",
83
+ BGR=True,
84
+ )
85
+
86
+ # Return SolutionResults
87
+ return SolutionResults(plot_im=im0, total_crop_objects=self.crop_idx)