Object Detection and More using YOLOv8

real-time-object-detection-segmentation-key-point-extraction-using-yolov8-and-python

In my last post, I guided you how to detect object using YOLOv3. In this post, we will see how we can perform object detection using latest YOLOv8 model.

Background

You Only Look Once (YOLO) is the most prominent deep learning model architecture that has gained immense popularity in the field of computer vision. It is getting more popular day by day because of its accuracy and fast image processing capability.

Joseph Redmon first introduced YOLO model in 2015. Since then several versions of YOLO models (like v3, v5, v7) are released. Ultralytics recently launched YOLOv8 model on 11th of January 2023.

Those who do not know what is Ultranalytics? it is an AI platform who also published YOLO v3 and YOLO v5.

Also Read: Object Detection using YOLOv3

Why YOLO v8

If we have v3, v5, and v7 then why should we use YOLO v8? Let’s explore why.

The YOLO v8 is a cutting-edge, modern model with new features to boost performance and versatility. It supports both the CPU and GPU and is built using PyTorch (not darknet).

Because it is fast, accurate, and easy to use, YOLOv8 is a great option for a variety of tasks like object recognition, image segmentation, and image classification.

One thing to note that YOLO v8 is not the official version of YOLO. It is created by a company called Ultralytics. Last official version of YOLO was YOLO v7.

All the pre-trained models (5 models for each: object detection, image segmentation, image classification, and pose estimation. In total 20 models) are in one place. So that you can download and use it easily.

Also Read: Train YOLOv8 on Custom dataset in Windows GPU

YOLO versions

Currently, YOLO has different versions (official & unofficial). Listing down some popular versions below:

Official Versions
  1. YOLOv1: The original YOLO model introduced in 2015 by Joseph Redmon and his team. It is no longer maintained and has been superseded by newer versions of YOLO.
  2. YOLOv2: An improved version of YOLO that was released in 2016. It features a new architecture and several enhancements that improve its accuracy and speed. It is also no longer actively maintained GitHub link: https://github.com/pjreddie/darknet/tree/master/cfg
  3. YOLOv3: The most popular version of YOLO, released in 2018. GitHub link: https://github.com/pjreddie/darknet/tree/master/cfg
  4. YOLOv4: released in 2020. YOLOv4 introduces several new features such as CSPDarknet53 and SPP-blocks, and improves upon YOLOv3’s accuracy and speed. GitHub link: https://github.com/WongKinYiu/yolov4-darknet
  5. YOLOv7: published in 2022

Above versions are official releases of YOLO model. Along with those, there are some popular unofficial versions also. Some of them are:

Also Read:  Install OpenCV GPU with CUDA for Windows 10
Unofficial versions
  • YOLOv4-tiny model: research published in 2021
  • YOLOR (You Only Learn One Representation): published in 2021
  • YOLOX: published in 2021
  • YOLOv5: published by Ultralytics in 2022
  • YOLOv8: Recently published by Ultralytics in 2023

Also Read: Train YOLOv3 on Custom custom object detection in Windows GPU

Install YOLOv8

So let’s set up UltraAnalytics YOLOv8 to perform tasks like object detection, image segmentation, classification, and Key Point extraction in Windows 10. You can follow same steps for Google Colab or Linux also. I will break the entire configuration into some steps:

Step1: Create Virtual Environment

You must have Python 3.7 or above to use UltraAnalytics YOLO v8. It will be a good idea to create a fresh virtual environment with Python 3.7. If you are using Anaconda, you can create an isolated virtual environment using below command.

conda create -n ENV_NAME python=3.7
activate ENV_NAME

Step2: Install Ultalytics

This is the fun part. You no need to do anything manually. You just need to run the below command and YOLO v8 will be installed in your Windows system.

pip install ultralytics

Step3: Setup Jupyter notebook

I think we all love Jupyter Notebook to write Python scripts. To use Jupyter Notebook inside our virtual environment, run below three commands inside your Virtual env.

conda install jupyter
conda install nb_conda
conda install ipykernel

Step3: Validate Installation

Now we need to check whether we installed YOLO v8 correctly or not. To do that open Jupyter Notebook (inside virtual environment) and run below Python script.

from IPython import display
display.clear_output()

import ultralytics
ultralytics.checks()

Output

setup-yolov8-with-cpu-in-windows-10-jupyter

As you can see we have successfully installed Ultralytics YOLOv8 with CPU. If you want to install YOLOv8 with GPU, read this post.

Object Detection

Now let’s see how we can perform object detection using YOLOv8. The code is to detect different objects in an input image.

Since the YOLOv8 model is trained on top of COCO dataset, it can detect all listed objects as per COCO dataset labels (for example: car, person, etc.).

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  # load a pretrained model

# Use the model to detect object
model.predict(source="input_image.jpg", save=True, show=True)

In the above code at line 4, we are mentioning a pre-trained YOLOv8 object detection model name. The model will download automatically in your working directory.

After running above code, a folder named “runs” will be created in your working directory. The output image will be saved inside the folder runs > detect > predict

object detection using yolov8

As you can see it is correctly predicting all objects.

You can try another model also. Untralytics released five YOLOv8 pre-trained models for object detection. You can check their size and speed in the below image. To download the model manually, specify the directory path in line 4. You can find all pre-trained models here.

yolo-v8-object-detection-model-list

Similarly, you can also try pre-trained models for Image segmentation and Image classification. You just need to change the model name. That’s it.

Also Read:  Use Opencv with GPU with just 2 lines of code

Course for You: Deep Learning Specialization – Andrew Ng

Image segmentation

To perform image segmentation using YOLOv8 model you just need to change the model name in the code. It will download the model and perform image segmentation automatically.

# Image segmentation using YOLOv8
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-seg")  # load a pretrained model

# Use the model to detect object
model.predict(source="input_image.jpg", save=True, show=True)

This time the output image will be saved inside the folder runs > segment > predict

image-segmentation-using-yolov8

Classification

Again you just need to change the model name and it will classify the image for you. Below Python code is for image classification using YOLOv8 pre-trained model.

# Image clsssification using YOLOv8
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-cls")  # load a pretrained model

# Use the model to detect object
result = model.predict(source="input_image.jpg", save=True, show=True)

After running the above code you will get the class names with prediction probability in the Jupyter Notebook console like below.

image 1/1 D:\yolo_v8\input_image.jpg: 224x224 minivan 0.33, beach_wagon 0.26, pickup 0.09, car_wheel 0.07, jeep 0.06, 22.0ms
Speed: 1.0ms preprocess, 22.0ms inference, 0.0ms postprocess per image at shape (1, 3, 224, 224)
Results saved to runs\classify\predict

You can also find the saved output inside the folder runs > classify > predict. Class names with their probability will be printed on the image.

image-classification-using-yolov8

Pose Detection

Pose detection is also known as keypoint extraction. Below Python code is for pose detection with YOLOv8.

# Pose detection using YOLOv8
from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n-pose")  # load a pretrained model

# Use the model to detect object
result = model.predict(source="input_image.jpg", save=True, show=True)

You can find the saved output inside the folder runs > pose > predict.

pose-detection-using-yolov8

As you can see dots are the detected key points of the person. Now if you want to remove the bounding box and only show the key points, currently, you can not do with ultraytics. To do that you need to show output using OpenCV. I will make a separate tutorial for this.

Realtime

Now let’s see how we can implement above code for real time video. There are mainly two ways: 1. Using Webcam, 2. Using external video. Let’s see both of them.

Webcam Code

Below Python code is for real time yolov8 object detection using Webcam.

# For webcam
from ultralytics import YOLO

# Load custom trained YOLOv8 model
model = YOLO("runs/detect/train/weights/best.pt")

# Use the model to detect object
model.predict(source="0", show=True)

Here source = 0 means webcam.

Video Code

To do the same thing for external video, you just need to mention the video path instead of 0 in the source.

# For video
from ultralytics import YOLO

# Load a pr-trained model
model = YOLO("yolov8n.pt")

# Use the model to detect object
model.predict(source="input_video.mp4", show=True)

Now the above code is to identify or recognize objects in video (using YOLOv8 pre-trained model). If you want to do the same thing for image segmentation, you just need to change the model name in above code.

Also Read:  7 Best Text to Image AI Image Generators Free tool

Output Videos

Okay let me show you my output videos for real-time object detection, segmentation, and keypoint extraction (pose detection), you can implement for webcam also.

Real-time Object Detection using YOLOv8

Real-time Segmentation using YOLOv8

Real-time Key Point Extraction using YOLOv8

Conclusion

In this tutorial, I explained how to do object detection, Segmentation, Classification, and pose estimation or key point recognition using YOLOv8.

While implementing YOLOv8, I found that it is really fast. The most important thing is that you are getting lots of things (detection, segmentation, classification, keypoint extraction) in one place for YOLOv8.

Since YOLOv8 is use PyTorch in its backend, it is super easy to install even for custom object detection.

Now our YOLOv8 is using CPU as the backend. If you want to use YOLOv8 with GPU, please read this tutorial.

I find it very easy to implement. If you have any questions or suggestions regarding this post, please let me know in the comment section below.

Similar Read:

Leave a comment