Comments (5)
Hello! It sounds like you're looking to extract and save trajectory and tracking details into a CSV file at a specific frame rate. Here's a concise way to achieve this with the YOLOv8 model:
You'll need to run the tracker, collect results, and then save the relevant data (ID, bounding box coordinates, frame number) to a CSV.
import csv
from ultralytics import YOLO
# Load your model
model = YOLO('path/to/model.pt')
# Setting up CSV to save the data
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Frame", "ID", "X", "Y", "Width", "Height"])
# Process video and track frame by frame
for frame_idx, (path, im, im0s, vid_cap, s) in enumerate(model.track(source='path/to/video.mp4', stream=True)):
if frame_idx % 20 == 0: # capture every 20 frames
for *xyxy, conf, cls, track_id in im0s["tracking"]:
if int(cls) == 0: # class '0' for people, change this based on your class labels
x1, y1, x2, y2 = map(int, xyxy)
width, height = x2 - x1, y2 - y1
writer.writerow([frame_idx, track_id, x1, y1, width, height])
# This script assumes that you are tracking 'people', as denoted by class '0'.
This script will save every 20th frame's tracking info into output.csv
, adjusting for the class label that corresponds to 'people' in your model. Update class labels as necessary for your specific dataset. Adjust the path to your model and video accordingly. Happy tracking! 🚀
from ultralytics.
Thank you so much!
from ultralytics.
You're welcome! I'm glad I could help. If you have any more questions or need further assistance as you work with YOLOv8, feel free to ask. Happy coding! 😊
from ultralytics.
You're welcome! I'm glad I could help. If you have any more questions or need further assistance as you work with YOLOv8, feel free to ask. Happy coding! 😊
Thank you for your time in advance. There is still two questions in print save trajectory and tracking details.
Firstly, I found add the ‘save_txt’ to ‘results = model track( )’ can put the text as s shown in the figure below. The last column shows all the pedestrian IDs recognized for each frame, but I'd like to also be able to derive what frame the recognition resulted in. I should know the time factor later. Is there anything that can be done?
Secondly, in order to get the trajectory and show all the pedestrian trajectories which are recognized within a period of time in the picture, How should it be done? Is there a better way than the previous one (my plan was to get the coordinate information of all pedestrians in each frame and concatenate the coordinates of pedestrians with the same id, but in reality, when the video is outputted, the trajectory is actually outputted)
from ultralytics.
@ArielZhanghj hello! I'm here to help with your questions about tracking and trajectory analysis using YOLOv8. 😊
-
Frame Information in Output: To include frame information in your output, you can modify the tracking loop to write the frame index along with the detection details. Here’s a quick example of how you might adjust your code:
for frame_idx, (path, im, im0s, vid_cap, s) in enumerate(model.track(source='video.mp4', stream=True)): results = model.track(im, save_txt=True) # Save or process results here, including frame_idx which is your frame number
This will allow you to keep track of the frame number alongside the detections.
-
Plotting Trajectories: Your approach to concatenate coordinates of pedestrians with the same ID over frames is correct. To visualize trajectories, you can use libraries like
matplotlib
orcv2
to draw lines or curves that connect these points across frames. Here’s a basic way to plot trajectories using OpenCV:import cv2 import numpy as np # Assuming 'tracks' is a dictionary where keys are track_ids and values are lists of (x, y) coordinates for frame in video_frames: for track_id, coordinates in tracks.items(): for i in range(1, len(coordinates)): cv2.line(frame, coordinates[i - 1], coordinates[i], color=(0, 255, 0), thickness=2) cv2.imshow('Trajectories', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
This method should help you visualize pedestrian paths effectively. If you need more detailed guidance or further assistance, feel free to ask. Happy tracking! 🚀
from ultralytics.
Related Issues (20)
- How to run YOLOv8 predict on GPU without CUDA? HOT 2
- Why my result predicted object has no bounding box? HOT 2
- how to save and load my custom model HOT 6
- segmentation fault(core dumped) HOT 1
- Parameter fusion HOT 3
- Install ultralytics with pre-installed Pytorch HOT 1
- argument save_dir doesn't work HOT 2
- Inference/postprocessing yolov8n on rockchip 3588 HOT 4
- I am looking for an Assisted Labeling tool where i can use my trained yolo models to assist during labeling process. HOT 1
- Instance segmentation custom model training HOT 2
- Fine-tuning YOLOv8n Model on Two New Classes HOT 2
- Exception with YOLOWorld ONNX export in dynamic mode HOT 3
- How cna I validate my model with no-square 【640*384(w*h)】image size? HOT 3
- RuntimeError:An attempt has been made to start a new process before the current process has finished its bootstrapping phase. HOT 5
- Bug report: FileNotFoundError due to special characters in pathnames at /ultralytics/data/base.py HOT 3
- Yolov9 pytorch model is empty, as in not the folder, the loaded pytorch model. HOT 1
- Model exported to CoreML showing 1.0 confidence values for all detections HOT 4
- YOLOv8-OBB learns 0° rotation HOT 12
- train with arg use_ray=True, raise exception OSError: [WinError 87], at _winapi.CreateProcess HOT 3
- New network structure with old weights HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ultralytics.