Object Detection using yolov8
Last Updated :
18 May, 2024
In the world of computer vision, YOLOv8 object detection really stands out for its super accuracy and speed. It's the latest version of the YOLO series, and it's known for being able to detect objects in real-time. YOLOv8 takes web applications, APIs, and image analysis to the next level with its top-notch object detection. In this article, we will see how yolov8 is utilised for object detection.
Overview of YOLO
YOLO (You Only Look Once) is a game-changing object detection algorithm that came on the scene in 2015, known for its lightning-fast processing of entire images at once. YOLOv8 is the newest version, taking previous iterations and making them even speedier and more accurate. The YOLO evolution includes versions like YOLOv1, v2, v3, v4, and v5, each bringing improvements like real-time processing, batch normalization, and better detection accuracy. YOLOv8 brings in cutting-edge techniques to take object detection performance even further.
Key Features of yolov8:
YOLOv8 has brought in some key features that set it apart from earlier versions:
- Anchor-Free Architecture: Instead of the traditional anchor-based detection, YOLOv8 goes for an anchor-free approach. This change makes training simpler and helps the model work well with different datasets.
- Advanced Data Augmentation: By using techniques like MixUp and Mosaic, YOLOv8 toughens up the model and helps it work well in real-world applications. Mixing images in training provides diverse examples, boosting the model's accuracy and reliability.
- Adaptive Training: This feature lets YOLOv8 adjust the learning rate dynamically and balance the loss function more effectively during training, leading to optimized performance and higher detection accuracy.
- Self-Attention Mechanism: YOLOv8 brings in a self-attention mechanism, which helps the model understand the relationships and dependencies between different features in an image. This is especially handy for complex scenes where understanding context is key.
- Improved Backbone and Neck Architectures: The model uses state-of-the-art architectures for feature extraction, which are crucial for accurate object detection. These improvements help the model handle a wide range of object detection tasks efficiently.
- Efficiency and Speed: Despite all these advancements, YOLOv8 strikes a balance between accuracy and speed, making it perfect for real-time object detection applications.
YOLOv8's development is a major milestone in the world of computer vision, especially for object detection tasks. Its strong architecture and innovative features ensure that it remains a top choice for developers and researchers looking to implement efficient and accurate object detection in their applications.
Prerequisites:
We need to install packages and set up the environment to implement object detection using yolov8:
pip install ultralytics
For Conda users: conda install -c conda-forge ultralytics
Using docker: sudo docker pull ultralytics/ultralytics
And to set up the environment:
- Install Python: Ensure Python is installed on your system.
- Create a Virtual Environment: Use python -m venv yolov8-env in your terminal to create a virtual environment.
- Activate Virtual Environment:
Unix/macOS: source yolov8-env/bin/activate
Windows: .\yolov8-env\Scripts\activate
YOLOv8 also lets you use a Command Line Interface (CLI) to easily train models and run detections without needing to write Python code. It's great for those who like using commands directly.
To download the video we are using in this video: click here.
Implementing YOLOv8 for Object Detection
When it comes to spotting and tallying up vehicles, here's how we do it in three simple steps:
Step 1: Importing Necessary Libraries
All the necessary that will be used for our model are imported.
Python
import ultralytics
import supervision
import torch
import cv2
from collections import defaultdict
import supervision as sv
from ultralytics import YOLO
Step 2: Loading the pretrained model
By using this code we load the YOLOv8 (You Only Look Once version 8) model from the ultralytics
library to perform object detection on a video file (d.mp4
).
Here is a detailed explanation of each step and argument in the code:
Python
# Load a pretrained YOLOv8n model
model = YOLO('yolov8n.pt')
# Run inference on 'bus.jpg' with arguments
model.predict(source="d.mp4", save=True, imgsz=320, conf=0.5)
Step 3: Tracking the Model
This code use the YOLOv8 model to include object tracking on a video file (d.mp4
). Here's a detailed explanation of each step and the parameters used in the track
method:
Python
# Configure the tracking parameters and run the tracker
model = YOLO('yolov8n.pt')
results = model.track(source="d.mp4",conf=0.3, iou=0.5, save=True, tracker="bytetrack.yaml")
Step 4: Line Crossing Detection in Video using ByteTrack
The code loads a YOLOv8 model to track objects in a video (d.mp4) and detects when they cross a defined line. It captures and processes each frame, annotating tracked objects and counting those that cross the line. The annotated video with crossing counts is saved as output_single_line.mp4.
Python
# Load the YOLOv8 model
model = YOLO('yolov8n.pt')
# Set up video capture
cap = cv2.VideoCapture("d.mp4")
# Define the line coordinates
START = sv.Point(182, 254)
END = sv.Point(462, 254)
# Store the track history
track_history = defaultdict(lambda: [])
# Create a dictionary to keep track of objects that have crossed the line
crossed_objects = {}
# Open a video sink for the output video
video_info = sv.VideoInfo.from_video_path("d.mp4")
with sv.VideoSink("output_single_line.mp4", video_info) as sink:
while cap.isOpened():
success, frame = cap.read()
if success:
# Run YOLOv8 tracking on the frame, persisting tracks between frames
results = model.track(frame, classes=[2, 3, 5, 7], persist=True, save=True, tracker="bytetrack.yaml")
# Get the boxes and track IDs
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Plot the tracks and count objects crossing the line
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 30 tracks for 30 frames
track.pop(0)
# Check if the object crosses the line
if START.x < x < END.x and abs(y - START.y) < 5: # Assuming objects cross horizontally
if track_id not in crossed_objects:
crossed_objects[track_id] = True
# Annotate the object as it crosses the line
cv2.rectangle(annotated_frame, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)), (0, 255, 0), 2)
# Draw the line on the frame
cv2.line(annotated_frame, (START.x, START.y), (END.x, END.y), (0, 255, 0), 2)
# Write the count of objects on each frame
count_text = f"Objects crossed: {len(crossed_objects)}"
cv2.putText(annotated_frame, count_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# Write the frame with annotations to the output video
sink.write_frame(annotated_frame)
else:
break
# Release the video capture
cap.release()
Output:
The output is a mp4 file which will be stored in your environment, the image below represents how line crossing detection is implemented in the entire video.
OutputIn this article, we dove into advances of YOLOv8 in object detection. We talked about how it's super speedy, accurate, and versatile. YOLOv8 is a big deal in computer vision, opening up new possibilities for research and development. Its impact on stuff like autonomous vehicles and surveillance is huge, and there's tons of potential for more innovation and exploration in the field.
Similar Reads
YOLO v2 - Object Detection In terms of speed, YOLO is one of the best models in object recognition, able to recognize objects and process frames at the rate up to 150 FPS for small networks. However, In terms of accuracy mAP, YOLO was not the state of the art model but has fairly good Mean average Precision (mAP) of 63% when
7 min read
Object Detection with YOLO using TensorFlow YOLO, or "You Only Look Once," is a family of deep learning models that enable real-time object detection by treating the task as a single regression problem. Unlike traditional methods that apply detection across multiple regions of an image, YOLO detects objects in one pass, which makes it fast an
15+ min read
Object Detection using TensorFlow Identifying and detecting objects within images or videos is a key task in computer vision. It is critical in a variety of applications, ranging from autonomous vehicles and surveillance systems to augmented reality and medical imaging. TensorFlow, a Google open-source machine learning framework, pr
7 min read
Object Detection with YOLO and OpenCV Object Detection is a task of computer vision that helps to detect the objects in the image or video frame. It helps to recognize objects count the occurrences of them to keep records, etc. The objective of object detection is to identify and annotate each of the objects present in the media. YOLO(Y
6 min read
How does YOLO work for object detection? Object detection is a vital component of various computer vision applications, ranging from autonomous driving to security surveillance. One of the most popular and efficient algorithms for object detection is YOLO (You Only Look Once). YOLO revolutionized the field by providing real-time object det
6 min read
Gun Detection using Python-OpenCV Gun Detection using Object Detection is a helpful tool to have in your repository. It forms the backbone of many fantastic industrial applications. We can use this project for real threat detection in companies or organizations. Prerequisites: Python OpenCV OpenCV(Open Source Computer Vision Libra
4 min read
Yawn Detection using OpenCV and Dlib In this article, we'll cover all the steps required to build a Yawn detection program using OpenCV and dlib packages. But before doing this project should be familiar with the basics of OpenCV and you should also know how to use face detection and landmark detection using dlib module. Requirements:
2 min read
Object Detection Models One of the most important tasks in computer vision is object detection, which is locating and identifying items in an image or video. In contrast to image classification, which gives an image a single label, object detection gives each object it detects its spatial coordinates (bounding boxes) along
15+ min read
Real-Time Object Detection Using TensorFlow In November 2015, Google's deep artificial intelligence research division introduced TensorFlow, a cutting-edge machine learning library initially designed for internal purposes. This open-source library revolutionized the field, which helped researchers and developers in building, training, and dep
11 min read
YOLO : You Only Look Once - Real Time Object Detection YOLO was proposed by Joseph Redmond et al. in 2015. It was proposed to deal with the problems faced by the object recognition models at that time, Fast R-CNN is one of the state-of-the-art models at that time but it has its own challenges such as this network cannot be used in real-time, because it
6 min read