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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. dgenerate_ultralytics_headless-8.3.134.dist-info/METADATA +400 -0
  2. dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
  3. dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
  4. dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
  5. dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
  6. dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
  7. tests/__init__.py +22 -0
  8. tests/conftest.py +83 -0
  9. tests/test_cli.py +138 -0
  10. tests/test_cuda.py +215 -0
  11. tests/test_engine.py +131 -0
  12. tests/test_exports.py +236 -0
  13. tests/test_integrations.py +154 -0
  14. tests/test_python.py +694 -0
  15. tests/test_solutions.py +187 -0
  16. ultralytics/__init__.py +30 -0
  17. ultralytics/assets/bus.jpg +0 -0
  18. ultralytics/assets/zidane.jpg +0 -0
  19. ultralytics/cfg/__init__.py +1023 -0
  20. ultralytics/cfg/datasets/Argoverse.yaml +77 -0
  21. ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
  22. ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
  23. ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
  24. ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
  25. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  26. ultralytics/cfg/datasets/Objects365.yaml +443 -0
  27. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  28. ultralytics/cfg/datasets/VOC.yaml +106 -0
  29. ultralytics/cfg/datasets/VisDrone.yaml +77 -0
  30. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  31. ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
  32. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  33. ultralytics/cfg/datasets/coco-pose.yaml +42 -0
  34. ultralytics/cfg/datasets/coco.yaml +118 -0
  35. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  36. ultralytics/cfg/datasets/coco128.yaml +101 -0
  37. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  38. ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
  39. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  40. ultralytics/cfg/datasets/coco8.yaml +101 -0
  41. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  42. ultralytics/cfg/datasets/dog-pose.yaml +24 -0
  43. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  44. ultralytics/cfg/datasets/dota8.yaml +35 -0
  45. ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
  46. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  47. ultralytics/cfg/datasets/medical-pills.yaml +22 -0
  48. ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
  49. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  50. ultralytics/cfg/datasets/signature.yaml +21 -0
  51. ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
  52. ultralytics/cfg/datasets/xView.yaml +155 -0
  53. ultralytics/cfg/default.yaml +127 -0
  54. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  55. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  56. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  57. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  58. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  59. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  60. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  61. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  62. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  63. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  64. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  65. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  66. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  67. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  68. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  69. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  70. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  71. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  72. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  73. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  74. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  75. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  76. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  77. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  78. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  79. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  80. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  81. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  82. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  83. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
  84. ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
  85. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  86. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  87. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  88. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  89. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  90. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  91. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  92. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  93. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  94. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  95. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  96. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  97. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  98. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  99. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  100. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  101. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  102. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  103. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  104. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  105. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  106. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  107. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  108. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  109. ultralytics/cfg/trackers/botsort.yaml +22 -0
  110. ultralytics/cfg/trackers/bytetrack.yaml +14 -0
  111. ultralytics/data/__init__.py +26 -0
  112. ultralytics/data/annotator.py +66 -0
  113. ultralytics/data/augment.py +2945 -0
  114. ultralytics/data/base.py +438 -0
  115. ultralytics/data/build.py +258 -0
  116. ultralytics/data/converter.py +754 -0
  117. ultralytics/data/dataset.py +834 -0
  118. ultralytics/data/loaders.py +676 -0
  119. ultralytics/data/scripts/download_weights.sh +18 -0
  120. ultralytics/data/scripts/get_coco.sh +61 -0
  121. ultralytics/data/scripts/get_coco128.sh +18 -0
  122. ultralytics/data/scripts/get_imagenet.sh +52 -0
  123. ultralytics/data/split.py +125 -0
  124. ultralytics/data/split_dota.py +325 -0
  125. ultralytics/data/utils.py +777 -0
  126. ultralytics/engine/__init__.py +1 -0
  127. ultralytics/engine/exporter.py +1519 -0
  128. ultralytics/engine/model.py +1156 -0
  129. ultralytics/engine/predictor.py +502 -0
  130. ultralytics/engine/results.py +1840 -0
  131. ultralytics/engine/trainer.py +853 -0
  132. ultralytics/engine/tuner.py +243 -0
  133. ultralytics/engine/validator.py +377 -0
  134. ultralytics/hub/__init__.py +168 -0
  135. ultralytics/hub/auth.py +137 -0
  136. ultralytics/hub/google/__init__.py +176 -0
  137. ultralytics/hub/session.py +446 -0
  138. ultralytics/hub/utils.py +248 -0
  139. ultralytics/models/__init__.py +9 -0
  140. ultralytics/models/fastsam/__init__.py +7 -0
  141. ultralytics/models/fastsam/model.py +61 -0
  142. ultralytics/models/fastsam/predict.py +181 -0
  143. ultralytics/models/fastsam/utils.py +24 -0
  144. ultralytics/models/fastsam/val.py +40 -0
  145. ultralytics/models/nas/__init__.py +7 -0
  146. ultralytics/models/nas/model.py +102 -0
  147. ultralytics/models/nas/predict.py +58 -0
  148. ultralytics/models/nas/val.py +39 -0
  149. ultralytics/models/rtdetr/__init__.py +7 -0
  150. ultralytics/models/rtdetr/model.py +63 -0
  151. ultralytics/models/rtdetr/predict.py +84 -0
  152. ultralytics/models/rtdetr/train.py +85 -0
  153. ultralytics/models/rtdetr/val.py +191 -0
  154. ultralytics/models/sam/__init__.py +6 -0
  155. ultralytics/models/sam/amg.py +260 -0
  156. ultralytics/models/sam/build.py +358 -0
  157. ultralytics/models/sam/model.py +170 -0
  158. ultralytics/models/sam/modules/__init__.py +1 -0
  159. ultralytics/models/sam/modules/blocks.py +1129 -0
  160. ultralytics/models/sam/modules/decoders.py +515 -0
  161. ultralytics/models/sam/modules/encoders.py +854 -0
  162. ultralytics/models/sam/modules/memory_attention.py +299 -0
  163. ultralytics/models/sam/modules/sam.py +1006 -0
  164. ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
  165. ultralytics/models/sam/modules/transformer.py +351 -0
  166. ultralytics/models/sam/modules/utils.py +394 -0
  167. ultralytics/models/sam/predict.py +1605 -0
  168. ultralytics/models/utils/__init__.py +1 -0
  169. ultralytics/models/utils/loss.py +455 -0
  170. ultralytics/models/utils/ops.py +268 -0
  171. ultralytics/models/yolo/__init__.py +7 -0
  172. ultralytics/models/yolo/classify/__init__.py +7 -0
  173. ultralytics/models/yolo/classify/predict.py +88 -0
  174. ultralytics/models/yolo/classify/train.py +233 -0
  175. ultralytics/models/yolo/classify/val.py +215 -0
  176. ultralytics/models/yolo/detect/__init__.py +7 -0
  177. ultralytics/models/yolo/detect/predict.py +124 -0
  178. ultralytics/models/yolo/detect/train.py +217 -0
  179. ultralytics/models/yolo/detect/val.py +451 -0
  180. ultralytics/models/yolo/model.py +354 -0
  181. ultralytics/models/yolo/obb/__init__.py +7 -0
  182. ultralytics/models/yolo/obb/predict.py +66 -0
  183. ultralytics/models/yolo/obb/train.py +81 -0
  184. ultralytics/models/yolo/obb/val.py +283 -0
  185. ultralytics/models/yolo/pose/__init__.py +7 -0
  186. ultralytics/models/yolo/pose/predict.py +79 -0
  187. ultralytics/models/yolo/pose/train.py +154 -0
  188. ultralytics/models/yolo/pose/val.py +394 -0
  189. ultralytics/models/yolo/segment/__init__.py +7 -0
  190. ultralytics/models/yolo/segment/predict.py +113 -0
  191. ultralytics/models/yolo/segment/train.py +123 -0
  192. ultralytics/models/yolo/segment/val.py +428 -0
  193. ultralytics/models/yolo/world/__init__.py +5 -0
  194. ultralytics/models/yolo/world/train.py +119 -0
  195. ultralytics/models/yolo/world/train_world.py +176 -0
  196. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  197. ultralytics/models/yolo/yoloe/predict.py +169 -0
  198. ultralytics/models/yolo/yoloe/train.py +298 -0
  199. ultralytics/models/yolo/yoloe/train_seg.py +124 -0
  200. ultralytics/models/yolo/yoloe/val.py +191 -0
  201. ultralytics/nn/__init__.py +29 -0
  202. ultralytics/nn/autobackend.py +842 -0
  203. ultralytics/nn/modules/__init__.py +182 -0
  204. ultralytics/nn/modules/activation.py +53 -0
  205. ultralytics/nn/modules/block.py +1966 -0
  206. ultralytics/nn/modules/conv.py +712 -0
  207. ultralytics/nn/modules/head.py +880 -0
  208. ultralytics/nn/modules/transformer.py +713 -0
  209. ultralytics/nn/modules/utils.py +164 -0
  210. ultralytics/nn/tasks.py +1627 -0
  211. ultralytics/nn/text_model.py +351 -0
  212. ultralytics/solutions/__init__.py +41 -0
  213. ultralytics/solutions/ai_gym.py +116 -0
  214. ultralytics/solutions/analytics.py +252 -0
  215. ultralytics/solutions/config.py +106 -0
  216. ultralytics/solutions/distance_calculation.py +124 -0
  217. ultralytics/solutions/heatmap.py +127 -0
  218. ultralytics/solutions/instance_segmentation.py +84 -0
  219. ultralytics/solutions/object_blurrer.py +90 -0
  220. ultralytics/solutions/object_counter.py +195 -0
  221. ultralytics/solutions/object_cropper.py +84 -0
  222. ultralytics/solutions/parking_management.py +273 -0
  223. ultralytics/solutions/queue_management.py +93 -0
  224. ultralytics/solutions/region_counter.py +120 -0
  225. ultralytics/solutions/security_alarm.py +154 -0
  226. ultralytics/solutions/similarity_search.py +172 -0
  227. ultralytics/solutions/solutions.py +724 -0
  228. ultralytics/solutions/speed_estimation.py +110 -0
  229. ultralytics/solutions/streamlit_inference.py +196 -0
  230. ultralytics/solutions/templates/similarity-search.html +160 -0
  231. ultralytics/solutions/trackzone.py +88 -0
  232. ultralytics/solutions/vision_eye.py +68 -0
  233. ultralytics/trackers/__init__.py +7 -0
  234. ultralytics/trackers/basetrack.py +124 -0
  235. ultralytics/trackers/bot_sort.py +260 -0
  236. ultralytics/trackers/byte_tracker.py +480 -0
  237. ultralytics/trackers/track.py +125 -0
  238. ultralytics/trackers/utils/__init__.py +1 -0
  239. ultralytics/trackers/utils/gmc.py +376 -0
  240. ultralytics/trackers/utils/kalman_filter.py +493 -0
  241. ultralytics/trackers/utils/matching.py +157 -0
  242. ultralytics/utils/__init__.py +1435 -0
  243. ultralytics/utils/autobatch.py +106 -0
  244. ultralytics/utils/autodevice.py +174 -0
  245. ultralytics/utils/benchmarks.py +695 -0
  246. ultralytics/utils/callbacks/__init__.py +5 -0
  247. ultralytics/utils/callbacks/base.py +234 -0
  248. ultralytics/utils/callbacks/clearml.py +153 -0
  249. ultralytics/utils/callbacks/comet.py +552 -0
  250. ultralytics/utils/callbacks/dvc.py +205 -0
  251. ultralytics/utils/callbacks/hub.py +108 -0
  252. ultralytics/utils/callbacks/mlflow.py +138 -0
  253. ultralytics/utils/callbacks/neptune.py +140 -0
  254. ultralytics/utils/callbacks/raytune.py +43 -0
  255. ultralytics/utils/callbacks/tensorboard.py +132 -0
  256. ultralytics/utils/callbacks/wb.py +185 -0
  257. ultralytics/utils/checks.py +897 -0
  258. ultralytics/utils/dist.py +119 -0
  259. ultralytics/utils/downloads.py +499 -0
  260. ultralytics/utils/errors.py +43 -0
  261. ultralytics/utils/export.py +219 -0
  262. ultralytics/utils/files.py +221 -0
  263. ultralytics/utils/instance.py +499 -0
  264. ultralytics/utils/loss.py +813 -0
  265. ultralytics/utils/metrics.py +1356 -0
  266. ultralytics/utils/ops.py +885 -0
  267. ultralytics/utils/patches.py +143 -0
  268. ultralytics/utils/plotting.py +1011 -0
  269. ultralytics/utils/tal.py +416 -0
  270. ultralytics/utils/torch_utils.py +990 -0
  271. ultralytics/utils/triton.py +116 -0
  272. ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,110 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from collections import deque
4
+ from math import sqrt
5
+
6
+ from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
7
+ from ultralytics.utils.plotting import colors
8
+
9
+
10
+ class SpeedEstimator(BaseSolution):
11
+ """
12
+ A class to estimate the speed of objects in a real-time video stream based on their tracks.
13
+
14
+ This class extends the BaseSolution class and provides functionality for estimating object speeds using
15
+ tracking data in video streams.
16
+
17
+ Attributes:
18
+ spd (Dict[int, float]): Dictionary storing speed data for tracked objects.
19
+ trk_hist (Dict[int, float]): Dictionary storing the object tracking data.
20
+ max_hist (int): maximum track history before computing speed
21
+ meters_per_pixel (float): Real-world meters represented by one pixel (e.g., 0.04 for 4m over 100px).
22
+ max_speed (int): Maximum allowed object speed; values above this will be capped at 120 km/h.
23
+
24
+ Methods:
25
+ initialize_region: Initializes the speed estimation region.
26
+ process: Processes input frames to estimate object speeds.
27
+ store_tracking_history: Stores the tracking history for an object.
28
+ extract_tracks: Extracts tracks from the current frame.
29
+ display_output: Displays the output with annotations.
30
+
31
+ Examples:
32
+ >>> estimator = SpeedEstimator()
33
+ >>> frame = cv2.imread("frame.jpg")
34
+ >>> results = estimator.process(frame)
35
+ >>> cv2.imshow("Speed Estimation", results.plot_im)
36
+ """
37
+
38
+ def __init__(self, **kwargs):
39
+ """
40
+ Initialize the SpeedEstimator object with speed estimation parameters and data structures.
41
+
42
+ Args:
43
+ **kwargs (Any): Additional keyword arguments passed to the parent class.
44
+ """
45
+ super().__init__(**kwargs)
46
+
47
+ self.fps = self.CFG["fps"] # assumed video FPS
48
+ self.frame_count = 0 # global frame count
49
+ self.trk_frame_ids = {} # Track ID → first frame index
50
+ self.spd = {} # Final speed per object (km/h), once locked
51
+ self.trk_hist = {} # Track ID → deque of (time, position)
52
+ self.locked_ids = set() # Track IDs whose speed has been finalized
53
+ self.max_hist = self.CFG["max_hist"] # Required frame history before computing speed
54
+ self.meter_per_pixel = self.CFG["meter_per_pixel"] # Scene scale, depends on camera details
55
+ self.max_speed = self.CFG["max_speed"] # max_speed adjustment
56
+
57
+ def process(self, im0):
58
+ """
59
+ Process an input frame to estimate object speeds based on tracking data.
60
+
61
+ Args:
62
+ im0 (np.ndarray): Input image for processing with shape (H, W, C) for RGB images.
63
+
64
+ Returns:
65
+ (SolutionResults): Contains processed image `plot_im` and `total_tracks` (number of tracked objects).
66
+
67
+ Examples:
68
+ >>> estimator = SpeedEstimator()
69
+ >>> image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
70
+ >>> results = estimator.process(image)
71
+ """
72
+ self.frame_count += 1
73
+ self.extract_tracks(im0)
74
+ annotator = SolutionAnnotator(im0, line_width=self.line_width)
75
+
76
+ for box, track_id, _, _ in zip(self.boxes, self.track_ids, self.clss, self.confs):
77
+ self.store_tracking_history(track_id, box)
78
+
79
+ if track_id not in self.trk_hist: # Initialize history if new track found
80
+ self.trk_hist[track_id] = deque(maxlen=self.max_hist)
81
+ self.trk_frame_ids[track_id] = self.frame_count
82
+
83
+ if track_id not in self.locked_ids: # Update history until speed is locked
84
+ trk_hist = self.trk_hist[track_id]
85
+ trk_hist.append(self.track_line[-1])
86
+
87
+ # Compute and lock speed once enough history is collected
88
+ if len(trk_hist) == self.max_hist:
89
+ p0, p1 = trk_hist[0], trk_hist[-1] # First and last points of track
90
+ dt = (self.frame_count - self.trk_frame_ids[track_id]) / self.fps # Time in seconds
91
+ if dt > 0:
92
+ dx, dy = p1[0] - p0[0], p1[1] - p0[1] # pixel displacement
93
+ pixel_distance = sqrt(dx * dx + dy * dy) # get pixel distance
94
+ meters = pixel_distance * self.meter_per_pixel # convert to meters
95
+ self.spd[track_id] = int(
96
+ min((meters / dt) * 3.6, self.max_speed)
97
+ ) # convert to km/h and store final speed
98
+ self.locked_ids.add(track_id) # prevent further updates
99
+ self.trk_hist.pop(track_id, None) # free memory
100
+ self.trk_frame_ids.pop(track_id, None) # optional: remove frame start too
101
+
102
+ if track_id in self.spd:
103
+ speed_label = f"{self.spd[track_id]} km/h"
104
+ annotator.box_label(box, label=speed_label, color=colors(track_id, True)) # Draw bounding box
105
+
106
+ plot_im = annotator.result()
107
+ self.display_output(plot_im) # Display output with base class function
108
+
109
+ # Return results with processed image and tracking summary
110
+ return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
@@ -0,0 +1,196 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ import io
4
+ from typing import Any
5
+
6
+ import cv2
7
+
8
+ from ultralytics import YOLO
9
+ from ultralytics.utils import LOGGER
10
+ from ultralytics.utils.checks import check_requirements
11
+ from ultralytics.utils.downloads import GITHUB_ASSETS_STEMS
12
+
13
+
14
+ class Inference:
15
+ """
16
+ A class to perform object detection, image classification, image segmentation and pose estimation inference.
17
+
18
+ This class provides functionalities for loading models, configuring settings, uploading video files, and performing
19
+ real-time inference using Streamlit and Ultralytics YOLO models.
20
+
21
+ Attributes:
22
+ st (module): Streamlit module for UI creation.
23
+ temp_dict (dict): Temporary dictionary to store the model path and other configuration.
24
+ model_path (str): Path to the loaded model.
25
+ model (YOLO): The YOLO model instance.
26
+ source (str): Selected video source (webcam or video file).
27
+ enable_trk (str): Enable tracking option ("Yes" or "No").
28
+ conf (float): Confidence threshold for detection.
29
+ iou (float): IoU threshold for non-maximum suppression.
30
+ org_frame (Any): Container for the original frame to be displayed.
31
+ ann_frame (Any): Container for the annotated frame to be displayed.
32
+ vid_file_name (str | int): Name of the uploaded video file or webcam index.
33
+ selected_ind (List[int]): List of selected class indices for detection.
34
+
35
+ Methods:
36
+ web_ui: Sets up the Streamlit web interface with custom HTML elements.
37
+ sidebar: Configures the Streamlit sidebar for model and inference settings.
38
+ source_upload: Handles video file uploads through the Streamlit interface.
39
+ configure: Configures the model and loads selected classes for inference.
40
+ inference: Performs real-time object detection inference.
41
+
42
+ Examples:
43
+ >>> inf = Inference(model="path/to/model.pt") # Model is an optional argument
44
+ >>> inf.inference()
45
+ """
46
+
47
+ def __init__(self, **kwargs: Any):
48
+ """
49
+ Initialize the Inference class, checking Streamlit requirements and setting up the model path.
50
+
51
+ Args:
52
+ **kwargs (Any): Additional keyword arguments for model configuration.
53
+ """
54
+ check_requirements("streamlit>=1.29.0") # scope imports for faster ultralytics package load speeds
55
+ import streamlit as st
56
+
57
+ self.st = st # Reference to the Streamlit module
58
+ self.source = None # Video source selection (webcam or video file)
59
+ self.enable_trk = False # Flag to toggle object tracking
60
+ self.conf = 0.25 # Confidence threshold for detection
61
+ self.iou = 0.45 # Intersection-over-Union (IoU) threshold for non-maximum suppression
62
+ self.org_frame = None # Container for the original frame display
63
+ self.ann_frame = None # Container for the annotated frame display
64
+ self.vid_file_name = None # Video file name or webcam index
65
+ self.selected_ind = [] # List of selected class indices for detection
66
+ self.model = None # YOLO model instance
67
+
68
+ self.temp_dict = {"model": None, **kwargs}
69
+ self.model_path = None # Model file path
70
+ if self.temp_dict["model"] is not None:
71
+ self.model_path = self.temp_dict["model"]
72
+
73
+ LOGGER.info(f"Ultralytics Solutions: ✅ {self.temp_dict}")
74
+
75
+ def web_ui(self):
76
+ """Sets up the Streamlit web interface with custom HTML elements."""
77
+ menu_style_cfg = """<style>MainMenu {visibility: hidden;}</style>""" # Hide main menu style
78
+
79
+ # Main title of streamlit application
80
+ main_title_cfg = """<div><h1 style="color:#FF64DA; text-align:center; font-size:40px; margin-top:-50px;
81
+ font-family: 'Archivo', sans-serif; margin-bottom:20px;">Ultralytics YOLO Streamlit Application</h1></div>"""
82
+
83
+ # Subtitle of streamlit application
84
+ sub_title_cfg = """<div><h4 style="color:#042AFF; text-align:center; font-family: 'Archivo', sans-serif;
85
+ margin-top:-15px; margin-bottom:50px;">Experience real-time object detection on your webcam with the power
86
+ of Ultralytics YOLO! 🚀</h4></div>"""
87
+
88
+ # Set html page configuration and append custom HTML
89
+ self.st.set_page_config(page_title="Ultralytics Streamlit App", layout="wide")
90
+ self.st.markdown(menu_style_cfg, unsafe_allow_html=True)
91
+ self.st.markdown(main_title_cfg, unsafe_allow_html=True)
92
+ self.st.markdown(sub_title_cfg, unsafe_allow_html=True)
93
+
94
+ def sidebar(self):
95
+ """Configure the Streamlit sidebar for model and inference settings."""
96
+ with self.st.sidebar: # Add Ultralytics LOGO
97
+ logo = "https://raw.githubusercontent.com/ultralytics/assets/main/logo/Ultralytics_Logotype_Original.svg"
98
+ self.st.image(logo, width=250)
99
+
100
+ self.st.sidebar.title("User Configuration") # Add elements to vertical setting menu
101
+ self.source = self.st.sidebar.selectbox(
102
+ "Video",
103
+ ("webcam", "video"),
104
+ ) # Add source selection dropdown
105
+ self.enable_trk = self.st.sidebar.radio("Enable Tracking", ("Yes", "No")) # Enable object tracking
106
+ self.conf = float(
107
+ self.st.sidebar.slider("Confidence Threshold", 0.0, 1.0, self.conf, 0.01)
108
+ ) # Slider for confidence
109
+ self.iou = float(self.st.sidebar.slider("IoU Threshold", 0.0, 1.0, self.iou, 0.01)) # Slider for NMS threshold
110
+
111
+ col1, col2 = self.st.columns(2) # Create two columns for displaying frames
112
+ self.org_frame = col1.empty() # Container for original frame
113
+ self.ann_frame = col2.empty() # Container for annotated frame
114
+
115
+ def source_upload(self):
116
+ """Handle video file uploads through the Streamlit interface."""
117
+ self.vid_file_name = ""
118
+ if self.source == "video":
119
+ vid_file = self.st.sidebar.file_uploader("Upload Video File", type=["mp4", "mov", "avi", "mkv"])
120
+ if vid_file is not None:
121
+ g = io.BytesIO(vid_file.read()) # BytesIO Object
122
+ with open("ultralytics.mp4", "wb") as out: # Open temporary file as bytes
123
+ out.write(g.read()) # Read bytes into file
124
+ self.vid_file_name = "ultralytics.mp4"
125
+ elif self.source == "webcam":
126
+ self.vid_file_name = 0 # Use webcam index 0
127
+
128
+ def configure(self):
129
+ """Configure the model and load selected classes for inference."""
130
+ # Add dropdown menu for model selection
131
+ available_models = [x.replace("yolo", "YOLO") for x in GITHUB_ASSETS_STEMS if x.startswith("yolo11")]
132
+ if self.model_path: # If user provided the custom model, insert model without suffix as *.pt is added later
133
+ available_models.insert(0, self.model_path.split(".pt")[0])
134
+ selected_model = self.st.sidebar.selectbox("Model", available_models)
135
+
136
+ with self.st.spinner("Model is downloading..."):
137
+ self.model = YOLO(f"{selected_model.lower()}.pt") # Load the YOLO model
138
+ class_names = list(self.model.names.values()) # Convert dictionary to list of class names
139
+ self.st.success("Model loaded successfully!")
140
+
141
+ # Multiselect box with class names and get indices of selected classes
142
+ selected_classes = self.st.sidebar.multiselect("Classes", class_names, default=class_names[:3])
143
+ self.selected_ind = [class_names.index(option) for option in selected_classes]
144
+
145
+ if not isinstance(self.selected_ind, list): # Ensure selected_options is a list
146
+ self.selected_ind = list(self.selected_ind)
147
+
148
+ def inference(self):
149
+ """Perform real-time object detection inference on video or webcam feed."""
150
+ self.web_ui() # Initialize the web interface
151
+ self.sidebar() # Create the sidebar
152
+ self.source_upload() # Upload the video source
153
+ self.configure() # Configure the app
154
+
155
+ if self.st.sidebar.button("Start"):
156
+ stop_button = self.st.button("Stop") # Button to stop the inference
157
+ cap = cv2.VideoCapture(self.vid_file_name) # Capture the video
158
+ if not cap.isOpened():
159
+ self.st.error("Could not open webcam or video source.")
160
+ return
161
+
162
+ while cap.isOpened():
163
+ success, frame = cap.read()
164
+ if not success:
165
+ self.st.warning("Failed to read frame from webcam. Please verify the webcam is connected properly.")
166
+ break
167
+
168
+ # Process frame with model
169
+ if self.enable_trk == "Yes":
170
+ results = self.model.track(
171
+ frame, conf=self.conf, iou=self.iou, classes=self.selected_ind, persist=True
172
+ )
173
+ else:
174
+ results = self.model(frame, conf=self.conf, iou=self.iou, classes=self.selected_ind)
175
+
176
+ annotated_frame = results[0].plot() # Add annotations on frame
177
+
178
+ if stop_button:
179
+ cap.release() # Release the capture
180
+ self.st.stop() # Stop streamlit app
181
+
182
+ self.org_frame.image(frame, channels="BGR") # Display original frame
183
+ self.ann_frame.image(annotated_frame, channels="BGR") # Display processed frame
184
+
185
+ cap.release() # Release the capture
186
+ cv2.destroyAllWindows() # Destroy all OpenCV windows
187
+
188
+
189
+ if __name__ == "__main__":
190
+ import sys # Import the sys module for accessing command-line arguments
191
+
192
+ # Check if a model name is provided as a command-line argument
193
+ args = len(sys.argv)
194
+ model = sys.argv[1] if args > 1 else None # Assign first argument as the model name if provided
195
+ # Create an instance of the Inference class and run inference
196
+ Inference(model=model).inference()
@@ -0,0 +1,160 @@
1
+ <!-- Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license -->
2
+
3
+ <!--Similarity search webpage-->
4
+ <!doctype html>
5
+ <html lang="en">
6
+ <head>
7
+ <meta charset="UTF-8" />
8
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
9
+ <title>Semantic Image Search</title>
10
+ <link
11
+ href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap"
12
+ rel="stylesheet"
13
+ />
14
+ <style>
15
+ body {
16
+ background: linear-gradient(135deg, #f0f4ff, #f9fbff);
17
+ font-family: "Inter", sans-serif;
18
+ color: #111e68;
19
+ padding: 2rem;
20
+ margin: 0;
21
+ min-height: 100vh;
22
+ }
23
+
24
+ h1 {
25
+ text-align: center;
26
+ margin-bottom: 2rem;
27
+ font-size: 2.5rem;
28
+ font-weight: 600;
29
+ }
30
+
31
+ form {
32
+ display: flex;
33
+ flex-wrap: wrap;
34
+ justify-content: center;
35
+ align-items: center;
36
+ gap: 1rem;
37
+ margin-bottom: 3rem;
38
+ animation: fadeIn 1s ease-in-out;
39
+ }
40
+
41
+ input[type="text"] {
42
+ width: 300px;
43
+ padding: 0.75rem 1rem;
44
+ font-size: 1rem;
45
+ border-radius: 10px;
46
+ border: 1px solid #ccc;
47
+ box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
48
+ transition: box-shadow 0.3s ease;
49
+ }
50
+
51
+ input[type="text"]:focus {
52
+ outline: none;
53
+ box-shadow: 0 0 0 3px rgba(17, 30, 104, 0.2);
54
+ }
55
+
56
+ button {
57
+ background-color: #111e68;
58
+ color: white;
59
+ font-weight: 600;
60
+ font-size: 1rem;
61
+ padding: 0.75rem 1.5rem;
62
+ border-radius: 10px;
63
+ border: none;
64
+ cursor: pointer;
65
+ transition:
66
+ background-color 0.3s ease,
67
+ transform 0.2s ease;
68
+ }
69
+
70
+ button:hover {
71
+ background-color: #1f2e9f;
72
+ transform: translateY(-2px);
73
+ }
74
+
75
+ .grid {
76
+ display: grid;
77
+ grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
78
+ gap: 1.5rem;
79
+ max-width: 1600px;
80
+ margin: auto;
81
+ animation: fadeInUp 1s ease-in-out;
82
+ }
83
+
84
+ .card {
85
+ background: white;
86
+ border-radius: 16px;
87
+ overflow: hidden;
88
+ box-shadow: 0 6px 14px rgba(0, 0, 0, 0.08);
89
+ transition:
90
+ transform 0.3s ease,
91
+ box-shadow 0.3s ease;
92
+ }
93
+
94
+ .card:hover {
95
+ transform: translateY(-6px);
96
+ box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
97
+ }
98
+
99
+ .card img {
100
+ width: 100%;
101
+ height: 100%;
102
+ object-fit: cover;
103
+ display: block;
104
+ }
105
+
106
+ @keyframes fadeIn {
107
+ 0% {
108
+ opacity: 0;
109
+ transform: scale(0.95);
110
+ }
111
+ 100% {
112
+ opacity: 1;
113
+ transform: scale(1);
114
+ }
115
+ }
116
+
117
+ @keyframes fadeInUp {
118
+ 0% {
119
+ opacity: 0;
120
+ transform: translateY(20px);
121
+ }
122
+ 100% {
123
+ opacity: 1;
124
+ transform: translateY(0);
125
+ }
126
+ }
127
+ </style>
128
+ </head>
129
+ <body>
130
+ <div style="text-align: center; margin-bottom: 1rem">
131
+ <img
132
+ src="https://raw.githubusercontent.com/ultralytics/assets/main/logo/favicon.png"
133
+ alt="Ultralytics Logo"
134
+ style="height: 40px"
135
+ />
136
+ </div>
137
+ <h1>Semantic Image Search with AI</h1>
138
+
139
+ <!-- Search box -->
140
+ <form method="POST">
141
+ <input
142
+ type="text"
143
+ name="query"
144
+ placeholder="Describe the scene (e.g., man walking)"
145
+ value="{{ request.form['query'] }}"
146
+ required
147
+ />
148
+ <button type="submit">Search</button>
149
+ </form>
150
+
151
+ <!-- Search results grid -->
152
+ <div class="grid">
153
+ {% for img in results %}
154
+ <div class="card">
155
+ <img src="{{ url_for('static', filename=img) }}" alt="Result Image" />
156
+ </div>
157
+ {% endfor %}
158
+ </div>
159
+ </body>
160
+ </html>
@@ -0,0 +1,88 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ import cv2
4
+ import numpy as np
5
+
6
+ from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
7
+ from ultralytics.utils.plotting import colors
8
+
9
+
10
+ class TrackZone(BaseSolution):
11
+ """
12
+ A class to manage region-based object tracking in a video stream.
13
+
14
+ This class extends the BaseSolution class and provides functionality for tracking objects within a specific region
15
+ defined by a polygonal area. Objects outside the region are excluded from tracking.
16
+
17
+ Attributes:
18
+ region (np.ndarray): The polygonal region for tracking, represented as a convex hull of points.
19
+ line_width (int): Width of the lines used for drawing bounding boxes and region boundaries.
20
+ names (List[str]): List of class names that the model can detect.
21
+ boxes (List[np.ndarray]): Bounding boxes of tracked objects.
22
+ track_ids (List[int]): Unique identifiers for each tracked object.
23
+ clss (List[int]): Class indices of tracked objects.
24
+
25
+ Methods:
26
+ process: Processes each frame of the video, applying region-based tracking.
27
+ extract_tracks: Extracts tracking information from the input frame.
28
+ display_output: Displays the processed output.
29
+
30
+ Examples:
31
+ >>> tracker = TrackZone()
32
+ >>> frame = cv2.imread("frame.jpg")
33
+ >>> results = tracker.process(frame)
34
+ >>> cv2.imshow("Tracked Frame", results.plot_im)
35
+ """
36
+
37
+ def __init__(self, **kwargs):
38
+ """
39
+ Initialize the TrackZone class for tracking objects within a defined region in video streams.
40
+
41
+ Args:
42
+ **kwargs (Any): Additional keyword arguments passed to the parent class.
43
+ """
44
+ super().__init__(**kwargs)
45
+ default_region = [(75, 75), (565, 75), (565, 285), (75, 285)]
46
+ self.region = cv2.convexHull(np.array(self.region or default_region, dtype=np.int32))
47
+
48
+ def process(self, im0):
49
+ """
50
+ Process the input frame to track objects within a defined region.
51
+
52
+ This method initializes the annotator, creates a mask for the specified region, extracts tracks
53
+ only from the masked area, and updates tracking information. Objects outside the region are ignored.
54
+
55
+ Args:
56
+ im0 (np.ndarray): The input image or frame to be processed.
57
+
58
+ Returns:
59
+ (SolutionResults): Contains processed image `plot_im` and `total_tracks` (int) representing the
60
+ total number of tracked objects within the defined region.
61
+
62
+ Examples:
63
+ >>> tracker = TrackZone()
64
+ >>> frame = cv2.imread("path/to/image.jpg")
65
+ >>> results = tracker.process(frame)
66
+ """
67
+ annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
68
+
69
+ # Create a mask for the region and extract tracks from the masked image
70
+ mask = np.zeros_like(im0[:, :, 0])
71
+ mask = cv2.fillPoly(mask, [self.region], 255)
72
+ masked_frame = cv2.bitwise_and(im0, im0, mask=mask)
73
+ self.extract_tracks(masked_frame)
74
+
75
+ # Draw the region boundary
76
+ cv2.polylines(im0, [self.region], isClosed=True, color=(255, 255, 255), thickness=self.line_width * 2)
77
+
78
+ # Iterate over boxes, track ids, classes indexes list and draw bounding boxes
79
+ for box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):
80
+ annotator.box_label(
81
+ box, label=self.adjust_box_label(cls, conf, track_id=track_id), color=colors(track_id, True)
82
+ )
83
+
84
+ plot_im = annotator.result()
85
+ self.display_output(plot_im) # display output with base class function
86
+
87
+ # Return a SolutionResults
88
+ return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
@@ -0,0 +1,68 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
4
+ from ultralytics.utils.plotting import colors
5
+
6
+
7
+ class VisionEye(BaseSolution):
8
+ """
9
+ A class to manage object detection and vision mapping in images or video streams.
10
+
11
+ This class extends the BaseSolution class and provides functionality for detecting objects,
12
+ mapping vision points, and annotating results with bounding boxes and labels.
13
+
14
+ Attributes:
15
+ vision_point (Tuple[int, int]): Coordinates (x, y) where vision will view objects and draw tracks.
16
+
17
+ Methods:
18
+ process: Process the input image to detect objects, annotate them, and apply vision mapping.
19
+
20
+ Examples:
21
+ >>> vision_eye = VisionEye()
22
+ >>> frame = cv2.imread("frame.jpg")
23
+ >>> results = vision_eye.process(frame)
24
+ >>> print(f"Total detected instances: {results.total_tracks}")
25
+ """
26
+
27
+ def __init__(self, **kwargs):
28
+ """
29
+ Initialize the VisionEye class for detecting objects and applying vision mapping.
30
+
31
+ Args:
32
+ **kwargs (Any): Keyword arguments passed to the parent class and for configuring vision_point.
33
+ """
34
+ super().__init__(**kwargs)
35
+ # Set the vision point where the system will view objects and draw tracks
36
+ self.vision_point = self.CFG["vision_point"]
37
+
38
+ def process(self, im0):
39
+ """
40
+ Perform object detection, vision mapping, and annotation on the input image.
41
+
42
+ Args:
43
+ im0 (numpy.ndarray): The input image for detection and annotation.
44
+
45
+ Returns:
46
+ (SolutionResults): Object containing the annotated image and tracking statistics.
47
+ - plot_im: Annotated output image with bounding boxes and vision mapping
48
+ - total_tracks: Number of tracked objects in the frame
49
+
50
+ Examples:
51
+ >>> vision_eye = VisionEye()
52
+ >>> frame = cv2.imread("image.jpg")
53
+ >>> results = vision_eye.process(frame)
54
+ >>> print(f"Detected {results.total_tracks} objects")
55
+ """
56
+ self.extract_tracks(im0) # Extract tracks (bounding boxes, classes, and masks)
57
+ annotator = SolutionAnnotator(im0, self.line_width)
58
+
59
+ for cls, t_id, box, conf in zip(self.clss, self.track_ids, self.boxes, self.confs):
60
+ # Annotate the image with bounding boxes, labels, and vision mapping
61
+ annotator.box_label(box, label=self.adjust_box_label(cls, conf, t_id), color=colors(int(t_id), True))
62
+ annotator.visioneye(box, self.vision_point)
63
+
64
+ plot_im = annotator.result()
65
+ self.display_output(plot_im) # Display the annotated output using the base class function
66
+
67
+ # Return a SolutionResults object with the annotated image and tracking statistics
68
+ return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
@@ -0,0 +1,7 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from .bot_sort import BOTSORT
4
+ from .byte_tracker import BYTETracker
5
+ from .track import register_tracker
6
+
7
+ __all__ = "register_tracker", "BOTSORT", "BYTETracker" # allow simpler import