Simplify track check with is_track attribute (#20792)

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
Laughing 2025-05-25 23:30:25 +08:00 committed by GitHub
parent 4b92e5ef42
commit 460cbff100
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 13 additions and 13 deletions

View File

@ -289,7 +289,7 @@ In the following example, we demonstrate how to utilize YOLO11's tracking capabi
result = model.track(frame, persist=True)[0]
# Get the boxes and track IDs
if result.boxes and result.boxes.id is not None:
if result.boxes and result.boxes.is_track:
boxes = result.boxes.xywh.cpu()
track_ids = result.boxes.id.int().cpu().tolist()

View File

@ -436,7 +436,7 @@ Ultralytics includes an `Annotator` class for annotating various data types. It'
cv2.namedWindow(window_name)
cv2.setMouseCallback(window_name, drag_line)
if results.boxes.id is not None:
if results.boxes.is_track:
if results.masks is not None:
masks = results.masks
boxes = results.boxes

View File

@ -409,7 +409,7 @@ def run(
# Run YOLO tracking
results = yolo_model.track(frame, persist=True, classes=[0]) # Track only person class
if results[0].boxes.id is not None:
if results[0].boxes.is_track:
boxes = results[0].boxes.xyxy.cpu().numpy()
track_ids = results[0].boxes.id.cpu().numpy()

View File

@ -156,7 +156,7 @@ def run(
# Extract the results
results = model.track(frame, persist=True, classes=classes)
if results[0].boxes.id is not None:
if results[0].boxes.is_track:
boxes = results[0].boxes.xyxy.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()

View File

@ -169,7 +169,7 @@
" results = model.track(im0, persist=True)\n",
"\n",
" # Check if tracking IDs and masks are present in the results\n",
" if results[0].boxes.id is not None and results[0].masks is not None:\n",
" if results[0].boxes.is_track and results[0].masks is not None:\n",
" # Extract masks and tracking IDs\n",
" masks = results[0].masks.xy\n",
" track_ids = results[0].boxes.id.int().cpu().tolist()\n",

View File

@ -558,7 +558,7 @@ class Results(SimpleClass, DataExportMixin):
)
idx = (
pred_boxes.id
if pred_boxes.id is not None and color_mode == "instance"
if pred_boxes.is_track and color_mode == "instance"
else pred_boxes.cls
if pred_boxes and color_mode == "class"
else reversed(range(len(pred_masks)))
@ -568,7 +568,7 @@ class Results(SimpleClass, DataExportMixin):
# Plot Detect results
if pred_boxes is not None and show_boxes:
for i, d in enumerate(reversed(pred_boxes)):
c, d_conf, id = int(d.cls), float(d.conf) if conf else None, None if d.id is None else int(d.id.item())
c, d_conf, id = int(d.cls), float(d.conf) if conf else None, int(d.id.item()) if d.is_track else None
name = ("" if id is None else f"id:{id} ") + names[c]
label = (f"{name} {d_conf:.2f}" if conf else name) if labels else None
box = d.xyxyxyxy.reshape(-1, 4, 2).squeeze() if is_obb else d.xyxy.squeeze()
@ -733,7 +733,7 @@ class Results(SimpleClass, DataExportMixin):
elif boxes:
# Detect/segment/pose
for j, d in enumerate(boxes):
c, conf, id = int(d.cls), float(d.conf), None if d.id is None else int(d.id.item())
c, conf, id = int(d.cls), float(d.conf), int(d.id.item()) if d.is_track else None
line = (c, *(d.xyxyxyxyn.view(-1) if is_obb else d.xywhn.view(-1)))
if masks:
seg = masks[j].xyn[0].copy().reshape(-1) # reversed mask.xyn, (n,2) to (n*2)

View File

@ -76,7 +76,7 @@ class AIGym(BaseSolution):
self.extract_tracks(im0) # Extract tracks (bounding boxes, classes, and masks)
tracks = self.tracks[0]
if tracks.boxes.id is not None:
if tracks.boxes.is_track:
track_ids = tracks.boxes.id.cpu().tolist()
kpt_data = tracks.keypoints.data.cpu() # Avoid repeated .cpu() calls

View File

@ -110,7 +110,7 @@ class Heatmap(ObjectCounter):
self.display_counts(plot_im) # Display the counts on the frame
# Normalize, apply colormap to heatmap and combine with original image
if self.track_data.id is not None:
if self.track_data.is_track:
normalized_heatmap = cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
colored_heatmap = cv2.applyColorMap(normalized_heatmap, self.colormap)
plot_im = cv2.addWeighted(plot_im, 0.5, colored_heatmap, 0.5, 0)

View File

@ -168,7 +168,7 @@ class ObjectCounter(BaseSolution):
self.annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
is_obb = getattr(self.tracks[0], "obb", None) is not None # True if OBB results exist
if is_obb and self.track_data and self.track_data.id is not None:
if is_obb and self.track_data and self.track_data.is_track:
self.boxes = self.track_data.xyxyxyxy.reshape(-1, 4, 2).cpu()
self.annotator.draw_region(

View File

@ -172,7 +172,7 @@ class BaseSolution:
)
self.track_data = self.tracks[0].obb or self.tracks[0].boxes # Extract tracks for OBB or object detection
if self.track_data and self.track_data.id is not None:
if self.track_data and self.track_data.is_track:
self.boxes = self.track_data.xyxy.cpu()
self.clss = self.track_data.cls.cpu().tolist()
self.track_ids = self.track_data.id.int().cpu().tolist()

View File

@ -194,7 +194,7 @@ while cap.isOpened():
result = model.track(frame, persist=True)[0]
# Get the boxes and track IDs
if result.boxes and result.boxes.id is not None:
if result.boxes and result.boxes.is_track:
boxes = result.boxes.xywh.cpu()
track_ids = result.boxes.id.int().cpu().tolist()
else: