This tutorial, we will cover how we can do object detection on images, videos and on the live webcam
feed using YOLO v nine.
So over here you can see that in my PyCharm Community Edition, I have created a new project by the
name Yolo v nine flask Web app in my local app directory.
You can create this project in any of your directory as well and you can find see over here I'm using
Python 3.10.
Okay.
So the first step we will do on the YOLO v nine GitHub repo over here.
So now you can see this is the YOLO v nine official GitHub repository.
I will just click over here and copy URL to clipboard.
And I will go back to PyCharm edition.
And over here in the terminal I will just write it long and I will just paste the link of the YOLO v
nine GitHub repository, which I have copied the I have copied the URL over here.
So now you can see over here I'm cloning the YOLO v nine GitHub repository over here.
And you can find this.
So now you can see we have this YOLO nine uh GitHub repository over here.
So I will just set this YOLO v nine as my current directory.
So I will just write CD yolo v nine.
And I'm just set this up as my current directory.
Now this clone folder.
So now I will just install all the required packages that I have added done in the requirements.txt
file.
So in the requirements or txt file you will find all the packages or the libraries that are required
to do object detection or images or videos, or all the packages that are required to run the detection
script.
So you can install all these packages one by one or by the easy most easiest way is to write install.
Minus R requirement.
Or you can say this is height one r.
So R basically stands for recursive.
So this will install all the packages that are uh or the libraries that are written in the requirements
dot.
TXT file.
So.
You just need to write pip install r requirements.txt txt and you can see over here all the required
packages are being getting stored.
It's always recommended to create a virtual environment.
In the previous lectures, I've told you how you can create your own, uh, create a virtual environment.
Uh, I'm not creating a virtual environment over here, but it's recommended that you create a virtual
environment, and it's good as well.
So these packages translation will take some time like this will take few more minutes.
So let's wait for all these packages to get installed and then we'll go ahead.
But while this packages gets installed.
So now you can see over here on these packages are being installed.
Like you can see the installation of these packages have been finished.
So while these packages get installed I have done two things.
First, I have just created a resources folder over here in this resources folder I've added some sample
images and videos.
You can see over here I've added four different images and two videos.
Along with this, I have just created another directory over here by the name weights.
And here inside this I have added any model weights.
So I'm using a yellow benign compact model weight okay.
Which I've added over here.
So let's see how we can do object detection or image and video.
So to do object detection or image video or on the live webcam feed using Yolov3 using detect-you.py
script over here.
So I will just write python detect-your.py.
And here in the source I will just pass uh, the, the source like, uh, source path over here, whether
I want to do object detection on image or on video.
So what I will do over here is I will just go to a serious sources directory.
And over here I will just write over here image one dot jpg.
So first I will show you how you can do object detection on this sample image.
And I would just add that weird spot over here as well.
So we'll just go to the weird folder which we can find over here.
Okay.
And in the weights folder I will just write your will like that.
See the heart.
But yes, all that's done over here.
Okay.
Let's click on enter.
So now we are doing object detection on this sample image.
Later on I will show you how we can do object detection on a video as well.
So let's see how does it work.
I am using CPU, I am don't have GPU in my local computer so the processing will be very slow.
Or this will take a few seconds before it's been done.
Well, you can see over here I have done object detection on this input image.
And our output image is being saved over here.
Image one dot jpg.
Okay.
So now you can see over here I am able to type this person okay dog as well.
Plus you can see this is being detected as the backpack over here as well.
So we have done detection on this input image.
In the same way we can do object detection on this on an output or an input video as well.
So.
To do object detection on video.
I'm just checking one thing over here.
So I will just add the part.
So I will just write over here.
I want to do object detection on video one dot mp4.
Last.
I want to see the output as well.
So I will just write view dash image.
And let's get started with it.
So now you can see over here I'm using YOLO v9 compact model weights.
And I'm trying to do object detection on this input video video one dot mp4.
So let's see what results do we get.
And if we want to do object detection on the live webcam feed, you can simply set source to zero and
you will be able to do object detection on the live webcam feed as well.
It.
Let's open this up.
So now you can see over there.
Uh, we are able to detect the cars.
Now, these are not correct detection.
These are not the cars.
But this is not a truck.
This is a boat.
Uh, this is wrong detection.
But you can see over here we are able to detect this car as well.
Car?
And as I told you, I am running it on CPU, so the processing is very slow.
Okay, but if you have a good GPU and Nvidia GPU available in your local computer, uh, the processing
will be very fast and we will be getting a very good frame rate as well.
Like I am just getting a very low frame rate, but if you test it on, uh, good ship, you idiot,
ship you 4060.
So you will be getting around 10 to 11 frame rate as well as you can see.
So now you can see that, uh, we are able to detect different cars in this image.
And this is the wrong picture in the same way, uh, you can test it on any other, uh, video as well.
And to do object detection on the live webcam feed, you can just need to do one thing.
You just need to, uh, replace this source path with zero.
Like, let me show you how this works.
So, and you will be able to do object detection on the live webcam feed as well.
So this might take a few more seconds before it gets executed.
Book.
So now you can see over here I'm able to do object detection on the live webcam feed.
And you can see my face over here.
And our results are very promising.
Like this is the wrong direction.
This is not a chair, this is a bear.
But I am a person and it has corrected me as a person, so that's good.
So in this way you can do object detection on the live webcam feed as well.
Okay.
So um, as you know that the main aim in this section is that we need to create a web app and we will
be integrating yolo v9 with flask.
So I have created a new.py file and I have adjusted that uh, script object detection script as per
our requirements that we need to go uh, in the further we need to, uh, integrate YOLO benign with
plaques.
So I made some changes into this code and re rewritten this, uh, complete code and, uh, so that,
uh, it would be easy for us to integrate the YOLO v9 object detection model with flask.
So I will not be writing that complete code over here.
So I will just show you that script file.
So let me just add it over here.
Over here you can see that I've created another.py file script file detection.py file.
And I have just made few changes into this code and adjusted it as per our requirements, so that we
can integrate your V9 with flask.
Over here you can see that if I just go below down, I have created a function by the name object detection.
And I have added all the object detection code over here so that we can do object detection with yolo
v9.
Okay.
And plus you can see that um, I have just created a dictionary over here.
And for example, uh, like if you want to do or use another model weights, like if you want to use
any other YOLO model weights like you know that YOLO v9 come with, uh, let me show you.
Well, for different models like compact, extended, medium, small, and if you have a good GPU available
in your local computer, you can use your the V9 extended model weights as well.
So yolo v9 extended model weights uh, or yolo v9 extended outperforms all the other YOLO v9 models
as well.
So currently I'm using YOLO v9 compact model weights.
But if you want you can use this model weights as well.
So like in this way you can vary different parameters.
You can change the confidence threshold IOU threshold as well.
Uh, if you don't want to do object detection on all the classes, you can select any specific class
as well.
Like if I want to detect only bicycle cars.
So you can add the class over over here.
And if you have a good GPU you can write device zero.
I am currently running it on my CPU, so I have ten device CPU.
And if you uh, maximum detection means like how many, uh, maximum number of predictions that you
want to do in an image or a video frame.
Okay.
So this is all what you have over here, okay.
And if you want to vary the image size you can do it over here.
So you can adjust these parameters as well as per your requirements.
And uh for now you can see you will see that uh I'm using yolo v9 pre-trained model weights.
So YOLO v9 has been trained on the Coco data set.
And Coco dataset consists of 80 different classes.
So I've added all the class names over here.
And if you're using some fine tuned YOLO inland water weights, you can change this and add the custom
class names over here as well.
And here I've just created colored labels.
So uh, like you can you know that in as benign model is being trained on the Coco data set.
And um, in the first we have person, then we have bicycles.
So if the class ID is zero, like if there is a person, then the bounding boxes will have the following
color.
And if the class ID is two, like if the uh car is detected, then the bounding boxes will have this
color.
And if, uh, class ID is five, like the bus is detected, then the bounding box will have this.color
else on the bounding boxes.
Uh, other than these classes will have this color.
And here I have added all the code that we have in the text that I have added few made few changes like
I have added uh, s okay, so uh, I have added, uh, I'm just uh, doing calculating the frame rate
as well over here.
Like you can see over here.
Um, let me show you.
So I hear I'm not getting that, uh, frame rate using traditional way.
I've just, uh, get reading the frame rate over here?
Yes.
Over here.
So.
We have the frame rate camera here.
I was looking at total number of detections in each of the frame or image.
So we'll have that total number of detections in each uh uh, frame or image.
You can say that.
Plus we are also getting the frame rate.
Or you can say last we will also be getting the uh frame number like uh frame count over here.
Like, uh, in this frame.
Uh, we are done.
Uh, how many total frames we have and how many, uh, processing on how many frames have been done.
So all this.
So, uh, I have just created this object detection function, like you can see over here.
And in the object detection function, we just need to pass one input.
You can either pass the video path, you can pass the image path.
Or if you want to do object detection using live webcam feed, you can just pass the path as zero.
So let's get started with this with an image.
So if I just go to the resources folder over here.
So first I will just write image three okay.
Right over here.
Image three dot jpg.
So first we will do object detection on this image.
So I will just open terminal over here.
In.
So I will just write Python detection.py.
So you just need to run this detection.py script file.
And over here no other arguments are required.
So let's see.
Okay.
Uh, we we get the results.
So now you can see this is our output image or a variable.
Okay, okay, okay.
If you want to close the image so you can just set cv2 dot wait zero okay.
As we can see the output image over here as well.
But let me show you the output image over here.
So I'm adding an infinite delay.
So we'll have the output image being displayed over here.
Previously there were one millisecond delay or one second.
Well, let's see how does it works.
And you can see here we have the our output image from the previous section.
Okay.
But this will take a few more seconds before we have.
So now okay.
That's it.
So now you can see over here this is our output image.
Uh, here you can see we have first person.
So you can see this person is very far away.
But our border has detected this person as well.
Uh, traffic lights and bad, uh, bicycle.
Everything is detected.
So the results is very promising.
Okay, so let me just do one, because we will be doing, uh, now object detection on video.
So now I will just test on video to dot before and let's see what results do we get.
But all over here you can see that, uh, we are doing object detection on this video as well.
Okay.
And you can see we are able to detect the person handbag, backpack.
And you can see currently the frame rate is 0.28.
Over here you can see that the frame rate is very low.
Okay.
And like if you test it on uh go Gpio.
So the frame rate will be uh very high like it will be around 10 to 11.
But currently the FPS frame per second like frame rate tells us how many frames are processed per second
in a second.
Okay.
Now you can see we are able to detect different person and bags over here.
So in this way you can see we have done object detection on a video as well.
We have done object detection on the image as well.
Plus two object detection on the live webcam feed.
You can simply set this as zero and you will be able to do object detection on the live webcam feed
as well.
So one thing which I would do over here.
So.
Now we will be integrating your log line.
So now currently you can see that we are doing object detection with YOLO v9.
But now we will be integrating this YOLO v9 with uh flask.
So I will just make a few changes over here.
So.
Exposed up as well.
Uh, let's let's watch this as well.
Okay.
Okay, okay.
And.
We're.
Okay.
Plus R over here.
Remote desktop.
And here I will add unit keyword.
So what is hidden keyword.
So basically you'd go keyword allows the function to return multiple values.
Like we will be uh getting the uh uh object detection on the current frame.
Plus we will be returning the frame rate or FPS.
Plus we will be in output.
We'll be getting the current frame shape and in the output will also be getting the total number of
rejections in each of the frame as well.
So like we are getting multiple outputs from here.
So uh, so here keyword uh yield keyword basically done as multiple uh values without terminating the
complete process.
So this all this makes us easy to process.
Basically, this all makes it easy to process all the frames.
Okay.
And to to do iterations over each of the frame one by one.
So I'll just write I am zero like this is my current frame.
And the frame rate, the frame rate will give me the FPS value.
A current frame shape.
And the total number of detections, like how many different, uh, objects are detected in the current
frame.
Like I forgot to tell you.
Like you can see over here, we get total number of detection.
30.
So this basically gives the detection count like how many different objects are detected in each of
the frame like total detection 30.
So all this up okay.
So that's all what we are getting from here okay.
So if you want me I can show you this as well like predictions.
So let me just uncomment this up from here.
Let me add zero okay.
And.
Let me show you the total number of this works.
So if I just write over here.
Image one.
Dot.
So let us see how this works.
Why it's not working currently.
It's.
Image one dot jpg.
But it was by point detection.
Dot py.
Okay let's see.
Hopefully this will work now.
Short word.
Now let's see.
Okay.
Right.
So I'll just make it out the keyword now.
So let me show you.
Okay.
So now you can see we have the dog person handbag this car as well.
Okay.
So if I just press the one key.
So now you can see that the total number of four detections and this is the frame rate.
So if I just open this over here.
So now you can see we have this card.
This is the first detection this dog uh which is the second detection.
So this is the car first two three.
This is the person and this is the handbag in beside you can see, uh, this handbag.
So this is the fourth detection.
So we have it total four different objects.
So we are getting the total detection count over here as well okay.
So much for this benign object detection model as the current frame, the frame rate like the FPS.
Currently you can see that this is very low.
It's over minus ten.
The frame size and total number of detections.
Like currently we have detected four different objects.
Okay.
So the frame size will be I think uh like this will be a 1080 or so like this.
Will the frame size what I see.
So that's all from this tutorial.
Uh, in the next tutorial we will see how we can integrate YOLO with flask.
So let's go ahead.