Transcript for:
Introduction to Object Detection Concepts

hey everyone welcome to my channel this is the object detection 101 course in this course we will cover everything to get you started with object detection along with four exciting and real world projects we will first start with the theory and have a look at its brief history then we will understand the working of object detection along with its evaluation Matrix from there we will install everything from the start and run the YOLO version 8 object detection pretend models using these models we will create two projects car counter and people counter here we will learn how to implement object tracking along with object detection we will also install and run YOLO with our GPU for better performance later we will learn how to train a custom YOLO model using our own data set and create two projects using our own training the first one will be personal protective equipment detector and the second one will be poker hand detector where we will first detect the playing cards and then create a classifier to classify the correct poker hands if you are a beginner don't worry this course is for you we will go step by step so it is easy to follow this is an information dense course that will provide you with maximum knowledge without wasting your time learning custom object detection is a superpower that can allow you to solve real world problems and it can be easily completed over a weekend so buckle up and get your nerdy glasses on and let's get started but wait we are going to use nvidia's 3080ti graphics card in this course now my question to you is would you like to have one too if yes then you are in luck I will be giving away a 3080 di graphics card and all you have to do is register to nvidia's GTC 2023 conference through my link in the description and attend one of their sessions to enter the giveaway automatically the conference will be held from 20 to 23rd March 2023. this is a great place to learn new things and keep up with the new trends in technology so sign up today and I will see you there foreign [Music] what is object detection object detection is a computer vision technique for locating objects in an image or a video object detection gives us the bounding box information of the object as well as the classification of that object what is the difference between object classification object detection and object segmentation object classification is a type of image recognition that identifies what type of object is present in an image here the complete image is sent for classification so the output is a single class object detection is a type of image recognition that is used to identify and locate the presence of an object in an image this gives us the bounding box information and the class as well here the results can be multiple bounding boxes and classes object segmentation is the type of image recognition that is used to identify and separate the distinct objects in an image on a pixel level here we get the exact shape of the object detected rather than just the bounding box although image segmentation provides more information image detection is still the first choice for most computer vision applications since it is less computationally expensive a brief history of object detection object detection was first started in the 1970s researchers started to develop automated methods for object detection the first automated object detection algorithm were based on simple features such as edges and corners the first real object detection was Viola Jones developed in 2001. it used sliding Windows to search for hard-like features which are simply rectangular features this method became popular since it was real time for the longest time period this method was used for face detection in smartphones and cameras the histogram of oriented gradients was another method released in 2005. that focused on the shape of the object it worked by extracting the gradient and orientation of the edges this method was mostly used to detect humans in an image then came the revolution of AI with neural networks gaining popularity the true potential was seen with convolutional neural networks when alexnet won the imagenet large-scale vision recognition challenge in 2012. but this solved a classification problem and not detection later Bruce Force methods were used to repurpose the CNN models to work as detection models this was a very inefficient approach so soon after came the rcnn regions with CNN that used selective regions to apply the classifiers this gave good results but it was slow so then came the Fast rcnn and the Foster rcnn which were faster but still not real time in 2015 came the Breakthrough of YOLO this outperformed all the other models and ran object detection in real time this was a different approach as it used single pass of the input image to make predictions of the objects methods like rcnn used Regional proposal to perform multiple iterations for the same image while YOLO gets it done in a single iteration this makes YOLO more efficient even though the architecture of YOLO is based on convolutional layers similar to its predecessors since then YOLO versions have been releasing faster than Android 8's 5 million dollar Bugattis every time I come back from the toilet I have to check if a newer version of YOLO was already released but don't worry the change between each iteration is not always very significant it's like apple telling us how their latest iPhone design is completely different from the previous one if you don't believe me have a look at these results can you guess which is the latest model absolutely not so even if a newer version comes in the previous versions don't go out of date performance evaluation metrics in order to evaluate our object detection models there are two main metrics that we need to understand one is to evaluate how good is the location and the other is how good is the classification first is the IOU to measure the localization and the second is the map for classification ious stands for intersection over Union it tells us how close the predicted bounding box is to the ground truth it's a value between 0 and 1. if the boxes overlap then it's a perfect detection with an IOU of one if the boxes have some overlap then the values are between 0 and 1. depending on how much the overlap is if there is no overlap then the IOU is zero the value of IOU is calculated by taking the ratio between the area of the intersection and the area of the Union of bounding boxes in order to understand average Precision we need to know confusion Matrix precision and recall confusion Matrix is a simple table that consists of actual classes on one side and the predicted classes on the other let's have a look at an example of a car classifier in the first cell the actual class is of a car and the model predicted it as a car so it's a true positive in the second cell the actual class is a house but the model predicted it as a car so it's a false positive in the third cell the actual class is a car but the model predicted it has not occurred so it's a false negative in the last cell the actual class is ball and the model predicted it as not a car so it's a true negative this value corresponds to not predicting a pounding box and is usually the background so it is not used in the metrics calculations using these values we can calculate the Precision and the recall Precision is the total positives out of the total positive predictions recall is the actual positives out of all predictions in mathematical terms Precision is true positive divided by true positive plus false positives recall is true positive divided by true positive plus false negatives since both Matrix provide us valuable information we combine them to one called the Precision average the area under the Precision recall curve the Precision recall curve maximizes the effects of both the Matrix and give us a better idea of the overall accuracy of the model this every Precision is of a single class if we have multiple classes then the mean of these Precision values is the mean average Precision also known as map so the first thing we will do is to download Python and to do that we can go to python.org so we will simply go to downloads and here you can see you can download the latest version but that is not recommended because it might have some errors or it might need some bug fixes so we'll go to all releases and here we'll go down and you can see python 3.11 will have support till 27 python 3.10 will have till 26 so you can see the information here so this is basically the long term support so here in the release version I would suggest to go uh don't go for the latest one go for the one that is one below the latest one so for example here you can see three points 10.9 so we can download this we can simply press on download and here you will go to Windows 64-bit and you will click on that so it will start the download now you can have multiple versions of python as well so you can have the same project running on different python versions so you can see which one is more stable which one gives you better results so I will show you how you can do that because I already have python 3.7.6 which I have been using for the longest time period and I will also show you how you can use python 3.10 in this case so we will be using python 3.10 but if we had to downgrade I will show you how you can do that or if we had to upgrade I will show you how to do that as well so what you will do is you will click on python so here make sure that you add python.exe to the path and you will click on install now okay so the setup is done and we can close now we will go to pycharm which is our IDE now if you're not familiar with IDE it stands for integrated development environment it's basically a notepad where you can write code but it will help you out to write the code it will tell you where the problems are how to fix the bugs and all that so what you will do is you will go to pycharm so this is jetbrains.com pycharm and you will go to download and in the downloads there is a professional version and there's a community version so you have for Mac OS and Linux as well so we will be working on windows so we will download for that so for the community version you can simply hit download because it is free and it is more than enough for what we need so now the download has started and we will wait for it to finish so now the download is complete and we will hit on pycharm community we'll click on next next and we will associate dot Pi files with that we'll admin folder to the path and part charm Community Edition we'll create the desktop icon as well install then we are going to restart our computer and hit finish and then we are going to open up pycharm so this is not the first time I am installing pycharm so it has a memory of what I've been doing so it has some of the default parameters so you might see some pop-ups that will ask you a few questions uh how to set up a few things so you can set them up click next next and it should be fine or you can close them and the main thing that I'm going to tell you that you need to do is right now so you will click on file and you are going to create a new project and here is the main thing so this is an environment so basically what you do is you have the actual Python and you copy that basically and you create a virtual environment that is separate to everything else so this project will have its own environment and whatever you install will be installed in this environment so this is based on a specific version of python so here the base interpreter you have to mention so right now the base interpreter is python 3.1.0 so you can also have python for example 3.7 so I have these two python 3.7 and python 3.10 so I can choose between these two so for now I'm going to choose for example this one so what I will do is when I'm creating the environment I'm putting down the name and I can write environments and I can write three one zero so this has a version as well so here we'll just name it python project and we are going to hit create so we will use the same window to create this project and here we have our python project and that is created with the main file and you can see the virtual environment 3.10 has been created so what I can do is I can remove all this and I can simply write print hello world so let's go ahead and right click and we are going to run Main and this will give us hello world so this is running python 3.10 how do I know this is because you can see here we have python 3.10 we don't have python 3.7 so what we will do is we are going to go to file and any package that we want to install we can go to settings and then we will go to Project python project and we'll go to python interpreter and here we can add any package that we want whether it's opencv whether it's CV Zone whether it's ultralytics whatever it is you can install here so for example we can go ahead and install numpy numpy so we will click on install package and it will take a while and you can also specify which version of numpy do you want to install we are installing the latest one that is available right now so we can hit close and here you can see now in the packages we have numpy installed this is the current version and this is the latest version so they are up to date so these three are already installed so you don't have to worry about those then we can simply click ok so here if I write Imports numpy and if I run this it will not give me any error which means numpy has been installed if I write anything else it will give me an error because there is no Library called nump2y right so that's the basic idea so this is import numpy now if I go to file and if I go to settings I can create a new virtual environment if I wanted to use two different pythons I could create a new virtual environment to do that you can click here you can go to show all and over here we can click on ADD so here we can create a new environment and here we will create for 3.7 so we will select 3.7 as our brace interpreter and we have named the virtual environment 3 7 and then we'll press ok so now it will create and it will shift it to 3.7 we'll hit OK but now you can see that we installed numpy earlier but it was installed in the virtual environment of 3.10 but now it's not installed in 3.7 because we are now using 3.7 as the main environment so that's why it is giving us an error over here so if I run this now it will give me an error that there is no numpy but if I click here and go to three points 10 and then it will load up and if I run it again it will not give me an error because numpy is already installed so you have to make sure that your libraries are installed if you are trying different versions of python so here you can go to library and you can see you have numpy installed so here in 3.7 you will not see numpy so what you can do is if you wanted to use in 3.7 you will make sure it's 3.7 over here and you will go to file settings and here you are going to install numpy now it will install it to 3.7 as well so the reason I'm telling you this is because later on you might face some issue in for example three points 12 or 3.15 and you want you might want to downgrade or you might want to upgrade later on so instead of creating a new project and doing all of these things together again what you can do is you can simply have a new virtual environment and have the same project running so it will not be very difficult to debug so here you can see we have installed numpy for 3.7 as well so now if I run with 3.7 or 3.10 it will run with both so for most project we are only going to use one python environment we will usually use the higher one unless there are some major issues in that so then in that case we are going to downgrade so now that we understand how to install pycharm and how to install python you can add more python versions now we can simply go ahead and start working on our project so here we are going to create our python project which is object detection YOLO and we are creating a new virtual environment and the environment is based on the base interpreter of python version 3.1.0 so 3.10 so it is up to you which python version you want to use so we are using 3.10 if you have a newer version you can use that too but don't use the latest one because it might heal some errors so what we'll do is we will simply hit on create and there you go we have our project and here we have a main file so what we can do is we can use this main file to test our code we can simply write prints print hello world and we can run it just to see if everything is working and it's working fine so in order to get started what we have to do is we have to do some installations on this virtual environment so if you look at this folder we have the virtual environment if you click on that in the libraries these are the site packages we need to install some packages in order to run our project so how can you do that you can go to file settings and in the project python interpreter you can click on ADD you can type in the version for example you can type in the site package name for example CV Zone and you can hit install now when we are working with object detection there's a lot of libraries that are working at the back end so installing them one by one will take a lot of time so what you can do is a simple way of doing this is copying a requirements file or you can create your own file as well so here this is a standard version uh it's a standard way of doing this so what you can do is you can simply paste requirements.txt uh in this project so where do you get this requirements.txt you can download it from our website uh computer vision.zone it is free to download all you have to do is sign up if you already have signed up just sign in with your account go to the course page and there you can download everything related to the course absolutely free so you don't have to pay anything you can just download and you can copy it here so here you can see as soon as we copy the requirements.txt file it will give us a notification package requirements all of these packages have not been installed so what you can do is you can click on install requirements and it will install all the requirements that you need so what do these mean so for example CV Zone double equals 1.5.6 this means use the exact same version 1.5.6 don't use anything above it don't use anything below it the exact same version so right now I'm using python 3.10 so with this all of these versions should work fine now when it is greater than or equals to then it means that use this at the very least or if there's a newer version use that instead so this can be a little bit dangerous or tricky because if the newer version is not compatible it might not work so you have to be careful which ones uh are you hard coding to be exact same version and which ones are you saying for example here for python we have hard coded it to the exact same version normally it's not done like this but because the latest version has some issues in Auto connecting we are going to use uh dot six zero this is the version that does not have this issue so that's why I've hard-coded it to be this exact version now if you have a later version that works fine for you that's okay but if it doesn't then it's better if you use the exact same thing now sometimes what happens is that you install everything but some of them do not install and again that might be the issue of that it's not compatible they don't have a version that satisfy their satisfies the requirement so what you can do is you can downgrade or you can upgrade it's based on that for example uh scikit-learn for example this scikit image let's say it doesn't work or we can simply comment it and it will remove it from the install installations so later on if I give it a wrong number let's say like that it will give us an error that it was unable to install so in that case either you can come here and fix the number here based on the version or you can go to file you can go to settings and in add you can you can search for PSI what was the spelling so I get I believe no s c i k i t Dash image yeah so you can click on that you can specify version and you can select which version do you need it might be a higher version it might be a lower version so if the latest one doesn't work go to the previous one by default okay so that being said let's just install everything we will click on install requirements it will ask us that you want to install all of these we will say yes install and then we are simply going to wait for the installation to complete so the main library that we are using is ultralytics that will provide us with the YOLO package and it will download the YOLO weights for us as well so we will be using version 8 of YOLO and if later versions come up you can use those two depending on whether they are um are related to ultra analytics or not so all of these packages are being installed and let's wait for it to finish so all the packages have now been installed as you can see all of them are installed and we are good to go so what we can do is we are going to create a new folder and we are going to call it chapter 5 and Within Chapter 5 we are going to say running YOLO so then we will right click and we will create a new python file and we will call it yolo basics basics so there you go we have our first file and all we have to do is we have to test whether the YOLO will work or not and how exactly can we run it so in order to test YOLO we need to have some images so we are going to run it on images first once we know it it works then we are going to try it with webcam in the next chapter so we have some images again you will find this on our website uh once you download the zip file everything will be included in it so here you will see we have images and if you open up images the first image is of a school bus with some kids going into the school bus then the second image is of course and the third image is of some bikes so motorbikes so these are the three image images that we are going to test on so once we have that now what we have to do is first of all we have to import ultralytics so we are going to write from ultralytics Imports YOLO so that is what we are going to import and then we also need to import CV2 and I will tell you why okay let's just run it and I will tell you why later on so it is very simple with only two lines of code you can simply run your mod so first of all we are going to create the model we'll call model equals YOLO and then we are going to give in the weights so there are different types of Weights you have the Nano the medium the large it's based on you which you want to use now the good thing about this method is that you just type in the name of the the weights that you need and it will download the weights for you so for example we will write YOLO version 8 and we are going to write n for Nano so this will download the Nano version so we'll write here PT then we simply have to write results equals model and then we have to give in the source of the image so the image source is basically Images slash and we can write one dot PNG so this is our image and then all you have to do is you have to write show equals true so we want to see the image at the end so there you go so if we run this now right click YOLO Basics we are going to run it let's see what happens first of all it will download the weights once it's downloaded it will tell you that it is running on CPU so that is another thing we are going to discuss later on how to run it with GPU but for now nothing happened it did run the image it it told us that it is running at this this speed and this is the size but it didn't actually show us anything so what exactly happened it actually opened up the image and it closed very quickly because there was no delay it just opened up and closed so we need to stop it so here we will write import CV2 in order to stop it we will simply write CV2 dot weight key and we'll give the delay of zero zero means that unless the user inputs don't do anything so if we're on this now this time around when we run it this is the image that we get so we have the bus being detected we have the person being detected handbag a backpack backpack so there's a lot of good detections here and they are also giving you the confidence level of each of these so 0.35 0.78 of course the higher the confidence value the better the detection is so this is quite good we can close that but here again you can see that we have downloaded the weights and they are in our uh chapter 5 folder now in order to um make it a little more efficient we don't want to download it for each chapter because each chapter or each project we are going to have some weights so if we are using the same weights we should not have it uh being downloaded every single chapter so what you can do is you can create a new folder in the object detection YOLO we will create a new directory and we will call it yolo weights and we are going to put our weight within your low weights when we refactor it now it's in the yellow weights so here we can go back double dot slash that means go back and then we will write YOLO Dash weights slash YOLO 8 9 8 n so if we run this now it will run exactly the same way because it is able to find uh this file within this folder so if you want to try out different uh what do you call versions you can simply change the name here and you can write for example large and we can try to run it and see if it works so it is downloading now and once it's downloaded it should give us the results so Nano will be faster medium will be a bit slower large will be slower and you can check out the other versions as well which one do you prefer so this version is 83 MBS I'm not sure how much was this let's open that up okay this one was 6 MB so it's it's a huge difference in terms of Weights so there you go it has been downloaded and now you can see uh we are getting better results we have multiple results here we also found the skateboard which was not found with the Nano version and there was a wrong detection here for the bus that is not detected as well but here it's saying handbag when it's a backpack so that is uh still a mistake but it works fine so there you go so this is how you can download and you can run the models so what we can do now is we can run images.png2 so let's see what the how that works so there you go so you have lots of cars being detected uh some with confidence of 0.88 some with 0.85 some with 0.67 you can you can see the pattern the closer it is to the camera it actually detects it very well the further it is from the camera it might not detect uh basically the idea is how clear is the site of viewing so if it's able to detect if it's able to find the car a bit more it will be easier to classify it will be easier to detect and the confidence level will be higher but if it's far if it's blur then of course the confidence level will not be that much so that's the basic idea and then we'll go to images three and let's see how that works out so right now we are using the large version and you can use the Nano version as well okay there you go so here we have a lot of detections motorcycles and we have a truck at the back uh we have a person and they are overlaying on each other and it's a little bit difficult to see what exactly is going on and right now we are not really detecting we are not really telling the system um that put these bounding boxes and put the names and the confidence level this is done by default when you are saying show equals true then it's doing all of that so what later on what we'll do is uh in the in the next chapter we are going to put all of these manually ourselves we will not use the default version and that way we will have much more control on how we can see because right now they are overlaying on each other and it's not very clear so it will it's not very useful uh in terms of viewing so that's the basic idea that's how we can run different versions of YOLO the large the Nano the small the medium so here if we write m uh by the way I wanted to check if we run it with the Nano what exactly happens how many detections do we get so there you go so it says a car car and the person is not detected that was detected earlier and actually let me let me take a screenshot of this and I will open it up in paint so this is the one with the Nano and let's run it with large and then we will compare that will be a fun experiment uh exactly how much are we getting when we are using the heavier version okay so here you can see we do have can we yeah so here you can see we don't see the truck uh we have some motorcycles the cars are limited here here we have more cars here we have less and here we have a red which is for a person here we don't and here there's another truck at the back which is green uh we don't get that so yeah definitely we are getting more but is it correct or not that is yet to be seen So based on your requirement whether you want it fast or whether you want it highly accurate um again all of these methods all of these versions they are good not in a very general sense but if you have a controlled environment for example you're detecting cars or you're detecting uh pedestrians in in a fixed environment with a fixed camera then yes it can be very useful you can have certain limitations certain constraints some certain constants that this this will always be this you can add a mask that do not detect in these regions only detect in these regions and things like that it will definitely be useful to put it in your project so that is exactly what we are going to do first of all we are going to run it with our webcam and then later on we will try it with uh different projects how we can implement this in real time so now in chapter number six we are going to run YOLO with our webcam so let's see how we can do that so we are going to need the help of opencv in this case and we are going to put bounding box ourselves manually so that it is easier for us to decide what to display and what not to display so what we'll do is first of all we're going to we'll go to new and directory and then we are going to write chapter 6. and chapter 6 is Yolo with webcam so then we are going to right click and we are going to create a new python file and we will call it yolo webcam so first of all we are going to import ultralytics as we did earlier and we also have to import python so we will uh not python opencv so we will write prom ultralytics import YOLO and then we will write uh Imports Imports CV2 and then we will also import CV zone so CV Zone we will be using to actually display all these um what they call detections it will make it a little bit easier you don't have to but it will make a little bit easier so the first thing we will do we will create the webcam object so we'll write Gap equals uh CB2 dot video capture so this is what I was referring to earlier so if you are not getting any suggestions so if you write dot video and you don't get any suggestions then it means you probably are using a newer version of python of opencv so and this is the issue that we are getting with the newer versions so where is it open CV opencv there you go so opencv as you can see we are using version 4.60 whereas 6 8 is the latest version but still 6 8 has this issue that's why we are using six zero so we will write CV2 dot video capture and the video capture ID basically you have to given the ID number for your webcam I'm using multiple webcams so I will give the ID number one because that is what I'm going to use but if you are using only one webcam if you don't have multiple webcams installed then you should write zero instead of one so I will write here one and then you can specify cap.set you are going to set the width and the height so the width uh basically is prop ID number three so you can write 1280 this is the width and cap dot set that is the prop ID number four that is 720. so this is the height and we're going to set it at 720. so this is the basic idea and again if you wanted to there's another one which is mostly used in webcam 640 by 480 these dimensions are commonly used so it's up to you which one you want to use then we are going to write while true and we are going to write success and the image equals cap dot read and then we are going to write cv2.im show we want to show the image so we will write image and IMG that is the image we want to show and CV2 dot weight key because we want to give it one millisecond delay so this should run our webcam by default so if we right click and run we should have our webcam running there you go so this is the webcam as you can see me it's running live so we can close this and if you wanted to you can write 1280 by 640 and oh not 640 720 my bad 1280 by 720 and that should work as well there you go so now you have a bit more area to work with so again it's up to you which one you want to use so we'll keep it on this and let's go ahead and create our model so the model as we have seen earlier if you go to YOLO Basics here you can see this is how you can create your YOLO model and then you can find the results like this so here we are going to write model equals uh we are going to write YOLO and then we have to give in the weights so the weights we are going to use Nano for this purpose so we will go back we will write YOLO YOLO Dash weights and then we will write the version so this is Yolo version 8 and Nano so YOLO version 8 and Nano so I wrote the one y extra so version 8 and Nano dot PT so here we will go down and we will write the results equals model and we are going to give in our image and we are going to write stream equals true now if you write stream equals to it will use generators and that will be a bit more efficient than without it so it is recommended to use stream equals true so if we run this now let's see what happens so we should not get any errors that's what we are trying to check if we are getting any errors it means we are heading in the wrong direction there you go we are not getting any error so that is good so now once we have the result what we can do is we can check for individual bounding boxes and see how well it performs so here once we get the results we are going to Loop through them so we are going to write poor R in results uh we are going to get the bounding box of each there's each of the results so we will write here box is equals r dot boxes and now we have to Loop through the boxes so we will write for box in boxes we are going to find the X Y of each of the bounding boxes so there are two methods you can use X Y X Y which means X1 y1 X2 Y2 or X1 y1 and width and height so which format do you need it depends on you so here you can write box dot X Y X Y or you can write box dot X Y width height so which one do you want so I would recommend X Y X Y because it will be easier to input directly to opencv so we're right here X1 y1 then X2 and Y2 equals this and what we can do is we can print these values so we will copy these and we are going to Simply print them so let's see what do we get so we are getting an error not enough values to unpack expected for got one so it means they are just packed insides so what we need to do is we just need to get the first element of it so hopefully that will work out there you go so this is what we are getting so we are getting the value of 279 one zero three one zero three eight and seven one twelve so again then we have multiple detections then we are getting two values so here we are getting X1 y1 X1 X2 and Y2 so these are the values that we are getting so what we need to do is we need to convert them into integers so that we can actually use them so we can copy this part and we can paste it and then we can put it into integer convert it into integer and integer and integer there you go so let's run it and see if it works so there you go now we are getting the values you can see here we are getting the actual values that we can use with opencv so let's go ahead and create a rectangle to actually find out if we are getting the correct bounding boxes or not so here we will write CV2 dot rectangle rectangle and we'll give in our image and then we have the points so X1 and y1 then we have the X2 and X2 and the Y2 so these are the points that we'll give in and then we have the color so color let's put it it's B so let's put it at zero green is let's 200 and zero or let's make it purple so two five five zero two five five that will make it purple okay then we have the thickness let's put it at three and yeah that's pretty much it so let's run it and see if it works and there you go so we are detecting so if I bring in my phone you will see there's a bounding box around it and it shows that it is working fine so um let's let's try a pen can it detect the pen yeah it detects a pen as well it detects the phone and let's try the remote it detects the remote as well but again we are not actually getting uh the feedback of what exactly the glass is and uh how confident it is so we are going to detect those as well now uh as I mentioned earlier you can use a fancy rectangle as well which we provide in CV Zone package so in order to do that you can simply write CV Zone dot Corner rectangle and you will give in the image and all you have to do is given the bounding box so you can write here uh bounding box PB b o x so where is this bounding box coming from uh this is actually X Y width and height so we can get that too so we can write here a bounding box X Y width height and this will give us the X1 y1 will be the same and then we will have the width and the height and then what we can do is we can put it in bounding box so bbox equals this will convert it into integers um and there you go so this will be worth and this will be height there you go now this is a repetition so again we can use one of them we don't have to use both of them at the same time so let's put this one over here so this one and let's put this with it so you can use either one of them so not both of them at the same time because that will be weird we can remove that so this one is for opencv and this one is for CV Zone let's run that and see if it works and there you go we are getting something but it is wrong so why exactly is it wrong so we are getting X1 um what happened here so X1 y1 and then W and H so okay something seems wrong so let's try that um let's just get these values and we can subtract to get the width and height so the width and height equals x 2 minus X1 and then Y 2 minus y1 and hopefully that will work out so let's open this up and we can remove this and here for the bounding box we can simply write X1 y1 and width and Heights let's try it this way hopefully it will work there you go so it works now so we have the green boxes and then we can also have for example for the phone you can see it's a little bit fancier again you can change the colors uh the colors of these edges you can change the color of the inner rectangle and all of that all of this information is given here if you click on the control and click on Corner rectangle here you will see where is it is it's Corner rectangle there you go so it will show you this is the length and this is the color for the corner the color for the rectangle and the rectangle thickness and the thickness of the corner and all of these details are given here so you can check here and based on that you can apply this okay so this is good so the next step would be to actually get the confidence values and the class names so we don't have the class names off yet so we only are showing the bounding box so let's find out the confidence first so the confidence is conf equals bounding box conf 0. so this is the confidence so let's print that out and see if it works so if we go down and there you go you can see the confidence values but we want it to be rounded so what you can do is you can use math so Imports import math and then over here you can change this value so here we can write math Dot uh you can write ceiling or floor it's up to you so seal and that's it or we and based on what exactly do you want to round it off so we want to round it to two decimal places so we are going to multiply it by 100 and then we are going to divide it by 100. so that's the idea so if we check the confidence values now it will be two decimal places so hopefully that should be good and we are getting one because we made a mistake here it should be the bracket should be here so you are doing the ceiling for this and then you are dividing it by 100. there you go so now you're getting 0.94 so that is good uh now we can display it so to display we are going to use CV Zone uh now what exactly are we going to display we are going to display the confidence and the class name so we need a box a rectangle on top of which we can display this because if we just display it on the image it is not very clear but depending on the background so if the background is in contrast it will actually display well if our if there are pretty much the same color it will fade away so that's why we want a rectangle and on top of the rectangle we want a text now uh if we create a rectangle using opencv and then we put the text on it then it won't be centered uh if the text is bigger it will move away it will move out of the box and things like that so in order to fix that we have a function in CV Zone that will create the rectangle it will put it uh it will put the text on the rectangle and it will Center it based on the length of the text and all of this is done automatic so we can write here CB Zone CV Zone dots uh put text tracks and we are going to give in our image and then we have to give in the text so the text let's put it an F string for now because later on we are going to add some stuff to it so the F string will contain the confidence for now and then we'll given the position now the position should be the starting value of our X and Y so it will be X1 and y1 but because why one will bring it a little bit down we wanted to push it up a little bit so we can say minus let's say uh 20 or 30 or 50. uh let me check something like that so let's run that and see if it works now this will give us an issue and I will tell you what that is there you go now it's giving us 0.96 and if if I bring this in 0.94 that's good if I bring in my phone there you go if I push it up it goes up if I bring it down it goes down there you go so if I go up a bit there you go so that's the idea but here the problem is that if I go up you can see the the text actually goes out um and that that is not what we want uh we want it to display so what we can do is we can write here take the max whether it's zero or this value and take the max whether it's 0 or this value so whichever one is bigger because if it goes in minus then use 0. so it will not go above that it will stay Within so let's try that out there you go it doesn't seem to work very well uh if we go here uh if we go to the side it's working on the side but it's not working on the Y because we already have a minus 20 in there so let's remove the minus 20. and hopefully this will work better or or what we can do is we can put here 20. so we go up let me just go like this yeah it's not working very well so let's try 20 or y1 yeah now it's better so maybe make it 35 so it doesn't go beyond that there you go so now it's actually showing us so if I have this remote and if I push it up the confidence level stays there even though it's going out of the image there you go so that's that's good that's good let's see the phone yeah it's working fine okay so this is good now what we need is the class so we need to know what class are we detecting is it a person is it a car is it a bike what exactly are we detecting so this is based on the Coco data set and what we can do is we can get the class names for that so I've already created um what you called a list and you can get this list from our website again you can download it for free now if you don't want to do that the other thing you can do is you can simply type it from here so right now you can see it's showing the complete thing on the screen so if you want to go ahead and simply pipe it out you can type all these 80 classes so person bicycle car motorbike airplane blah blah blah and all of these so it should be in a list called class names so once we have that we need to find the ID number so if the ID number is zero it will be a person if the ID number is one it will be a bicycle ID number two is a car and so on so what we need to do is we need to find the confidence uh sorry the the class so here we found the bounding box this is for pounding box let's write here bounding box this here is for the uh confidence and now we are going to do it for the class name so for the class name what we have to do is you are going to use the same format you will write CLS you can't write class because because you can't use class for obvious reasons so here you will write class CLS and then box and you will write here CLS so that will give you the class name so let's print it out uh or let's put it on the rectangle so let's remove it here and we will put it directly on the rectangle we will write here uh CLS we'll give it a space and then this there you go so this is for the confidence this is for the class and let's see if that works out so right now it will give us the class ID it will not give us the actual name because we are not referring it to our list once we refer it to our list it will give us the name as well so for now it will just give us it should give us ID number zero so here you can see 0.0 it's decimal places I'm not sure why but uh we will figure that out the phone is 67 and that's a very high class remote is 65. so what about the chair can you find the chair can you find the chairs confused about about the chair anyways so what we have to do is instead of class what we will write here we'll write here class names and then we will put in here class but if you remember it was a floating value so we have to convert it into integer so we'll put it here as integer and then if we run this now this time around it should give us the class name as well foreign so if we open this up here you go person 0.96 confidence if we move around that's a suitcase I'm not sure why uh so sometimes it's a chair and then we have the remote that's very good then we have the phone the cell phone that's very good so that's the basic idea and let's see the pen oh it's actually a toothbrush I didn't know that so it is actually detecting this as the toothbrush that's fine okay so uh if you wanted to check it on a video you can do that too and if you wanted to make these uh names these labels a bit smaller you can do that too so here input text rect here you can write scale and you can given let's say 0.5 so that will squeeze everything down and it'll make it smaller this will be helpful in case you have a lot of detections for example you have a lot of cars on a highway or you have a lot of p a lot of people being detected so this will make it easier so there you go but then it's a little bit harder to read so you have to change let's say we'll put 0.7 then you also have to change the threshold sorry in the thickness the thickness let's put it as zero point five actually the thickness is not 0.5 if we check the default thickness it is no this is not the corner Rex where is put extracts put extract is here the default thickness is three so let's put it as one so if you put it as one hopefully it will be able to read it even if it's a bit smaller because we have a drastically reduced scale from 3 to 0.7 so the thickness well I I don't think we can go below one I'm not sure so there you go now you can actually read person 0.9 0.6 so if you bring in the remote you can actually read it so that's how you can increase and decrease the scale for it by default it's three and three so it's up to you if you wanted to check that okay so um actually let's keep it there because we are going to try it with the video and videos they usually are bigger so let's put scale as one just a little bit bigger and you saw that the Box actually scales up and scales down automatically so that's the best part of this function okay so let's try it with some videos so what we will do is we will copy and we are going to paste our videos folder in object detection YOLO so here we have some videos bikes cars motorbikes people PPE 2 ppe3 so all our these are different videos we are going to use throughout the course so we have all of them in one folder so here instead of video capture one let me copy that and I will write here or web webcam and here we will write for video and we will remove for the webcam and for the video all we have to do is we have to point it to the correct path so here if we go back we will go to videos and in the videos let's try let's try bikes bikes dot MP4 and this is the size and all that so in in case of the video you cannot actually set the size so let's put it here with the webcam like that so for the video that is good let's run it and see if it works there you go so we have a lot of people and we have bicycle we have person we have car it's a lot of detections we have traffic lights being detected as well so that's pretty good and then let's try with um cars cars again you can try with different versions so there you go we have some cars being detected looks pretty good and then we can try with motorbikes dot MP4 there you go so we have some cars being detected we have motorbike we have person so those are some pretty good results let's try it with the large version same video with the large version and see what happens most probably it will be a lot slower there you go and we but we are getting better results in terms of detections so that's good okay and uh then let's put it back to Nano and the people let's try people here p and E people and let's run up there you go so we are detecting people as well and that's pretty good so this is the basic idea of how you can use webcam with the YOLO version 8 to detect these different objects and this is quite an efficient way to do it so later on we are going to use it so right now we are actually using it with CPU so that's why when we go to the large version it actually is very slow so in the next chapter we are actually going to use the same code but we are going to run it with the GPU so it will be much faster than this so let's go ahead and try that out now in the previous chapter we saw that how we can run a YOLO version 8 with our webcam and we we took the help of opencv to actually do it now the problem that we saw that it is using a CPU to actually run all the computation and while I do have a very good CPU which is core I9 and it is 12th generation it is still unable to handle real-time information when it comes to the large version when we are using Nano version it's fine but when it comes to large or medium version then it lags very much so as you can see here if we run it right now we are detecting a video and then we are we are running a video with yolo version 8 large model and you can see the lag is very much so it takes almost 300 milliseconds to run an inference so only three frames per second you can say so that's not very good so what exactly can we do we can use our GPU to actually run it and hopefully the GPU will give us real-time results so how exactly can we run a GPU so first of all you need to have nvidia's GPU you can check the GPU level on their website the compute compatibility and based on that it will perform very good or very bad based on how powerful it is now when it comes to installations there's a lot of installations required to run the GPU but it is all well worth it because once once you actually get it running you will be able to run your GPU and you will be able to run your YOLO version 8 in real time and that is something that is very good to know so I've listed down everything in detail so let's go ahead and follow those steps the first thing you will need is visual studio so you have to install Visual Studio this is the requirement for the Cuda toolkit so this is the two a Cuda toolkit so this is the requirement of this kit so you have to install this first so visual studio but what exactly do we need in Visual Studio once you run it you have to download the community version so whichever one is the latest one you can download that but then the the biggest thing that we need is the development with C plus so you will need that to install that as well so make sure you run uh install it so once we have that then you have to go ahead and this will take a while because it's uh quite a few GBS so it will take a while to download and install once that is done you will go to the official nvidious drivers so for example I have the nvidia's um RTX 3080 TI so I will select the g-force in GeForce I will select the 30 series and then GeForce RTX 3080 TI and I'm using Windows 10 so I will select that you will select the studio driver and English and you will search and you are going to download it now this is a simple wizard you have to click next next next and it should install quite easily once that is installed you will go ahead and install Cuda toolkit you can install by going to their website so this is for uh for the Nvidia developer you will click on Windows then x8664 then you will click on 11 and then you will download the local version so I have I believe Coda 11.6 let me check so once you download it will install and I will check my version as well so it will install in program files and if you go down Nvidia GPU Computing toolkit and if you go to Cuda this is 11.5 so I have 11.5 based on the version the latest version you can download as well so here it's 11.5 we will open that up and you can see again this will be an installer it will be a wizard you have to click next next install it in C and in C this is where you will find all the files so keep this folder open because next we are going to install Cuda DNN which is which stands for deep neural networks Cuda deep neural networks so again you have to click on download uh the Cuda DNN and this will need what you call the registration on their website so make sure you have registered and it is free to download so just make sure that you have registered and it will let you download the Cuda DNN so once you download that this is what you will get so make sure the versions are compatible so right now this is for 11.5 which I installed as the toolkit so I have to download it for 11.5 uh the Cuda DNN so this is what you will get so if we open up our folder to install this all you have to do is you have to open up your folder here so you will see here you have a bin folder so this is the Cuda toolkit I don't know why it's so hard to pronounce this so this is the Cuda toolkit so this is that and this is the DNN so if you go to bin here you have a bin folder here you have a bin folder open up bin and open up bin here and copy all of these files and paste it here so just drag and drop so I have already done that so it will ask me to do it again I will not do that again so then you will go back then you will go back and you will go back here we have already done the bin then you will go to include so in include again you will open include here copy all these files drag and drop them here so in the main folder like that so then you will go back and then you will go to lib and inside lib you will see all these files but here you will not paste them directly you will go to x64 and over here you are going to paste all these so drag and drop all these over here so this way it will install your Cuda toolkit along with the Cuda deep neural networks Cuda DNN so this is basically the installation and one last thing you have to do is you have to go to your environmental variables so you will click on edit system variables and here you will go to environment variables and over here you will go to Cuda path 11.5 so this is the version that I have and you will make sure that it points to the correct directory so if you remember we had it in program files Nvidia GPU Computing toolkit Cuda and then version 11.5 so make sure this and this they are pointing to the right path so if they are pointing then it's fine if they are not type it in make sure they are in the correct path so we'll hit OK and that is it so now the the drivers and the Cuda toolkit and the Cuda deep neural network is installed but there is one more step we do need to install Pi torch which is compatible with Cuda so here you can see in the requirements we have already installed torch but now we have to install the torch which is compatible with the GPU or with Coda so how can you do that we can go to their website so this is the pytorch website and this is how we can install so we are using the stable version for Windows we are going to use pip to install and we are installing it on Python and we are installing for Cuda 11.6 and this is the command that we need to run so we are going to copy that again I will share this command so that it's easier for you to install so you will copy that and here at the bottom you are going to go to terminal now this is very important because some of you might make this mistake and it is very frustrating if you make this mistake make sure you are not in local click on this down arrow and select command prompt because if you are at local it will install it globally we want to install it in this virtual environment so you have to go to the command prompt and here you can see it's installing it in the virtual environment so here we are going to paste our link and we are going to press OK or we are going to hit enter so this will take a while because I have done this already uh it has all these files archived uh in the cache so that's why it is able to install this very quickly otherwise it will take a while to actually install all of these so once it is done we can go back and we can go to we can close all of these and here in the webcam we can right click and we can run YOLO so if it's actually using the GPU then it will right here GPU so right now it's not actually showing GPU so it means there is something wrong uh it did not install it properly so if we go to file settings an interpreter if we go to the torch over here it is using the same torch that we had earlier so it it's probably not using it properly so what we can do is we can uninstall foreign just to make sure and we can uninstall the torch audio and the torch vision we can uninstall those we'll press OK and then we'll go to our terminal and then we will run the command again so again you can see it is using the cached otherwise it is quite a big file it's almost 2.4 GBS so it will take a while to download and install I think what happened earlier was that all the requirements were already satisfied the torch was there Vision was there so it didn't actually reinstall it didn't install anything so I believe that's what happened so you need to uninstall and install again so it is currently installing so let's see how it works so it is saying that requires a later version of opencv we are using this um I believe it should be fine so otherwise it has installed so let's run that and see if we are getting so it is updating that's why it's not running let's run that again there you go so now you can see it says ultralytics YOLO python blah blah blah cuda0 Nvidia GeForce uh 3080 ti so this means now it's not using the CPU it's actually using the GPU and if we open up there we go now it's lighting fast so it is using the GPU earlier it was using the CPU and it was dead slow but now it's using the GPU and remember we are using the large version for this demonstration and still it is running real time so that is very good it means the gpus has been running properly and it has accelerated um our video our inference and that's how it's running so right now the inference is 10.5 so 10 milliseconds earlier the inference was 300 milliseconds so that is a difference of 30X so that is a very big big difference when we are using the GPU so that's great and uh again if you run into any issues go back and check the video again follow through the steps one by one and it should work fine at the end so again it's a big difference so it's well worth trying this and if it runs properly you will have a much faster system that will be running YOLO in real time do you want to implement your computer vision ideas to solve real world problems or upgrade your resume by enhancing your computer vision skills then the computer vision.zone is the perfect platform for you CV zone is a One-Stop computer vision platform with over 100 000 users with courses sold in over 80 different countries if you want to commercialize your computer vision ideas then the computer vision web development course is the perfect course for you here you will learn the basics of web development and computer vision as well as how to integrate them to solve real world problems like customer engagement car counter face attendance shirt size measurement and a lot more advanced Zone programming is another great course that focuses on the Practical implementation here you will learn the basics of drone programming as well as advanced concepts like face following body following gesture control and a lot more if developing mobile apps is your thing then check out our computer vision mobile apps course here you will learn the basics and create several apps including object detection augmented reality face detection document scanner and a lot more the best part is that you will create a single app that will work both on IOS and Android if you are passionate about integrating Hardware with software then check out our computer vision Arduino course here you will learn the basics along with amazing projects such as conveyor belt assembly face tracking lab gesture control phase door lock and a lot more the computer vision game development is another great course where fun meets programming here you will learn to create games such as Fruit Ninja balloon pop squid game Cookie Cutter all using the latest computer vision techniques not only that you will learn to compile your awesome game to an exe file to make it accessible to all you can also learn to implement computer vision on embedded devices with our computer vision with Jetson Nano cores here you will learn the basics of Jetson Nano and computer vision along with creating exciting projects like Lane following robot eye tracking object following and a lot more all these courses have a clear path from Basics to Advanced with maximum knowledge in a short amount of time so check out the links in the description to get started with their computer vision Journey today so now that we understand how to run YOLO and how can we run using GPU we are going to create some projects out of this so the first project that we are going to do is a car counter so within a video we are going to count the number of cars that pass a certain region so to do that we'll first create a new folder we'll go to oh not a new file we'll go to new and we'll click on directory and here we are going to write project one and the name of this project we are going to call it car counter So within our car counter we are basically going to copy the code of YOLO webcam so we'll copy YOLO webcam and we are going to paste it in the project one so here we will call it car dash let's write it capital car dash counter lot Pi so we will have the exact same code that we were running earlier and all we have to do is we have to make sure it's running with the videos we can remove the webcam part and we are going to run cars.mp4 so we will have a lot of things done and set it up already while we start this project so right now you can see it's running on GPU it's using 3080 TI and there you go we are getting the cars and we are getting there bounding box information their class their confidence and you can see uh when when it's too small it's not actually putting the corner rectangle properly so what we can do is first of all let's fix that so here uh where is it Corner rectangle uh what we can write is the length equals I think by default it's 30. yeah it's dirty by default let's put it at 15 so this is the length of the corner so the green part that you see that is what we are referring to so we can make it smaller yeah so there you go now it's smaller so the green part and if you wanted to you can even reduce it further you can make it like say five then it will be really small let's see how that works out there you go um it's too small maybe eight or nine let's say there you go I think this is better okay so we have the cars coming in we have the rectangle on top of it we can actually make it smaller so that we can see it better so let's make it 0.3 or let's say 0.6 and the thickness is still one we cannot reduce it further I believe so there we have it now we have it smaller but you can see the the the title itself is smaller but there is some offset around the region which is quite huge so we can reduce that too so there is a very there's a variable called offset uh called offset and by default I believe it's 15. do we have an offset here no the bounding box um yeah put text right we have offset as 10. so let's make it 5. or let's put it three because we don't want a huge offset we want the cars to be visible there you go so now it's much better you can see we have the offset and we can see the cars coming in it looks good okay so this is the basic idea that we have these cars coming in now the thing is that it might not be a car it might be a truck but then what exactly are we trying to find so if you just want to do cars you can add a if statement that if it's a car then detect it otherwise we can find out what are the vehicles that we want to find for example in our case let's say we will detect the car we'll detect motorbike we'll detect bus and then we will detect truck so these are the four categories that we are going to detect so if we go to our cars video let's open that up there you go so if you go a little bit back there you go I believe this will be detected as a truck this is a car that's fine this is a motorbike so we want to detect that too and then later on we also have uh yeah I believe this will be detected as truck as well and this should be detected as a bus so we have these different categories and we want to detect all of them so in that case first of all we have to give an if statement that only detect these categories so here what we can do is we can write here current class equals uh the last names at CLS so this will give us the class name so over here as well instead of writing this we can write current class and over here we can write if the current class equals car then only we are going to display it so in case of motorbike and in case of the truck it will not get detected and what we can do is we can put a zero here so every time we press the keyboard button it will go forward otherwise it will not go forward by its own so there you go let's I will keep pressing and let's move forward and here you can see it is detecting it but it is not displaying the confidence and the class value over here as well it's not it's because we are displaying the rectangle the rectangle is displayed here the rectangle is if you wanted to remove that as well we can do that but right now it is not displaying the confidence and the class values so that is what we are trying to do so here it's not detecting it so it means the motorbike is not being detected and then here the truck as I mentioned it's not been detected then let's move on to the bus so as you can see the bus is detected here you can see it is detected but it is not classifying it because we said that only when it's a car so this is a bus and this is a truck it's not detecting these two because we did not clarify we did not Define them so here we can say and we can also put the confidence level so if the confidence level is very low then you should not display so here we can write and confidence is greater than at least 0.3 so that's what we are writing here that if the confidence is greater than 0.3 and it's a car then only you can detect it so we have four categories so we will write for all four of them so we'll write here it's a car or it's a truck or it's a bus or uh it's a motorbike so let's go down or it's a motorbike we have motorbike right yeah we have motorbike so car motorbike uh bus and truck so these are the ones that we're detecting rest of them will not detect and we will not even put the rectangle around them so we can bring it down here in the if statement and we can paste it here so let's run it and see how it works so now it will detect only those so here you can see it was detecting something here it was detecting probably a traffic sign but now it's not detecting that because it's not part of it and here you can see it says motorbike at 0.56 or five nine percent uh confidence so 59 confidence so here you can see car this is detected as a truck this is also detected as a truck so then we'll move forward and we'll wait for the bus whether it's selected as a bus or a truck so this is truck as well and this is truck as well then there you go we have the truck so earlier it was detecting as a truck but here it's detecting as a bus so actually this gives us a good uh point that this point is actually where most of the cars will be classified properly at the very end it might not classify properly at the very back it might not classify properly but at the middle it should detect and classify properly so that is the region where we should count so this is where it gives us the idea so if you're trying to find that position try to Loop through these images and try to find that correct position where the detections are correct so that's how you can tell oh the good position where the good position is okay so that's done so we have the cars now but as you can see we have it in the wrong places as well so for example we don't care about what's Happening Here if we have some counts over here it will interrupt our count for example if a car is com going out from here we don't want to detect that we just want to detect at this point on the main road so you have to constrain your values so to get good results otherwise if it's completely open it's not constrained at all you might not get the best results okay so how can we constrain this how can we make sure that only this region you detect uh what you call uh the cars or whatever the the object is so once one thing you can do which is very simple and that you can do within opencv or within this pycharm environment is that you can create a rectangle so you can crop this image you can create this rectangle here and you can say okay only detect in this and how can you say that you will crop that image and that image you are going to send it to your detector but that is not a very good idea because the shape over here is not rectangular or not Square so what we want is the exact shape so how can we do that now it is very simple we will go to canva.com and we are going to create a new design and in this new design this design is 1280 by 720. so this is the size of our video and that is exactly the size of the design and then we are going to import this video so here this is the video you can see it's the exact same one now what you will do is you will resize it so that it is the correct size it fills up the whole screen and then you are going to press r that will give you a rectangle so with this rectangle what you have to do is you have to determine the region so we are going to rotate it and we are going to align it with this dotted line this Lane we are going to align it with this and for the other one we are going to align it with this Lane so let's go ahead and do that so we will align we'll try to align it as much as we can so something like that something like that so there you go and you can move it around like that there you go so this is the first one and the second one we are going to basically what we are trying to do is we are trying to create a mask so that it only views in that mask region so we are going to put another image here there you go another rectangle here and then this area again is very small and it's not very useful so we are going to remove that as well so we'll create another rectangle and we'll put it over here something like that and at the bottom as well we don't want to detect so we'll create another one and we will put it here something like that so depending on which region you are trying to detect you can create this so the then you're going to select all of them and you are going to change the color to Black you can do that in the beginning as well and then we are going to extend it so that only that region is visible there you go that's how simple it is that's how you can create this mask and all you have to do now is click on the video and delete it that will give you the white reason so then you can simply share download and you don't need transparent background you don't need to compress all you have to do is you have to download the image once it's downloaded we are going to import it so here is the downloaded file we are going to bring it to our project so project one car counter and we are going to name it mask dot PNG so if we open that up in projects let's close the others so here mass.png this is our mask so we can remove that and now what we have to do is we have to Overlay these images so that only this region is detected and uh we only find the cars within these regions so now what we need to do is we need to import this mask so here at the bottom we are going to say mask equals cv2.im read and we are going to read from our image so we'll write mass.png and once we have that and we get our webcam image or we get our video image then we are going to Overlay uh the mask on our main image we are going to use a bitwise operation of and that will give us only that region so we are going to we are going to say image re Jin equals CB2 Dot bitwise and and we are going to give in our image and we are going to give in our mask and this will give us the IMG region and we are going to display that image region so let's copy that and paste it here so we'll write it as region and we will write image region so let's run that and see if we get that region as well and make sure they are of the same size if they are not of the same size it will not work right now I know it's 1280 by 720 that's why we have created the image of 1280x720 of the Mask otherwise it will not work so there you go this is your mask this is the image region and as you can see it will only detect in this region that's what we need now so we want to detect only in this part actually the mask is wrong um it's it's not perfect because this car is is not visible so this is not correct something is wrong let's go back to canva and let's undo yeah this is not correct we need to go a little bit higher there you go I guess this is a better representation there you go so then we are going to delete this so now it is our new mask then we are going to download and then we are going to delete this previous mask and we are going to drag in our new mask and we will call it mask dot PNG and then we are going to run it again and hopefully this time around we will not face the same issue okay so this is our image and this is our mask there you go so if we plate that seems fine to me yeah that's good okay so now we will send only this image to our system so in terms of computation it will be more efficient as well so instead of image we will send image region that's how simple it is so let's run that and there you go so based on our image region as you can see only this area is being detected as cars So within this region we are detecting cars behind it we are not getting it on the side we are not getting it so only in the middle in the specified region we are getting the detections so if I play it once it reaches that region it detects it as a car or a bike and all that so that's good as you can see it looks good yeah okay so now that we have a specified region and we can see all the cars being detected now we need to count them so in order to count we need to have a certain region that this is the line when it passes this line we will consider it as counted so we need to know that second thing we need is an actual tracker so right now what we are doing is we are detecting the cars but in the next frame we do not know where the car has gone so what we need is a tracking ID so if we detect a car in the first frame we need to know where did that car go in the next frame so that we can assign it the same ID so in the first one if it's ID number one in the second frame it should remain ID number one it should not go to item number two or three or four or five so this is basically a tracking problem so within the consecutive frames we need to find out where is our object moving to so that we can assign them unique IDs so to count we need to find first a Tracker so the tracker that we are going to use is called sort and you can find it on GitHub and here you can see it's by Abby Welly and thanks to him and the contributors who actually created this tracker and it is very easy to use and here they are showing us how to use as well so all we need is this sort dot Pi so if we click on that this is the code what we will do is we will download this file of course this file will also be available uh with our ZIP file so you can find it there or you can come to GitHub and download it from here so we also have some requirements uh filter by scikit image and lab so these requirements are already fulfilled because we added that in our requirements as well so filter by scikit image and lab so these are the ones that we have already installed so what we need to do is we need to bring in the file so let me copy that and I will bring it here into our project one and this is a simple sorter a simple online and real-time tracker copyright Alex Beverly you can see here so this is all the code we are not going to do anything uh to this all we have to do is we need to know how to use it so here to import we are going to write import sort or actually what we can do is we can write from sort from sort import everything so it will be easier for us to work with so this is what we are going to do and then we are going to create an instance for the sorter so what we will do is sorting or let's call it tracking tracker equals sort and then we have to give in the maximum age so what is the limit of the number of frames uh that it is gone and we still recognize it within that region so if ID number one is lost how many frames do we wait to detect it back so the higher uh the longer it will wait for it to come back so maximum age let's put it as 20 minimum hits let's put it as two and then we have the IOU threshold again these are parameters that you can change but they have default values as well if you go to sort maximum age is one which is really bad uh it's not really useful then we have minimum hits as three and the IOU threshold as 0.3 so I will I will keep them the same uh let's put it as three and IOU threshold is 0.3 so again um this we have explained in the theory part so if you're not familiar with this go ahead and check that out this is the intersection over Union threshold to find how good the overlap of the bounding boxes is so uh these are the values you can play around with them and see what type of results you get now in order to run this it's very very simple all you have to do is you have to write tracker dot update and that's it so you need to update it with a list of detections so we will write here detections now we need to find these detections which we already have by the way because here we have the bounding boxes but what we need to do is we need to put them in an array we need to put them in a list and we need to make sure that the format is exactly what this requires so what format does it need we don't know we can go into the sort and we can check here so uh in the update it says this is a numpy empty so by default it's a numpy array of 0 by 5. so this is the dimension so we have five values so the format is X1 y1 X2 Y2 and the score so this is what we have to feed in and what do we get out of it we get erect concatenated um maybe it's not mentioned here what we get as the output or the return is X1 y1 X2 Y2 and then we also get the main thing which is the ID number so that is what we need so let's just copy that so where is the update here so our detections by default if we don't have any detections this should be our type so we should have a numpy list so right after we get the results we are going to create a list or a array of detections detections and this will be empty 0 by 5. so that is the idea and once we have that then we are going to once we detect it as a car or a truck or a bus or a motorbike and the confidence is greater than 0.3 then only we are going to save it to our detection list or detection array so then what we are going to write we are going to write that our current array equals numpy dot array and we are going to give in the X1 X1 y1 X2 and Y2 and we will also give in the confidence value so these are the five values that it requires and then what we have to do is so normally if we have a list what we do is we write dot append so if you want to add it to the list you just write append it goes down it goes down goes down like that but in in a numpy array we don't write append uh what we do is we stack so we will do a vertical stack that's how simple it is so we'll write the Texans equals numpy dot vertical stack and we have to give in our current detection the old detections that we already had and the current array the current array so we stack them together so that's the ideal so once we have these detections now we can simply send it to the update and again this update is not dependent on what did we detect new because we need to keep updating it from the previous times as well so we already have an empty one and it will keep updating so here we have the detections and then we need to get the results of this so we can write here results uh for the tracker equals this results of the tracker equals this and then we can Loop through the results of this tracker so you can write for example for results in results tracker we are going to get the X1 y1 and the X2 and the Y2 and the ID so which is the most important thing that we get the ID and that is basically the result so let's print out the results actually we can print out this as well later on we are going to use this that's why we are writing it like that so let's run that and see if we get something hopefully there are no errors um otherwise we are heading in the wrong direction okay there you go so now we have our three IDs perfect so we have ID number one ID number two ID number three let's press spacebar and then again it is moving on to the next one and you can see it's the same ID then it moves on to the next one and you can see the fourth one has come up and it has increased that ID there you go and then the fifth one comes in and it shows so it couldn't find probably uh the other two and that's why it's showing only three okay so in order to understand this better what we are going to do uh we are going to display these ID numbers so it will become easier so instead of showing it here or let's do both we'll show it here and we will show it here as well we'll keep the thickness of this bigger so we are talking about the rectangle so the rectangle thickness here so rectangle thickness equals let's say five and for the other one we will make it a little bit smaller so we can see what is the actual detection and what does it think where is the tracking part so how well the tracker detects the bounding box so let's let's try to find that so what we'll do is uh again what uh we will simply use these values are they integers yeah they seem to be integers so what we can do is we can simply uh create our Corner rectangle so we will write here CV Zone dots Corner rectangle image and we have to give in the bounding box information so the bounding box information uh will be like we did here let's copy that and uh actually let's copy this corner rectangle because we're lazy so there you go so X1 y1 width and height and this time around we'll put the rectangle as let's say two and we will put it as blue the color so the color for the rectangle let's put it as 255 0 and 0. and let's show the ID as well so here we are showing using put text rect right so we will copy that and we will paste it here and instead of confidence in current class we are going to display ID entical oh the ice Capital my bad so this here why did I put I Capital let's put it small ID like that okay so ID and yeah that's fine and positioning is X1 y1 so X1 y1 is already bugged like it's something is already there we have the confidence and all that actually let's remove that because we know already we have the classes and all that uh for its car or bus or whatever so we can remove that and now we'll get the ID instead of that okay so let's run that and hopefully we'll get some good results so expected sequence length 2 got 4. what is happening here um so it detected something then it said Corner rectangle we have an issue image color RT opencv by argument rectangle expected length 2 got 4. okay um so we are getting an error here the corner rectangle uh why are we getting an error here uh let me check the corner rectangle color R and color C r t or is it TR it's RT yeah RT what is this uh let's let's print out print out X1 I just want to make sure that X1 is actually an integer and not a floating value because if it is then we have a problem there you go that's why yeah that's the issue so we have to do the same thing we did earlier uh we have to do this we have to convert them into integers there you go so let's run that and hopefully it will be fine now there you go so now it's showing us okay the IDS are very small let's make them bigger so put text rect ID Max is this and that scale let's put it as two and thickness let's put it as three offset let's put it as 10. so it will be big values we can clearly see what's going on there you go uh point hit the point so ID we need to convert the ID into integer as well so let's just do it here integer ID Okay so there you go so this is id1 id2 ID3 now is the moment of truth so if we go to the next one these IDs should remain same if they flip around it means it's not tracking it's only detecting so if I press spacebar and there you go it's the same so if I press spacebar again again it's the same so one two three four if I keep pressing there you go so the four was lost but it kept it uh it brought back the four from where it left off and you can also see the Blue Line the blue line is basically what the tracker is detecting and the purple one is what the detector or the YOLO is detecting so if we keep going forward you can see this is now five and this one is nine now this is not an issue because if it doesn't go to Five it might have detected some wrong IDs but as long as the ID Remains the Same if 9 remains 9 then it's fine because we will have a line after which we cross we will detect it or we will count it so that's fine so there you go it's it's 14 it's 14 12 is 12 18 is 18 19 is 19 20 is 20. uh 23 is 23 24 is 24 and that's all good so we can make a line here we can make a line here and whenever that line crosses whenever that ID crosses that line we are going to detect it as a count now uh how can we do that it's very simple all we have to do is we have to first of all create the line so we are going to write here that our line equals so this value should be actually there's image region coming here let's remove that because we don't need that anymore now this line uh I have checked the values already so we are going to put that uh let's call it limits let's call it limits instead and then we are going to write here 423 297 673 and 297. so these are the limits and using these limits we are going to create a line so this line let's draw it after the tracker so here we'll write cv2.line and we'll give in the image and then we have to given the points so the points are basically limits at zero and limits at 1. then the second point will be limits at zero uh sorry limits are two and uh limits at three and then what do we need then we need the color so the color let's put it as red so it will be 0 0 BGR so 255 and then we have the thickness let's put it as five and that should be good so if we run this now we should have a line right in the middle of our road so there you go so this is our line if these IDs if any of these cars they cross this line then it will be detected as what you call detection actually what we can do is we can extend it to the left a little bit because there's a gap here so let's go over here to the limits and in the uh X we can make it let's say 400. we'll move back 20 pixels 23 to be exact yeah Knight's perfect so now it should be fine uh now what we need to do next is uh let's remove the the detection part so we don't need to draw the the rectangles when we are detecting only for the tracker so here the corner rectangle we can remove that so now it will only show us the blue ones there you go now it's only showing us the blue ones actually I like I like the purple color let's make it purple uh 255 and make this to five five as well so now the the rectangles will be purple there you go so yeah I like that better so uh the next step is to find the center point so once we have those Center points we need to check if that Center Point actually touched this line if it touched the line then we are going to say it was a it was a count so that's the idea so how can we find this this is our results we have the X we have the Y we have the where we have the height we have everything all we need to do is we need to find the Center so we will call it CX and c y this is the center X and the center Y and how can we find it it will be X1 Plus the width divided by 2. and it will be y1 plus the height divided by 2. so just to make sure that we are headed in the right direction we can draw it CV2 dot Circle and we'll give in the image we will give in the CX and the Cy and then we are going to give in the radius let's give it five I don't know why I'm giving everything 5 today so then we'll give in the color two five five zero and two five five purple again and the the thickness let's put it as CV2 dot filled so we want it filled completely so just to make sure that uh the circles are at the correct position we are going to draw them and there you go so here you can see the circles and as soon as this circle actually falls in this region we are going to count it as detected one there you go so how can we do that we can write that if we need to check the limit of the X and we need to check the limit of the Y now the limit of the x is straightforward we have starting from here ending till here it should be within this region that's simple enough so we can write that the limits at zero this is our first limit and the limits at one no at two this is the second limit so x and x these are the two limits so the first one is 400 the second one is six seven three so that's the idea so we are going to say that if CX is in between that and c y is in between what limit now we don't want to say if c y equals the exact value of our limit no we don't want to say that because this is just single value the height is a single value 297. so we don't want to say if the value is exactly 297 then detect it as a count because sometimes the car may be fast and it might not touch that pixel value so putting it one as one pixel value might be a problem so what we need to do is we need to create a region so this is our line we will have this region can you see yeah so you will have this region in which if it lies then it is detected as a count so what we can do is we can simply write that limits at zero uh no at one plus 20 or for this instance minus 20 and for the next one we can say Plus 20. so if it's if it falls in that region then only it will detect it as detected so then we are going to say that total counts plus equals 1. and we didn't Define total count so we will write here after tracking we will write here uh total count equals zero so total count plus equals one and all we have to do is now we have to show the total count so let's copy this CV Zone function um uh we'll put it on the image we will put we will write count and Colin and then we will write total count uh it's already integer so we don't need to worry about that and we will given the value of x as um let's say 50 and Y as let's say 50 as well so 50 50 and the rest we will keep at default uh what happened okay so let's run that there you go so the total count right now is zero because nothing past that region so if we keep pressing and there you go now it entered that region and it counted as one but now you will see an issue if I press again it counted it as two because it is still in that region uh because it's not just a single line it's a region where it lies so because it's already there it's counted it as two now if we make this region very small it might not it might not count it at all because it's very low but if it make it if we make it very big then it it will have multiple counts so we need to have a balance first of all we need to have the balance and the second thing that we do need is we need to check if this ID is already counted or not if it's already counted we will not add it to the counter that's how we will fix this issue because if I run it again at that same point it has counted three times so that's not good okay so how can we do that instead of making it a variable we will make it a list I'm talking about the total count so instead of making total count a variable we'll make it a list and in that list we will put IDs so one two five eight nine things like that so then whenever we detect a new count we will check if that ID is already present we will not count it if it's there we will not count if it's not there we will add it okay so how will it look like so total count we will make it a list and then we will go down and here we will say total count dot append and we are going to append the value of the ID okay but how can we check if it's already there or not in the total count we will check we will write if uh the total counts dot count of the ID equals zero then do this now what does this mean so basically we are telling it to find the number of times when we write dot count it means count the number of times this ID is present in this list so for example if it was 5 here it will count the number of times 5 is available here if 5 is not available it will be 0. if there were two fives it will be one oh sorry two and then if there is no 5 it will be zero so this is what we are saying so we are saying if this ID is not already present then total count dot append ID so this time it should be correct but now it is a list it is not an actual value that we can just write here so all we have to do is we have to find the length of this list so that will be the total count so let's stop that and run it again and this time around if we click this is count is one now if I go to the next frame it should not count there you go it didn't count if I go again to the next frame it should not count didn't count now it should count because that's the second ID then it should count third ID fourth fifth sixth seventh eight and so on so if you if you see that some of the detections are missing uh what you can do is you can increase this value you can make it 30. because right now uh we have another check that is checking uh whether it's a duplicate or not so we don't have to worry about that and the second thing we can do is we can change the color of this line we can change the color of this line so that whenever it's detected something it turns green so it's an indication that it had a count so we can print it maybe here at the end or or we can simply print it again in red color I know it's it's a weird way to do this but you can do this you can just simply print it again and instead of red I will print it as green so it's basically overlaying on top of it okay there you go so if we run this now at this point it is detecting as a count because it's within that region and then it's detecting maybe it's too early okay maybe it's too early Let's uh reduce this to 20 back or let's put it as 15. and let's put this as 15 as well yeah I think that's better two three four five six that's correct then seven that's correct uh then we have eight that's correct nine this will be 10 this will be 11 12 13 14. so it should be 14 till here uh then 15 16 it should be 16 when it crosses these all 16 that's correct then 17 18 19. 1718 that was 19 then 2021 it should be 21 after this this is 22. okay this is getting confusing that's why we have the computer to actually find it for us so what we can do now is we can go down and we can put it as one and let's see the magic happen so there you go it's really fast and it's detecting all those counts and and of course it's using the GPU that's why it's so fast 62 63 64 75 blah blah blah blah blah so that's good so um so now everything seems to work fine but what we can do next is add a little bit of Graphics so that it looks a little more appealing so I have a graphics file here again you can find it on our website uh within the zip folder it's free to download just go ahead and download so this is the graphics.png we are going to Overlay it on our main video and then we are going to display the numbers on this so how can you do that we are going to read this file we are going to say image Graphics equals cb2.im read now the reason we are importing it uh within the while loop is because if you don't do that the graphics quality will be really bad over time it will be it will give very bad results so you have to import it every single iteration so graphics.png and we will write CV2 dots unchanged image unchanged IM read unchanged so you have to write that otherwise it will not work properly because we are going to remove the transparency so then what we need to do is at the very end we can overlay the image or yeah we can overlay it over here as well it doesn't matter so here we are going to write that um cvzone dot overlay PNG so image uh the first one is the background or the the first one is the background so this will be image and the second one is our image Graphics it will overlay and where is the position the position is zero zero it's right at the corner and we will put it back to our image um in the main file so let's run that and see if it's in the correct position if it is then we will overlay the count on it there you go so now the image is there looks good and now we will remove this rectangular count and instead of this count we are going to Simply write CV2 Dot put text and we are going to write the image the text will be basically string of the length of the total count and then we will have what do we have next the origin uh the origin let me check it should be 2 5 5 and 100. then we have CV2 dots font Hershey plane and then the the font scale let's put it as five uh the color let's put it as black or let's put it as red 50 let's let's read 50 50 255. and the thickness let's put it as eight so let's run that and see if it works and there you go so now you can see the count it looks really good so normally you won't be able to count it this fast so finally the the robots or the AI is helping us solve problems so this was our project for car counter and it was a really really simple and as you can see it's very fun to do and it is very informative so I will see you in the next one so now that we have completed our car counter project we are going to do a similar project to that which will be people countered so the difference between car counter and people counter will be that we will have multiple directions that we are counting in so if we go to our video people .mp4 and we have a look at the MP4 you can see that we have an escalator that has two directions uh one is going up and the other one is going down so there are people going down there are people going up so we are going to count how many people went up and how many people went down so that will be the task for this project so what we will do is first of all we will copy which is the best thing ever will copy and paste our complete project now what this will allow us to do is to use our previous code and we can adjust it to our new video and our new purpose so we can write here people counter and this is Project number two so we will hit OK so project number two people counter first of all we will open it up and we will rename it uh where is the rename here is the rename so we will write here people counter there you go so the right people counter here we have sort mask and Graphics now the mask and Graphics are going to change but for now just to make sure everything works fine uh first of all we are going to change the video we will write here people and then we are going to run it to see what happens so as you can see we are still using the large version so there you go so we have the graphics over here which is for the car and we have a line here which is basically counting but as you can see it is not counting anything because it's not detecting anything because if you remember we asked it to only detect uh cars bikes trucks and um motorbikes so that that are those are the four classes that we want to detect so that is not being detected here that's the first thing the second thing is that we have the mask which is being overlaid um uh in the wrong place we need to remove that too and then we also have the counter or the graphics which which is wrong as well so first of all let's bring in the graphics again you will find the graphics um in the zip file when you download it from our website so I've created this earlier and this is how it looks so this is the graphics so what we can do is we can simply replace these Graphics right away so that we have one less thing to worry about so here in the graphics we have Graphics or PNG and we will not put it at zero zero what we'll do instead is let me check the exact values it's 730 7 30 and 2 16. so this is the exact position where it should be so if we're on this now we will have the graphics being shifted there you go so we have the graphics over here so that is the first thing done towards our people counter project then the second thing that we can do is we can open up the detections for people so here we have car and here we have the confidence so the confidence will keep the same the rest we will delete and we will write here person if the current class is person and the confidence is greater than 0.3 so now it should give us the detections but we will have the issue of the Mask because as you can see the mask is around here so it's only detecting in this region so the mask is completely wrong so what we need to do we need to go ahead and create our new mask so let's go ahead and do that so we are back here in canva and as you can see it's the same file we had this mask earlier and now we are going to create a new mask and this is the same video that we saw earlier and first of all we are going to fill it up in the space and now we are going to start creating our mask we can go back and simply copy one of the rectangles so that it will be easy for us to work with so here we will rotate it like that maybe a little bit lower there you go I think that's good we will copy that and we will paste it here okay it's not straight it's not aligned with the with the escalator so maybe around like that so let's play the video so that we can see till what points do we get the people coming in out there you go so I think yeah maybe a little bit lower and maybe a little bit tilted why does it start in the wrong position there you go so yeah I think that should be fine and we can remove this because this is correctly aligned and we can paste it here oh no the angles are different okay so we need to rotate it again maybe like that and we will put it here there you go so this should be our new mask and we can push this back here and we will delete this video so that becomes White we need to fill this Gap there you go so we can download this mask now we need to select which page we're talking about page number two and we don't want any transparency or any compression and now it's downloaded so we will drag it in our people counter and we will write here mask the reason I'm showing this is because if you have a different video or if you are working on a different project you can actually do it yourself otherwise for me to just copy and paste it's very easy because I've already done this but I'm showing you this so that you can do it yourself for your own project as well so this is the mask and now let's open up the region as well so that we are 100 sure that it is um working as we expected to so there you go so this is the mask and as you can see it is detecting very well so one two three four five six seven and now we need to fix the counter so how many people are going up and how many people are going down so in this case we will have two lines so we don't have just one line we will have two limits first of all let's comment this out and we will go up and in the count or in the limits we will have two of each so we will have limit limits up and limits down limits down and then we have total count up and total count down so we need to put the lines here so I have the lines for up it is one zero three one sixty one and two nine six two nine six and one six one and then for the limit noun is five two seven four eight nine and seven three five and four at night seven three five and four eight nine yeah so yeah that's good and here we have uh limits because now we'll have some errors because limits is not actually there so what we can do is we can comment all these errors you can comment this control and slash is how you comment and then we will comment this as well and anything else no okay so now that this is commented and we know there will be no errors what we can do is we can change this line for example to limits up and we can write here up and up why am I writing it wrong up and up okay and then we will write again for the other line we will write down limits down and I'm going to copy it so that I don't have to manually write it again and again okay so let's run that and hopefully we will have two lines uh one for it going up and one for going down there you go so this line is for going up and this line here is for going down so we need to check for both of these individually how many people are crossing that line and how many people are crossing that line okay so then we will come to the limits so here let's do for one so instead of limits we will write limits up see how lazy I am I'm going up just to copy it the limits up I'm going to paste everywhere uh you can actually do a search and replace but again you see how lazy I am okay total count we'll put up why I I don't know why okay total count up dot band ID and blah blah and here in the in the put text we are going to write up [Music] okay so let's run that and let's see uh what kind of results do we get are we getting anything good because it is supposed to uh get red green yeah so there you go it went green one count two count and three count there we go and four count excellent so this is working fine all we need to do now is take that and put it down uh in the correct region and make it green instead of red so let me check the values exact values for the count is 929 and three four five three four five and Hershey playing Five and seven and this should be 139 195 and 74. so we're giving it a bit of a green color uh green is more up red is for down so there you go so here you have zero so the first one goes so you get one and then the second one is two and the third one is three so now we need to do for the people going down okay so for the people going down it's very simple all you have to do is you have to uh yeah here the if condition copy that and paste it and instead of up now we are going to write down limits down so if we go down and here we'll write down down down down down down down down down okay and then we have count up so that will become down count up will become down we will copy the text and here the values are one one nine one and three four five and the color will be 50 50 and 230. so this should be total down so let's run that and see if it works there you go so we have the red part here the green part here so this is going up one two three and now it should detect these two as well one and two there you go so we have a total number of people four people going up and we have a total number of two people going down so again if it was crowded uh it will detect all the uh other people as well and it will do the calculations so as you can see it's very easy to actually change the code a little bit and uh cater it to your own project and it is very fun to play around with these values and because this is something that you can Implement in real time and it is uh something you can implement it as a real world application that's why it's really informative and fun to do as well and in the next part we are going to learn how to create your own custom model so you can create your own data set and use that data set to train your model and using that model you can create your project so that is what we are going to do next so let's go ahead so now we have already studied and already learned how actually we can use pre-trained model to create our projects but now we are going to create custom trained models with our own data set and we are going to use it in our projects so how can we do that so we can do that by using either nvidia's graphic card or we can use it by running Google collab online so we will be using Google collab since everyone has access to it and it's free to use and if you have your own graphics card installed you can do pretty much the same thing offline as well pretty much the same code to run it on your own PC so here we have a Google collab opened up and it's called YOLO version 8 and right now it's empty and what we have to make sure is that the runtime is if you go to change we have to make sure that the runtime is GPU we are going to save that now uh before we actually go and write some code on How to Train Your custom model we need to know what kind of data set that we need so the project that we are going to do today is called the PPE detection or the personal protective equipment basically the hard hat and the construction jacket and The Mask so we need to find whether a person is wearing that or not so we have this data set available on roboflow and you can see we have the version YOLO 8 as well you can simply download it from here so if you click on that and you can download uh ZIP to computer and you can press continue Now by doing this what will happen is that you will get the data set as well as the yaml file that will allow you to run with yolo version 8. now if you don't have a data set available online and if you wanted to create your own data set then you actually need to know what is the format in which the data set should be so in order to understand that we are going to open up this construction data set so here is that data set if you download it in a zip format and if you extract it you will have test train and valid these are the three folders and then you will have a data file so in the test file you will have images and labels in the train file you will have images and labels and valid also images and labels so if you open up test file if you open up images you will see all of these different images that you have and each image will have a corresponding text file and in that text file you will see all the different classifications so for example you will find the class number eight at this at these coordinates so these are basically normalized values it means they are from 0 to 1 in between those values if you multiply it with the actual image size you will get the pixel values so these are the four values X1 y1 and the width and the height so you can find all these values for all these different classes for all these different images so if you go to for example train you will find images and within these images you will find these labels and within the labels you will find all these corresponding coordinates so this is the file format and then the important thing to have is the data so the data yaml file basically it gathers all this information uh into one place and if you open that up you will see that you have train validation and test folders so you have to mention where these folders are and then you have the number of classes that is equal to 10 in this case and you have the names of all these classes so you have hard hat mask no hard hat no mask no safety vest person and safety cone safety West machinery and so on so and the rest of this is uh not very important uh this is just for the copyright uh and all that so what you can do is and there's one more thing we need to add when we bring it to Google collab so what we will do is we will copy all of this data to Google Drive so if you go to my drive right now I have a data sets folder and within that data set we have construction safety so this is the exact same folder you can see here you can see here construction safety so you have read me read me and then you have the data Dot yaml and then you have the test train and valid these are the three folders if you open that up each folder will have labels and images inside of it there you go so this is the basic idea if we go back to our data sets construction safety and this is the main thing so here data.yaml we have to make some changes here so here in the file you'll go to the text editor if you remember here our file contains strain validate and test these are the folders that we have but we need to define the path where these are present so we have to write here paths and then we have to define the path in our Drive in the Google Drive so that is what we have done here so you will open that up and you will write here paths and inside that you will write drive my drive and then whatever your folder is so for me inside my drive I have the folder data sets inside that I have the construction safety and inside that we have train valid and test so you have to write this path the rest of it Remains the Same so this is the basic idea of how you can upload your files to Google Drive and how you can edit it to run it using Google collab so that's the only thing you have to do you have to write here uh path and you have to give in the path file I will show you it I will show you this again once we go into our Google Drive okay that's done but what if you have your own images and you want to create this uh if you want to label it what can you do then now to do that we have a very famous label IMG project that is available on GitHub and you can see it is by heart ex labs so thanks to all the contributors and the creators so what you have to do is you have to go to the releases you can click on binary and here you can download the zip file once you download the zip file you can extract it and once you extract that you will get something like this label image.exe now for example if you were to find the poker chips if you were detecting poker chips here in the data you will go to predefined classes and here I have given 5 10 20 50 and 100. these are our five classes in case of construction safety we have hard hat no hard hat jacket no jacket and stuff like that so you will do that and then you will go back and click on labelimage.exe once you open that up you can open directory you can select for example these are the images I want to label or whatever so for example these are the images so then you can press R sorry you can press W to actually select and you can Define for example this is class number five and hit OK so this will create this label then you'll press W again and this is for example for 100 you'll click on 100 and press ok now the easy way to do this is do all the fives first then all the tens then all the hundreds all the 20s things like that and once you want to export you will make sure that this here it's YOLO format and you will click on Save and here it will save the file if you hit save it will save the file uh in that location so if we open the directory so here we have this directory so let's say we save this one so if we open that up you can see it's the same format so zero is for I think five that's for five so the location of this is at this point so these are the coordinates for that then class number one is present here class number two you have here and so on so this is how you can label these images so the data.yml file you can create yourself as I've mentioned it's very easy to do all you have to do is you have to write the paths the Train the validation and the test folders and then you have to write number of classes and the names of the classes that's it and you will save the file it's a simple text file basically by The Format Dot yaml so once you have done that now we can simply go ahead and upload everything uh for example in this case uh we can download it from here the construction safety image data set and once we have that we can upload it to our Google Drive once Google Drive these are the basically the files we'll select all and drag but I will not do that now then once you have that because it will take some time then we have YOLO version 8. this is our Google collab file so what we have to do is uh first of all you will go to folder and here in the folder you will see that we have an option here to connect your Drive Mount drive so make sure you click on that and it will Mount your drive it will ask you to give access once you give the access this will have a cross it means it has been mounted so if you go back and you click on content not the bin the content I think it's still loading let's wait for it content there you go so now you have the drive so if I click on the drive and then my drive and then I can go to data sets and inside the data sets we have construction safety and inside construction safety we have test strain valid and data.yml if you double click that this is what we have to make sure so this path should be correct so if I right click here let's say construction safety if I right click and I can say copy path and then I can paste it here so this is the path that we need to have content drive so after content uh we need the drive so from drive we are going to write my drive data sets construction safety so this is what we need so we need to make sure it is at the path over here so once that is done we can close this and we can close all of that and now we can start writing some code again just make sure the runtime is GPU and we will save this and over here we are going to first of all write a few lines of code that's it we will need five lines of code and we will have our YOLO running and we will have our YOLO training so first of all we need to check whether we are using a graphics card or not so we will write here and video SMI SMI and then we are simply going to run it so once we run this we will have some information so here you can see Tesla T4 we are actually using 153 60 MBS so that's good so we are actually using a graphics card so that's done now what I like to do I like to uh clear all the outputs whenever we do that so then we can write another line of code now in this we are going to install ultranetics so we will write here pip install Ultra lit l y t i c s ultralytics we will hit enter or sorry we'll hit play so now it's done installing so that's done again I will go back to edit and clear outputs because I don't like them then we are going to go and we are going to import YOLO so like we did earlier from ultralytics import YOLO YOLO is capital and we are going to press enter and there you go it has been executed so now we are going to write the actual command that will run YOLO and detect it on an image so first of all we are going to write YOLO and then we will Define the task to detect from an image we need to detect objects it also has the option of classification and it also has the option of segmentation but for now we will just do detection you can write segmentation as well if you wanted to segment then we have mode predict there is training as well there's validation as well but right now we are using the mode as predict then the model so which model we are using we are using yolo8 version uh YOLO version 8 which is large so we are using the large model and the confidence level minimum should be 0.25 and what is the source the source is basically the image so you it can be here in your drive or you can give an online source so for now we are just giving it an online source this is basically ultrolytics.com Images slash bus dot jpg if you had your own image you can import it here and test it out or if you have an image online you can test it from there as well so once that is done this is the Moment of Truth we will press enter control enter and then it will execute this command so first of all as always it is going to download the weights because it already does not have that that is done now it is running and it is using 2.0 Tesla T4 this is the GPU that it's using so that's good we know that it's working it's downloading the image and the image has been downloaded and it has executed everything so as you can see this is now done and you can see it downloaded the file and then it gave us the result that there were five people detected one bicycle and one bus so if we open this up uh it will not actually show you the bounding boxes and all that but we will need to write some code for that but for now you can see there's one person here second here third here fourth hair and maybe a fifth inside well over here so five people uh one bicycle one bus so this is the bus I'm not sure where the bicycle is though um and yeah so that's the idea so it means it is working so what we need we don't need to test any images here we can run it on our actual offline code and we can write the same code here and it will work fine as well but we just wanted to check whether it's working or not whether the installations are working or not so once we have confirmed that the installations are done what what we need is a simple one-line code to actually uh do our custom custom Training oh custom data training so that's it and then what we will do we will add our code and I'm going to paste it here so now we have a similar command like before we have YOLO then we have task again we have detection but this time around the mode is training not prediction it is training and it is training based on again the large um what you call the model you can use the Nano small or medium as well and where exactly is the data so this is uh what you have to Define this is what you have to change so data basically equals uh double dot slash content drive so this is the exact path that you will copy and paste here so Double Dash back uh double dot back content drive my drive data sets construction safety slash data dot yaml so you have to give that file that file is the linking file for all the data set so you need to give the location of that file then you can Define the number of epochs and the image size so that's pretty much it and now all you have to do is press run and see the magic happen so it will take a while to actually train so we will let it train and then we will come back to it so first of all it is setting up all the weights here you can see 589 of 595 items from pre-tained weights so we are using the pre-drained weights to actually train our own model you can say it is transfer learning as well so the optimizer is has a learning rate of 0.01 and let's see when does it start so there you go so right now it has started so this is Epoch number one uh did it start not yet let's wait for it to start so now you can see the epoch has started the counter has started and here you can see the time that has taken already and the estimated time that is left so this is basically the idea so each one of these will take for example uh two minutes or three minutes so you can calculate from there it will take for example 150 minutes so anyways let's wait for it to finish and I will see you once it's done so now you can see that the process has completed the training is completed and if you go to runs this is basically in our content if you go to runs and in detect you go to train you will see all these files generated so the main thing that we need to look at is the best.pt these are the weights and this is the last weight dot PD so and the rest of these are basically uh the parameters the response of our model so for example you can see over here results dot PNG you also have the so here you can see the mean average position values and you also have the recall and the Precision values and you have the training loss for the class and for also for the box and then you also have the confusion Matrix so all of these things you can download you can click here and download it will be easier to see otherwise you can open it up on Google collab as well so here you can see the confusion Matrix of how well it responded uh to the training so then what you will do is you will download the best file and once that is downloaded you are going to bring it to our project so here we are going to name our projects so we will copy YOLO with webcam because that's pretty much what we're doing we are running it with webcam and what we will do is we will remove all of this and we'll write project three and here we will write PPE detection so this is our project three and it says YOLO webcam so we will double click that and right click and rename and we will write PPE detection there you go so now what we can do is we can use the PPE videos so if we go here we can open in our Explorer and you can see we have these three videos this is the first one this is the second one and this is the third one so we are going to test on all three and we'll also test on the webcam I do have a hard hat and uh I'm pretty sure we can find a mask and I also have the construction jacket so maybe we'll try that as well so uh let's write here PPE dash one I believe that's the name and uh yeah that's good and then we have to change the model so here we are just simply going to drag and drop so here this is the model so we are going to drop it here and we are going to call it PPE so here we will change it to PPE and we will remove all of this so let's go ahead and run it so let's uh actually one more thing I forgot we do have to change the class names so if we open up our files uh that we got the data dot yml file we have the names of all the classes so we can simply copy it from there so this is in the data.yml file and we can replace it with our class names there you go so these are the class names and now if we run it it should work fine there you go so it's detecting the person it's detecting the hard hat no mask and you can see it also detects safety or no safety vest so it's not the most accurate but it is giving us some uh good readings so what you can do is you can train it with more data set and for longer ebooks for more epochs to get better results so here you can see safety vest no mask hard hat and person person hard hat no mask and there you go let's try for number three there you go we have safety vests hard hat person no safety vest here we have no mask safety West started okay so what we can do is uh this is a little bit difficult to see what exactly has been detected or and what's not been detected so what we can do is we can check which classes are we detecting and based on that we can assign a color so here we can put a normal rectangle or do we need to put yeah let's put a normal rectangle instead of the corner rect so we will write here CB2 Dot uh rectangle and image and we will give in the X1 y1 and the X2 and the Y2 and then we are going to and give it in the color so this will be uh my color let's say my color so by default my color my color equals red so 0 0 2 5 5. and then we are going to write down the thickness the thickness let's say it's three and that's pretty much it so and then for this part here put extract we are also going to assign the color color for the rectangle or color for the background and color for the text so the color for the background let's make it my color and for the text we have to make it white color for the text we are going to make it white so 2 5 5 2 2 5 5 and 255. so let's run this and see what happens oh we need to remove the corner rectangle so what we are trying to do is that it should detect uh it should show us the red color and in in the case where there's no mask or no hard hat and in case it is present it should give us green so okay so yeah now it's my color is showing but it didn't show for the put text rect let's put it for the color wrecked as my color as well yeah now it's good so it's showing red for all of these so now we need to make sure if uh by default it will be red if we are detecting one of those classes that have the green then it should detect green for example we have the hard hat we have the mask and we have the uh safety vest so these are the three classes so what we will do is we are going to check first of all we will write here current class equals uh class names at CLS and if current class equals hard hat is it capital yes hard hat then my color equals zero two five five and zero let's try that out so now all of them are green so what's the issue if current class equals hard hat my color is zero two five five um current class is coming from here put text um and we need to give it after so the rectangle because this has my color needs to be here so we will put it here and current class equals hard hats hard hats why did it show all of them green that's weird let's print the current class so when we are printing the current class it shows person safety West safety best safety vest oh we need to write else uh else because it's not changing after that my color equals my color equals uh zero uh two uh zero zero and two five five now hopefully it will be better yeah so there you go we have green for hard hat and the rest are red okay so if my color class is hard hats or my current class is save the vest uh safety vest and mask or my current class equals mask then it is this otherwise the rest of them are red um and what else can we do we can make it smaller because scale is not that good um we are using the put text rect we can make the scale 0.5 let's say and the thickness is one that's fine um and the offset offset let's make it five instead of 10. so it will be a little bit smaller so that it's easier to know what is going on there you go it's there's a lot of overlap that's why it's a little bit confusing but you can see that when it's the hard hat it's detecting it's showing a screen when it's not it's showing us red uh let's try it actually the scale it's very small so let's try one and let's try another video the first one was the most clear let's try that one out there you go so we have the hard hat we have the person no mask and safety vest uh or what we can do is we can assign a third color as well because if it's a person then we don't need to worry about that so all the red colors and all the green colors and then we're given else so if it's current mask and all of that then we can write else if else if and then we can write else else my color is purple or let's say blue two five five zero and zero okay so uh it's no hard hat no hard hat and no safety vests and no mask yep these are the ones for red uh then safety cone safety vests we don't really care about that the the rest are blue so let's run that so the person now should be blue and we should have green for okay it's opposite hard hat safety vests and we also need to give in maybe a threshold so that we don't get a lot of wrong detections if the confidence level is greater than 0.5 let's say then only we'll do all of that so yeah we were talking about the color so no hard hat uh all of this is is supposed to be red these are supposed to be green so and the rest are blue let's run that again there you go so we have the safety vest safety hard hat and the person and then we have the no mask so when it's not very clear it's not giving good results but overall it seems to work fine so now we can also try it with the webcam so I will put it okay let's keep it for video and for webcam we can open this up and let's try it out so as you can see I'm not wearing a hard hat no mask and no safety vest but it's detecting the person so if I wear a mask you can see it's detecting the mask and if I wear the hard hat you can see it detects the hard hat there you go if I remove the mask says no mask there you go and we have the hard hat and no safety West so now we are headed to project number four and in this project we are going to create a poker hand detector in which we will first of all detect all the different cards and once we have the cards we are going to determine what poker hand do we have so this is a very interesting project because it has multiple layers and the first one is to actually detect the cards which we will do by YOLO and the second one is actually classifying uh what hand do we have so there will be a little bit of code involved there so where are we getting the data from the data is available on roboflow universe and we are downloading the playing cards image data set and we will download the YOLO version 8 so you can simply click on that download zip to computer and you can press continue once it is downloaded you upload it to the Google Drive so let me show you uh where that is so this is the playing card data set so we have validation training and test data sets so we have labels and we have images for each of these labels so like we have seen before in our BBE project it is the same format and the thing that is different is again the data.yml file so let's open that up in a text editor and let's see what changes have we made there so this is our data dot yml file and here you can see we have number of classes as 52 and all of these classes are based on the rank and the suit so here it's 10 of Clubs 10 of diamond for example so all of these as you can see we have here and then we have uh the training validation and test folders but we also have defined the path so the path is drive my drive data sets and playing cards fixed so they had an issue earlier where they had a duplicate so uh I believe it's fixed now earlier they had 53 classes because there was a duplicate but now it's fixed so I had to upload it again and use this as playing cards fixed so this is what we have and then what we do next what I did next is actually to save some time I ran the exact same Google collab so you check the Nvidia driver or the Nvidia graphics card and we have the similar one that we had earlier which is Tesla T4 it is 16 GB of memory and then we install ultralytics and after that we import Yolo from ultralytics and this you don't have to perform just to make sure it's running you can do that and then you start the training process so in the training process all you have to do is you have to change the data path so it will be content drive my drive data sets playing cards fixed slash data dot yaml so this is what we have to do and uh we'll have 50 epochs so right now it's taking a long time so meanwhile it's training we are going to go ahead and understand the basic concepts of Poker and how we can create a classifier for poker so that part does not involve object detection but it is quite crucial to the project so we'll first go through that meanwhile this will finish what do you call training and then we can take the data and start uh the detection as well so this is up and running and by the way this is the data.yml file you can see it's here as well so that's good so let's go back and try to understand what exactly uh is a poker game and how we can create a classifier for it so to do that I've created a simple uh poker hands uh what you call classifier or you can see this is the data sheet or the cheat sheet so we have a total of 10 different classes so high card pair two pair three of a kind and so on so we will start by understanding that first of all you have to compare five cards so everyone has two cards and the rest are laid out uh in in public domain so everyone has access to those cards that are in public domain but the two cards that you have are only for you to view so what you have to do is you have to check your two cards and you have to check the rest of the cards that are in public domain to find the best possible scenario so the biggest hand so whenever we have these five cards we call it a hand so we have a total of 10 types of hands and to understand these we need to understand what is Rank and what is suit so in poker uh or in playing cards we actually have for example this is an a of Hearts this is Queen of Clubs so a is basically the rank and hearts is basically the suit so five is a rank and clubs is a suit so similarly we have seven as the rank and Diamond as a suit so what we need to do is we need to understand that first of all we will differentiate these two some hands are based on just the ranks and some hands are based on just the suits and some of them are a combination so let's start with the easiest one which is appear we will get to the high card later on but let's start with the pair the pair you have same rank of two cards so the rest of them they don't match but two of them they match so it can be 2-2 it can be five five it can be seven seven it can be a a so whatever the card is you should have a pair of the rack not the suit of the rack so for example you can see this is a pair of suit but that does not count so you don't have a pair of suit you only have a pair of rank okay then we have two pair two pair is similar to one pair instead that you have two of them instead of one so you have a king king and five five so that's two pair then you have three of a kind in three of a kind you have three of the same ranks so it can be 777-88 king king king queen queen queen or ten ten ten in this case the other two that don't matter then you have a straight in straight you have consecutive numbers so for example seven eight nine ten Jack now here again the rank matters not the suit we are not looking at the suits at all uh till this point so for example you have two three four five six that is also a straight then you have uh seven eight nine ten Jack then you can also have 910 Jack queen king that is also a straight so you should have consecutive ranks so uh if you do seven plus one that should be the next one eight plus one nine so that's the idea by the way the rank for Jack is 11 so after 10 you have uh Jack queen king and Ace so Jack is 11 ranked number 11. Queen is 12 King is 13 a is 14. so that's the biggest rank okay uh after straight now comes the suits so if you have all the same suits then you have a flush so it can be all diamonds it can be all Hearts it can be all uh clubs all Spades whatever it is um if all of them or five of them uh are of the same suit then it is a flush then you have a full house in Full House you have three of a kind which is you have three cards that are of the same rank and then you have a pair so you have two two two and then ten ten it can be five five five and seven seven so three of a kind plus a single pair that is your full house then you have four of a kind which is very simple you have four of the same ranks so five five five five seven seven seven seven nine nine nine nine king king king and so on so the ranks are pretty much the same again we don't care about the suits actually the suits you will have all the suits so uh it's it's obvious and the last card we don't care about the last card okay then we have a straight flash in the straight flush we have a straight and a flush so we have the ranks and we have the suits as well so the suits as you can see all of them are clubs and uh straight is eight nine ten Jack Queen again it can be two three four five six or any other uh any other formation but it also has to have the suits that as the same and the last one which is the highest one it is that you have you have a straight but starting from Ace so Ace king queen Jack 10. so that is the highest straight plus you have a flush so that is a royal flush so uh you have for example hearts hearts hearts hearts and then you have a king queen Jack if it starts from queen or if it starts from King King Queen Jack 10 9 no it's not a royal flush in that case it is simply a straight or a flush so uh sorry straight flush in that case it's a straight flash so that is the basic idea and the last one if you don't have any combination then you have the highest card so the biggest card so for example if somebody uh had a pair and you had the highest card the pair will win so with the high card if both of you don't have anything like there are two players playing against each other your five cards versus their five cards if both of them have no classes at all none of them then you check which one has the highest card so if you have the king and the other one has Queen then you win so that's the idea so what we will do is we will classify one hand so we'll not do two hands we'll do just one hand uh of uh our own hat so uh we will send in Pi values uh as strings and it will output us the name of the hand so that will be interesting too uh accomplish so we will keep this image open and I will put it on the side and we will come back to it every now and then so let's start by creating the folder and we are going to call it projects for poker hand detector so poker hand detector we're going to start off with that okay so what we'll do is we will create a function and this function we will input it five strings or you can say a list with five strings and it will output us the result so we will create it as a module so that it is a separate code in a separate file and we can simply link it to our project so here we will create a new python file and we will call it poker hand the tector uh function let's call it so here we are going to write uh def find poker hand so that's how you write what you call a function and we have to return something and right now we will Return to Zero okay so that's good and then what we have to do is uh we we will be testing it as well so we need to make sure that we run this individually and then we will integrate it to our project so we will write if underscore underscore name equals underscore underscore main so this means that it is checking if this is the main file that is running then it will run this code if some other file is calling this function it will not run this code so yeah we have to put double equal there you go so now what we can do is we can uh send in a few uh what do you call hands so let's start off so here is our image so let's start off by creating all these hands and one by one we are going to send it to see what it gives so first of all uh let's start we are going to start at the top we'll start with royal flush so a of Hearts K of Hearts Queen and so on so here we will write uh find poker hand and we will send in a list and inside the list we will give a of hearts then K of hearts then we will give a queen of hearts then we will give Chuck of hearts Jack of Hearts and then we are going to give 10 of hearts so this will be our royal flush and in front of it we are going to write royal flush and then we will copy this and we are going to paste and then the second one we will do is straight flush so again Queen Jack 10 9 8 but all of them are clubs so Queen queen of Club then Jack of Club then 10 of Club nine of Club and eight of Club so this is not a royal flush it is a straight Str a ight straight Flash so these are the two that we are starting off with so let's run that and see what do we get so no actually we'll get nothing so what we have to do is we have to return uh let's return we will put here hand and we will return the hand okay or no we'll not return the hand we want to print it so we will print the hand okay so let's run that and see what do we get uh now we are running the previous one right click and run and there you go so now we are getting this uh these two as the output so that's good we have started and the first thing we will do is we will find the ranks and we'll find the suits individually and put them in a list because what we are trying to do is we are trying to understand some of them we will classify based on ranks some of them classifying based on suits so if we have them in a list we can play around with them very easily so that will be our first step we need to make sure that we have the ranks and the suits uh all in two different lists so here we will write ranks equals a list and then suits equals list so these are the two lists that we have and we are going to Loop through the hands and uh for card in hand we are going to Loop through the card and first of all uh what we can do is if if I wanted the rank for example I want the rank which is the first element here so all I have to do is I have to say card at zero right so let me print that out so if I write that that will be my rank so I'm getting a I'm getting K Queen Jack one uh and all that so basically I'm getting what I need but if you look at the last one I'm getting one that's wrong that's not the actual rank so what I can do is I can I can put an if statement that if the digits are three then it means it is a 10 okay so then I will take two values instead of one so I need to get 0 and I need to get till 1. because that's not inclusive so then I will get 10 but for the rest of them I should not get 2 I should get only one so that's the idea so I have to check here if the length of card equals 2 or should we write yeah equals to then it will be rank equals uh card at zero else rank equals card at zero sorry zero two two so now if we print the rank then it will show us so for a king queen Jack it's fine it's getting one and for 10 it is getting two so that's the idea so for eight nine it's fine for 10 it is getting the 2 that is exactly what we needed so this way we are basically uh getting the rank and we also need the suits so if if the card has the length of two then the suit is the next value right so the suits equals card at one but if that's not the case then the suit is basically card at two so 0.2 so that's the idea and now we will have the rank and the suit so they will be separately detected there you go so a of Hearts King of Hearts Queen of Hearts ten of hearts and so on so now we have it separately so all we have to do now is we have to append it to our list so here we will say ranks dot append rank and suits.append suit and we have to do it for each card so this should be inside and we can remove that so once that is done just to make sure we are headed in the right direction we will print the ranks this will be a list of ranks and then we will also print suits suits there you go so we have the list of ranks and we have the list of Suits we have the list of ranks we have the list of suits so that's good so now first of all we are going to start off by Royal Flush so here we are going to check for royal flush and we'll check the rest later on now how can we check for Royal Flush in Royal Flush the first thing we need to check is whether it is a flush or not if all of them are same if all of them are same in the suits then it is a flush so how can you do that so basically what you can do is you can check if or let me just print it so that it's easier to see actually what is happening so what we can do is we can check for the suits dot count the first element okay it can be anything because all of them have to be same in order for this to be true so you can pick any element from the list so you can write suits at zero so basically we are asking it to count the number of times you are getting this element so for example it will take H and it will count how many times we have H in this list if that is equal to 5 then it means all of them are uh it's a flush then all of them are the same then it's a flush so let's run it so in the first one it's true and in the second one it's also true but let's put another hand poker hand we are going to put let's say we will put another hand uh let's put four of a kind which is next so it is five five five five and queen so it is five of Club five of Spade five of hearts five of diamond and we have Queen of Hearts Queen of Hearts so this is what is it for of a Kind so in that case it will give us a false because all of them are not same so the last one it is giving us false because it is not a flush so this is only checking for a flush by the way the next one no the one after that is a flush so let's put a flush as well so we'll have another one so let's just put full house as well we'll put Full House and flush both of them so these are two two two two and ten ten uh so two of so I'm writing it exactly like it is so that there is no confusion of what we are doing uh two of diamonds and two of spade two of spade two of spade what is this why is that like like this it should be hard not Club okay two of spade and then ten of hearts and then turn off space Club there you go so this one is what is its Full House uh full house and the next one next one will be uh plush so in flush you have uh I'm using again the same one two of diamonds then all of them they will be diamonds so this will be king of diamonds this is seven of diamond then we have six of diamond six of diamonds and five of diamond so let's run that uh it's giving us a lot of outputs so let's not print the hand and let's not print the ranks and suits and we run that and there you go so the first one has a flush so it's true the second one is also a straight flash so it's true the third one is four of a kind so it's false the fourth one is also false because it's a full house and then we also have another flush so it's true so this means this methodology will first of all tell us whether it's a plush or not so we can check for royal flush and we can check for straight flush and we can also check for flush so we can check three of these things right away so if we go in if we have this first condition if that is true it means it is a flush now once we know it is a flush we can get to the Second Step checking whether it's a royal flush or not so in in the royal flush we must have the ranks so uh but the thing is that it might be a of Hearts King of Hearts but it might be sorted wrong so we need to make sure first of all that they are sorted properly so the biggest one the highest rank should be the first but a king queen does not have a rank yet because if we print it out if we print out the ranks uh let's just write pass here if we print out the ranks it will not give us for a king and queen it's simply writing a king queen Jack which is which is wrong in our case because uh we need to work with values uh when we sort them out we want 14 to be a instead of just writing a otherwise it will not be organized because if I put if I remove a here and I put a here then it will give us K first and a second which is still a royal flush but it's not in sequence so we are not able to get the rank properly so what we'll do here we'll simply write if rank equals um a then rank equals 14. that's how simple it is so else if rank equals King we'll copy that paste and paste okay so rank is a the ranking will be 14 rank is K it will be 13. rank is Q it will be 12. rank is Jack it will be 11. there we go so now if we run it we will have numbers so 13 14 12 11 10. now what we need to make sure is that they are sorted properly if if they're sorted properly then it will be easier for us to check for oil flush so what we can do is we can simply write sorted so sorted ranks actually we will do it after the sorted ranks equals sorted and we will give in our ranks that's it so if we print ranks again you will see that now it will be sorted okay so ranks when we are sending them in uh they are not integers they are um they are strings so we need to convert it into integers there you go so now you will see uh actually it didn't do anything why is that oh because we printed ranks not sorted ranks okay let's run that again and you will see for the first one you will see this one is 13 14 12 but now you can see it's 10 11 12 13 14. so it has sorted it so now what we can do is um actually let's forget about this order right because or we can use the sorted ranks but what we can do is uh instead of saying that the first one is 10 and the second one is 11 we can just say if it's in this uh that that should work as well so we can write if uh 14 in sorted ranks and so that's how you can check if 14 is present in the assorted ranks and uh and then we will write another and then another and then we will write another and and then another end so if 14 is there then 13 is there then 12 is there then 11 is there and then 10 is there I wrote one extra if that is the case then it means it is a royal flush so we will print Royal royal flush so let's run that and there you go so here you can see it says Royal Flush because this is true for this the rest of them it's not true so what we will do is in order to get the output we are going to say uh the we are going to create a list called possible for simple ranks and this list will contain all the possible ranks so if for example we are getting a royal flush we are going to put the value of 10 inside it we are going to append because sometimes what you can have we can also detect a single pair when it's two pair right so we need to find the highest one so we will put all the possible scenarios inside this list and we will pick the highest one so if it was for example if it detected that it is a three of a kind and it also detected it is a full house then for Full House it will be seven and for three of a kind it will be four so we will pick seven and that will be our highest uh hand so that's what we are going to do so in this case our Royal Flush is 10. so we are going to put um in inside here we are going to write uh possible ranks dot append 10 so we are going to append that and once we do that in the return we are going to return uh the possible ranks maximum value so that's the basic idea um or should we return no that will return only a number wait what we can do is we can write the poker poker and ranks equals we are going to create a dictionary and for each one of them we are going to give a number and a name so it will be royal Lush then for the second one for number nine uh it will be straight flush and for number eight will be four of a kind then for number seven it will be Full House then for number six it will be flush then for number five it will be straight ght and then for number four it will be three of uh kind then for number three it will be two pair for number two it will be pair for number one it will be I covered so these are all our ranks and now what we can do is we can check the max possible and then we can write here poker ranks at this value so we will return that and we can simply print that out so here for each one of them we can simply write print print print there you go so uh now let's remove all the prints and um what we will do is or should we print here no let's not print here let's print it inside so we'll not have to write it again again so we will print the hand and we will print what it is like what did it detect so here print hand and prints this or let's give it uh let's let's call it output is this and then print the output and then put the output here return the output there you go so yeah let's run that and see what we get Max argument is an empty sequence so the first one you can see it is a royal flush so it is returning that if nothing is there uh if if there are no we can simply write if uh not possible ranks it means if possible ranks is empty then possible ranks dot append one it means it is high card right if we have not actually coded for the flash or the straight or full house for now it will just say uh high card so as we go along and we keep adding uh it will keep fixing so for now you can see it says Royal Flush for the first one for the second third fourth and fifth it says high card so now we are heading in the right direction we are getting a good output we can see the results and here we are checking for this sorted ranks let's keep it for now maybe we will need it later but actually we don't really need it for now uh so once we check that if this is the case uh the second one we are going to check is for straight flush so if we know how to check for straight we can just put the straight code inside this if statement and it will become straight flush because this statement is checking for flush because if we write here else possible dot append is uh what is flush flush is number six so we'll put here six and if we run this now you will see that this one is detected as a flush and this one is detected as a flush so this one is correct it's a flush but this one is a straight flush so we need to fix that we need to add another if statement here to check for straight how do you check for straight let's do that first so here we are going to write okay so what happens is in order to check for straight now we need the sorted ranks so for example we will have in the sorted ranks we will have let's say 10 11 12 13 14. so this is our sorted ranks so what can we do we can take the first element or we can take this element and we can say check the element before this and add 1 to it so right now we are talking about 11 we will take 11 and then we will take the element before this which will be 10 and we will write here 10 and we will say that add 1 to that element Plus 1. and check if they both are equal so then it will become 11 equals 11. so that is true then it will go to the next element so it will go to 12. then it will check the one before it it will be 11 so it will write 11 and it will add Plus 1. so it will check if 11 plus 1 is 12 if that is true it will become true so we will keep checking for all of them so if all of them give us true true true true true true true then we will say okay it's a straight if even one of them gives us something else for example uh this one is let's say seven so we write here 11 and it checks 7 plus 1 is 8. this one is false but the rest of them are true then it's not as straight so that's how we will check it so to do that we are going to write some efficient code so we are going to write 4 I in range so we are basically looping uh and we need to Loop through one because we are starting off uh let me put it back because we are starting off with this value not this value we will pick this value and add 1 to it uh sorry add 1 to the previous one because this value does not have a previous value so we will start from 1 and not zero so here we are going to write for I in range one till the length of our sorted ranks it will be five so it should be fine but we are still writing it so if that is the case so to make it a little more efficient we are going to write a single line for Loop so instead of writing it after we will write it before that so what exactly are we writing we are writing that sorted ranks at I so sorted ranks at I uh not one not in banks at I so this will be for example 11. this is equal to sorted ranks at Pi minus 1 plus 1. so this is what we are doing we are putting 11 equals 10 plus 1. right if that is the case and what we can do is we will keep getting the answer and all of this will be stored in a list so in that list we can just write here all if all of them are true then it will give us true if even one of them is false it will give us false so that's the basic idea so we need to put another bracket here and we will put an if statements or is there a problem one two three okay there should not not be a bracket here okay so that's the idea let me comment this as well um let me put this as well here so that you understand what exactly happened so if that is true it means it is a straight so we can write possible ranks uh dot append what is a straight a straight is number five number five did we add a straight so far no actually let's add all of them because that that's annoying you know going back and forth so after flush we have straight we have straight then three of a kind two pair pair and high card so flush then we have straight then we have three of a kind then we have two pair then we have pair and then we have High Court there you go so now we did till flush now we are going to do straight so for straight we have Jack of Spades clubs jack off Club then we have 10 of hearts then we have nine of Club then we have eight of Club and then we have seven of diamond so this is our straight and then poker hand three of a kind we have ten of hearts ten of Hearts then ten of Spades 10 of Spades or is it no it's not Spade it's Club my bad uh ten of clubs and then ten of diamonds ten of diamonds and then two of diamonds two of diamonds and then two or five of uh Spades then two pair we have king of King of Diamond then we have King of Hearts then we have five of clubs then we have five of spade and then we have six of diamond then we have a pair pair we have two of diamond two of two of uh what is it Spades then we have nine of nine of Clubs and we have King of Diamond then we have 10 of clubs the last one is king of diamonds so King of Diamonds five of diamonds then two of diamond or five of Hearts my bad hearts two of diamond and then ten of Clubs and then jack off diamonds a Jack of Hearts I'm getting mixed up okay so that's the idea and now let's run it um and see what you will get so we should have the correct straight so if we run this now for straight this is straight it is uh Jack ten nine eight seven and it is correct so this is straight now what we need to do is we need to take this straight part and we need to add it to our if statement here so we will put else if so first it will check if it's a flush and then it will check if it is a straight then it will become straight flush so straight flush is number nine so you can see here this is straight flash so it will become number nine so if we run that now over here we have the royal flush it's fine the straight flush is now being detected as well so that's very good so we have the straight flush here okay so we have done one two uh three and four so we have done four and we have also done the fifth which is the high card so we have done five out of ten so we are halfway done now what we will do is we are going to check for uh four of a kind then we will check for three of a kind then two pair and then a single pair so uh to understand how can we check or why is it showing a search to move that Okay so to check let's do it after straight this is four of a kind now four of a kind if you have a list right four of a kind will have four of them as the same and one card would be different so for example you will have three three three three and then five so three of four of them are same and one of them is different right so what we can do is we can tell it we can tell the computer to find to create a set what does a set do a set will put all these three together and it will put this as separate so when you do a set to this when you do a set this will become 3 and 5. so we can check how many do we have we have three and five so after completing the set how many values do we have if we have two values this means it might be four of a kind because there is another condition where you can have two set and that is when you have let's say 333 and 5 5. so in that case if we do a set then also it will give you three and five so we need to check first of all whether when we convert it into a set does it give us two values if that is two then we need to check whether uh this value comes three times or four times if it comes three times then it is 4 of a kind if it comes three times then it is Full House because these are three three of a kind and this is a pair so it becomes Full House so how can we do that first of all what we will do is we will get unique values so Hand unique values equals set of sorted ranks sorted ranks but this will be a set you cannot use it as a list so we will convert it into a list to make it easier for us to work with so we will do that so if I print unique hands now print unique or hand unique values so let's print that and for each hand it will print the unique values so for the first one there are no unique values so 8 9 10 11 12 so it's printing that for the second one again oh sorry the first one was here the second one no unique values in the third one you have unique values so you have five five five it's three of them four of them are five so it's giving us five and one of them is a 12. so it's giving us 12 and 5. so this one is basically four of a kind and after four of a kind we have a full house so that is also giving us two so it was giving us 2 and 10. so these are 3 and this is 2. so now we need to check if if the unique hand list basically has two uh values so first of all we need to check if it has two values if the length of hands unique values equals 2. if that is the case then it will be one of these four of a kind or full house actually we should we should put this here somewhere but it's okay I think we should put this one outside because this one is Journal it will also be used for three of a kind it will also be used for Full House for two pair and so on because this is unique values so now we are checking for four of a kind so now what we will do is we are going to check we are going to Loop through all the values for okay for value in hand unique values so it will Loop through the values which will be 12 and 5 okay so for each value we need to count how many times is it present in our complete list so we will say that how many times the value of let's say uh 3 is present in this whole list we need to check that so we will say that sorted ranks dot count so this is our sorted list where is it this sorted list is basically 8 9 10 11 12 or in this case it is five five five five Q right or queen is 13 I know it's 12. so it's uh 555 and then 12. so that's our sorted so we are going to check this value how many times does it come in the main list so we will write here Val and if that comes four times then we are going to write uh we need to write if that is the case then we will write possible rank dot append uh what is four of a kind four of a kind is number eight so we will append eight and if it is not four times if it's three times then it means it is number seven which is Full House if it's three times and the the set is two this is a this is a unique value this is a unique value the set is two and one of them is three it means the other one will be two automatically so we know that if it's three then the rank is seven so if we run this now you will see that here we have the royal flush straight four of a kind it's detected properly this is four of a kind there you go and then if we have a full house it is also detected properly oh sorry this is Full House this is uh four of a kind so here we have two and ten so this is Full House and this is four of a kind there you go and already we have the flush and the straight working so that's good so the same pattern we are going to use with uh the next the next one which is three of a kind so here we have done the flash we have done the straight now we are going to three of a kind so now we are going to three of a kind and two pair so if let's say we have a three of a kind so we will have five five five five and five and we will have uh let's say six and seven because if we have six six it becomes a full house we are not talking about that right uh we are not talking about that because we have already done that so if it's six and seven then it will give us how many unique values it will give us three unique values because when we send it to a set the set will be 5 6 and 7. so it will give us three values uh we can also have three values in another scenario what is that scenario where you have let's say eight eight this is a peer you then you have seven seven that's also appear and then you have three let's say or let's say two so when you create a set of this so you will have eight seven and two so how many unique values you have unique values are three also here unique values equals three so in both cases it's three in this case it is uh three of a kinds and in this case it is uh two pair right so we need to check first of all if they are three actually we can write here as well unique values are two and unique values R2 there you go so four of a kind Full House and that okay so here we need to check if the length of unique values is basically three so here we are checking four of a kind and uh Full House here we will be checking three of a kind and pair there you go so for both of them we first need to check the unique values there are three then we will check the values if the value comes how many times we will Loop through the value and if the value comes so here the value will come three times so if the value comes three times then it is a three of a kind so a three of a kind is 10 uh not 10 it's 4. so three of a kind is four and if the value comes two times which means here for example is 2 and here for example it's 2 then it is a and it is a two pair which is three there you go uh sorry when it comes two times so if it's three times it is three of a kind if it's two times it is two pair we can write it here as well so it's easier to know what's going on here also we can write four of a kind and this one is Full House so it's it's pretty easy all you have to do is just think a little bit how exactly can we do this and I believe uh only one is left let's run this first and there you go this is three of a kind because we have 10 10 and 10 and the other two are different so it's not a full house it's three of a kind and actually let's change it to Full House here three of a kind will make it full house if we write here 10 as well let's see if it detects as a full house there you go so where is it Full House there you go no Full House is here and then it's a four of a kind sorry I made it four of a kind my bad uh I wanted to make it full house which means this is 2D so this will be also 2D my bad so this will become a full house there you go see now it's a Fallout it means our methodology is working fine so we have three of a kind working properly then we have two pair so King King is working fine five five that is a two pair excellent so now we have these uh unique values and then the last one is very simple um we are checking for a pair is simply when you have four values so for example you have let me share an example here so you have five five and then three six seven and eight so the set of this did I write more thank you so the set for this will be five five uh will be five three five three six and seven which are four unique unique values equals 4 which means a pair that's how simple it is so you will simply write possible rank dot append and you will write two because one is already there and that is your high card so if we run this now and we go down and there you go so here you are getting a pair here you're getting a high card so this is basically how we can um get these values so to repeats or to summarize what we have done we have first of all extracted the ranks and the suits and we have created a variable called possible ranks not a variable a list in which we will store all those ranks actually let's print out the possible ranks before we uh print possible ranks and let's see if there are more than one yeah here you can see it's three three both of them are three so it's nine and five so it is also a flush and it is also a straight so uh and then it is both of them together so it's straight flush um and then here you have four of a kind so the rest of them they don't have multiples but this one has multiple as well uh because it is a flush as well and it is a royal flush as well so it will take the bigger one which is royal flush so yeah that's the idea and then uh as we were mentioning so you extract all the ranks and all the suits once you have them in lists you sort them because sorting them will make it easier uh especially when you are uh finding the straight that will make it much easier so you will first check for the royal flush no you will first check for the flush so check let me write here uh check for blush and then it will check for Royal Flush over here and then it will check for straight if it is flush and it is Royal then it will be royal flash if it's flush and it is straight then it is straight flush otherwise it will be simply flush so this is flush basically then we have straight in straight uh what we are doing is we are uh we are checking the previous value and we are adding one to it and comparing it with our current value so 11 is been compared with 10 plus 1 12 is been compared with 11 plus 1 if all of them give us True Value if they are same then it becomes all of them are consecutively uh listed and ranked so that's why we will write it as straight then comes the unique values so we will check the unique values by putting a set to the sorted ranks if the unique values are 2 it can have two possibilities four of a kind or full house if one of the values has four uh counts then it is a four of a kind if one of the values has three counts uh then it is Full House same way we are checking the unique values and if the unique values are three there are two possibilities one of them is three of a kind if the value has at the count of three and if the value has a count of two it means it's two pair and then if it's single pair then the unique values the set will have a count of four and um if if nothing else works out then it is um uh high card and once that once all of that is done we have our uh what do you call dictionary with all the rankings and the numbers and then from that we extract what output do we get what is our result so let me let me remove all the prints so that it is neat and clean we don't want uh too many things printed yeah so then we only have the hand and what it is detected as so Royal fly straight flash four of a kind two pair and so on so this is basically how we can classify our poker hand and now we once we have written this code now we can go and use our trained model to find to detect that hand and then we will send it to our find poker hand and it will give us the output of what type of hand it is so let's go ahead and do that okay so now our process has been completed the training is done and we had 50 epochs completed in 7.5 hours so for further details you can look at the Precision and recall curves and we also have the confusion Matrix and all these training and validation batches so what we will do now is we'll download the best one and we are going to use that in our project so we already have project 4 listed and we have created our poker hand function and now what we will do is we will copy YOLO webcam because we'll be using our webcam to run it so we'll simply copy that and we will name it poker and detector so once we have that we will bring in our model so here you can see this is best seven so let's drag that in and we will call it playing cards dot PT and over here now we have to make the changes so that it detects these so first of all the class names we need to change so to change that we can go back to our data.yml file and we can copy all the names and we can simply paste them here and we can just cut this and paste it here there you go so to make it easier to understand what's going on and we have all the classes we can simply press enter after each of the ranks are done so after two is done then three four five six seven eight nine Ace Jack king and queen so as you can see we have all the classes listed here and the the video we are not going to use any video so we can remove that and we can open up the webcam and we will be using webcam one and this is the size and we will be using our own weight which is playing cards dot PT and let's run this now and see if it works so I have my webcam upside down and we have some playing cards here and we are going to test them out one by one and see how it performs once we know that all of these are detected properly we can go ahead and merge our poker hand function with it so that we don't have any issues so let's see if that works it's actually running at the background so here let's put in two there you go we are getting two h which is basically two of hearts uh then we have Jack of Clubs that's good we have queen of Spades that's good so in order to make it a little bit more efficient what we can do is we can put all of the twos together and see if they work fine there you go two of diamond two of clubs two of spades and two off Hearts so we are detecting them properly that's good then we have the clubs oh sorry the threes let's try those and three of diamond three of Club three of heart three of Spades that's working fine as well uh we have to do this to make sure everything is working fine there might be one or two classes that are not detected properly so we have to make sure all of them are correct heart Spades diamond and clubs so those are fine as well so so far it is giving us really good results and it's very fast as well as you can see so this is five of paid five diamond heart and Club there you go then let's check the six let's put them like that Diamond Club heart and Spade that's fine then let's put seven let's try it like that seven all of them are correct let's try eight eight all of them are correct nine yep all of them are correct then yep correct then we have track yep correct then let's try the queen yep correct let's try the Kings wait I'm missing a king oh what happens I am missing a king okay so let's let's try with the cards we have oh no I found it never mind okay Club heart spade diamond excellent and now the Moment of Truth the last one the ace ace of spade heart club and Diamond perfect so we have 100 out of 100 being detected properly um actually 52 out of 50 52. so what we will do now is once we detect first of all we we are already displaying it we are going to put it in a variable called hands or hand hand is equal to empty now once we have this we are going to send this hand to our poker hand function so here if we import now in order to import this we'll have to rename it rename file and we will write it poker hand function without the dashes so that it's easily imported so right here from poker so we can write here import poker and function so now we will have the ability to use find poker hand so we can go down here and we can send it to find so we can write poker hand function Dot poker hand function dot find poker hand and we will send in our hand so we will check the results equal this and what we have to do is we have to first of all make sure that we have five different cards so if there are four or two or three that then it should not work so we should have a total of five so if the length of hands if the length of hand equals 5 then only we are going to send this if it's more or less we should not send it so once that is done we can simply print the results we can display it we will display it now but for now we'll do that okay so once we are actually detecting this we can put for example a limitation that if the confidence level is greater than 0.5 then hand dot append we are going to append the class name so the class name is basically CLS so this is not actually the class name the class name is class names of CLS there you go so we will append that so what we can do is we can we can print uh hand so let's try that out and see how it works because we want to see what exactly are we sending before we actually see the results so let's put two cards in and let's see what result do we get so there you go we have uh some results here so there you go uh it is appending oh oh it keeps adding to its which is not good the hand should be after each iteration it should update and it should clear out so we need to have a fresh start every time it runs an iteration so there you go we have we have these asah and ah A S H and H okay so the problem here is that it's showing it twice so that's not good so what we need to do is we need to uh create a set and send it back to a list so this way it will only have unique values because we cannot have two of the same cards uh it cannot be possible so we can simply just do a set and then it should work fine so here we can write and equals set of hand and then we can put it in a list again and we will print it before and we will print it after so there you go so if this is the case then the set is like this so we are getting one of each so that's good that's exactly what we want so as you can see here it is detecting but we are getting one of them okay so now what we can do is we can add five of these and it should give us the results so let's put a four of a kind with because it's really easy here I have already A's lined up and we have these A's and then we can add simply another card which is a king and we should get an output here so let's go at the end and let's see so there you go we are getting four of a kind so the Moment of Truth has been revealed and we're getting a four of a kind excellent so that is really amazing to see how it works now what we can do is we can simply put it on on display so that we can see it in real time so here CV's onput text we can copy that and we can paste it here and instead of the class name we'll do the result uh results and we will write your hand and we will write it like that so here we are going to given the position of zero and zero let's say or should we put it in the middle um let's do a little bit in the middle what's the size 1280 maybe 300 and the height may be 50 and the scale we want it big so let's put it at five and the thickness let's put it at six um hopefully this will give us some good results foreign we need to push it down a little bit and we need to make it a little bit smaller so maybe three and thickness is five and we push it down further to 75. let's run that there you go your hand four of a kind excellent so what if what if I remove one of the A's and I put a king there then it becomes Full House your hand is a full house that's excellent and what if I put instead of an ace I put another king and okay where's this okay I need to remove that and then I put a random card let's say six then it should be two pair excellent so what we'll do is we'll go one by one and we will test out each of these scenarios so let's put it together and let's see what do we get so let me try it to put this here so the first one we are going to do is royal flush so in which we have Ace then we have of the same type so we will have the king of diamond then we have the queen of diamond and then we have the Jack of diamond and then we have the 10 of diamond so that should give us Royal Flush so there's another card coming in here let's put them straight there you go so now it should give us the result but it's not uh what is the issue 10 of diamond j d q d k d and a D uh what seems to be the issue akh JD 10d QD and KD so there's a 10d somewhere for some reason yeah there is a 10d 10d is supposed to be there a k j there's another k H where is the cat oh okay so it's detecting it here my bad so there was another card on the side that's why it was detecting that okay so there you go so you have a royal flush excellent now let's try a straight flush so uh we have let's say King Queen Jack 10 and if we put a nine uh nine of diamond then it will become a straight flush because it will not be a royal it will be a straight flush there you go so now it is a straight flush and let's try four of a kind so we are going to add Ace Ace Ace and a king so or a nine so Ace Ace Ace and where is the other Ace there you go there you go hand four of a kind that's perfect then we will try a full house so we will remove the A and we will put a nine so that is a full house excellent now we will try a flush so let's put all diamonds so random random diamonds uh doesn't have to be straight or anything so we are going to put six and a 5. there you go and we have another one so there you go so even if they are not exactly straight it will still detect them so Full House then we had a flush the flush is working fine now let's try out a straight so uh let's try out five then we will try um two three two three now we need a four we also need a six and where is the four command where is the four there you go so we have a four so there you go your hand is straight so it's upside down it's not really organized but still you're getting a straight it's already stable I'm not sure why if you push it further yeah there you go now it's much um stable much more stable so we're getting it straight uh that's perfect then we have to try a three of a kind so let's try with Queens so these are three Queens and then we have a two and a king so your hand is three of a kind perfect uh then we have two pair so let's remove the queen and let's add a king so that will become two pair what happened um there you go it becomes two pair and that then let's try a single pair there you go your hand disappear and uh let's remove the queen and let's put a five here and that should give us high card there you go it gives us high card so this is basically our project successfully done and it looks really good and we are getting real-time outputs and let's let's try to try it in our hand and see what it gives us there you go it gives us high card so it gives us high card and uh let's try it uh with a beer or something so that we can see if it works properly or not um there you go so it is appear it is a beer and then let's put another king here it will become three of there you go three of a kind three of a kind and then let's make it four of a kind I think it fell down you know um we need another king where is the king there you go so we will get four of a kind we have four of a kind and then let's try a flash so let's make a flush of diamonds blush of diamonds there you go it's a flash excellent and what else can we try let's try a full house so we have queen queen and king and a king and we need another queen there you go so this should be a full house Full House yep and if we remove the queen and put let's say two then it will become two pair there you go so that's pretty good so as you can see um our object detection model is working really good and our poker hand detector is also very working very good so this was our project as you can see you can put all these things together and it really creates a great project and it gives you a lot of information and it is fun to play around with as well so object detection is like a superpower once you understand how to train your custom models you can really explore the possibilities and create some really good projects that are very useful in real world applications so I hope you have learned something here and I hope you will apply these methods to some good use if you like this course give it a thumbs up if you loved it share it with your friends and I will see you in the next one