Learn Object Classification in 20 mins | OpenCV CVZone Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to my channel in this video we are going to create an object classifier that can classify any objects that we feed in so in this particular case we will be using arduino raspberry pi and jetson nano so we will classify between these three and see how well it performs now we will be writing the python code and we will be using an online platform called teachable to actually train the system so you don't have to write a lot of code and for testing the code that we will write will be pretty much around 10 to 15 lines at max so without spending hours and hours on training and testing we will be able to get our classification model up and running within just few minutes so let's get started so the first thing we will do here is to go to teachable machine so this is provided by google you can google teachable machine or you can write teachable machine dot with google.com this actually allows us to create our classification models and then we can simply download them and use it in our python project so what we can do is we can click on get started so here we will select the image project and for this project we are going to go with the standard model with 200 pixel images so here we have a couple of options what we can do is we can upload our files and then we can also use our webcam now if you have seen my previous video on mask detection where we had a gun shooting whoever didn't wear a mask so in that case i actually used my own data sets by that i mean i uploaded my files and i captured the files using a python script and then i uploaded it here so that it is in a similar scenario when it's testing or when we are utilizing it but in this case uh we can directly get it from the webcam because i have set up my webcam here and we can bring in our raspberry pi and arduino and see how well it performs so here we can add uh through a webcam and we can select the webcam that you have connected so here you can see my table and what i can do is i can bring in the raspberry pi and what i will do is i will start recording by pressing the hold to record button so there you go so this is an older raspberry pi we can bring in the new one as well but for this example i think this should be fine we just want to see how it performs so we will take about 300 images for each so now that we have these 300 images we will go on to arduino so here first of all we will name our class raspberry pi so uh take a note of the name that you are writing here because this is the exact name that will appear on the result so whatever name you want write it correctly over here and then in the labels automatically it will classify and give you the output with the name okay so then we will do the arduino so let's write here arduino and we will use the webcam again we will switch and there you go so a little bit in out rotated so okay so we have around 300 images of these as well so let's go ahead and train this now you do have some options to actually uh change the epochs or the batch size or the learning rate so i would recommend to keep it at default if you don't know a lot about this so if you do go ahead and feel free to change these parameters so we will click on the training part the training button and it will start preparing the data and you will be surprised how quickly this works so i'm not going to pause or i'm not going to go forward you will see how quickly this finishes uh as it is so here you can see it is training and it is going for 50 epochs so that's why you have here 50 so this is 30 32 so it's almost about to finish in less than a minute or even half a minute and there we go so the model is now trained and we can switch our webcam so this is the testing part we can also add files we will actually try with the files as well uh just in a short moment so let's let's go ahead and try it out so right now it's saying raspberry pi because we didn't set any default uh values so sorry the default class so that's why it is giving a raspberry pi so it will give one of them by default so let's try the arduino first there we go so it is detecting the arduino and then let's try the raspberry pi there we go so it is detecting the raspberry pi this one i think is raspberry pi 2 yeah raspberry pi 2 it's a really old one anyways now if i wanted to add another class i would simply click on add a class and here i will write jetson nano and we are going to use the webcam again to add our uh okay i'm having i have a fan on top of this which might interfere uh maybe i should remove it let's remove it okay so i have removed the fan now and we can go ahead and take the samples so i will press okay this one is a little bit bigger than the other two so let's hold and record so we have about 300 samples now and what we can do is so we already have a trained model but what we need to do is we need to add this new class so we will have to train it again so again it doesn't take a long time so it shouldn't be that difficult so let's train it so now we have our third class training as well by the way you can see the images here so you can see the samples that you have taken you can scroll horizontally to actually see and the the idea of classification is that you are detecting the complete image as an object so it's not part of the image it should be the complete image so if the object is very far away it will not detect it properly and in a lot of cases what you can do is you can find the contours and you can find where the object is and based on that you can say okay this region i want to classify so we will see that and i will share with you how i did the what you call the mask project so in the in the next video we will look at how we can detect the face and then how we can classify whether it's wearing a mask or not so we will look at that as well so now it is trained and now we can bring in the jetson nano and you can see it detects it as jutsu nano so that is pretty good uh we bring in the arduino okay it's confused okay when again so if it goes too close it won't be able to classify properly as i said it has to be the whole image so here when when it is visible completely then it is very sure that it is arduino and uh then if we go back you can see the raspberry pi again if you zoom in it might be flickering because it's not sure so next we can try out some images so i will go to google and i will search for some images so here you can see we have a few images so let's take this one let's save it on the desktop and let's search for another image then let's go and search for arduino and we can get this one okay and let's get another one let's say this one uh this one might not detect it properly because it's uh angle but let's see it will be interesting to see if it works or not okay then we have jetson nano okay uh that's a nano let's take this image and let's take let's say this one so we are trying two images for each of these and we will see how it performs okay i will put it on the side and i have all the images on the desktop which is not a very good practice but anyways uh what we can do is we can simply change this to file and we can drag it in so let's drag in the raspberry pi and there you go it's 100 sure that it's raspberry pi uh let's try the other one uh it's 99 sure that's very good let's try the arduino okay so this one got it wrong it thinks that it's just a nano let's try the third one oh it's very sure that it's just a nano so the arduino is not working properly and then we have the jetson nano and then the jetson nano is good okay let's go ahead and try some more images for arduino maybe we can take top views maybe it will be better let's try maybe this one so the thing is what we did was um we trained the model in less than one minute and we quickly added some data set that we had so it will not be very useful in all cases so like for example in arduino it's not very uh accurate when it's from a file because of the background or because of the lighting it can be anything so what we have to do is we have to add as much data as we can so that our model is very robust but i believe if we go to our webcam it should detect it should detect the arduino properly as well so uh let's try it here so there you go so it is detecting arduino properly if it's like this but through the images it's not so that is a good way to test if your model is working properly or not so what you can do is you can add more images and you can add more uh different types of backgrounds you can even remove the background and then take the images some something like that so that's why sometimes it's better to do the images using a python script and then bring it in to upload anyways so for now i think we are done and what we will do is we will export our model we will click on export and then we are going to go to tensorflow and we will download the kiras model so we will click on download model now you don't need to worry about this code we have already created a module out of this and you can simply import it and run it so you don't need to go into the details of how this works but if you want of course you can go through they have given these comments that are very helpful so it might take a while because it will convert the model and then it will download okay so it is downloaded and you can see here that we have two files kira's model.h5 and we have labels.txt so we will be needing both of these files because label.txt has the names of your classes and then the kiras model is actual actually the model that will help us find or classify our objects so let's go ahead and open up a new project in pycharm so now we are in our pycharm and we are going to create a project we can call it classification model test so now our project is opened up and the first thing we will do is we can delete yeah let's just delete this and we will keep the main file and we will go to file settings and here we are going to add our what you call packages so we'll go to projects python interpreter and we will add so here the most important packages that we need are cv zone we will install that now this here is 3 1.3.2 so any version above this or at least this version if you go below this it will not work it will be this version or above this so let's install that and then we also need tensor tensorflow so that is the library that will help us classify so we will hit install i'm using the version 2.5.0 so it depends if you have a later later version you can use that as well but at any given point if it doesn't work you can come back to these versions and i'm using python 3.7.6 so if you are using a later version and it doesn't work for you come back to python 3.7.6 you can always find it in the archives uh on the python website so you can download and install that so let's wait for the tensorflow to actually install it is a very big library and it requires a lot of different libraries as well so right now we have just installed cv zone that automatically installs opencv and numpy so these are the two main libraries that we will be needing but other than that we will be also needing the tensorflow and it will install a lot of libraries uh you will see it will be filled up uh just in a moment but it is quite big so it will take a while so we will wait for it to install and then we will come back okay so now tensorflow is installed and we can hit ok so the first thing we will do is we will import our packages so we will write here import cv zone and then we will write import cv2 then we are going to create our video object so we will write here cap is equals to cv2 dot video capture and we are going to give in the id number zero so this is the camera number and then we are going to create our classifier now you can call this anything you want we can for now we can just call it my class c fire so this classifying class actually belongs to cv zone so you can write here cv zone dot classifier so what this will do is it will take in an image and it will classify which object does it belong to but for that to work we need to give it a model path and we need to give it a labels path so what i like to do is this is up to you if you want to do it this way what i do is i go to show in explorer and here i like to create a new folder and i call it my model then we will bring in our model files that we downloaded from the teachable website so these are the two files we are going to drag them in so these two will be here we will not change the names we will keep them the same and then here you will see now my model has appeared so you can write here my model slash and then you have to write this name so we will write here kiras underscore model dot h5 and then for the labels we have to do the same we have to write my model my model and then we have to write labels dot txt labels should be e here okay so just to make sure that everything is working fine we can just right click and run and if it doesn't give us any error and it exits it means it is working fine so these are just warnings and i don't see any errors so it should be fine okay then the next thing we have to do is we have to create our while loop so we will write here while true and then we are going to uh get in our image using the cap dot read cap dot read and then we are going to ask our uh classifier to classify our object or to classify our image so what we can write here let's say these are the predictions is equals to our classifier my classifier dot gets prediction and all we have to do is we have to send in our image now there are a lot of other parameters but for now the the least that is required is the image so we will just write in that and then we will write here cv2 dot im show and we are going to write here lets say image and we want to display our original image and then we will write cv to dot weight key and we will give it one so that should be fine so let's go ahead and run this okay we are getting an error so mostly when you get this 215 error it is because the image is empty so probably the camera id is not zero so let's try another one let's try one yeah so now it is working but it is not the correct camera oh okay i think i get the problem in teachable machine you can see here it gives us a red dot it means it is using the webcam so we need to close this or we need to at least disable the webcam so it doesn't use it yeah there you go so now it's not showing that red dot so if we go back and write zero here it should run the webcam okay so now it's working and it is giving us the output that it thinks as i mentioned before it will give us a default value so let's try out uh with arduino there you go it says arduino and then let's try jutsu nano it says it's a nano excellent let's try out raspberry pi it is going to say raspberry pi again arduino arduino is a little bit tricky still thinks it's raspberry pi okay we have to go back a lot and it is um it is confused between arduino and jutsu nano a bit so again you need to train it a little bit more so now we are able to write on our image which of the objects was detected but we need to know the actual values to actually use them in some projects so the idea is that it is actually uh returned back in the predictions and the classifier actually returns two things so we have to write here index as well so it will give you the index of the class detected and it will give you all the predictions that it found so it will contain all the probabilities so what we can do is we can simply print here the predictions and let's run that so there you go so at the bottom here you can see that these are all the predictions that we are getting so right now it's saying raspberry pi let's bring in the jetson nano so when we bring in the nano you can see uh this goes to 0.999 which is almost one so it is almost 100 sure that this is just a nano uh and the index is two so the index uh we are getting it here so we can print that too so we can write here index and we can print that so there you go so the index is two now if i remove it it goes to zero and if i bring in arduino uh again it's not detecting very well the arduino let's put it straight now we need more data for arduino but uh we can see for raspberry pi and jutsu nano it is detecting as two and then it is detecting as 0. so there you go so this is basically the idea of how you can create your own classification model and if you want to check the frame rate you can simply write here fps reader cv zone dots fps and then at the bottom here you can write fps reader dot update and we don't need what you call the result so we just can input an image on which it will draw or we can let's let's not draw let's write it here we need the fps and the second parameter we are going to ignore do we have yeah so let's print that out print fps and let's see what do we get and we can remove this part oh i think it's only fps my bad so if we are inputting an image then we will get an image output but if you're not inputting any image we will only get the number there we go so we are getting around 14 15 frames per second so this is how fast it can perform again this model is a little bit heavy so that's why they also have the option of a lighter model on their websites so 1415 frame rate is a decent frame rate it's not that it's completely unusable but uh in in real time applications it might not be the best but in a lot of different scenarios it should work fine so uh you could even write the fps on the image so as i mentioned you can write the image and you can return the image from here and here you have some parameters that you can give i believe you can give the position uh position of the what you call the placement so here you can write let's say 20 and then say 150 150 might be too much but let's try it out yeah 150 is too much so we can push it up a little bit maybe 100 and this is too big the the prediction it's drawing on the image is too big so there is an option for scale uh scale we can reduce it to 1.5 let's see so it's not that big there you go so now we are getting a smaller let's put it as one and let's push this to let's push this to 50 let's push this to it is 640 so let's say 575 or that might be too low so 550 maybe yeah it's going outside so we can write here let's say 450. yeah there you go so now we are getting a better uh image that is giving us all the information that we need so there you go so this is it for today's video i hope you have learned something new i hope you will use this in a lot of different projects and let me know how that goes for you and if you haven't subscribed do subscribe and hit the like button and i will see you in the next video
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 15,663
Rating: undefined out of 5
Keywords:
Id: M0EA4u1x4fs
Channel Id: undefined
Length: 27min 8sec (1628 seconds)
Published: Mon Jun 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.