And now we will run it on webcam and see what results do we get.
So I will just click over here and create a new directory by the name running YOLO.
V8.
Webcam.
Okay, so now we will be running YOLO V8 on the webcam.
So just create a new folder over here.
Yolo.
He end webcam dot pi.
So I'm just created a new file over here.
Yolo v8 webcam.py.
And first of all, I will just write.
Just close the previous ones.
From Ultralytics.
Import.
Yolo.
Okay, so we have just imported the YOLO V8 version over here.
And I will write import cv2.
Then I will just write import math.
Over here will read.
Currently just remove this.
I will just explain this as we go further on.
So.
Okay.
Then just right.
App is equal to Cv2 dot videocapture.
And just right as I just want to run YOLO on V8 on my webcam.
So I will just write zero over here.
But if you have multiple webcams or connected with your system, then you can just check which webcam
are you using?
It can be 1 or 0 in any case.
Then I will just calculate the frame rate over here.
It.
Or to get the frame rate just right.
And app dot get three.
And to just get the frame height just right.
Frame height is equal to end cap.
Dot get four.
So in this way we are just getting the frame width and the frame height.
I just want to save the output video with detection I will just write out is equal to cv2.video writer.
And just write a name.
Output dot you.
This is the name, uh, the name of my output file.
Like after doing the detections or after doing detection on video or using webcam, the output video
will be saved by the name output dot avi.
I mean, we just write cv2.video writer dash for CC and.
That's right.
I'm over here.
And Omar J.
Omar.
Anyway.
Okay.
Then just pass the frame rate over here and then just pass the frame rate and frame height over here.
Okay, so just define my output.
Video writer function over here using cv2.video writer.
Then just click on enter.
Just right model is equal to.
Journal and.
If I just go to the YOLO V it's for YOLO weights folder over here.
You can see that here we have the YOLO V8 and our Pre-trained weights placed over here.
So just write dot dot and just say that go to store.
We just need to get out this out of this running YOLO V8 webcam folder and just need to go to the YOLO
weights folder.
So just write YOLO dash weights.
Okay.
And just write YOLO V8 and Dot over here.
So here I'm just passing the Pre-trained weights path over here and just click on enter over here.
Then just right.
Wine through.
Okay.
Success is a common image.
Is equal to cap dot read.
So we are just.
Reading frame by frame.
Okay.
Okay, so let's just test this.
Either our webcam is working fine or is there an issue?
Or just write CV two dot I am show.
Image.
My image over here and just write Cv2 dot, wait key over here and just click over here.
One and I think.
That's fine or just further elaborated.
Cv2 dot Waitkey is one.
And.
Order is equal to.
One then.
Great.
Okay, so that's completely fine.
So let me just run it and see if I am able to access the webcam or not.
And if I'm able to save the output video or not.
So let's just click over here and click on Run YOLO V8 Webcam.
Okay.
So let me just I think it might take a few seconds for it to appear, so let's just wait.
Okay.
I don't think any other.
Okay, let's just.
Just navigate my screen over here.
Just give me a minute.
So now you can see over here the webcam is working fine.
And now you can see by the image on your screens.
Okay, So I think the webcam is working fine.
Let's see if this output video is being saved into the directory or not.
Okay, so let me just go back over here.
Okay.
So now you can see that here we have the output for file has been created over here.
But let me just go back from here and just let me just pause, stop this process by just clicking over
here.
Okay.
And let me just see if the output video is working or is there an issue.
So just click over here.
So now we can see that our output video is being saved.
Okay.
So now you can see that.
Uh, as when we run the webcam, our video has been saved, so that's fine.
That's perfectly fine.
Okay, so let me just go back towards the code.
For now.
Now, I will just add the I'm not using the V8 model, which I've just called over here and just saved
in the variable model.
Now I will just run the detections on the live web feed.
So let me just add that code over here and run detection on the live webcam feed as well.
Read it.
Well, here I have just written the complete code, so let me just explain you the complete code over
here.
So now here you can see that we are just doing the detections using YOLO V8 model on frame by frame.
So here we have Stata.
Stream is equal to true, so stream is equal to true.
We'll use the generator and it is more efficient than the than if we just don't write stream is equal
to true.
Okay.
So we are doing detection using YOLO v8 frame by frame and our result is are saved in this variable
results.
So once we have the results, we can check for individual bounding boxes and see how does it performs.
So here you can see here, once we have the results, we can check for the individual bounding boxes
and see how well it performs.
So we will just look through each of the bounding boxes and for each of the result and see how how it
detections are performing.
Okay.
So now you can see that here we are just looping through each of the individual bounding boxes.
You can see over here.
So here we have the X1Y1 coordinate of the bounding box and here we have the x2 and y2 coordinate of
the bounding box.
So let me just show you how it is.
Basically it works.
So let me just open the paint over here.
Um.
Okay.
Just give me a minute.
I'm just opening.
Okay.
So, for example, if we have this bounding box.
Okay, so you can see here we have the four coordinates for each of the bounding box.
Let me just write over here.
So this is the x one.
Y1 coordinate and this is the x2 and y2 coordinate.
Okay.
So for each of the bounding box we have the X1Y1 coordinate and this is the X2Y2 coordinate.
So X1Y1 is the like you can see that at top left corner coordinate and this x2 y2 is the bottom right
corner coordinate.
Okay.
So if you just go back towards the board again.
Okay, so now you can see for each of the bounding box we have the x1, y1 and x2 y2 coordinates.
Okay, that's perfectly fine.
If we just go over here.
So now you can see that we are just converting this coordinates values to integers.
Why?
We just need to convert this coordinate value to integers.
Because the output which we are getting over here is in the form of tensors.
We need to convert this output into integers.
So let me just show you what kind of output we are getting.
And after converting into integers, what kind of output do we get?
So just run this from here and just let me just show you the output which we are getting.
The webcam is just currently on, so might take a few seconds to just make the webcam working.
So.
Okay.
So now you can see over here we have the output in the form of tensors by two further processes and
create bounding boxes around the detected object.
We need to convert this output from tensors into integers.
So now we will just convert this output from the tensors into integers.
So to convert this output from tensors into integers.
So just comment this out from here and suggest you need to write int X1Y1.
So we are just converting each of the coordinates from tensor into integer.
So now just run this again and see what output do actually we get.
So now basically our output is being converted from tensors into integers over here.
So we will have the output very soon over here.
Okay.
So now you can see over here, now we have the output in the form of integers.
So we can't see the tensors over here.
So you can see that we have the output in the form of not integer.
So let me just pause this down here.
So now you can see that previously we have the tensors around each of the coordinate, but now we don't
have the tensors around each of the coordinate.
We have the output in the form of integer for zero are the X1Y1 coordinate value.
This is the x one coordinate value.
This is the y one.
Coordinate value.
This is the x two coordinate value and this is the y coordinate value.
Okay.
So now we just need to create a rectangle over here.
So we use a or you can say that we need to create a bounding box around each of the directory object.
So basically we have the coordinate value for each of the detected object, like we have the coordinates
value for each of the detected object.
We just need to create a rectangle around each of the detected object.
So using cv2 dot rectangle, we will create a rectangle around each of the detected object.
Okay.
So if I just go over here and show you what are the parameters we have.
CV to tango.
Okay.
So let me just open this from here.
So.
Okay.
So now you can see over here in the rectangle we just passed the our image or the current frame.
Okay.
And then we have the starting point and then we have the ending point and the color of the rectangle.
And then we have the thickness.
Okay.
So now you can see over here if you just see this.
So this is our starting point.
You can see over here, this is the ending point.
And we will just define the color and the thickness of this bounding box.
So if I just go back towards the code.
So now you can see over here we have this is the current frame and this is our starting point.
This is our ending point and this is the color of the bounding box.
It's around pink and this is the thickness of the bounding box.
Okay.
So let me just comment this out currently.
Let me just display the bounding boxes first and then we will discuss the.
Okay.
So.
I am just currently focused on displaying the bounding box.
I am not interesting in displaying anything else.
Okay, so let me just display the bounding boxes and see what results do actually we get.
Okay, so let me just just checking a few things over here.
If there is an issue so I can just correct it.
So let me just comment this out correctly as well.
Okay, so let me just run this and see what what do we get?
Are we able to draw bounding boxes around detected object or not?
So currently our goal is to draw bounding boxes around each of the detected object and then we'll go
ahead and see further work.
Now you can see that a bounding box is being drawn around the as in a person.
So a bounding box is are drawn around me.
So that's perfectly fine.
That's what we are expecting.
You can see over here, a bounding box is being drawn around me.
So this is the output which we expect.
Basically, that bounding box should be drawn around each of the detected objects.
Okay, so this is our server expectation, but now we just want to have the label and the confidence
score as well.
Like label means if a person there should be shown that I'm a person label above the bounding box,
there should be a label that am person and the confidence score like how much our YOLO V8 model is confident
that am a person.
So I have already told you as our V8 model we are just using the V8 Pre-trained model which is trained
on coco dataset.
So the Coco dataset consists of 80 different classes.
So I have just written the name of all those classes over here.
These are all the classes which we have in the Coco dataset, which are around 80 different classes.
Okay.
So to get the confidence over over here, I'm just passing this using bad dot.
See, we are just getting the confidence score values.
The confidence score basically appears in the form of tensors.
Okay.
So to just convert into integer, I'm just using this math dot.
See, let me show you what originally we have the confidence score value.
So just write print box dot three sense, okay?
And just write zero and let's see, what does the confidence score value do we get?
So then we will discuss later, few things later.
So just comment this out.
We just don't want to show the output currently.
I just want to show you the confidence score value.
So just click on Run YOLO V8 Webcam.
So now we'll have the confidence score value over here as well.
So this might take a few seconds.
So now you can see here.
Okay, So just let me just stop this so you can see that we have the confidence score value in the form
of tensors.
We just need to convert this value into integers.
So I'm just converting this value into integers over here.
Okay.
Thus now you can see over here we have the confidence pour value into it.
We are just converting the confidence score value into integers and now using int box dot zero.
We are just getting the class ID.
Okay.
So here will give us the class ID and here I've just written the class name.
So if the class ID is zero, it means it is a person.
If the class ID is one, it means it is a bicycle.
And if the class ID is two, it means it is a car.
Okay, so here I'm just getting the class ID and here I'm just finding the class name.
And in the label I am just putting the class name and confidence value combined.
And here I'm just finding using cv2 dot get text size function.
I'm finding the size of this label so I can just create a rectangle with rectangle.
Yes, but the size of this table.
And here I'm just creating the rectangle above the bounding box so that I'm just can just put text around
it.
Okay.
And just I'm just saving the my detections in the out in the file name as output.in this output dot
file.
My detections will be saved.
And just let me just show you the reductions frame by frame, Right?
And if I just click on one, the process will stop and just clicking auto release.
So let's just run this and see what results do actually we get.
And is our output saved or not?
Or production video saved or not?
So just run Yolo v webcam and see what results are we are getting now.
Okay, so this might take a few seconds more.
Mike can see that over here.
The label person as well as you can see over here, the contents pour over here as well.
Plus on the back side, I have the sofa so you can see that.
So over here as well.
Plus, you can see that a bounding box is created around me.
And as I'm a person, so you can see the person over here.
Plus on the back side, I can see the sofa and see the label soap over here as well.
Okay.
So let me just show all this.
Can see that so far And.
Now, the questions are very well.
You can see over here.
So these are the results which we expect.
And this is all from this let me just show you, is our output video is saved or not?
I'm just stopping this video.
And let me show you if our output video is saved or not.
So let me just go YOLO V8.
Okay.
So let me just go towards the YOLO V8 Crash course run, YOLO V8 Webcam.
And so you can see over here, here we have the output video.
So if I just click over here, okay, so now you can see that that's the results are very impressive.
Our output is being saved.
You can see over here.
Okay, so.
You can see that we are able to do the on the live webcam and our output video is saved there as well.
So that's perfectly fine.
That's the results which we am expecting and that's pretty impressive.
So.
The next video tutorial, we will run on Windows and see what results do we get.