Closed lwensveen closed 11 months ago
š Hello @lwensveen, thank you for your interest in YOLOv8 š! We recommend a visit to the YOLOv8 Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.
If this is a š Bug Report, please provide a minimum reproducible example to help us debug it.
If this is a custom training ā Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.
Join the vibrant Ultralytics Discord š§ community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.
Pip install the ultralytics
package including all requirements in a Python>=3.7 environment with PyTorch>=1.7.
pip install ultralytics
YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.
@lwensveen your error indicates that boxes.id is None, not boxes itself. Your continue logic only checks whether boxes is None.
@glenn-jocher
I've tried adding other checks before, but the error persists:
if result.boxes is not None and result.boxes.id is not None:
if result.boxes is not None and hasattr(result.boxes, 'id') and result.boxes.id is not None:
continue
This is the first time I've ever written anything in python, so I'm sorry if I'm mistaken in my presumptions.
@lwensveen,
It seems that you are encountering an issue where AttributeError: 'NoneType' object has no attribute 'cpu'
occurs when an image has no detections.
It seems that your continue logic checks whether the result.boxes
attribute is None, but not whether either result
itself or the id
attribute of result.boxes
are None.
You might want to try incorporating the following if
statement to check id
attribute of result.boxes
as well.
if result.boxes is None or result.boxes.id is None:
I hope that helps!
@glenn-jocher
But that's what I did in my previous post? It didn't solve the issue, it still produced the same error.
@lwensveen,
I apologize for the confusion. It seems that my previous suggestion did not resolve the issue. Upon further analysis, it appears that the error occurs when result.boxes
is None, and you are trying to access the id
attribute of result.boxes
using result.boxes.id.cpu().numpy().astype(int)
.
To prevent this error, you need to modify your continue logic to include a check for the id
attribute of result.boxes
as well. You can do this by updating your existing if
statement as follows:
if result.boxes is None or result.boxes.id is None:
This updated check ensures that both result.boxes
and result.boxes.id
are not None before accessing the cpu()
method.
I apologize for any confusion caused by my previous response. Please try implementing this updated logic, and let me know if you encounter any further issues.
Thank you for your patience.
š Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.
For additional resources and information, please see the links below:
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!
Thank you for your contributions to YOLO š and Vision AI ā
It worked for me.
from collections import defaultdict import cv2 import numpy as np
from ultralytics import YOLO
model = YOLO('model_name.pt')
video_path = "" cap = cv2.VideoCapture(video_path)
track_history = defaultdict(lambda: [])
while cap.isOpened():
success, frame = cap.read()
if success:
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
for result in results:
if result.boxes is None or result.boxes.id is None:
continue
# Get the boxes and track IDs
else:
boxes = result.boxes.xywh.cpu()
track_ids = result.boxes.id.cpu().numpy().astype(int)
# Visualize the results on the frame
annotated_frame = result.plot()
# Plot the tracks
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
track = track_history[track_id]
track.append((float(x), float(y))) # x, y center point
if len(track) > 30: # retain 90 tracks for 90 frames
track.pop(0)
# Draw the tracking lines
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(0, 255, 0), thickness=10)
# Display the annotated frame
cv2.imshow("YOLOv8 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
cap.release() cv2.destroyAllWindows()
@TUday1998 hello!
From the provided code block, we can see that you're using the YOLOv8 model to track objects in a video file. You're persisting the tracking between frames and storing the track history. Only those results where a box and track ID successfully exist are considered.
The 'xywh.cpu()' method is used to derive a tensor containing bounding boxes, and track IDs are transformed to a CPU-placed, integer NumPy array. Afterwards, these are used to plot the tracks and draw tracking lines directly on the frame.
Your results are visualized with the '.plot()' method, and you let the tracking be displayed until the video finishes running or the user manually ends it.
As long as your specified model runs properly, the video file path is correct and your software environment is correctly configured, the code should work as expected. It provides a great example of tracking and visualizing object trajectories in a video using YOLOv8.
Let us know if any further clarification is needed. Thank you for sharing your work with us!
š Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.
For additional resources and information, please see the links below:
Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!
Thank you for your contributions to YOLO š and Vision AI ā
Search before asking
YOLOv8 Component
Detection
Bug
I'm using the Ultralytics YOLO library for object detection and tracking. However, I'm encountering an issue where my program crashes with an AttributeError when there are no detected objects in a frame.
Here's the error message I receive:
This error occurs when there are no detections (result.boxes is None). I thought I had correctly handled this scenario with the following code:
However, it appears that the AttributeError occurs regardless of this check. Could anyone provide insight into why this continue statement isn't preventing the error when result.boxes is None?
Environment
Minimal Reproducible Example
Additional
@AyushExel @Laughing-q
Are you willing to submit a PR?