But now we will see how we can integrate YOLO v9 with flask.
So I will just go over here and create a new uh.py file by the name last app.py.
Okay, so here I will just writing all my code to create a flask app.
So first of all we will import all the the required libraries over here.
So I am just importing all the required libraries.
I would just like, uh, before this, we need to install the last package, so just install last.
Okay.
Here I will just write from flask import.
Thus rendered template.
We will be using this library later on.
Response.
There Sonify requests.
Ah, we don't need to disturb.
Okay.
Then we can just import OS and.
Import C window opens with Python package.
So in the meanwhile our class library has been installed, so that's good.
Last from this detection.py file.
We will be importing this object detection function as well.
So I will just write over here from detection import object detection okay so that's good.
So in the step number one we need to initialize the class application.
So to initialize the flask application I will just write over here.
Now you can see that we have initialized the flask application.
Okay.
So now next we will configure.
Biosecurity.
Well, we'll just write one app dot config.
Bigger.
And you can write any key over here.
That's not necessary.
So I will just write my own name over here.
So as you know that we are creating a web app using class.
So we will create an upload folder, uh, where if the user uploads any input video image so that uh
file will be saved into that folder.
Boredstalker uploaded.
So for this I will just write app dot one.
Sick.
Upload folder.
That date.
Well, but you can see that we have not created this folder anywhere over here.
So I will just create a new directory that date.
Okay.
And over here I will just create another decade cycles okay.
So now you can see that we have created the required directories.
So now we will just create another function by the name generate frames, and the input to this function
will be the input file path, whether it can be image image audio okay.
So.
Or it can be for live webcam feed.
You can pass it over here.
So.
But within this right, you'll win an output.
And from this object detection function which we have imported.
So let's write object detection action.
So now you can see this object detection function.
Take only the input file as an input whether it can be image video.
So the input file path will be passed uh to this function as input okay.
So input file path.
So and this function of basically yields I to image uh or current frame or then the frame rate in which
the shape of the current frame and total number of iterations.
So these are the four outputs I am zero frame.
So now you can see that we have the encoded image over here.
Is not there should be in code.
Okay.
So now you can see that, uh, we have the encoded image over here.
So any flask application basically requires the encoded image, uh, to be converted into bytes and
rendered into the HTML.
So.
To convert this encoded image to into bytes.
We'll be using dot two bytes uh function.
So what I will do over here is I will write frame is equal to buffer dot two bytes.
So two bytes.
Dot two bytes.
Convert the encoded image into bytes.
That.
And using dot gain keyword like you can see over here we will display the individual frames.
Okay.
And, uh, we will loop through all the individual frames and, uh, go through all the individual frames
and to convert the individual frames are into subsequent frames will use the content type.
So we need to convert all these individual frames into subsequent frames okay.
It's all.
Right content type so that I can convert the individual frames into subsequent frames.
Okay, so that's good.
We have done good enough.
So we have created a generate frame function.
I'm not currently calculating the frame rate frame shape because I don't require this yet.
Then I will just write app dot route so that I can create a URL.
That you can just.
Let's try it out.
This as well.
That films.
And here we will be calling the generate frame function, which are created above.
So currently I will just pause the video part.
So I will just running video two dot mp4.
Okay, so that's all.
I don't see any error yet.
So just right over here.
And out run so that we can make some changes in the runtime.
So I've just set debug is equal to true so that we can make changes in the runtime.
So let's get started with this.
So I will just open the terminal now and I will just write over here.
Python.
Ask app dot Pi, and let's see if there is an error so that we can fix this up.
You can similarly pass an input image path over here or zero for the live webcam feed.
Let's open this URL.
But this will take some time before we have the results over here.
The legislature bought seconds.
I'm not going to pause the video so we can see how many time lag we get.
So this will take a few more seconds.
Get.
Okay, so the processing on the frames have started, but why not just appearing over here?
I can see over here the perception.
I will be starting this.
Oh, yeah.
It's working now.
Yeah.
So that's great.
So now you can see over here, uh, we have the URL over here.
I've just passed this URL and this is the frame size which we are getting.
Um, the width of this frame is 12, uh, 80 and the height is uh, 720, which you can find over here.
And you can see that we are able to track different cars over here.
Uh, I'm testing on CPU, so the processing is very, very slow.
But, uh, if you have a good GPU and a processing will be very fast, like, you can see the car is
moving, but the as the processing or frame rate is very low over here.
So now you can see that we are able to to churn on this video.
We have written different cars over here.
This is a wrong detection.
This is not a truck.
And these are also false positive.
Like these are the wrong detections okay.
So in the same way uh you can pass uh like you don't need to close up this program simply, uh, we
can also do object detection over an image like click or.
Okay.
So let's see.
You just need to refresh this up and let's see if we are able to detect different objects in this image
or not.
It is, though, it's worth a few more seconds.
Okay, so now here you can see we have our output image.
We are able to detect this talk person this handbag.
Okay.
And let's test out on other image.
Image.
So let's try this out.
Okay.
So I will just click on refresh from here.
And let's see if we are able to detect different objects in this image or not.
Like you can see over here we are able to score a person dog and bag.
So.
The results are very good overall, but let's see if it works on this other image or not.
So as I'm trying it on, CPU processing is very low.
But you can see here we have the results.
We are able to detect this bus, traffic lights, person bag, bag, bicycle, hand bag, all this stuff
are being detected and you can see this person are very far away.
Plus they are blurred.
But we are also able to track these persons as well.
And we have both the traffic lights as well.
So you can try it out on some other video as well.
Like uh video tool dot uh mp4.
Let's try it out.
This.
Similarly, you can also test it on the live webcam feed as well.
I'll show you as well.
Are you going to try it out on the live webcam feed?
So it's Wednesday full time and you can see here we have the frame size for this video.
Okay.
So let's see what is it.
So now you can see that we are able to detect different persons in this video.
Okay.
Like you can see in the processing is very slow.
I'm using CPU else everything is okay.
So in this way you can see that we are doing object detection on the video.
And if you want to do it on the live webcam feed, just change it to zero.
And let's run this up now.
So this will take some time.
Okay.
Let's see.
How does it goes?
Uh, this will take a few more seconds, uh, before we have the results.
Okay, so now you can see our fear of the live webcam feed is on.
Okay.
And you can find me young.
But okay, so now you can see over here, uh, we are able to track the person as well.
Over here.
This is a false positive.
Like you can see this is not the chair.
So in this way you can do object detection with the, like webcam feed over here as well.
Okay.
So.
So this is how it goes.
Okay, so in this tutorial we have seen that how we can integrate YOLO v9 with flask and how we can
do object detection on image video and on the live webcam feed.
And that's all for this tutorial.
Thank you for watching.