Using Ultralytics YOLOv8 with SAHI on Videos

Ultralytics
4 min readSep 19, 2023

--

Object detection is a crucial aspect, particularly when handling small objects. However, it’s not uncommon for the model’s performance to fall short of expectations even after successful detection. There can be various factors contributing to this, such as the presence of blurry or noisy objects, limitations in dataset size, or image quality issues.

This is where the concept of SAHI comes into play. SAHI splits the image into slices, running the object detection model on each slice, and then consolidating the results from these slices to generate an output image. In this article, we will explore the process of implementing SAHI with Ultralytics YOLOv8 for analyzing video files.

Fig-1.1: Using Ultralytics YOLOv8 with SAHI on Videos

If you just want to run on images, you can check our guide Ultralytics Docs: Using YOLOv8 with SAHI for Sliced Inference.

To utilize SAHI with YOLOv8 for video analysis, you can embark on the following comprehensive steps. These instructions have been tested on multiple platforms, including Ubuntu 18.04, 20.04, and Windows 10.

  • Installation of required libraries
  • Inference with YOLOv8
  • Inference with YOLOv8 and SAHI
  • Speed and Accuracy Comparision of SAHI with YOLOv8

Installation of Required Libraries

Begin by setting up the essential modules and dependencies to ensure a seamless workflow. This step is crucial for preparing your environment for subsequent inference tasks.

Clone the Ultralytics repository and navigate to the cloned directory using the provided command below.

# Clone the ultralytics repository
git clone https://github.com/ultralytics/ultralytics/tree/main

# Navigate to the cloned directory
cd ultralytics

Execute the provided command to install the Ultralytics and SAHI packages.

# Install the ultralytics package
pip install sahi ultralytics

Inference with YOLOv8

After completing the module installation, you can proceed with performing inference using the YOLOv8 model. This stage will involve the detection and identification of objects in different videos, utilizing the power and capabilities of YOLOv8, and verifying If the ultralytics package is installed correctly.

You can use the mentioned command below for inference using YOLOv8.

# Inference command for YOLOv8
yolo task=detect mode=predict source="https://youtu.be/8r8hYenr5j8" \
model=yolov8n.pt show=True
Object Detection Using YOLOv8 Ultralytics (Inference)
Fig-1.2: Object Detection Using Ultralytics YOLOv8 (Inference)

Inference with YOLOv8 and SAHI

You can proceed with the steps outlined below to perform inference with SAHI using YOLOv8. Begin by navigating to the examples directory using the following command:

# Move to the examples directory
cd examples

# Move to SAHI code directory
cd "YOLOv8-SAHI-Inference-Video"

Use the mentioned command to run the SAHI on a video file.


# The --save-img flag is used to indicate that you want to save the results
python yolov8_sahi.py — source “path/to/video.mp4” — save-img

# If you want to change weights file
python yolov8_sahi.py - source "path/to/video.mp4" - save-img \
--weights yolov8n.pt
Fig-1.3: Object Detection Using YOLOv8 and SAHI (Inference)

Speed and Accuracy Comparision

The crucial aspects of computer vision models revolve around the balance between speed and accuracy, and it’s important to take several key considerations into account:

  • SAHI with YOLOv8 excels in superior object detection by dividing images into segments and applying the detection model sequentially. This method, while effective, is hardware-intensive and not suitable for real-time processing. In contrast, YOLOv8 operates efficiently on a CPU, providing faster performance compared to the SAHI integration.
  • Certain use cases and applications prioritize accuracy over speed, where the precision of results is paramount. In such scenarios, SAHI emerges as an ideal technique to achieve favorable outcomes.
  • With SAHI, the accuracy is directly impacted by the image slices created, with greater slices leading to improved precision. However, this results in slower inference speeds due to the increased computational demands of processing more slices.

In essence, striking the right balance between speed and accuracy is important when choosing the best methodology for computer vision tasks.

Thank you for joining us on this exploration of “Using Ultralytics YOLOv8 with SAHI on Videos”.

Feel free to try it out in your own video content; we look forward to seeing your results!

Ultralytics
Ultralytics

Ultralytics is on a mission to empower people and companies to unleash the positive potential of vision AI. With a commitment to simplicity, flexibility, and usability, Ultralytics YOLO and HUB lower the barriers to getting started solving the world’s challenges. Ultralytics technology enables businesses to transform their operations, enhance customer experiences, and drive innovation by seamlessly integrating state-of-the-art models and user-friendly platforms. It’s been a remarkable journey, but we’re just getting started.

--

--