ultralytics 8.3.87__py3-none-any.whl → 8.3.89__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 (69) hide show
  1. tests/test_solutions.py +34 -45
  2. ultralytics/__init__.py +1 -1
  3. ultralytics/cfg/__init__.py +46 -39
  4. ultralytics/data/augment.py +2 -2
  5. ultralytics/data/base.py +7 -9
  6. ultralytics/data/converter.py +30 -29
  7. ultralytics/data/utils.py +20 -28
  8. ultralytics/engine/model.py +2 -2
  9. ultralytics/engine/tuner.py +11 -21
  10. ultralytics/hub/__init__.py +13 -17
  11. ultralytics/models/fastsam/model.py +4 -7
  12. ultralytics/models/nas/model.py +8 -14
  13. ultralytics/models/nas/predict.py +7 -9
  14. ultralytics/models/nas/val.py +7 -9
  15. ultralytics/models/rtdetr/predict.py +6 -9
  16. ultralytics/models/rtdetr/train.py +5 -8
  17. ultralytics/models/rtdetr/val.py +5 -8
  18. ultralytics/models/yolo/classify/predict.py +6 -9
  19. ultralytics/models/yolo/classify/train.py +5 -8
  20. ultralytics/models/yolo/classify/val.py +5 -8
  21. ultralytics/models/yolo/detect/predict.py +6 -9
  22. ultralytics/models/yolo/detect/train.py +5 -8
  23. ultralytics/models/yolo/detect/val.py +5 -8
  24. ultralytics/models/yolo/obb/predict.py +6 -9
  25. ultralytics/models/yolo/obb/train.py +5 -8
  26. ultralytics/models/yolo/obb/val.py +10 -15
  27. ultralytics/models/yolo/pose/predict.py +6 -9
  28. ultralytics/models/yolo/pose/train.py +5 -8
  29. ultralytics/models/yolo/pose/val.py +12 -17
  30. ultralytics/models/yolo/segment/predict.py +6 -9
  31. ultralytics/models/yolo/segment/train.py +5 -8
  32. ultralytics/models/yolo/segment/val.py +10 -15
  33. ultralytics/models/yolo/world/train.py +5 -8
  34. ultralytics/models/yolo/world/train_world.py +21 -25
  35. ultralytics/nn/modules/__init__.py +9 -12
  36. ultralytics/nn/tasks.py +7 -12
  37. ultralytics/solutions/__init__.py +14 -6
  38. ultralytics/solutions/ai_gym.py +39 -28
  39. ultralytics/solutions/analytics.py +22 -18
  40. ultralytics/solutions/distance_calculation.py +25 -25
  41. ultralytics/solutions/heatmap.py +40 -38
  42. ultralytics/solutions/instance_segmentation.py +69 -0
  43. ultralytics/solutions/object_blurrer.py +89 -0
  44. ultralytics/solutions/object_counter.py +35 -33
  45. ultralytics/solutions/object_cropper.py +84 -0
  46. ultralytics/solutions/parking_management.py +21 -9
  47. ultralytics/solutions/queue_management.py +20 -39
  48. ultralytics/solutions/region_counter.py +54 -51
  49. ultralytics/solutions/security_alarm.py +40 -30
  50. ultralytics/solutions/solutions.py +594 -16
  51. ultralytics/solutions/speed_estimation.py +34 -31
  52. ultralytics/solutions/streamlit_inference.py +34 -28
  53. ultralytics/solutions/trackzone.py +29 -18
  54. ultralytics/solutions/vision_eye.py +69 -0
  55. ultralytics/trackers/utils/kalman_filter.py +23 -23
  56. ultralytics/utils/__init__.py +5 -8
  57. ultralytics/utils/checks.py +25 -35
  58. ultralytics/utils/downloads.py +25 -48
  59. ultralytics/utils/instance.py +9 -11
  60. ultralytics/utils/ops.py +5 -9
  61. ultralytics/utils/plotting.py +8 -428
  62. ultralytics/utils/torch_utils.py +23 -33
  63. ultralytics/utils/tuner.py +5 -9
  64. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/METADATA +2 -2
  65. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/RECORD +69 -65
  66. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/LICENSE +0 -0
  67. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/WHEEL +0 -0
  68. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/entry_points.txt +0 -0
  69. {ultralytics-8.3.87.dist-info → ultralytics-8.3.89.dist-info}/top_level.txt +0 -0
@@ -3,10 +3,12 @@
3
3
  from collections import defaultdict
4
4
 
5
5
  import cv2
6
+ import numpy as np
6
7
 
7
8
  from ultralytics import YOLO
8
9
  from ultralytics.utils import ASSETS_URL, DEFAULT_CFG_DICT, DEFAULT_SOL_DICT, LOGGER
9
10
  from ultralytics.utils.checks import check_imshow, check_requirements
11
+ from ultralytics.utils.plotting import Annotator
10
12
 
11
13
 
12
14
  class BaseSolution:
@@ -42,11 +44,13 @@ class BaseSolution:
42
44
  >>> solution.display_output(image)
43
45
  """
44
46
 
45
- def __init__(self, IS_CLI=False, **kwargs):
47
+ def __init__(self, is_cli=False, **kwargs):
46
48
  """
47
- Initializes the `BaseSolution` class with configuration settings and the YOLO model for Ultralytics solutions.
49
+ Initializes the BaseSolution class with configuration settings and the YOLO model.
48
50
 
49
- IS_CLI (optional): Enables CLI mode if set.
51
+ Args:
52
+ is_cli (bool): Enables CLI mode if set to True.
53
+ **kwargs (Any): Additional configuration parameters that override defaults.
50
54
  """
51
55
  check_requirements("shapely>=2.0.0")
52
56
  from shapely.geometry import LineString, Point, Polygon
@@ -63,13 +67,16 @@ class BaseSolution:
63
67
  self.clss = []
64
68
  self.track_ids = []
65
69
  self.track_line = None
70
+ self.masks = None
66
71
  self.r_s = None
67
72
 
73
+ self.LOGGER = LOGGER # Store logger object to be used in multiple solution classes
74
+
68
75
  # Load config and update with args
69
76
  DEFAULT_SOL_DICT.update(kwargs)
70
77
  DEFAULT_CFG_DICT.update(kwargs)
71
78
  self.CFG = {**DEFAULT_SOL_DICT, **DEFAULT_CFG_DICT}
72
- LOGGER.info(f"Ultralytics Solutions: ✅ {DEFAULT_SOL_DICT}")
79
+ self.LOGGER.info(f"Ultralytics Solutions: ✅ {DEFAULT_SOL_DICT}")
73
80
 
74
81
  self.region = self.CFG["region"] # Store region data for other classes usage
75
82
  self.line_width = (
@@ -79,16 +86,17 @@ class BaseSolution:
79
86
  # Load Model and store classes names
80
87
  if self.CFG["model"] is None:
81
88
  self.CFG["model"] = "yolo11n.pt"
82
- self.model = YOLO(self.CFG["model"])
89
+ self.model = YOLO(self.CFG["model"], verbose=self.CFG["verbose"])
83
90
  self.names = self.model.names
91
+ self.classes = self.CFG["classes"]
84
92
 
85
93
  self.track_add_args = { # Tracker additional arguments for advance configuration
86
- k: self.CFG[k] for k in ["verbose", "iou", "conf", "device", "max_det", "half", "tracker"]
94
+ k: self.CFG[k] for k in ["iou", "conf", "device", "max_det", "half", "tracker", "device"]
87
95
  }
88
96
 
89
- if IS_CLI and self.CFG["source"] is None:
97
+ if is_cli and self.CFG["source"] is None:
90
98
  d_s = "solutions_ci_demo.mp4" if "-pose" not in self.CFG["model"] else "solution_ci_pose_demo.mp4"
91
- LOGGER.warning(f"⚠️ WARNING: source not provided. using default source {ASSETS_URL}/{d_s}")
99
+ self.LOGGER.warning(f"⚠️ WARNING: source not provided. using default source {ASSETS_URL}/{d_s}")
92
100
  from ultralytics.utils.downloads import safe_download
93
101
 
94
102
  safe_download(f"{ASSETS_URL}/{d_s}") # download source from ultralytics assets
@@ -103,24 +111,26 @@ class BaseSolution:
103
111
  Applies object tracking and extracts tracks from an input image or frame.
104
112
 
105
113
  Args:
106
- im0 (ndarray): The input image or frame.
114
+ im0 (np.ndarray): The input image or frame.
107
115
 
108
116
  Examples:
109
117
  >>> solution = BaseSolution()
110
118
  >>> frame = cv2.imread("path/to/image.jpg")
111
119
  >>> solution.extract_tracks(frame)
112
120
  """
113
- self.tracks = self.model.track(source=im0, persist=True, classes=self.CFG["classes"], **self.track_add_args)
121
+ self.tracks = self.model.track(source=im0, persist=True, classes=self.classes, **self.track_add_args)
122
+ self.track_data = self.tracks[0].obb or self.tracks[0].boxes # Extract tracks for OBB or object detection
114
123
 
115
- # Extract tracks for OBB or object detection
116
- self.track_data = self.tracks[0].obb or self.tracks[0].boxes
124
+ self.masks = (
125
+ self.tracks[0].masks.xy if hasattr(self.tracks[0], "masks") and self.tracks[0].masks is not None else None
126
+ )
117
127
 
118
128
  if self.track_data and self.track_data.id is not None:
119
129
  self.boxes = self.track_data.xyxy.cpu()
120
130
  self.clss = self.track_data.cls.cpu().tolist()
121
131
  self.track_ids = self.track_data.id.int().cpu().tolist()
122
132
  else:
123
- LOGGER.warning("WARNING ⚠️ no tracks found!")
133
+ self.LOGGER.warning("WARNING ⚠️ no tracks found!")
124
134
  self.boxes, self.clss, self.track_ids = [], [], []
125
135
 
126
136
  def store_tracking_history(self, track_id, box):
@@ -152,7 +162,7 @@ class BaseSolution:
152
162
  self.Polygon(self.region) if len(self.region) >= 3 else self.LineString(self.region)
153
163
  ) # region or line
154
164
 
155
- def display_output(self, im0):
165
+ def display_output(self, plot_im):
156
166
  """
157
167
  Display the results of the processing, which could involve showing frames, printing counts, or saving results.
158
168
 
@@ -160,7 +170,7 @@ class BaseSolution:
160
170
  the processed frame with annotations, and allows for user interaction to close the display.
161
171
 
162
172
  Args:
163
- im0 (numpy.ndarray): The input image or frame that has been processed and annotated.
173
+ plot_im (numpy.ndarray): The image or frame that has been processed and annotated.
164
174
 
165
175
  Examples:
166
176
  >>> solution = BaseSolution()
@@ -173,6 +183,574 @@ class BaseSolution:
173
183
  - The display can be closed by pressing the 'q' key.
174
184
  """
175
185
  if self.CFG.get("show") and self.env_check:
176
- cv2.imshow("Ultralytics Solutions", im0)
186
+ cv2.imshow("Ultralytics Solutions", plot_im)
177
187
  if cv2.waitKey(1) & 0xFF == ord("q"):
188
+ cv2.destroyAllWindows() # Closes current frame window
178
189
  return
190
+
191
+ def process(self, *args, **kwargs):
192
+ """Process method should be implemented by each Solution subclass."""
193
+
194
+ def __call__(self, *args, **kwargs):
195
+ """Allow instances to be called like a function with flexible arguments."""
196
+ return self.process(*args, **kwargs) # Call the subclass-specific process method
197
+
198
+
199
+ class SolutionAnnotator(Annotator):
200
+ """
201
+ A specialized annotator class for visualizing and analyzing computer vision tasks.
202
+
203
+ This class extends the base Annotator class, providing additional methods for drawing regions, centroids, tracking
204
+ trails, and visual annotations for Ultralytics Solutions: https://docs.ultralytics.com/solutions/.
205
+ and parking management.
206
+
207
+ Attributes:
208
+ im (np.ndarray): The image being annotated.
209
+ line_width (int): Thickness of lines used in annotations.
210
+ font_size (int): Size of the font used for text annotations.
211
+ font (str): Path to the font file used for text rendering.
212
+ pil (bool): Whether to use PIL for text rendering.
213
+ example (str): An example attribute for demonstration purposes.
214
+
215
+ Methods:
216
+ draw_region: Draws a region using specified points, colors, and thickness.
217
+ queue_counts_display: Displays queue counts in the specified region.
218
+ display_analytics: Displays overall statistics for parking lot management.
219
+ estimate_pose_angle: Calculates the angle between three points in an object pose.
220
+ draw_specific_points: Draws specific keypoints on the image.
221
+ plot_workout_information: Draws a labeled text box on the image.
222
+ plot_angle_and_count_and_stage: Visualizes angle, step count, and stage for workout monitoring.
223
+ plot_distance_and_line: Displays the distance between centroids and connects them with a line.
224
+ display_objects_labels: Annotates bounding boxes with object class labels.
225
+ segmentation_mask: Draws mask for segmented objects and optionally labels them.
226
+ sweep_annotator: Visualizes a vertical sweep line and optional label.
227
+ visioneye: Maps and connects object centroids to a visual "eye" point.
228
+ circle_label: Draws a circular label within a bounding box.
229
+ text_label: Draws a rectangular label within a bounding box.
230
+
231
+ Examples:
232
+ >>> annotator = SolutionAnnotator(image)
233
+ >>> annotator.draw_region([(0, 0), (100, 100)], color=(0, 255, 0), thickness=5)
234
+ >>> annotator.display_analytics(
235
+ ... image, text={"Available Spots": 5}, txt_color=(0, 0, 0), bg_color=(255, 255, 255), margin=10
236
+ ... )
237
+ """
238
+
239
+ def __init__(self, im, line_width=None, font_size=None, font="Arial.ttf", pil=False, example="abc"):
240
+ """
241
+ Initializes the SolutionAnnotator class with an image for annotation.
242
+
243
+ Args:
244
+ im (np.ndarray): The image to be annotated.
245
+ line_width (int, optional): Line thickness for drawing on the image.
246
+ font_size (int, optional): Font size for text annotations.
247
+ font (str, optional): Path to the font file.
248
+ pil (bool, optional): Indicates whether to use PIL for rendering text.
249
+ example (str, optional): An example parameter for demonstration purposes.
250
+ """
251
+ super().__init__(im, line_width, font_size, font, pil, example)
252
+
253
+ def draw_region(self, reg_pts=None, color=(0, 255, 0), thickness=5):
254
+ """
255
+ Draw a region or line on the image.
256
+
257
+ Args:
258
+ reg_pts (List[Tuple[int, int]]): Region points (for line 2 points, for region 4+ points).
259
+ color (Tuple[int, int, int]): RGB color value for the region.
260
+ thickness (int): Line thickness for drawing the region.
261
+ """
262
+ cv2.polylines(self.im, [np.array(reg_pts, dtype=np.int32)], isClosed=True, color=color, thickness=thickness)
263
+
264
+ # Draw small circles at the corner points
265
+ for point in reg_pts:
266
+ cv2.circle(self.im, (point[0], point[1]), thickness * 2, color, -1) # -1 fills the circle
267
+
268
+ def queue_counts_display(self, label, points=None, region_color=(255, 255, 255), txt_color=(0, 0, 0)):
269
+ """
270
+ Displays queue counts on an image centered at the points with customizable font size and colors.
271
+
272
+ Args:
273
+ label (str): Queue counts label.
274
+ points (List[Tuple[int, int]]): Region points for center point calculation to display text.
275
+ region_color (Tuple[int, int, int]): RGB queue region color.
276
+ txt_color (Tuple[int, int, int]): RGB text display color.
277
+ """
278
+ x_values = [point[0] for point in points]
279
+ y_values = [point[1] for point in points]
280
+ center_x = sum(x_values) // len(points)
281
+ center_y = sum(y_values) // len(points)
282
+
283
+ text_size = cv2.getTextSize(label, 0, fontScale=self.sf, thickness=self.tf)[0]
284
+ text_width = text_size[0]
285
+ text_height = text_size[1]
286
+
287
+ rect_width = text_width + 20
288
+ rect_height = text_height + 20
289
+ rect_top_left = (center_x - rect_width // 2, center_y - rect_height // 2)
290
+ rect_bottom_right = (center_x + rect_width // 2, center_y + rect_height // 2)
291
+ cv2.rectangle(self.im, rect_top_left, rect_bottom_right, region_color, -1)
292
+
293
+ text_x = center_x - text_width // 2
294
+ text_y = center_y + text_height // 2
295
+
296
+ # Draw text
297
+ cv2.putText(
298
+ self.im,
299
+ label,
300
+ (text_x, text_y),
301
+ 0,
302
+ fontScale=self.sf,
303
+ color=txt_color,
304
+ thickness=self.tf,
305
+ lineType=cv2.LINE_AA,
306
+ )
307
+
308
+ def display_analytics(self, im0, text, txt_color, bg_color, margin):
309
+ """
310
+ Display the overall statistics for parking lots, object counter etc.
311
+
312
+ Args:
313
+ im0 (np.ndarray): Inference image.
314
+ text (Dict[str, Any]): Labels dictionary.
315
+ txt_color (Tuple[int, int, int]): Display color for text foreground.
316
+ bg_color (Tuple[int, int, int]): Display color for text background.
317
+ margin (int): Gap between text and rectangle for better display.
318
+ """
319
+ horizontal_gap = int(im0.shape[1] * 0.02)
320
+ vertical_gap = int(im0.shape[0] * 0.01)
321
+ text_y_offset = 0
322
+ for label, value in text.items():
323
+ txt = f"{label}: {value}"
324
+ text_size = cv2.getTextSize(txt, 0, self.sf, self.tf)[0]
325
+ if text_size[0] < 5 or text_size[1] < 5:
326
+ text_size = (5, 5)
327
+ text_x = im0.shape[1] - text_size[0] - margin * 2 - horizontal_gap
328
+ text_y = text_y_offset + text_size[1] + margin * 2 + vertical_gap
329
+ rect_x1 = text_x - margin * 2
330
+ rect_y1 = text_y - text_size[1] - margin * 2
331
+ rect_x2 = text_x + text_size[0] + margin * 2
332
+ rect_y2 = text_y + margin * 2
333
+ cv2.rectangle(im0, (rect_x1, rect_y1), (rect_x2, rect_y2), bg_color, -1)
334
+ cv2.putText(im0, txt, (text_x, text_y), 0, self.sf, txt_color, self.tf, lineType=cv2.LINE_AA)
335
+ text_y_offset = rect_y2
336
+
337
+ @staticmethod
338
+ def estimate_pose_angle(a, b, c):
339
+ """
340
+ Calculate the angle between three points for workout monitoring.
341
+
342
+ Args:
343
+ a (List[float]): The coordinates of the first point.
344
+ b (List[float]): The coordinates of the second point (vertex).
345
+ c (List[float]): The coordinates of the third point.
346
+
347
+ Returns:
348
+ (float): The angle in degrees between the three points.
349
+ """
350
+ a, b, c = np.array(a), np.array(b), np.array(c)
351
+ radians = np.arctan2(c[1] - b[1], c[0] - b[0]) - np.arctan2(a[1] - b[1], a[0] - b[0])
352
+ angle = np.abs(radians * 180.0 / np.pi)
353
+ if angle > 180.0:
354
+ angle = 360 - angle
355
+ return angle
356
+
357
+ def draw_specific_kpts(self, keypoints, indices=None, radius=2, conf_thresh=0.25):
358
+ """
359
+ Draw specific keypoints for gym steps counting.
360
+
361
+ Args:
362
+ keypoints (List[List[float]]): Keypoints data to be plotted, each in format [x, y, confidence].
363
+ indices (List[int], optional): Keypoint indices to be plotted.
364
+ radius (int, optional): Keypoint radius.
365
+ conf_thresh (float, optional): Confidence threshold for keypoints.
366
+
367
+ Returns:
368
+ (np.ndarray): Image with drawn keypoints.
369
+
370
+ Note:
371
+ Keypoint format: [x, y] or [x, y, confidence].
372
+ Modifies self.im in-place.
373
+ """
374
+ indices = indices or [2, 5, 7]
375
+ points = [(int(k[0]), int(k[1])) for i, k in enumerate(keypoints) if i in indices and k[2] >= conf_thresh]
376
+
377
+ # Draw lines between consecutive points
378
+ for start, end in zip(points[:-1], points[1:]):
379
+ cv2.line(self.im, start, end, (0, 255, 0), 2, lineType=cv2.LINE_AA)
380
+
381
+ # Draw circles for keypoints
382
+ for pt in points:
383
+ cv2.circle(self.im, pt, radius, (0, 0, 255), -1, lineType=cv2.LINE_AA)
384
+
385
+ return self.im
386
+
387
+ def plot_workout_information(self, display_text, position, color=(104, 31, 17), txt_color=(255, 255, 255)):
388
+ """
389
+ Draw workout text with a background on the image.
390
+
391
+ Args:
392
+ display_text (str): The text to be displayed.
393
+ position (Tuple[int, int]): Coordinates (x, y) on the image where the text will be placed.
394
+ color (Tuple[int, int, int], optional): Text background color.
395
+ txt_color (Tuple[int, int, int], optional): Text foreground color.
396
+
397
+ Returns:
398
+ (int): The height of the text.
399
+ """
400
+ (text_width, text_height), _ = cv2.getTextSize(display_text, 0, self.sf, self.tf)
401
+
402
+ # Draw background rectangle
403
+ cv2.rectangle(
404
+ self.im,
405
+ (position[0], position[1] - text_height - 5),
406
+ (position[0] + text_width + 10, position[1] - text_height - 5 + text_height + 10 + self.tf),
407
+ color,
408
+ -1,
409
+ )
410
+ # Draw text
411
+ cv2.putText(self.im, display_text, position, 0, self.sf, txt_color, self.tf)
412
+
413
+ return text_height
414
+
415
+ def plot_angle_and_count_and_stage(
416
+ self, angle_text, count_text, stage_text, center_kpt, color=(104, 31, 17), txt_color=(255, 255, 255)
417
+ ):
418
+ """
419
+ Plot the pose angle, count value, and step stage for workout monitoring.
420
+
421
+ Args:
422
+ angle_text (str): Angle value for workout monitoring.
423
+ count_text (str): Counts value for workout monitoring.
424
+ stage_text (str): Stage decision for workout monitoring.
425
+ center_kpt (List[int]): Centroid pose index for workout monitoring.
426
+ color (Tuple[int, int, int], optional): Text background color.
427
+ txt_color (Tuple[int, int, int], optional): Text foreground color.
428
+ """
429
+ # Format text
430
+ angle_text, count_text, stage_text = f" {angle_text:.2f}", f"Steps : {count_text}", f" {stage_text}"
431
+
432
+ # Draw angle, count and stage text
433
+ angle_height = self.plot_workout_information(
434
+ angle_text, (int(center_kpt[0]), int(center_kpt[1])), color, txt_color
435
+ )
436
+ count_height = self.plot_workout_information(
437
+ count_text, (int(center_kpt[0]), int(center_kpt[1]) + angle_height + 20), color, txt_color
438
+ )
439
+ self.plot_workout_information(
440
+ stage_text, (int(center_kpt[0]), int(center_kpt[1]) + angle_height + count_height + 40), color, txt_color
441
+ )
442
+
443
+ def plot_distance_and_line(
444
+ self, pixels_distance, centroids, line_color=(104, 31, 17), centroid_color=(255, 0, 255)
445
+ ):
446
+ """
447
+ Plot the distance and line between two centroids on the frame.
448
+
449
+ Args:
450
+ pixels_distance (float): Pixels distance between two bbox centroids.
451
+ centroids (List[Tuple[int, int]]): Bounding box centroids data.
452
+ line_color (Tuple[int, int, int], optional): Distance line color.
453
+ centroid_color (Tuple[int, int, int], optional): Bounding box centroid color.
454
+ """
455
+ # Get the text size
456
+ text = f"Pixels Distance: {pixels_distance:.2f}"
457
+ (text_width_m, text_height_m), _ = cv2.getTextSize(text, 0, self.sf, self.tf)
458
+
459
+ # Define corners with 10-pixel margin and draw rectangle
460
+ cv2.rectangle(self.im, (15, 25), (15 + text_width_m + 20, 25 + text_height_m + 20), line_color, -1)
461
+
462
+ # Calculate the position for the text with a 10-pixel margin and draw text
463
+ text_position = (25, 25 + text_height_m + 10)
464
+ cv2.putText(
465
+ self.im,
466
+ text,
467
+ text_position,
468
+ 0,
469
+ self.sf,
470
+ (255, 255, 255),
471
+ self.tf,
472
+ cv2.LINE_AA,
473
+ )
474
+
475
+ cv2.line(self.im, centroids[0], centroids[1], line_color, 3)
476
+ cv2.circle(self.im, centroids[0], 6, centroid_color, -1)
477
+ cv2.circle(self.im, centroids[1], 6, centroid_color, -1)
478
+
479
+ def display_objects_labels(self, im0, text, txt_color, bg_color, x_center, y_center, margin):
480
+ """
481
+ Display the bounding boxes labels in parking management app.
482
+
483
+ Args:
484
+ im0 (np.ndarray): Inference image.
485
+ text (str): Object/class name.
486
+ txt_color (Tuple[int, int, int]): Display color for text foreground.
487
+ bg_color (Tuple[int, int, int]): Display color for text background.
488
+ x_center (float): The x position center point for bounding box.
489
+ y_center (float): The y position center point for bounding box.
490
+ margin (int): The gap between text and rectangle for better display.
491
+ """
492
+ text_size = cv2.getTextSize(text, 0, fontScale=self.sf, thickness=self.tf)[0]
493
+ text_x = x_center - text_size[0] // 2
494
+ text_y = y_center + text_size[1] // 2
495
+
496
+ rect_x1 = text_x - margin
497
+ rect_y1 = text_y - text_size[1] - margin
498
+ rect_x2 = text_x + text_size[0] + margin
499
+ rect_y2 = text_y + margin
500
+ cv2.rectangle(
501
+ im0,
502
+ (int(rect_x1), int(rect_y1)),
503
+ (int(rect_x2), int(rect_y2)),
504
+ tuple(map(int, bg_color)), # Ensure color values are int
505
+ -1,
506
+ )
507
+
508
+ cv2.putText(
509
+ im0,
510
+ text,
511
+ (int(text_x), int(text_y)),
512
+ 0,
513
+ self.sf,
514
+ tuple(map(int, txt_color)), # Ensure color values are int
515
+ self.tf,
516
+ lineType=cv2.LINE_AA,
517
+ )
518
+
519
+ def segmentation_mask(self, mask, mask_color=(255, 0, 255), label=None, alpha=0.5):
520
+ """
521
+ Draw an optimized segmentation mask with smooth corners, highlighted edge, and dynamic text box size.
522
+
523
+ Args:
524
+ mask (np.ndarray): A 2D array of shape (N, 2) containing the object mask.
525
+ mask_color (Tuple[int, int, int]): RGB color for the mask.
526
+ label (str, optional): Text label for the object.
527
+ alpha (float): Transparency level (0 = fully transparent, 1 = fully opaque).
528
+ """
529
+ if mask.size == 0:
530
+ return
531
+
532
+ overlay = self.im.copy()
533
+ mask = np.int32([mask])
534
+
535
+ # Approximate polygon for smooth corners with epsilon
536
+ refined_mask = cv2.approxPolyDP(mask, 0.002 * cv2.arcLength(mask, True), True)
537
+
538
+ # Apply a highlighter effect by drawing a thick outer shadow
539
+ cv2.polylines(overlay, [refined_mask], isClosed=True, color=mask_color, thickness=self.lw * 3)
540
+ cv2.fillPoly(overlay, [refined_mask], mask_color) # draw mask with primary color
541
+
542
+ # Apply an inner glow effect for extra clarity
543
+ cv2.polylines(overlay, [refined_mask], isClosed=True, color=mask_color, thickness=self.lw)
544
+
545
+ self.im = cv2.addWeighted(overlay, alpha, self.im, 1 - alpha, 0) # blend overlay with the original image
546
+
547
+ # Draw label if provided
548
+ if label:
549
+ text_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, self.sf, self.tf)
550
+ text_x, text_y = refined_mask[0][0][0], refined_mask[0][0][1]
551
+ rect_start, rect_end = (text_x - 5, text_y - text_size[1] - 5), (text_x + text_size[0] + 5, text_y + 5)
552
+ cv2.rectangle(self.im, rect_start, rect_end, mask_color, -1)
553
+ cv2.putText(
554
+ self.im,
555
+ label,
556
+ (text_x, text_y),
557
+ cv2.FONT_HERSHEY_SIMPLEX,
558
+ self.sf,
559
+ self.get_txt_color(mask_color),
560
+ self.tf,
561
+ )
562
+
563
+ def sweep_annotator(self, line_x=0, line_y=0, label=None, color=(221, 0, 186), txt_color=(255, 255, 255)):
564
+ """
565
+ Draw a sweep annotation line and an optional label.
566
+
567
+ Args:
568
+ line_x (int): The x-coordinate of the sweep line.
569
+ line_y (int): The y-coordinate limit of the sweep line.
570
+ label (str, optional): Text label to be drawn in center of sweep line. If None, no label is drawn.
571
+ color (Tuple[int, int, int]): RGB color for the line and label background.
572
+ txt_color (Tuple[int, int, int]): RGB color for the label text.
573
+ """
574
+ # Draw the sweep line
575
+ cv2.line(self.im, (line_x, 0), (line_x, line_y), color, self.tf * 2)
576
+
577
+ # Draw label, if provided
578
+ if label:
579
+ (text_width, text_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, self.sf, self.tf)
580
+ cv2.rectangle(
581
+ self.im,
582
+ (line_x - text_width // 2 - 10, line_y // 2 - text_height // 2 - 10),
583
+ (line_x + text_width // 2 + 10, line_y // 2 + text_height // 2 + 10),
584
+ color,
585
+ -1,
586
+ )
587
+ cv2.putText(
588
+ self.im,
589
+ label,
590
+ (line_x - text_width // 2, line_y // 2 + text_height // 2),
591
+ cv2.FONT_HERSHEY_SIMPLEX,
592
+ self.sf,
593
+ txt_color,
594
+ self.tf,
595
+ )
596
+
597
+ def visioneye(self, box, center_point, color=(235, 219, 11), pin_color=(255, 0, 255)):
598
+ """
599
+ Perform pinpoint human-vision eye mapping and plotting.
600
+
601
+ Args:
602
+ box (List[float]): Bounding box coordinates in format [x1, y1, x2, y2].
603
+ center_point (Tuple[int, int]): Center point for vision eye view.
604
+ color (Tuple[int, int, int]): Object centroid and line color.
605
+ pin_color (Tuple[int, int, int]): Visioneye point color.
606
+ """
607
+ center_bbox = int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)
608
+ cv2.circle(self.im, center_point, self.tf * 2, pin_color, -1)
609
+ cv2.circle(self.im, center_bbox, self.tf * 2, color, -1)
610
+ cv2.line(self.im, center_point, center_bbox, color, self.tf)
611
+
612
+ def circle_label(self, box, label="", color=(128, 128, 128), txt_color=(255, 255, 255), margin=2):
613
+ """
614
+ Draw a label with a background circle centered within a given bounding box.
615
+
616
+ Args:
617
+ box (Tuple[float, float, float, float]): The bounding box coordinates (x1, y1, x2, y2).
618
+ label (str): The text label to be displayed.
619
+ color (Tuple[int, int, int]): The background color of the circle (B, G, R).
620
+ txt_color (Tuple[int, int, int]): The color of the text (R, G, B).
621
+ margin (int): The margin between the text and the circle border.
622
+ """
623
+ # If label have more than 3 characters, skip other characters, due to circle size
624
+ if len(label) > 3:
625
+ print(
626
+ f"Length of label is {len(label)}, initial 3 label characters will be considered for circle annotation!"
627
+ )
628
+ label = label[:3]
629
+
630
+ # Calculate the center of the box
631
+ x_center, y_center = int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)
632
+ # Get the text size
633
+ text_size = cv2.getTextSize(str(label), cv2.FONT_HERSHEY_SIMPLEX, self.sf - 0.15, self.tf)[0]
634
+ # Calculate the required radius to fit the text with the margin
635
+ required_radius = int(((text_size[0] ** 2 + text_size[1] ** 2) ** 0.5) / 2) + margin
636
+ # Draw the circle with the required radius
637
+ cv2.circle(self.im, (x_center, y_center), required_radius, color, -1)
638
+ # Calculate the position for the text
639
+ text_x = x_center - text_size[0] // 2
640
+ text_y = y_center + text_size[1] // 2
641
+ # Draw the text
642
+ cv2.putText(
643
+ self.im,
644
+ str(label),
645
+ (text_x, text_y),
646
+ cv2.FONT_HERSHEY_SIMPLEX,
647
+ self.sf - 0.15,
648
+ self.get_txt_color(color, txt_color),
649
+ self.tf,
650
+ lineType=cv2.LINE_AA,
651
+ )
652
+
653
+ def text_label(self, box, label="", color=(128, 128, 128), txt_color=(255, 255, 255), margin=5):
654
+ """
655
+ Draw a label with a background rectangle centered within a given bounding box.
656
+
657
+ Args:
658
+ box (Tuple[float, float, float, float]): The bounding box coordinates (x1, y1, x2, y2).
659
+ label (str): The text label to be displayed.
660
+ color (Tuple[int, int, int]): The background color of the rectangle (B, G, R).
661
+ txt_color (Tuple[int, int, int]): The color of the text (R, G, B).
662
+ margin (int): The margin between the text and the rectangle border.
663
+ """
664
+ # Calculate the center of the bounding box
665
+ x_center, y_center = int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2)
666
+ # Get the size of the text
667
+ text_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, self.sf - 0.1, self.tf)[0]
668
+ # Calculate the top-left corner of the text (to center it)
669
+ text_x = x_center - text_size[0] // 2
670
+ text_y = y_center + text_size[1] // 2
671
+ # Calculate the coordinates of the background rectangle
672
+ rect_x1 = text_x - margin
673
+ rect_y1 = text_y - text_size[1] - margin
674
+ rect_x2 = text_x + text_size[0] + margin
675
+ rect_y2 = text_y + margin
676
+ # Draw the background rectangle
677
+ cv2.rectangle(self.im, (rect_x1, rect_y1), (rect_x2, rect_y2), color, -1)
678
+ # Draw the text on top of the rectangle
679
+ cv2.putText(
680
+ self.im,
681
+ label,
682
+ (text_x, text_y),
683
+ cv2.FONT_HERSHEY_SIMPLEX,
684
+ self.sf - 0.1,
685
+ self.get_txt_color(color, txt_color),
686
+ self.tf,
687
+ lineType=cv2.LINE_AA,
688
+ )
689
+
690
+
691
+ class SolutionResults:
692
+ """
693
+ A class to encapsulate the results of Ultralytics Solutions.
694
+
695
+ This class is designed to store and manage various outputs generated by the solution pipeline, including counts,
696
+ angles, and workout stages.
697
+
698
+ Attributes:
699
+ plot_im (np.ndarray): Processed image with counts, blurred, or other effects from solutions.
700
+ in_count (int): The total number of "in" counts in a video stream.
701
+ out_count (int): The total number of "out" counts in a video stream.
702
+ classwise_count (Dict[str, int]): A dictionary containing counts of objects categorized by class.
703
+ queue_count (int): The count of objects in a queue or waiting area.
704
+ workout_count (int): The count of workout repetitions.
705
+ workout_angle (float): The angle calculated during a workout exercise.
706
+ workout_stage (str): The current stage of the workout.
707
+ pixels_distance (float): The calculated distance in pixels between two points or objects.
708
+ available_slots (int): The number of available slots in a monitored area.
709
+ filled_slots (int): The number of filled slots in a monitored area.
710
+ email_sent (bool): A flag indicating whether an email notification was sent.
711
+ total_tracks (int): The total number of tracked objects.
712
+ region_counts (Dict): The count of objects within a specific region.
713
+ speed_dict (Dict[str, float]): A dictionary containing speed information for tracked objects.
714
+ total_crop_objects (int): Total number of cropped objects using ObjectCropper class.
715
+ """
716
+
717
+ def __init__(self, **kwargs):
718
+ """
719
+ Initialize a SolutionResults object with default or user-specified values.
720
+
721
+ Args:
722
+ **kwargs (Any): Optional arguments to override default attribute values.
723
+ """
724
+ self.plot_im = None
725
+ self.in_count = 0
726
+ self.out_count = 0
727
+ self.classwise_count = {}
728
+ self.queue_count = 0
729
+ self.workout_count = 0
730
+ self.workout_angle = 0.0
731
+ self.workout_stage = None
732
+ self.pixels_distance = 0.0
733
+ self.available_slots = 0
734
+ self.filled_slots = 0
735
+ self.email_sent = False
736
+ self.total_tracks = 0
737
+ self.region_counts = {}
738
+ self.speed_dict = {}
739
+ self.total_crop_objects = 0
740
+
741
+ # Override with user-defined values
742
+ self.__dict__.update(kwargs)
743
+
744
+ def __str__(self):
745
+ """
746
+ Return a formatted string representation of the SolutionResults object.
747
+
748
+ Returns:
749
+ (str): A string representation listing non-null attributes.
750
+ """
751
+ attrs = {
752
+ k: v
753
+ for k, v in self.__dict__.items()
754
+ if k != "plot_im" and v not in [None, {}, 0, 0.0, False] # Exclude `plot_im` explicitly
755
+ }
756
+ return f"SolutionResults({', '.join(f'{k}={v}' for k, v in attrs.items())})"