Hello everyone.
This video tutorial.
We will see how we can use YOLO V8 for segmentation on images and videos.
So this is a complete step by step guideline, so don't skip any part of the video and do watch the
complete video.
So YOLO V8 comes with five different models.
So starting from your V8 segmentation and ending to YOLO, V8 X segmentation, YOLO V8 and segmentation
is less accurate, but it is more fast than other YOLO V8 model, while YOLO V8 X is the most accurate
model in the YOLO V8 series, but it is not very much fast as compared to the other YOLO V8 model.
So we can say that YOLO v8 n is the fastest, but it is less accurate while your YOLO v8 X is the most
accurate, but it is in less fast than other YOLO V8 models.
So here we will see how we can implement YOLO V8 segmentation in Google CoLab.
Okay, so first of all I am just importing the required library from IPython dot display import image
importing this library.
Basically we use this library if we want to display any input or output image in our Google CoLab notebook.
So then I am installing ultralytics.
So yolo.
It can be implemented in two ways by cloning the GitHub repo of YOLO V8 which is over here.
So you can simply go over here and just clone this GitHub repo or you can install the package of YOLO
V8 using ultralytics.
So.
Week so we can simply implement YOLO V8 by installing the package.
Ultralytics.
So here I'm just so if you want to simple train test or validate your model so it's always better to
implement YOLO V8 by installing the package.
Ultralytics.
Or if you want to make some changes in the predict.py or in the training.py file.
So then it's all you need to clone the github repo.
For example, if I want to add a speed estimation or distance calculation code in the predict.py file,
then I need to clone the github repo while in case of simple train test validation you just need to
install the package ultralytics to implement YOLO v8.
So here I'm just doing it.
So before running the script please make sure that you are selecting the runtime as GPU.
Then just run this cell those two cells.
So.
Okay, so this might take a few seconds more.
Okay, So we have checked that whether the GPU is available or not.
So I think this should be at the start.
Okay, So just make it a start by just going over here and change runtime.
Please make sure that you have GPU.
Then I will import torch.
Basically YOLO v8 is built using PI torch, so we need to uh.
So we need to have a PI torch set in our Google colab notebook.
To do this, we import that torch and here we can see that we have the GPU available and the torch version
is 1.13 point one and 2.16.
So basically to test the segmentation model on images or videos, let's download some images and video
from Google Drive into our Google CoLab notebook.
So I'm just downloading a sample image and video from Google Drive into my Google CoLab notebook and
then I will run segmentation on this sample image which we have downloaded.
So just running the segmentation on this sample image and let me show you the output image which we
get.
So you can see that as I'm performing segmentation.
So I've written toss is equal to segmentation, mod is equal to prediction.
And here we have the YOLO V8 Pre-trained model path over here.
So you just need to write the model name.
It will automatically download it from the GitHub repo into the Google CoLab notebook.
So now my results are saved in runs per segmentation predict.
So this is the place where my results are saved.
Let me show you like runs segment predict.
And here is my output image which I have displayed over here.
So you just need to copy path and just paste it over here.
And if you run this you will get this output image so you can see that we are able to detect the person
and we have a mask around it as well.
Like you can see this uh, pink color mask and you can see this green color mask on the bus.
So, uh, you can see that we are able to do the detection as well as the segmentation as well.
Okay?
So if you want to hide these labels and this confidence value, basically 0.91 represents the confidence
value person is the label.
So confidence value means how much model is showed that this is the person.
So if I want to hide this confidence value, you can just simply write hide dash labels, hide dash
label will hide the person label, which is over here, or backpack label over here and hide dash Confidence
will hide this confidence value, which is 0.91 over here and 0.53 over here and 0.54 over here.
So let me show you.
If you set hide dash labels, it will hide this label person and hide dash.
Confidence will hide the confidence value over here.
So this might take a few seconds.
And here is our output image you can see over here.
So now you can see that we don't have any label or the confidence value.
So similarly we can run segmentation on any video as well.
So let's run the segmentation on a video.
So this might take a few seconds for the execution, but let's wait and see.
What results do we get.
So you can see that the model is able to detect the cars.
So let's divide the complete video into 1314 frames and it is doing the detections frame by frame.
So we can see eight cars, one truck detected in this frame, three cars, five cars, three trucks
are detected in this frame and five cars are detected also in this frame.
Similarly, four cars, one truck is detected in this frame.
And here we are detected six cars.
So this might take some take some time for the execution.
So let's wait until the execution gets complete and then we proceed further.
So.
Okay, so.
The processing on the video is done and our results are saved in runs.
Segment Predict three And here is our output video.
So I have already displayed the output.
I have already previously run the script, so if you display the output video it will be like this.
So let me just download this output video and show you how our results look like.
So let me just navigate my screen towards this output video.
So just give me a minute.
Okay.
I'm just navigating my screen.
Okay, So now in front of you, you can see the output video over here in front of your screen.
So I think you can see the output video over here.
Okay?
So you can see that we are able to do that segmentation as well.
Like you can see the mask as well.
So you can see that here we have implemented a semantic segmentation like when there is a car over here,
you can see that we have the same color of the bounding box and the same color of the mask as well,
while in the case of truck or any other object, uh, so the same object have the same color of the
bounding box and the same mask.
Like you can see that car and foot truck as well, like the same color of the bounding box and the same
color of the mask as well.
Like you can see here, the trucks over here, they have the same color of the bounding box, which
is a green and the same color of the mask as well.
While for the car they have the same color of the bounding box and the same color for the mask as well.
So that's great.
So if we go towards the CoLab file again over here, so now we can explore the YOLO VR segmentation
model in the Onnx format.
So if you have some Pre-trained model, you can also export in the Onnx format.
But if you have some custom trained, you have trained a custom model or for segmentation in YOLO,
you can also export into the Onnx format as well.
Okay, so here I'm just exporting the model into the Onnx format over here.
So this might take a few seconds for execution.
So let's see what results do actually, we get over here.
Okay.
But now you can see here, here, should we have the onnx file so you can see that our YOLO V8 segmentation
model is being converted into the ONNX format.
And here we have the Onnx file over here.
Okay, so in this way you can convert the model into the onnx format or you can simply implement YOLO
v8 model in a.py file as well.
So to run yolo v8 model in a.py file, simply copy this.
All this and just paste all this code in any.py file and you can execute the YOLO V8 model on any custom
on any video or image.
Or you can also implement you train yolo v8 custom model train the V8 model on a custom dataset in a.
In App.py file as well.
Okay, so first of all, you from Ultralytics, you will import YOLO, so it will automatically import
YOLO V8 as well.
And then you can do the prediction on any sample image.
Okay.
So you can simply copy this code in a.py file and execute it.
So this might take a few seconds.
Okay.
31.2, 42.7 and 46.
And.
Basically it's downloading the segmentation model.
From the.
Uh, GitHub repo.
And save is equal to true means we will save the results of the output will save the output prediction.
Or you can say the output image as well.
So you can see that we have saved the output.
Image image one over here, which is the result over here.
And here we have the labels files which contains the coordinates of the bounding box.
Okay, so here we have the label dot text files, which contains the coordinates of the bounding box
over here.
You can see here.
Okay.
Okay, So you can display this output image over here as well.
So in a similar way, you can also run segmentation on a demo video as well, which you can see over
here.
So here we have run the segmentation on the demo video.
I have already done this trip so I will not run again.
Okay.
So and you can display this output video over here as well.
Like you can see that we have the bounding box around the detected object.
Plus we have also have a mask as well on the car as well.
Okay.
So simply you can also export the model in Onnx format as well.
So just by just writing model is equal to YOLO.
So here you can pass any custom trained YOLO V8 model as well.
YOLO V8 model trained with the custom data set as well.
And here I've just written the format as onnx.
Okay, so we will have the onnx file over here.
Okay.
So this is for the previous model and here we have the YOLO V8.
Previously we have the YOLO V8 and so YOLO V8 segmentation dot Onex file we have over here.
Okay.
So in this way you can implement your segmentation in Google CoLab.
So that's all from this video tutorial.
See you all in the next video tutorial.
Till then, bye bye.