Now we will see how we can integrate Yolo V8 with flask.
So this is all the code which I have already written and explained you previously.
But one change, which one change which I have made is that I have just created a function video detection
and added all this code inside this function.
Video detection.
Okay.
And in the end of this function or the output of this function is the output image or the frame with
the bounding boxes around the detected object with labels and the confidence score.
So this is that you can see here in the end we have the image or the current frame with output bounding
or with output bounding boxes around detected object with the labels and the confidence score.
So now, plus I have just created a file by the name plus app.py file.
So let's start from the start.
So as we are integrating with flask, so in the first step you need to make sure that you have installed
class on your system.
So if you have not installed flask on your system, just install it by just writing PIP install.
Okay, so this will install flaws on your system because.
We are creating a web app using flask.
Okay.
And we are just integrating your V8 with class to create a web.
So as I've already installed the flaps.
So you can see over here the requirements already satisfied.
But you have if you haven't installed flask already, it will take some time to install.
Okay.
And then we are just importing from flask import flask render template.
So why we need to import render template is that we just we need a render template to render our HTML
file.
But we are not using this currently over here.
So you can just remove this from here.
Like this session request json require response.
You can remove this render dash template currently because we are not using it.
Plus you can also use this session because we are not saving the video file but the session storage.
We will do this later on.
Thus you can see simply remove just one you as well.
We use JSON file when we want to convert our output like frame rate or total number of detections into
JSON format to display on our HTML page.
And if.
Go ahead.
I am just importing cv2.
So Cv2 library is required if you want to run the YOLO V8 model.
So then I am just from the yolo v dash v video.py file from this file.
I'm just importing the video dash detection function.
So now you can see here in this yolo-video.py file.
I have this video dash detection function so I'm just importing this function and here I'm just initializing
class.
You can see here and here I'm just creating a secret key which I have written by name.
You can just change it as well.
So now here we have the generate frames function.
So generate frame function, take part of input video file and gives the output with bounding boxes
around detected objects.
So here we are just creating a function by the name generate frame.
So this in the part dash x, we will just pass the input video file path and it will give us the output
with bounding boxes, labels and the confidence score around each of the detected objects.
Okay.
So now here I'm just calling the video detection function.
And so now you can see here, here we have the video detection function.
And in the video detection path function, we just pass the path of the input video file.
Okay.
So here I'm just calling the video detection function.
So the video detection function will give us the in the output.
The detection function will give us the bounding boxes around the detected object with labels and the
confidence bar.
Okay.
So as we are doing detection frame by frame.
Okay.
So now you can see here we are just encoding the detection.
So basically any in-class application requires the encoded image to be converted into bytes.
So we are just encoding the current frame of the video.
So basically any flask application required the encoded image to be converted into bytes.
So.
So here you can see over here we are just converting the encoded image like we can see here or the encoded
frame into bytes.
You can see over here using dot two bytes.
I'm just converting the encoded image or the frame into bytes.
Okay.
So now we will look over all the individual frames and display them as video.
So now we will look each.
So basically our video is divided into multiple frames.
When we run object detection on a video, the complete video is divided into multiple frames and we
do the detection on each of the frame one by one.
Okay.
So after doing detections on each of the frame, so you can see here, we will look over all in visual
frame and display them in this video.
So after doing detection on each of the frame one by one, now basically we want to display our output
as a video.
So when we want the individual frames to be replaced by subsequent frames, like in the form of video,
the content type or mini type will be used.
So now you can see here we are just using the content type.
Plus we just want to replace the individual frames detection by the subsequent frames.
Okay.
And.
So you can see over here, here we are just using the yield keyword, which you can see over here.
So using get keyword, we will uh, uh, uh, after using keyword in the video detection function output,
we will get the individual frames with bounding boxes around the detected object along with the label
and the confidence score.
Okay, so this is all so again, explaining the generating function in the generate frame function,
we just pass the input path of our input video file.
Okay.
And here I'm just calling the video detection function.
So video detection function will give us the output with bounding boxes around detected object.
And here we are just encoding our frame.
And so basically in uh, any classification required, the encoded in English to be converted into bytes.
So now we can see over here we are just converting the encoded image into bytes using dot two bytes.
And basically we will loop over all the individual frames and do the detection on each of the frames
as one by one, and then we'll replace the individual frame by the subsequent frames using content type
or mini type.
Okay, so this is all and then I'm just calling this using apt route method.
I'm just creating a dash video.
So using apt route method, I'm just creating a dash video.
So in the dash video I'm just creating, passing, generate frames calling this generate frame function
in the.
Response.
I am just recalling the dash video URL.
I've just calling the generate frame function and in the dash x I will just pass the path of my input
video file.
So just remove this and you can see over here, you can see that here I've just passed the path of my
input video file.
So currently I'm just in this folder of last tutorial.
So I've just written dot dot.
So just go out of this folder, get out of this folder and just go to the videos folder.
And in the videos folder I have the bikes dot mp4, so I'm just going to the videos folder and here
I have the bikes dot mp4.
So as I already told you to, uh, when you want to the individual frames to be replaced by subsequent
frames, we either use many type or content type.
Okay, so here I'm just using mine type multipart to replace the individual frames by the subsequent
frames.
And here I've just passed the path of my input video file and just now run this flask app.py file and
see if we are able to integrate flask with yolo V8 and how our results look like when we just call the
url dash video.
So just.
Just need to go to the terminal.
And if you just click over here and just click on Copy path and just write CD like want to set this
path as our current directory.
Okay.
And just over here, copy path and just copy this absolute path.
Okay?
And just write CD and just add this path.
Now just write Python and just run this flask app dot.
Flask app.py.
Just click on enter now.
Okay.
So.
Now, basically we are integrating it with Flask and currently I'm just running on a video basically.
So just click, click on this URL.
So basically, this is not our URL.
Basically, we have created a URL dash video.
Okay.
So we have not created a root page yet.
We have just created a golden dash video.
So just click over here, dash video and just click on Enter.
So now you can see over here we are just doing the reductions on our input video which have passed.
Okay.
So now you can see that we have the bounding boxes around the detected object, like around the person.
We have the bounding box and we have the label person and the confidence score around the bicycle.
We have the bounding box and we have the label bicycle and the confidence score over here as well.
And on the traffic light, we have the bounding box around the traffic light, we have the label and
we have also have the confidence score over here as well.
So the results look impressive.
So now you can see over here we have integrated YOLO V8 with class.
And now when we call, call this video, it do the detections on the input video which have passed over
here.
So this is the input video which have passed.
And let me just show you this video.
So if I just go to.
My folder.
YOLO V8 Crash course.
Okay, so if I just go to video.
So I'm just passing this video as my input.
So.
And here the detection is done on frame by frame.
So now you can see that I'm just using this video, uh, to do the detections.
Okay.
And you can see over here, uh, I've just passed this video as my input video file in the flask app.py
file, and we can see that we are able to detections uh, of bicycle person and in traffic light.
So.
You can pass any part of what you want over here and you can do the reductions on it as well.
So now we have done reductions on a video.
Now in the next video tutorial or in the next part, we will see how we can do reductions on a live
webcam feed.
So how we can create a live class so that we can do reductions on the live webcam feed.
So let's discuss in the next part.