ultralytics 8.3.5__py3-none-any.whl → 8.3.7__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.

Potentially problematic release.


This version of ultralytics might be problematic. Click here for more details.

tests/test_solutions.py CHANGED
@@ -19,8 +19,8 @@ def test_major_solutions():
19
19
  cap = cv2.VideoCapture("solutions_ci_demo.mp4")
20
20
  assert cap.isOpened(), "Error reading video file"
21
21
  region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
22
- # counter = solutions.ObjectCounter(reg_pts=region_points, names=names, view_img=False)
23
- heatmap = solutions.Heatmap(colormap=cv2.COLORMAP_PARULA, names=names, view_img=False)
22
+ counter = solutions.ObjectCounter(region=region_points, model="yolo11n.pt", show=False)
23
+ heatmap = solutions.Heatmap(colormap=cv2.COLORMAP_PARULA, model="yolo11n.pt", show=False)
24
24
  speed = solutions.SpeedEstimator(reg_pts=region_points, names=names, view_img=False)
25
25
  queue = solutions.QueueManager(names=names, reg_pts=region_points, view_img=False)
26
26
  while cap.isOpened():
@@ -29,8 +29,8 @@ def test_major_solutions():
29
29
  break
30
30
  original_im0 = im0.copy()
31
31
  tracks = model.track(im0, persist=True, show=False)
32
- # _ = counter.start_counting(original_im0.copy(), tracks)
33
- _ = heatmap.generate_heatmap(original_im0.copy(), tracks)
32
+ _ = counter.count(original_im0.copy())
33
+ _ = heatmap.generate_heatmap(original_im0.copy())
34
34
  _ = speed.estimate_speed(original_im0.copy(), tracks)
35
35
  _ = queue.process_queue(original_im0.copy(), tracks)
36
36
  cap.release()
@@ -41,16 +41,14 @@ def test_major_solutions():
41
41
  def test_aigym():
42
42
  """Test the workouts monitoring solution."""
43
43
  safe_download(url=WORKOUTS_SOLUTION_DEMO)
44
- model = YOLO("yolo11n-pose.pt")
45
44
  cap = cv2.VideoCapture("solution_ci_pose_demo.mp4")
46
45
  assert cap.isOpened(), "Error reading video file"
47
- gym_object = solutions.AIGym(line_thickness=2, pose_type="squat", kpts_to_check=[5, 11, 13])
46
+ gym = solutions.AIGym(line_width=2, kpts=[5, 11, 13])
48
47
  while cap.isOpened():
49
48
  success, im0 = cap.read()
50
49
  if not success:
51
50
  break
52
- results = model.track(im0, verbose=False)
53
- _ = gym_object.start_counting(im0, results)
51
+ _ = gym.monitor(im0)
54
52
  cap.release()
55
53
  cv2.destroyAllWindows()
56
54
 
ultralytics/__init__.py CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ultralytics YOLO 🚀, AGPL-3.0 license
2
2
 
3
- __version__ = "8.3.5"
3
+ __version__ = "8.3.7"
4
4
 
5
5
  import os
6
6
 
@@ -10,3 +10,7 @@ show: True # Flag to control whether to display output image or not
10
10
  show_in: True # Flag to display objects moving *into* the defined region
11
11
  show_out: True # Flag to display objects moving *out of* the defined region
12
12
  classes: # To count specific classes
13
+ up_angle: 145.0 # Workouts up_angle for counts, 145.0 is default value
14
+ down_angle: 90 # Workouts down_angle for counts, 90 is default value
15
+ kpts: [6, 8, 10] # Keypoints for workouts monitoring
16
+ colormap: # Colormap for heatmap
@@ -1,13 +1,18 @@
1
1
  # Ultralytics YOLO 🚀, AGPL-3.0 license
2
2
 
3
3
  import json
4
+ import random
5
+ import shutil
4
6
  from collections import defaultdict
7
+ from concurrent.futures import ThreadPoolExecutor, as_completed
5
8
  from pathlib import Path
6
9
 
7
10
  import cv2
8
11
  import numpy as np
12
+ from PIL import Image
9
13
 
10
- from ultralytics.utils import LOGGER, TQDM
14
+ from ultralytics.utils import DATASETS_DIR, LOGGER, NUM_THREADS, TQDM
15
+ from ultralytics.utils.downloads import download
11
16
  from ultralytics.utils.files import increment_path
12
17
 
13
18
 
@@ -588,15 +593,13 @@ def yolo_bbox2segment(im_dir, save_dir=None, sam_model="sam_b.pt"):
588
593
 
589
594
  - im_dir
590
595
  ├─ 001.jpg
591
- ├─ ..
596
+ ├─ ...
592
597
  └─ NNN.jpg
593
598
  - labels
594
599
  ├─ 001.txt
595
- ├─ ..
600
+ ├─ ...
596
601
  └─ NNN.txt
597
602
  """
598
- from tqdm import tqdm
599
-
600
603
  from ultralytics import SAM
601
604
  from ultralytics.data import YOLODataset
602
605
  from ultralytics.utils import LOGGER
@@ -610,7 +613,7 @@ def yolo_bbox2segment(im_dir, save_dir=None, sam_model="sam_b.pt"):
610
613
 
611
614
  LOGGER.info("Detection labels detected, generating segment labels by SAM model!")
612
615
  sam_model = SAM(sam_model)
613
- for label in tqdm(dataset.labels, total=len(dataset.labels), desc="Generating segment labels"):
616
+ for label in TQDM(dataset.labels, total=len(dataset.labels), desc="Generating segment labels"):
614
617
  h, w = label["shape"]
615
618
  boxes = label["bboxes"]
616
619
  if len(boxes) == 0: # skip empty labels
@@ -635,3 +638,58 @@ def yolo_bbox2segment(im_dir, save_dir=None, sam_model="sam_b.pt"):
635
638
  with open(txt_file, "a") as f:
636
639
  f.writelines(text + "\n" for text in texts)
637
640
  LOGGER.info(f"Generated segment labels saved in {save_dir}")
641
+
642
+
643
+ def create_synthetic_coco_dataset():
644
+ """
645
+ Creates a synthetic COCO dataset with random images and existing labels.
646
+
647
+ This function downloads COCO labels, creates synthetic images for train2017 and val2017 subsets, and organizes
648
+ them in the COCO dataset structure. It uses multithreading to generate images efficiently.
649
+
650
+ Examples:
651
+ >>> create_synthetic_coco_dataset()
652
+
653
+ Notes:
654
+ - Requires internet connection to download label files.
655
+ - Generates random RGB images of varying sizes (480x480 to 640x640 pixels).
656
+ - Existing test2017 directory is removed as it's not needed.
657
+ - If label directories don't exist, image creation for that subset is skipped.
658
+ """
659
+
660
+ def create_synthetic_image(image_file):
661
+ """Generates synthetic images with random sizes and colors for dataset augmentation or testing purposes."""
662
+ if not image_file.exists():
663
+ size = (random.randint(480, 640), random.randint(480, 640))
664
+ Image.new(
665
+ "RGB",
666
+ size=size,
667
+ color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
668
+ ).save(image_file)
669
+
670
+ # Download labels
671
+ dir = DATASETS_DIR / "coco"
672
+ url = "https://github.com/ultralytics/assets/releases/download/v0.0.0/"
673
+ label_zip = "coco2017labels-segments.zip"
674
+ download([url + label_zip], dir=dir.parent)
675
+
676
+ # Create synthetic images
677
+ shutil.rmtree(dir / "labels" / "test2017", ignore_errors=True) # Remove test2017 directory as not needed
678
+ with ThreadPoolExecutor(max_workers=NUM_THREADS) as executor:
679
+ for subset in ["train2017", "val2017"]:
680
+ subset_dir = dir / "images" / subset
681
+ subset_dir.mkdir(parents=True, exist_ok=True)
682
+
683
+ label_dir = dir / "labels" / subset
684
+ if label_dir.exists():
685
+ label_files = list(label_dir.glob("*.txt"))
686
+ image_files = [subset_dir / f"{label_file.stem}.jpg" for label_file in label_files]
687
+
688
+ # Submit all tasks
689
+ futures = [executor.submit(create_synthetic_image, image_file) for image_file in image_files]
690
+ for _ in TQDM(as_completed(futures), total=len(futures), desc=f"Generating images for {subset}"):
691
+ pass # The actual work is done in the background
692
+ else:
693
+ print(f"Warning: Label directory {label_dir} does not exist. Skipping image creation for {subset}.")
694
+
695
+ print("Synthetic COCO dataset created successfully.")
@@ -39,24 +39,11 @@ def init_explorer_form(data=None, model=None):
39
39
  else:
40
40
  ds = [data]
41
41
 
42
+ prefixes = ["yolov8", "yolo11"]
43
+ sizes = ["n", "s", "m", "l", "x"]
44
+ tasks = ["", "-seg", "-pose"]
42
45
  if model is None:
43
- models = [
44
- "yolov8n.pt",
45
- "yolov8s.pt",
46
- "yolov8m.pt",
47
- "yolov8l.pt",
48
- "yolov8x.pt",
49
- "yolov8n-seg.pt",
50
- "yolov8s-seg.pt",
51
- "yolov8m-seg.pt",
52
- "yolov8l-seg.pt",
53
- "yolov8x-seg.pt",
54
- "yolov8n-pose.pt",
55
- "yolov8s-pose.pt",
56
- "yolov8m-pose.pt",
57
- "yolov8l-pose.pt",
58
- "yolov8x-pose.pt",
59
- ]
46
+ models = [f"{p}{s}{t}" for p in prefixes for s in sizes for t in tasks]
60
47
  else:
61
48
  models = [model]
62
49
 
@@ -183,11 +183,10 @@ class Exporter:
183
183
 
184
184
  # Get the closest match if format is invalid
185
185
  matches = difflib.get_close_matches(fmt, fmts, n=1, cutoff=0.6) # 60% similarity required to match
186
- if matches:
187
- LOGGER.warning(f"WARNING ⚠️ Invalid export format='{fmt}', updating to format='{matches[0]}'")
188
- fmt = matches[0]
189
- else:
186
+ if not matches:
190
187
  raise ValueError(f"Invalid export format='{fmt}'. Valid formats are {fmts}")
188
+ LOGGER.warning(f"WARNING ⚠️ Invalid export format='{fmt}', updating to format='{matches[0]}'")
189
+ fmt = matches[0]
191
190
  flags = [x == fmt for x in fmts]
192
191
  if sum(flags) != 1:
193
192
  raise ValueError(f"Invalid export format='{fmt}'. Valid formats are {fmts}")
@@ -544,6 +544,8 @@ class Model(nn.Module):
544
544
 
545
545
  if not self.predictor:
546
546
  self.predictor = predictor or self._smart_load("predictor")(overrides=args, _callbacks=self.callbacks)
547
+ if predictor:
548
+ self.predictor.args = get_cfg(self.predictor.args, args)
547
549
  self.predictor.setup_model(model=self.model, verbose=is_cli)
548
550
  else: # only update args if predictor is already setup
549
551
  self.predictor.args = get_cfg(self.predictor.args, args)
@@ -469,11 +469,11 @@ class BaseTrainer:
469
469
 
470
470
  if RANK in {-1, 0}:
471
471
  # Do final val with best.pt
472
- LOGGER.info(
473
- f"\n{epoch - self.start_epoch + 1} epochs completed in "
474
- f"{(time.time() - self.train_time_start) / 3600:.3f} hours."
475
- )
472
+ epochs = epoch - self.start_epoch + 1 # total training epochs
473
+ seconds = time.time() - self.train_time_start # total training seconds
474
+ LOGGER.info(f"\n{epochs} epochs completed in {seconds / 3600:.3f} hours.")
476
475
  self.final_eval()
476
+ self.validator.metrics.training = {"epochs": epochs, "seconds": seconds} # add training speed
477
477
  if self.args.plots:
478
478
  self.plot_metrics()
479
479
  self.run_callbacks("on_train_end")
@@ -1,127 +1,79 @@
1
1
  # Ultralytics YOLO 🚀, AGPL-3.0 license
2
2
 
3
- import cv2
4
-
5
- from ultralytics.utils.checks import check_imshow
3
+ from ultralytics.solutions.solutions import BaseSolution # Import a parent class
6
4
  from ultralytics.utils.plotting import Annotator
7
5
 
8
6
 
9
- class AIGym:
7
+ class AIGym(BaseSolution):
10
8
  """A class to manage the gym steps of people in a real-time video stream based on their poses."""
11
9
 
12
- def __init__(
13
- self,
14
- kpts_to_check,
15
- line_thickness=2,
16
- view_img=False,
17
- pose_up_angle=145.0,
18
- pose_down_angle=90.0,
19
- pose_type="pullup",
20
- ):
10
+ def __init__(self, **kwargs):
11
+ """Initialization function for AiGYM class, a child class of BaseSolution class, can be used for workouts
12
+ monitoring.
21
13
  """
22
- Initializes the AIGym class with the specified parameters.
23
-
24
- Args:
25
- kpts_to_check (list): Indices of keypoints to check.
26
- line_thickness (int, optional): Thickness of the lines drawn. Defaults to 2.
27
- view_img (bool, optional): Flag to display the image. Defaults to False.
28
- pose_up_angle (float, optional): Angle threshold for the 'up' pose. Defaults to 145.0.
29
- pose_down_angle (float, optional): Angle threshold for the 'down' pose. Defaults to 90.0.
30
- pose_type (str, optional): Type of pose to detect ('pullup', 'pushup', 'abworkout'). Defaults to "pullup".
14
+ # Check if the model name ends with '-pose'
15
+ if "model" in kwargs and "-pose" not in kwargs["model"]:
16
+ kwargs["model"] = "yolo11n-pose.pt"
17
+ elif "model" not in kwargs:
18
+ kwargs["model"] = "yolo11n-pose.pt"
19
+
20
+ super().__init__(**kwargs)
21
+ self.count = [] # List for counts, necessary where there are multiple objects in frame
22
+ self.angle = [] # List for angle, necessary where there are multiple objects in frame
23
+ self.stage = [] # List for stage, necessary where there are multiple objects in frame
24
+
25
+ # Extract details from CFG single time for usage later
26
+ self.initial_stage = None
27
+ self.up_angle = float(self.CFG["up_angle"]) # Pose up predefined angle to consider up pose
28
+ self.down_angle = float(self.CFG["down_angle"]) # Pose down predefined angle to consider down pose
29
+ self.kpts = self.CFG["kpts"] # User selected kpts of workouts storage for further usage
30
+ self.lw = self.CFG["line_width"] # Store line_width for usage
31
+
32
+ def monitor(self, im0):
31
33
  """
32
- # Image and line thickness
33
- self.im0 = None
34
- self.tf = line_thickness
35
-
36
- # Keypoints and count information
37
- self.keypoints = None
38
- self.poseup_angle = pose_up_angle
39
- self.posedown_angle = pose_down_angle
40
- self.threshold = 0.001
41
-
42
- # Store stage, count and angle information
43
- self.angle = None
44
- self.count = None
45
- self.stage = None
46
- self.pose_type = pose_type
47
- self.kpts_to_check = kpts_to_check
48
-
49
- # Visual Information
50
- self.view_img = view_img
51
- self.annotator = None
52
-
53
- # Check if environment supports imshow
54
- self.env_check = check_imshow(warn=True)
55
- self.count = []
56
- self.angle = []
57
- self.stage = []
58
-
59
- def start_counting(self, im0, results):
60
- """
61
- Function used to count the gym steps.
34
+ Monitor the workouts using Ultralytics YOLOv8 Pose Model: https://docs.ultralytics.com/tasks/pose/.
62
35
 
63
36
  Args:
64
- im0 (ndarray): Current frame from the video stream.
65
- results (list): Pose estimation data.
37
+ im0 (ndarray): The input image that will be used for processing
38
+ Returns
39
+ im0 (ndarray): The processed image for more usage
66
40
  """
67
- self.im0 = im0
68
-
69
- if not len(results[0]):
70
- return self.im0
71
-
72
- if len(results[0]) > len(self.count):
73
- new_human = len(results[0]) - len(self.count)
74
- self.count += [0] * new_human
75
- self.angle += [0] * new_human
76
- self.stage += ["-"] * new_human
77
-
78
- self.keypoints = results[0].keypoints.data
79
- self.annotator = Annotator(im0, line_width=self.tf)
80
-
81
- for ind, k in enumerate(reversed(self.keypoints)):
82
- # Estimate angle and draw specific points based on pose type
83
- if self.pose_type in {"pushup", "pullup", "abworkout", "squat"}:
84
- self.angle[ind] = self.annotator.estimate_pose_angle(
85
- k[int(self.kpts_to_check[0])].cpu(),
86
- k[int(self.kpts_to_check[1])].cpu(),
87
- k[int(self.kpts_to_check[2])].cpu(),
88
- )
89
- self.im0 = self.annotator.draw_specific_points(k, self.kpts_to_check, shape=(640, 640), radius=10)
90
-
91
- # Check and update pose stages and counts based on angle
92
- if self.pose_type in {"abworkout", "pullup"}:
93
- if self.angle[ind] > self.poseup_angle:
94
- self.stage[ind] = "down"
95
- if self.angle[ind] < self.posedown_angle and self.stage[ind] == "down":
96
- self.stage[ind] = "up"
97
- self.count[ind] += 1
98
-
99
- elif self.pose_type in {"pushup", "squat"}:
100
- if self.angle[ind] > self.poseup_angle:
101
- self.stage[ind] = "up"
102
- if self.angle[ind] < self.posedown_angle and self.stage[ind] == "up":
103
- self.stage[ind] = "down"
41
+ # Extract tracks
42
+ tracks = self.model.track(source=im0, persist=True, classes=self.CFG["classes"])[0]
43
+
44
+ if tracks.boxes.id is not None:
45
+ # Extract and check keypoints
46
+ if len(tracks) > len(self.count):
47
+ new_human = len(tracks) - len(self.count)
48
+ self.angle += [0] * new_human
49
+ self.count += [0] * new_human
50
+ self.stage += ["-"] * new_human
51
+
52
+ # Initialize annotator
53
+ self.annotator = Annotator(im0, line_width=self.lw)
54
+
55
+ # Enumerate over keypoints
56
+ for ind, k in enumerate(reversed(tracks.keypoints.data)):
57
+ # Get keypoints and estimate the angle
58
+ kpts = [k[int(self.kpts[i])].cpu() for i in range(3)]
59
+ self.angle[ind] = self.annotator.estimate_pose_angle(*kpts)
60
+ im0 = self.annotator.draw_specific_points(k, self.kpts, radius=self.lw * 3)
61
+
62
+ # Determine stage and count logic based on angle thresholds
63
+ if self.angle[ind] < self.down_angle:
64
+ if self.stage[ind] == "up":
104
65
  self.count[ind] += 1
66
+ self.stage[ind] = "down"
67
+ elif self.angle[ind] > self.up_angle:
68
+ self.stage[ind] = "up"
105
69
 
70
+ # Display angle, count, and stage text
106
71
  self.annotator.plot_angle_and_count_and_stage(
107
- angle_text=self.angle[ind],
108
- count_text=self.count[ind],
109
- stage_text=self.stage[ind],
110
- center_kpt=k[int(self.kpts_to_check[1])],
72
+ angle_text=self.angle[ind], # angle text for display
73
+ count_text=self.count[ind], # count text for workouts
74
+ stage_text=self.stage[ind], # stage position text
75
+ center_kpt=k[int(self.kpts[1])], # center keypoint for display
111
76
  )
112
77
 
113
- # Draw keypoints
114
- self.annotator.kpts(k, shape=(640, 640), radius=1, kpt_line=True)
115
-
116
- # Display the image if environment supports it and view_img is True
117
- if self.env_check and self.view_img:
118
- cv2.imshow("Ultralytics YOLOv8 AI GYM", self.im0)
119
- if cv2.waitKey(1) & 0xFF == ord("q"):
120
- return
121
-
122
- return self.im0
123
-
124
-
125
- if __name__ == "__main__":
126
- kpts_to_check = [0, 1, 2] # example keypoints
127
- aigym = AIGym(kpts_to_check)
78
+ self.display_output(im0) # Display output image, if environment support display
79
+ return im0 # return an image for writing or further usage