Object Detection and Tracking with Ultralytics YOLOv8 | Episode 7
Вставка
- Опубліковано 22 сер 2023
- Join us for our seventh video in this series as Nicolai Nielsen shows you how to do object tracking with Ultralytics YOLOv8.
🔗 Colab Notebook: colab.research.google.com/git...
In this episode, you will learn how to use YOLOv8 to do object tracking live on a webcam and videos. Instead of just running object detection on individual frames, we can now track the objects over time between frames. We are going to see how to set it up and run it. We will also see some examples and results with object tracking.
🌟NEW: Register for YOLO VISION 2023: yolovision.ultralytics.com/
For more information, please visit:
Ultralytics ⚡️ resources
- About Us - ultralytics.com/about
- Join Our Team - ultralytics.com/work
- Ultralytics License - ultralytics.com/license
- Contact Us - ultralytics.com/contact
- Discord - ultralytics.com/discord
YOLOv8 🚀 resources
- GitHub - github.com/ultralytics/ultral...
- Docs - docs.ultralytics.com/ - Наука та технологія
Awesome! 😃
Hi, does the tracking work if the object changes class as time goes? For example for the football field dataset if my labels were "player static" and "player running" would the tracking be able to follow the same player as it changes states? Thank you!
If there is a modification to the class, the tracking ID will undergo an automatic update.
Thanks
Ultralytics Team
Are you guys doing a YOLO VISION conference again this year? I would love to attend and/or give a talk!
Absolutely, YV24 is tentatively scheduled for September 2024. If you're interested in attending, please provide your email, and our team will get in touch with you :)
Thanks,
Ultralytics Team!
Hello Ultralytics team, does the bytetrack algorithm runs on GPU? Or the box data is bought back onto CPU and then the tracking algorithm operates on CPU? Does track uses detection in between at regular intervals?
The ByteTrack algorithm is not limited to GPU usage; it is compatible with both GPU and CPU environments. When ByteTrack runs on GPU, the bounding box data is converted into GPU tensors for processing on the GPU. Conversely, in the case of running on CPU, all bounding box data is processed on the CPU.
It's important to note that ByteTrack utilizes object detection data for its processing.
@@UltralyticsThanks very much for your quick response. When you mean the bytetrack utilising object detection result, does it happen at every frame or the detector will pitch in for every N frames where N is a fixed number?
Both possibilities are available, and it depends on the feature matrix as well. Generally, the detection results of each frame will be passed to the tracker for processing.
hi just curious does ultralytics has speed estimation for moving objects?
Yes, it's feasible to estimate the speed of moving objects, such as cars or people in motion.
Thanks
Ultralytics Team!
what GPU do i need to have, to be able to run yolov models (at least the smallest one nano version) with minimal delay on streams, to achieve something close to realtime stream??
You can use Nvidia GPUs such as the RTX 3050 Ti or 2080 Ti.
HHi Ultralytics team, I would like to limit yolov8 to bound only the object with the highest confidence score on the screen at a time. How can I do that? And, I realized that If I made a query in code such as using 'IF' blocks, FPS is down drastically from 22 to 12. I wonder what is the reason for this?
You can adjust the `conf` values based on your requirements without impacting the FPS or requiring additional If-else conditions.
For instance, if you wish to identify objects with a confidence value greater than 0.7, you can achieve this by using the following command:
```yolo task=detect mode=predict source="path/to/video/file.mp4" conf=0.7```
Thanks
Hello Sir, how can I resize the "show=True" window?
You can use imgsz=320 to specify the image res used
did you do any labelling before abour the dancing girls? how does it work without labelling?
The video utilizes the pre-trained Ultralytics YOLOv8 model. It hasn't undergone fine-tuning with custom data, and there's been no annotation of data. The person class is already included in the pre-trained model.
Ultralytics requirement ['lapx>=0.5.2'] not found, attempting AutoUpdate...
I am getting this error while running the bytetracker. Is any installation need for bytetracker?
Well, this is not an error, it's the warning and Lapx will be automatically installed. It will take a few seconds, but the code will run smoothly, and there will be no error. Thanks
Hello ultralytics team, Does this(tracking )only work for the yolov8 detection models or do segmentation yolov8 models work too with tracking
Offcourse :) Object Tracking is compatible not only with Object Detection but also with Object Segmentation.
Is the object tracking in yolov8 using deepsort so it can tracking the object ? And i wonder how can i use this tracker with yolov8 to make a camera that will folllow a person and make the object stay in the center
Yes, YOLOv8 can use DeepSORT for object tracking. To make a camera follow a person and keep them centered, you can integrate YOLOv8 with DeepSORT and adjust the camera based on the tracking information.
Hi, I collected data with video to use in my project. Can I use this video to train my model or do I need to train the model using only photos? If I can use it, how should I label the data in this video? I would appreciate it very much if you could help me on this issue.
To train the Ultralytics YOLOv8 model on custom data, you'll need to gather various images and annotate them. Subsequently, you can utilize these images to fine-tune the model for custom data. Thank you.
Helloo... If I want to track peoples in a region of interest or crossing a line can I use the same method.
Yes, you can utilize the same approach! In fact, we've included an example for object counting in specific regions. You can explore it by visiting the following link: github.com/ultralytics/ultralytics/tree/main/examples/YOLOv8-Region-Counter
never seen an expert spending to much time to answer question on a youtube channel. thank you so much. this is super helpful for me, a newbie@@Ultralytics
How to import Ultralytics on visual like you, do I need to download any libraries? Please help me
Hi there! You can get started by executing a single command in your terminal: "pip install ultralytics"
results = model.track. How do you overcome the issue about the missing lap imports? ModuleNotFoundError: No module named 'lap'
Hi there! You can easily install lap package by using the following command: "pip install lapx".
@@Ultralytics thank you.. it is working now after I pip install lap
Can we change the id start from 1 for each class? For example, there is three object detected in image and the id by default is helmet: 1, person: 2, jacket: 3. Can we change it to helmet: 1, person: 1, jacket: 1 ? Thank you.
Indeed, you can achieve this by adjusting the tracker initialization _count value to 1. The necessary modifications can be found at: github.com/ultralytics/ultralytics/blob/59ed47c4482a09d0261b8801791258a241e562d8/ultralytics/trackers/basetrack.py#L55
@@Ultralytics I have tried adjusting the _count value to 1 but there is no change. In one frame, the ID for each class remains helmet: 1, person: 2, jacket: 3 and does not change to helmet: 1, person: 1, jacket: 1.
There is something issue in yolo object while giving live feeding by live feeding I means( frame to frame data) it is not been able to hold ID of object properly every new frame comes it will assign new ID, is there solution around for it
To maintain the object ID across multiple frames, ensure you use the "persist=True" parameter in the algorithm. For example:
```
yolo track source="path/to/video.mp4" persist=True
```
Thank you!
@@Ultralytics thank you , I was creating project that calculate speed of the vehicle in real time, then I searched on google related to it I found out we need to give two static points in the video and then calculate according to it but I am not sure how precise it is as we don't known the angle in which camera is being hold so distance will not be precise as I am calculating using euclidean distance, is there another way to solve this problem ?
Hello, how do I do it if I want to replace it with a model that I made myself, I'm confused about which one to change, help me☹️
If you want to utilize a fine-tuned model, you can use the mentioned code below.
```python
from ultralytics import YOLO
model = YOLO("path/to/custom/model.pt")
results = model.track(source="path/to/video.mp4")
```
@ultralytics how do count the object in the frame while tracking do I have to count the track IDs? Can you provide me the code please help and how can I count object with their class.
The code for object counting is indeed available in our Documentation: docs.ultralytics.com/guides/object-counting/#__tabbed_1_1
Thanks 💙
can you apply the source as real time stream data chart?
you can use a live video stream as a source. We offer support for inference on videos, images, and real-time streams. Thanks
could you please help me with how i can get coordinates of the bounding boxes ?
Certainly, you can utilize the provided code for extracting bounding box coordinates. Alternatively, you can find detailed guidance in our instructional video on this subject, available at the link: ua-cam.com/video/QtsI0TnwDZs/v-deo.html.
"""
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
# Perform inference on an image
results = model('ultralytics.com/images/bus.jpg')
# Extract bounding boxes, classes, names, and confidences
boxes = results[0].boxes.xyxy.tolist()
classes = results[0].boxes.cls.tolist()
names = results[0].names
confidences = results[0].boxes.conf.tolist()
# Iterate through the results
for box, cls, conf in zip(boxes, classes, confidences):
x1, y1, x2, y2 = box
confidence = conf
detected_class = cls
name = names[int(cls)]
"""
i want to print the results on my terminal with coordinates
btw i am asking it for live detection using web cam
You can ask your queries at Ultralytics Github Issues: github.com/ultralytics/ultralytics/issues
In the real-time video, the same white color chair is again tracked with different IDs when it is out of the camera. But that is the same chair, which must be tracked with the same ID for the entire video capturing. How to solve that.
To ensure consistent tracking with the same ID throughout the video, you can implement object tracking algorithms that maintain object identity across frames, such as Kalman filters or centroid tracking. These methods help associate the correct ID with the object, even when it temporarily disappears from view.
Thanks :)
Hello, how can I create a timer for detected object?
The timer functionality can be achieved using the time.time() function. You'll need to implement a custom solution based on your specific requirements. Thank you.
How can i draw trails for vehicles while tracking?
Sure, you can use mentioned code to draw trails during object tracking with Ultralytics YOLOv8.
"""
import numpy as np
from pathlib import Path
from ultralytics import YOLO
from custom_functions import *
from collections import defaultdict
from ultralytics.utils.plotting import Annotator
track_history = defaultdict(lambda: [])
# Path to model file
model = YOLO("yolov8s.pt")
names = model.model.names
# Path to video file
video_path = "path/to/video.mp4"
if not Path(video_path).exists():
raise FileNotFoundError(f"Source path {video_path} does not exist.")
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True, verbose=False)
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
track_ids = results[0].boxes.id.int().cpu().tolist()
annotator = Annotator(frame, line_width=2, example=str(names))
for box, track_id, cls in zip(boxes, track_ids, clss):
# Draw bounding box
annotator.box_label(box, label=names[int(cls)], color=(0, 255, 0))
track = track_history[track_id]
track.append((float(box[0]), float(box[1])))
if len(track) > 30:
track.pop(0)
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
# Draw trails
cv2.polylines(frame, [points], isClosed=False, color=(37, 255, 225), thickness=1)
cv2.circle(frame, (int(track[-1][0]), int(track[-1][1])), 5, (218, 100, 255), -1)
cv2.imshow("Ultralytics YOLOv8 Object Detection with Tracking", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
"""
Thanks
How do I store the information of the tracked objects?
You can use the mentioned code below to store the track information in the JSON file.
""
import cv2
import json
from ultralytics import YOLO
model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
bounding_boxes_data = []
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
boxes = results[0].boxes.xyxy.cpu()
clss = results[0].boxes.cls.cpu().tolist()
track_ids = results[0].boxes.id.int().cpu().tolist()
if results[0].boxes.id is not None:
for box, tid, cls in zip(boxes, track_ids, clss):
print(f"Track ID {tid}, Bounding Box {box}")
bounding_boxes_data.append(f"Track ID {tid}, Bounding Box {box}")
with open("track_data.json", "w") as json_file:
json.dump(bounding_boxes_data, json_file, indent=4)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
""
Thanks
@@Ultralytics Thank you!
Hello, I have followed the steps in the video, but my model is not readable, what should I do, help me?
Can you please provide the error logs. Thanks
I fine-tuned the pretrained yolov8n model to road damage detection dataset. However, when I run the tracking command, the ids of the object in the video are not shown. Could you please tell me how to show the ids?
Certainly, you can show the object tracking ID by following the link mentioned, which leads to the relevant answer to your query.
github.com/ultralytics/ultralytics/issues/5278
How can i detect cars only?
To detect the car, you can modify line 7 in the code (shown in the UA-cam video) as follows "results = model.track(source='gymnasts.mp4', classes=2)."
For more Information, you can check the Ultralytics Docs: docs.ultralytics.com/modes/track/#tracking
Can you please share the files? (model & video)
You can get started with everything at github.com/ultralytics/ultralytics
hello i wanna config custom in tracker but it looklike not corectly ( i wanna change color of box and not show conf and id )
results = model.track(frame, persist=True,tracker='person.yaml')
person.yaml
more than comman like
device: cpu # device to run the tracker, ['cpu', 'cuda']
visualization:
draw_id: false
box_color: [0, 255, 0] # BGR color, e.g., green
line_width: 2 # width of the bounding box and id
show_conf: false # show confidence
Your inquiry appears to be more technical. We suggest posting your questions in our GitHub Issues section: github.com/ultralytics/ultralytics/issues
How can you make the model detect the object and give voice feedback of the object it detects
Well, to incorporate voice feedback, consider utilizing third-party libraries, as Ultralytics does not offer support for voice functionalities.
@@Ultralytics thankyou for the reply. Does yolov8 support transfer learning. How do i make it such that im able to detect more object classes on top of the classes present in pretrained model.
@@yaseenpk1739 If you fine-tune the YOLOv8 model on a custom dataset, the pretrained classes will no longer be detected, and the model will exclusively recognize your custom classes.
@@Ultralytics i think there was some misunderstanding. I want the pretrained model to detect the objects (80 object classes) along with some extra object classes. The pretrained model already has the objects that i want to get detected but i want to add more objects to that pretrained model
Why is the ID number always updated? I want to keep the ID number constant in every frame, how can I do it?
The ID number may be subject to updates in situations involving an unstable camera or object position. Generally, Ultralytics supports two tracking methods:
- ByteTrack
- BotSort
You can opt for either, depending on your requirements, and adjust their parameters to enhance ID association and consistency.
Ultralytics Object Tracker's configuration: github.com/ultralytics/ultralytics/tree/main/ultralytics/cfg/trackers
Ultralytics Object Tracking Docs: docs.ultralytics.com/modes/track/
Sometimess error happens if one particular entity got assigned multiple ids ,reasons can be camera flactuation or any other factor.
Thank you, @warrior_1309, for providing the information. We will investigate and address any issues that have been identified from yourside.
How do I dynamic get the new person id,and append it to the list.
Hi, for your questions, we recommend joining our Discord server and asking the community for help. Please find out Ultralytics Discord server here: ultralytics.com/discord
@@Ultralytics thank you
@@Ultralytics Ihave joined,but Ican discuss with others.
@@March_Awake be sure to read the rules and assign yourself a role! Once you've done this, you'll have access to the rest of the server.
@@Ultralytics Thank you,I will try it.
Hello Ultralytics how can I count the the object by their class like
People:4
Car:3
Currently, classwise object counting is not supported, but we have plans to introduce this feature in the future. You can find the object counting code available at: docs.ultralytics.com/guides/object-counting/
Thanks💙
@@Ultralytics could you verify his code iam getting the count of object with thier class in the terminal but i cant display the count in the opencv window
import cv2
from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator
# Load the YOLOv8 model
model = YOLO("yolov8n.pt")
names = model.model.names
# Open the video file
video_path = "Path/to/video/file.mp4"
cap = cv2.VideoCapture(video_path)
assert cap.isOpened(), "Error reading video file"
# Define the window name
window_name = "YOLOv8 Inference"
# Set the size of the window
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, 800, 600) # Set the desired width and height
# Dictionary to store the count of each class
class_count = {name: 0 for name in names}
while cap.isOpened():
success, im0 = cap.read()
if success:
results = model.predict(im0, show=False)
boxes = results[0].boxes.xyxy.cpu().tolist()
clss = results[0].boxes.cls.cpu().tolist()
annotator = Annotator(im0, line_width=3, example=names)
if boxes is not None:
for box, cls in zip(boxes, clss):
cls_name = names[int(cls)]
annotator.box_label(box, color=(255, 144, 31), label=cls_name)
# Increment count for the detected class if it exists in the class_count dictionary
if cls_name in class_count:
class_count[cls_name] += 1
# Display the count of each class on the frame
text_y = 30
for name, count in class_count.items():
cv2.putText(annotator.im, f"{name}: {count}", (10, text_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
text_y += 30
# Display the total number of items detected
total_items = sum(class_count.values())
cv2.putText(annotator.im, f"Total items: {total_items}", (10, text_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Display the annotated frame
cv2.imshow(window_name, annotator.im)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
continue
print("Video frame is empty or video processing has been successfully completed.")
break
cap.release()
cv2.destroyAllWindows()
Yesterday, we just released classwise object counting feature. You can simply upgrade the ultralytics package with `pip install -U ultralytics` and follow docs.ultralytics.com/guides/object-counting/#__tabbed_1_2
@@Ultralytics thanks
how can ı count the people in webcam with yolo
The code for object counting can be found in our documentation at this link: docs.ultralytics.com/guides/object-counting/
where can i see the source code and how can i get the yaml file?
We are creating colab notebooks that will include the codes for our UA-cam videos, we will share them soon.
The sample YAML file is available at the link: docs.ultralytics.com/datasets/detect/coco/#dataset-yaml
Thanks
@@Ultralytics where can i find the bytetracker.yaml tho ?
github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/trackers/bytetrack.yaml
Hey how to recognise face and label the name of the person and track it
To recognize and label faces while tracking, you can use face recognition libraries like OpenCV and Dlib. Implement face detection, then employ a face recognition algorithm to associate names. For tracking, consider using object tracking methods provided by Ultralytics YOLOv8: docs.ultralytics.com/modes/track/
Thanks
How to count the each class of objects in the frame using this?
To count the number of objects, you can easily determine it by counting the length of the results. Here's an example:
```python
results = model.track(source=im0, ....)
print(len(results[0].boxes.xyxy.cpu()))
```
Thanks
@@Ultralytics thankyou. I have noticed that you are giving reply to all comments really appreciate your effort. And I love yolo it's intriguing to work with it 👍♥️
Thanks, We regularly interact with the community to solve their problems.
Can you change the name from person to gymnast?
Directly, it's not possible. The model will utilize the names you specified during training.
we trained model for class up and class down , what that id symbolize basically
During object tracking, each object receives a distinct ID, which can be utilized for tracking purposes. Thanks
show=True doesnt work on my side. No window pops up
This might be possible! What is the error logs code showing?
Can re_id for this project SIR!!
Thanks for your comment! ReID is currently available with a short buffer length, so if an object leave sthe scene for a few frames it may be successfully reaquired. Longer-lived ReID is a work in progress and hopefully a feature we will release in 2024.