Computer Vision With Arduino | 2 Hour Course | OpenCV Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to the world's first computer vision with arduino course here we are going to learn the basics of how to create real-world computer vision applications using the extremely popular arduino microcontroller we will first start with the basic exercises and then move on to creating exciting projects such as lab gesture control pid face tracking angle finder hand gesture control color sorter with conveyor belts and a lot more the aim of this course is not to create random projects but to understand the fundamentals and give you enough knowledge and experience so you can build your own million dollar ideas this course has a clear pathway from basic to advanced with lots of practice examples this video is part of the course the complete course can be found on our cv zone platform the link is in the description below so without further ado let's get started let's start by understanding the core principles of arduino using an example let's say we have a smart car that is equipped with automated headlights this car checks the light intensity of the environment and turns on at night and turns off during the day so what sort of equipment would be required to build such a system we would need a headlight to output the lights and a light sensor to give us information of the ambient light now there has to be something in between that gets the input from the light sensor and outputs the signal to the headlight this something is the brain of the system we call it the microcontroller unit in short mcu mcu is like a small computer that can handle small amounts of information really fast so where does the arduino come in arduino is a company that produces microcontroller boards they have different versions that cater different scenarios the most commonly used is the arduino uno they also have more compact versions like arduino mini and bigger versions that can handle more sensors like the arduino mega the main difference is the size capacity and number of pins between these boards uno has 14 digital and 6 analog pins mega has 54 digital and 16 analog pins this means we can connect 20 different sensors and devices to arduino uno and 70 to the arduino mega note that the arduino does not produce microcontrollers they produce microcontroller boards that contain microcontrollers so what's so special about these arduino board even though when referred to arduino the hardware comes to mind but the speciality of the arduino is in its software it makes the process of programming much easier than any other similar device out there in earlier times we used to write tons of lines of code just to turn on an led but now with arduino we can achieve the same result in just three simple lines of code so we can say it is a high level language that takes care of the coding details for us so that we can focus more on the actual project so how does the arduino work as mentioned arduino has its own software through which we can program different sensors and devices and we can upload it to the arduino board we can use a regular usb connection to upload the code using a computer once the code is uploaded it keeps running as long as it keeps getting the power so let's have a closer look at the most commonly used arduino the arduino uno the main component is the mcu that handles all the logic then we have the usb connection to upload the program to the board we also have a power jack that allows external battery to be connected to make it portable the reset button resets the code without deleting it then we have the pins where we attach our components we have 13 digital pins 6 analog pins and we also have power pins with 5 volts and ground so what do these digital and analog pin mean digital signal has only two states which means it can be on or off whereas analog signal has a range of states let's look at an example of a fan if we control the fan digitally we have the option of turning it on and turning it off where on would mean one and off would mean zero if we control the fan with analog signal this would mean we have a range of values therefore we can now control the speed of the fan rather than just on and off for example the range is from zero to two five five zero would mean off two five five would mean full speed whereas 127 would mean half the speed but where did this 255 value come from well this is an 8-bit value which means we have 256 values bit values are used all around us for example when you buy a smartphone you have the option of buying a 64 gb 128 256 512 or even 1 terabyte hard disk phone so where do all these random values come from these values are binary values where 64 is basically 2 to the power of 6 128 is 2 to the power of 7 256 is 2 to the power of 8 5 12 is 2 to the power of 9 and 1 0 to 4 is 2 to the power of 10. this means if we were to send a 6-bit signal to our fan we would have 64 different speeds to control if it was an 8-bit value we will have 256 speeds to control if we include the 0 as well then our values will range from 0 to 255. coming back to our automated headlight example we have an analog input in the form of the light sensor and analog output in the form of a light now this time we will use the arduino controller to run the system for analog input arduino uses a 10 bit value and for analog output it uses an 8 bit value so this means for our light sensor we get a range of values from 0 to 1 0 to 3. so zero would mean dark and one zero two three would mean fully bright similarly based on the light intensity we received from the light sensor we can output a value to the headlight to turn it on accordingly for the 8 bit value we have the range of 0 to 255 so we will have 256 brightness values let's have a look at some examples of input and output devices a distance sensor would be an analog input a push button would be a digital input motor would be analog output whereas led lights would be a digital output the distance sensor is an analog input because it gives us a value of distance for example in centimeters rather than just saying the object is near or far the push button is a digital input because it has only two states it can either be pressed or not pressed the motor is an analog output since it can be controlled with the varying speed the led light is a digital output because it can be controlled to be either on or off note that the motor and led can be used both as digital outputs as well since they both can be simply turned on or off so to summarize arduino is a company that produces microcontrollers which we can use to control multiple sensors and devices these devices could be inputs or outputs of the system and we can have two different types of signals that we can send or receive from these devices now these signals can be digital or analog digital signals have two states whereas analog signal has a range of states for analog input the arduino uses a 10 bit value which ranges from zero to one zero two three and for analog output it uses an eight bit value which ranges from zero to two five five in this video we have to scratch the surface of the arduino microcontroller as there is tons of information related to arduino to truly understand it we have to get our hands dirty by experimenting and inventing new projects the possibilities are endless we can even build our own custom arduino boards and use them to create our own projects in the next video we will look into the details of how different sensors could be connected to arduino and how we can use the arduino ide to code them we will see step by step how the connections are made and how we can read and write information from different sensors and display it to our screen so how can we connect sensors and devices to arduino in this video we will look at the basics of sensor connections and their coding so we have a lot of sensors and devices that can connect to arduino and there is something that is common in most of these devices so what is that it's the power pins most of them have a positive and negative pin let's take an example this is a temperature sensor that has three pins the first pin is the negative or the ground it is also referred as g and d sometimes then we have the positive pin most of the times it is denoted by the voltage level for example 5 volts 12 volts and so on the last pin is the signal pin mostly written as s now depending on the sensor or device it can be input or output in this example we are getting values from the temperature sensor therefore it is an input to the microcontroller in other examples such as a buzzer the signal would be an output so how do we connect these pins to arduino they can be connected by special wires known as jumper wires these are easy to use and plug in directly to most sensors and microcontrollers jumper wires have two different types of heads the one with the pin is known as male and the one without is known as female so when purchasing jumper cables we have the option of purchasing three different types the one we see here is male to female since one end is with pin and one is without then we have male to male and also female to female wires so which sensor is connected to which pin number of the arduino board as we discussed earlier the signals can be of analog or digital type in arduino uno we have 14 digital pins and 6 analog so one could say that all analog devices are connected to the analog pins and all digital devices are connected to the digital pins well that is not correct since arduino does not have true analog output most of the analog output devices such as motor and dimming lights are connected to digital pins where we send a simulated analog signal we will discuss this more in detail in the upcoming videos for now we can say that the analog pins are mostly used for analog input and for the rest we use digital pins so let's see how we can connect an led light to arduino as we know an led light has a positive and a negative connection so we will connect the negative pin to the ground and the positive pin to one of the digital pins let's say pin number two but with led light we should use a resistor to limit the current to avoid burning the led so we will need to use a breadboard when using arduino many projects require the use of breadboard so it is a good idea to learn the basics using a breadboard so a breadboard consists of power pins and component pins here we can see the conductive layers inside a breadboard the connections in the power pins are vertical whereas the connection in the component pins are horizontal this means that this point at the top of the breadboard is connected to the bottom of the breadboard whereas in the component pins this pin at the top is not connected to this pin at the bottom instead this point is connected to the horizontal point here in the same line the space in the center indicates the breaking point this means that this point here is not connected to this pin here so how do we connect the components let's look at some examples most components can be connected in two different ways if we connect vertically they can be connected in the same region when connecting horizontally they have to be connected between the cap this means a resistor cannot be connected like this the power pins are connected in the power pins section this way multiple devices can be powered from the same line so let's see how we can connect an led circuit to a breadboard if we look at the circuit diagram we can simply follow the current flow so first we will add the led then the resistor and then the battery we can label each terminal of the components to further understand this we will connect our power wires starting from the positive side of the battery the current flows towards the first terminal of the resistor so we can connect this with the wire here then the current passes through the second terminal of the resistor and moves toward the positive terminal of the led we can add another wire from the second terminal to the positive side of the led then the negative side of the led goes to the negative side of the battery so we can add a wire from the negative point to the negative point so we can see the current flow and how it creates a loop so now that we know how to connect an led light how can we control it with arduino we can simply replace the battery with arduino where the negative side will go to the ground and the positive side will go to one of the digital pins of arduino which we selected earlier as pin number two so when we send a high signal to pin number two the voltage level at this pin goes to five volts therefore turning on the led light and when we send a negative signal to the pin the voltage level drops to zero and the led light turns off so let's connect this using the breadboard first we will add our components then we will start from the positive side which is our pin number two we will connect it to the resistor and then from the resistor we will connect to the led positive side then we will complete the loop by connecting the led negative to the ground of arduino so let's program this led to blink in arduino we have three sections of the code first one is the initializations and declarations second is the setup and third is the loop the first one we will discuss later the setup is the section where we define which pin numbers are our sensors and devices connected to and whether they are input or output this section of the code runs only once now the loop is where we run the main code it iterates over and over again so in the case of our led we would write the code of turning on and off in this section so let's write the code first we will add the setup function then we will define that our led is connected to pin number 2 and it is an output device for that we can write pin mode 2 and then output next we will close the setup function and write the loop function inside this we will send a high signal which is 5 volts to the led pin number 2. we can write digital write pin to as high now we will add a delay of one second by writing 1000 milliseconds to make it blink we have to turn it off as well so we will write digital write pin 2 as low and add 1 second delay again now we will close the bracket so this code is enough to run the led blinking example but it is not the best way to write the code let's say we want to change the pin number from 2 to 5. we will have to replace the pin 2 to 5 manually in a lot of places this is a simple and short code so it would not take that long but imagine if we have hundreds of lines of code that would take a while to find and replace so what we can do is to add a variable now the variable can be added in the first section of the code that we did not discuss before so a variable is just like a box that has a name and it can store a value inside so we will write integer my led pin is equals to 2. here the my led pin is the variable name and 2 is the value that is stored inside this variable but what is this int well this is a data type integers can have values between minus 32 000 and 32 000. they cannot have decimal values now to upload this code to our arduino board we have to first define the type of arduino board we are using we are using arduino uno so we will go to tools board and select arduino then we will select the port number again from tools port and then the port number note that you might have different port number all you have to do is to make sure that you select the correct port number once you have selected you will see a tick mark next to it the last step is to click on upload and now your arduino blinking code is uploaded if you do not have an led light you can use the built-in led light of arduino to test the led light is connected to pin number 13. so all you have to do is change my led pin to 13. let's say we have a motor and a 9 volt battery if we connect them together the motor starts to move now what happens if i change this 9 volt supply to 5 volts well the speed will decrease we can say at 9 volts we have high speed and at 5 volts we have low speed in other words speed is directly proportional to the voltage but the question is what if we wanted to change the speed of the motor without changing the power supply because we cannot change the power supply every time we want to change the speed well we could add a resistor so there will be some voltage used by the resistor and the rest will be dropped at the motor so the motor will move slower now so let's say 20 percent of the energy is used by the resistor and 80 is used by the motor so the question is where does this 20 of the energy go well the bad news is that it dissipates as heat therefore this is not a good way of controlling the speed since we are wasting energy this was the practice up till 1960s but then somebody came up with a better solution so let's have a look at that so let's say we have the same circuit as before but now instead of the resistor we add a switch this switch we can turn on and off given that our voltage is 9 volts if we turn on the switch the motor receives 9 volts and if we turn off the motor receives 0 volts now if we rapidly turn the switch on and off our average voltage will decrease so if we turn on for 50 of the time and turn off for 50 percent of the time then our average voltage will decrease to 4.5 volts now if we turn on for 80 of the time and turn off for 20 of the time then our average will be higher which will account for 7.2 volts similarly if we turn on for 30 percent of the time and turn off for 70 of the time the voltage will average at 2.7 volts so this here is our pulse and by changing its width we are controlling the speed therefore the term arises pulse width modulation the percentage of the on time is known as the duty cycle so in this example the duty cycle would be eighty percent now to implement this on a motor using an arduino or raspberry pi is actually fairly simple with arduino there is a negative function analog write which takes in the pin number of the motor and the speed required ranging from 0 to 255 here our pin number is 9 and the speed is 127 which is half of 255 along with this we can specify the digital right to change the direction of the motor as well in raspberry pi the gpio0 package contains the motor module that allows easy use of the pwm after specifying your motor pins you can simply type forward or backward with the value ranging from 0 to 1. to control another device such as led the arduino code would be same using analog write but for the raspberry pi the pwm led module could be used where the value of the pwm can be specified by writing led dot value and this value is basically the percentage for example in our case it is 0.5 which means 50 percent to install arduino first of all we are going to go to arduino.cc and we are going to download the software so here you can see the software tab and you can click on that and that will take you to the downloads and this is the latest one arduino ide ide stands for integrated development environment so it is like a notepad but with extra features that will allow you to write your code it will tell you where you made mistakes and it will help you compile the code so this basically uh is the windows installation uh windows 7 and newer this is the windows zip file now most cases i would recommend using windows uh the first link and for the second link you can use if you are using an office pc or a university pc which does not allow you to install then you can use the zip file but there are some cases where it doesn't work properly so the best case that installs the drivers as well is the windows 7 and newer link so go ahead and click this if you are on windows then if you are on mac os then you can click on this uh 10.10 or newer and of course you have linux versions as well so what we will do is click on windows 7 and newer and if you want to contribute then go ahead and do that you can see here it is downloaded 54 million times impressive in fact yes and yes so you could contribute and download or you can say just download so that will start the download it's just 113 mbs so it should not take a long time once it is downloaded then we will install so now we are going to install we will click on it so we are going to agree with the license agreement and then we are going to click on next you can change the location if you want i'm going to keep it the same we will hit install and then we will wait for it to extract and install okay so now it is completed we will close so here you can see we have the arduino icon so we can click on that so here is an automatic tab generated for us so here we can write our code so once you have opened up arduino what you can do is you can go to file examples and there will be a lot of examples that you can start off with but what we will do is we are going to go and install a library that will help us communicate with python and therefore we will be able to run a lot of different programs so we will go to sketch and include library and then we will add a zip library so now you will make sure that the arduino library that we have for cv zone is downloaded and that zip file you have stored in a folder so you will go to that folder and you will click on the cvzone.zip file again all of this you can download from our website so it should be available in the downloads so we will click on this and hit open and this will install the library so you can see here it says library added to your libraries check include library menu so how can we confirm this we can click on file examples and we can go down and we can find cv zone so here you can see these are the examples get led and send potentiometer these are the two examples that come with the installation of this library so that is the indication that it is installed and we are good to go so here we have our typical arduino uno there are other arduino types as well that you can use but for this course we are going to use arduino uno throughout the course so you can use another version that is very similar to this there you go so only the actual chip that is a little bit different but the functionality is exactly the same so you can use either one of these so the first example that we are going to do is of an led light now there is an led light connected directly to pin number 13 and that is the one that we are going to use and it also has a resistor with it so normally we need a resistor along with the led light so that it limits the current and it does not burn the led light so here pin number 13 is directly connected to an led the one that says l over here and we are going to use that for this example so let's go ahead and do that so here in our arduino code we have two sections the first one is void setup this uh code will run only once so this is a comment mentioning that and then you have a void loop that will run again and again repeatedly so we don't need these comments so we are going to ignore those or remove those and then what we have to do is we have to declare our pins so which pin numbers are we using so here we will say pin mode we are using pin number 13 and we are using it as an output so we are the ones sending commands to it so if we are sending commands to a sensor that is an output if we are receiving the commands from a sensor that is an input so this is basically the idea so we will write here pin mode pin number 13 as output and then to run it we have um because we are connecting it to a digital pin which is pin number 13. digital means we have the option of on and off so if you have uh analog analog means you have a range of values for example 0 to 1023 for example so in this case we have zero and one so we will write digitalwrite because we are writing we are sending the value so we are saying rights and we will say we are sending it to pin number 13 and we are sending the signal of 1. so 1 or you can write it as high as well but if you write it high you have to write capital letters you cannot use h capital and i g h as small so all of them need to be capital so then we can simply put a delay of let's say a thousand milliseconds so this is milli seconds and if you want uh for example five seconds you would write here five thousand if you wanted ten seconds you were write here 10 so this will be 10 000. so we are going to use one and then we will write here digital rights and we will write pin number 13 is zero and then we will write delay 1000 again so this will blink our led lights that is connected by default so all we have to do now is to connect our wire to our arduino and run it so let's go ahead and do that so here we are going to connect the wire to arduino so you can see we will just plug it in and you can see the sound and you can see also here we have the led that is on by default maybe there is a code already that is running but we are going to go ahead and upload this code so to upload this we are going to go to tools and here in the ports we are going to select the arduino uno so this is com6 some of the arduinos they don't really show you that they are arduino so sometimes you have to guess as well if you have multiple ports so we can click on com6 and the other thing you have to make sure is that the board is arduino uno so here you have to make sure that this is arduino uno there are other types like nano mega micro there's a lot of different types of arduinos the ones we are using is the one we are using for this course is arduino so once that is done you can see on the bottom right corner it says arduino uno on com 6 and then you can either compile um compilation is just to verify whether your code is correct or not if there are any mistakes otherwise you can upload because it will first check whether your code is correct or not and then it will send the code so now we will go to the location of our course folder and there we are going to save this arduino code so here we are going to create a new folder and we are going to call it arduino code and inside here we are going to write inputs and then we have outputs so right now we are working with outputs so we are going to write here led basic so for each input or output we are going to look at the basic code and then we are going to look at the code that will allow us to do serial communication with the python code so this basic means if at any point you are facing any issues you should come back and check the basic code if the basic code is working fine that means the hardware is fine there's an issue with the software so this is a very good idea uh whenever you are using different sensors so now it is uploading it is saying compiling sketch it is checking if everything is correct and once it is done checking it's going to upload now it is uploading and now it's done uploading so we are going to go to the arduino board and we will see whether it blinks or not so there we have our arduino uno and you can see it is blinking so let me zoom in here and you can see the orange light is blinking continuously it is turning on and off for one second on and one second off so this is exactly what we wanted to do now in the next step we are going to do the same thing but through python code instead of arduino code so now we are going to create a new file and the other one on the other side now we can simply zoom in and out by pressing the control button and scrolling the mouse if you want to do that you can do that to see more clearly okay so what exactly do we do now now if you go to file examples and you go to cv zone where is the cv zone and this is get led so getting is in terms of arduino so arduino is getting the value that's why we are writing get uh led now this is the basic code of receiving the command from python and getting it to arduino so how does it work first of all what we do is we write include include the cv zone package so cv zone so here we call it library so we are going to import or include our library and then there is a class within this library called the serial data so basically how we are sending the information from python to arduino and arduino to python is using serial data so what we will do is we will simply write here serial data and we are going to create an object so we will write here serial data it's pretty much the same thing but with uh s small so this this you can write anything this is the name of the object you can pretty much write anything you want but we are using serial data so we will use that same so then you have to write the number of values you want to receive so number of values you want to receive so i want to receive for example two values but in this case we only have one led so we will write here one so we are receiving one value it doesn't matter how long the value is it can be four digits it can be two digits three digits but it here we are just writing the number of values so we want to receive only one value from python and then digits per value so for example if we were receiving 0 or 1 then it will be only one digit if we were receiving so this is 0 or 1 is basically 1 digit if we are receiving the value from 0 to 99 then we have two digits then if we are receiving from 0 to 999 then it is 3 digits so i hope you understand uh this point i will keep it here in case you want to review that so in this case we are only going to receive one digit so either it will be on or off we don't need to add anything extra so we will write here one digit and we will put a semicolon there you go so i can just copy this for your reference this is number of values this is number of values and this is digits per value received so this is basically the idea and once that is done we are going to create an array now this array basically whatever value is received it will be stored in this array because you don't want to create variables one by one and then store for example if you had five values coming from python you have to create five variables to store these values instead we will create one array in which we will store all the five values in this case we only have one value so even in this case we have to create an array because this is how the library works so what type of values are we getting we are getting integers so we are going to write here values receive and we are going to write here only one and then here we have the void setup in the void setup we have to define that we have our pin mode pin mode pin number 13 as output so this is the code for the led light and the code that is specific for the library is to initialize our library so that can be done by writing serial data dot begin so if you have used arduino before sometimes we write serial.begin so this is similar to that instead of writing serial.begin which is for the serial monitor we are writing serialdata.begin and here inside you can give in the value of uh the baud rate so by default it is 9600 if you are using a different baud rate and if you don't know what portrait is then you don't need to go into that much detail but by default it is 9600 then in the loop which will happen again and again we are going to write the command to receive the value so simply we have to write serial data dot get and whatever we get we want to put it in values received values come on received there we go so it will be stored over here automatically and then whichever value we want to use we can use it so if i wanted value number one i will write here values receive at zero so this will give me the first value if i wanted the second value i will write values receive one so in this case we are only asking for one value so numbers of values received is only one so we are going to write that we will only get the zero but what do we do with this we send it to the led light so we will write here digital rights and we will write pin number 13 as values received at zero so earlier we were writing here high or low one or zero but right now what we will say is get it from the led get it from the python code and then we will decide whatever the value is we will send it here so from python we can send our value directly here so what happens in the python code is basically it sends a string so a string will be something like this it will start with a dollar and then there will be zero or one so if we want to send one value um we will write it like this if you want to send zero value we will send it like this because it is one digit so for example if we had multiple values so it will send it like this so let's say we had uh two values two values each with three digits so the first one will be for example 0 0 1 the second one let's say is 265 so these values will come in like this right so if i had three values with uh three digits then there will be another digit it will be let's say 13 so 0 1 3. so it will come in like this so this will be our value number one so it will be stored in zero this will be value number two and this will be value number three so if we want to test it without going to the python code we can test that too so how can we do that we can simply upload this code so here we are going to write led instead of led basic now we are going to write led which means it is led serial so this will save the code and it will compile and now what we can do is we can click on the serial monitor so this is basically how serial commands are sent so if you wanted to send a serial command you can send it from here so right now you can see that the arduino light is off there is nothing on on the arduino site so what i can do is i can write dollar and one and that should turn on the led light so let me bring it a little bit closer let's try to focus so there you go now you can see it is focused so if i write here dollar zero now you will see that the led light turns off and if i write dollar one you see that the led light turns on so now we need to send these commands from our python code so i'm actually holding it in one hand and i'm typing from the other that's why it's shaking a lot so because here it's not very clear so that's why i'm holding it up okay so now what we will do is we will go to the python site and then we are going to write the code from there so here we have opened up by charm and we are going to write here arduino cv so computer vision arduino and we are going to create this project so this opens up a main file and we can delete that uh in fact we can delete the whole thing we can right click and where is delete there we go so we can delete that and this creates a virtual environment for us and this is our project and we can create uh all the files in here so we will right click and first of all we'll create a folder which is called a directory here so we are going to call it outputs so we are working with outputs later on we will work with inputs as well so we can write that to directory and inputs so here in the outputs we are going to create a new file a python file and we are going to call it led so this is basically our led code and what we will do is we will first of all install our cv zone library which includes all of the functions so we will go to settings and here we will go to projects and we will click on python interpreter and here we are going to add so here we will write here cv zone and we are going to hit install package now keep note of the version so if any at any point if you are not able to work or if you're not able to work the code if it's not running properly then you should come back to the exact same version so do try the latest version no problem with that but if it doesn't work you can always click on specify version and then you can choose which version you want to run so here we are using 1.5.1 so make sure you use this and this is automatically going to install numpy and opencv for us which is the back end of our computer vision functions so everything which is related to computer vision it is done by the opencv library we have the cv zone package which is kind of a wrapper for it which allows it to have extra functionality and that is pretty much it so now to run the led light what we have to do is you have to send a serial command so to send the serial command we need to import the serial module so we will simply write here from cv zone dot serial module import serial object and then we are also going to delay so to delay we need time so we will write from time import sleep import sleep so if you remember in arduino we wrote a delay delay 1000 here it will be sleep and we can give it a delay okay so now here is the interesting part what you will do is you will create the arduino object so you write here arduino this is just the name you can call it anything you want and then this is the object so serial object that we imported so we will write it here and we will not give it any arguments so no input parameters so why is that by default it will detect the arduino if there is something if there is any arduino connected to the computer if your arduino does not get connected then you can specify the com port so here if you go to tools you go to ports here you can see it says com6 so if it doesn't work by default you can put quotations and you can write here com6 so if i do that it will connect to arduino so we can right click and we can run led okay so uh there is an error import serial and that is a good point because we forgot to add a module so while we are working with the cv zone package it does not install the serial library for python by default so we have to install it so we will write here pi serial there you go so we will write here pi cdl and we will hit install again keep note of the version it's 3.5 so we will hit ok and then we will run it again there you go so it says serial device connected so it means it is working properly but as i mentioned before you don't need to write anything here it will automatically detect any arduino that is connected and it will run that so if i run it again here it says arduino ono is detected on com6 and it is already connected so this is the first part and then we can write a loop so we will write here while true so this is uh like writing the infinite loop in arduino so we will write here while true and we will write arduino dot send data and remember we are always sending a list or an array so in arduino we are receiving an array in python we are sending an array or a list so here we are going to write brackets and in that brackets we are going to send the value so let's say one if we were sending more than one value we can write here let's say two and then six and five and whatever values we are sending we can send it like this but here we are only sending one value so we will simply write that then we can give it a delay so we will write sleep for one second and then arduino dot send data and then we are going to write zero so we want to send one and then we want to send zero and then we will write sleep for one second so this is basically the idea actually let's do something we will turn it on for two seconds or let's say three seconds and we will turn it off for one second so that is a little bit different than our plain arduino example so we know that our python code is working it's not just the previous code running on arduino okay so as you can see on the arduino nothing is happening right now so if i run this code it says arduino uno connected and now you can see the led light it turns on for three seconds and it turns off turns on and then turns off okay let me zoom in a little bit so you can see there you go so here you can see it turns on for three seconds and then it turns off turns on turns off there you go so this is how you can send the command from python to arduino now we do not have to change any code in arduino site all we have to do is we have to change code in the python site to work as we wish so this is very convenient so now that we have the led working what we can do is we can add a little bit of graphics so we did not use the computer vision library so far so what we can do is we can give it some sort of visual effects some graphics so that we can see it working in real time so here we are going to write import cv2 which is our computer vision library cv stands for computer vision and then what we can do is we can import some images and then we can display whether the led is on or off so what we are going to do is we are going to create a new folder here and we are going to call it resources resources and inside this resources we are going to add our images so again all of this can be found on our website you can download it from there and you go to resources and you can so all of these all of this folder you can direct uh you can download directly here i will bring in one image one by one so that it is easy to know what are we dealing with so now what we are going to do we are going to use this led off this is led on uh since it is an orange led we are using the orange color and then we also have an image that has this orange light on here and then orange light off here so we can use either one of these as we wish so how can we do that let's go ahead and find out so here we are going to first import our images so we will write here image led on is equals to cv2 dot i am read so we are reading the value reading the image we are reading it from the resources folder resources and within the resources folder we have uh should we use this or that let's use this one first led led on dot jpg and we will copy the same thing and we will paste it here and this time we'll write off and we will write here off so now what we can do is we can display this image so how can we display that we can write here cv2 dot im show and we are going to write the name of the window lets say image and we will write image led on because here we are sending the command of on so we will write here on and then we will give it a delay now this delay we cannot use sleep there is a specific delay when we want to use the oh i did a mistake i should have used uh another file led graphics uh graphics so it will be easier for you to know what is happening let me copy that here and we can push this back to the original code here we will remove import cv2 led and there you go so this is what this was our old code and that we will keep it like this and then we will go to the led graphics where we left off so we are writing here and we were saying that we cannot use the sleep because there is a delay method for the computer vision library and it is called cv2.weight key and here we can add delay like we did in arduino so here we will write milliseconds so here we will write 3 milli 3000 milliseconds which is equals to 3 seconds and then we will copy this part and we will remove the sleep and we are going to write here image led off and we will delay for 1000 milliseconds so we can remove the sleep uh from the time library and yeah that should be good so let's go ahead and run that and let's see if we get any image so we are facing an error so there is an issue with the device did we turn it on somewhere no it seems fine let's run that again okay so yeah there is an issue with cv2im show minus two one five assertion failed empty see the reason i'm still showing all these error all these mistakes is so that you can understand uh how this works i could very easily edit all of this but i'm not doing that so that you learn life so learning live is very valuable you can see what sort of mistakes other people do so you can learn from them as well okay so this seems to be an issue and i will tell you there is a very small issue here the issue is that the file that we are using is in this folder outputs and if i want to use the resources folder what i need to do is i need to go back to the previous folder which i can do by writing dot dot slash again dot dot oh sorry dot dot slash so if i do this it will go to the main folder and then it will search for resources and then it will search for led otherwise it will not be able to find so let's run that again and there you go so three seconds it will be on and one second it will be off actually let's let's make it even let's make it one second on one thing off it will be easier to see what is going on so on off on off let me zoom in the arduino as well so there you go it's on off on off at the same time so we could do the same thing with the other images so we can copy this part and we can comment it by pressing ctrl and slash and then we can paste it here and instead of the led on oops instead of the led on we are going to write here pin 13 on and here we will write pin 13 off there you go so let's run that and there you go so uh where's there you go so on off on off see the the result is very subtle so it is a little bit hard to see but i hope you get the idea so it turns on and off based on this led example so you can bring in any kind of image that you want and you can have on and off images as you wish and based on that you can write your own code so now we have a arduino uno attached to a board this is a plate which is made of metal now if you have a wooden plate that is recommended so later on we are going to use wooden plates as well so and we also have a breadboard attached to it and the only thing that we have done is that we have connected the positive and negative which is 5 volts and the ground to our breadboard so the red wire here is the positive and the blue wire here is the negative and sometimes with breadboards what happens is not sure if that is clear let me remove one light there you go i think that is more clear so what happens sometimes is that you will see that there is a dash or there is a gap in between this line so this red line and this blue line so if there is a gap between these lines it means that the breadboard is not internally connected so you can attach a power supply here and a separate power supply here so if you want to use the same power supply all the way then you have to attach these small cables so we are going to use these we have a box of them so these are specifically breadboard wires so they come in this box all different sizes so we can use these to attach to our breadboard so what we are going to do we are going to attach a potentiometer a potentiometer is basically a variable resistor so resistor can be used to limit the current and this is basically a variable resistor which means that it can vary uh its resistance let me try to focus that there you go so how does it change the resistance how does it vary it by rotating so if you go to your car and you change the volume there is a potentiometer inside so when you rotate it it increases and decreases the resistance which increases and decreases the volume so this is how it works in your car and this is the same concept that we are going to use and we are going to check the resistance within our arduino board and we are going to receive the values in our python code so that we can plot it we can we can plot a graph we can use these values in different projects so how can we use this we cannot directly attach it to the arduino so what we have to do is we have to attach it to the breadboard and from the breadboard we can attach the power and the negative the ground and then the signal pin we will attach to one of the analog pins so this will give us an analog reading which means that instead of on and off it will give us a range so whenever we rotate the potentiometer it will give us a different value and this value will range from 0 to 1 0 to 3 because it is a 10 bit value so the resolution is 0 to 1 0 2 3. so where exactly is the positive and negative so the positive and negative it's not exactly defined so you can use this as positive or you can use this as positive it's pretty much the same thing and the middle one here this is the one that is a signal so this will send us the signal the positive and negative on the side you can flip as well and it will not be a big difference so here how are we going to attach this we are going to simply place it here let's say over here and now we are going to attach our positive and negative cables so we can we can put it at the end and we can attach it to the positive and negative because this line is positive and negative so we don't need to go to our arduino board to connect it we can simply connect it here and the signal pin we will bring it up from here and we will connect it to a0 over here so let's go ahead and do that so here we have different size of cables let me bring in a little more lights there you go so we have a lot of different sizes here so let me open that up there you go and what we will do is i'm going to find a small one because there's not a lot of distance here so we will grab maybe this red one so the red one is attached and the yellow one is a little bit bigger i believe and we are going to attach it to the negative so the negative will be one two three four here there you go so let me show you here so let's remove the light a little bit and hopefully you can see here so this here is the positive cable it is connected to the red part and this here is the yellow one it is a negative cable so all we have to do now is to place it here with the negative and positive there you go there you go so the circuit diagram of this is also available on the website so if you're not able to understand it from here you can always check the circuit diagram and the last pin that we have to connect is basically the signal one so what we will do is we will take let's say an orange color so orange is normally signal red is basically positive and black is usually ground so we are going to place it in between here and what i like to do is i don't like to place it directly like this because then it is visible like this it is in the air so i i don't like to do this instead what i like to do is i like to push it down therefore i have added these gaps so these are these spacers you can see so you can push through the wire from down so that it is easy to manage and it will look much cleaner than before so here you can see i have done the same for the positive and negative so i'm going to push it down here and hopefully it will come from the other side there you go and then i can simply plug it in here so pin number e0 and now i can push the remaining parts inside so it is not that visible there you go so it is much neater this way so there we have it so now that our connection is done we will go ahead and write the code in arduino so the first thing we will do is use the basic code to see if everything is working fine and then later on we are going to add our serial communication part that will send uh the commands to our cv zone package in our python code so here what we will do is we will declare that our pin mode pin mode number a0 is basically input now you don't necessarily have to do this because by default it is an input but just in case if you want to you can write it here then we are going to write serial.begin 9600 this is for the serial monitor and we are not going to use this when we are doing serial communication with python we will only use this when we are just testing the device so here we are testing the potentiometer so we are going to use it so here we are going to write for example integer uh let's say potentiometer value is equals to analog so now we are going to read the value and the value that will be read will be analog which means it will be a range it will not be just 0 or 1. it will be from 0 to 1 0 2 3 because it is 10 bits what does 10 bit mean 10 bit means 2 to the power of 10 which is equals to 1024 values so this is the resolution so you can have 1024 different values and because we use from zero to we start from zero therefore it is zero to one zero two three if we start it from one then it will be one two one zero two four but because in programming we start from zero so we use zero to one zero two three so this is how this value comes about then analog read what we are going to read a0 and then we are simply going to print it so we will write serial dot prints and we will write print always in a new line ln means new line so we will write here what do we want to print we want to print the potentiometer value so if i run this now now before we run it we actually have to connect it so we are going to connect it to the arduino board there you go and then we are going to click on tools ports and we will select our arduino so you might see different arduinos each time we are doing a different device because we are diff we are using different arduinos for different projects so uh then we are going to click on upload and again we are going to save it in our course folder here in the inputs we are going to write here potentiometer basic okay so now it's done uploading and we can click on the serial monitor and this should give us the value so right now it is giving us zero so let me go ahead and move it and we'll see if the value changes so here i will rotate it and there you go so now you can see the value is changing quite dramatically and there you go it stopped moving stop moving stopped so there you go so it goes from zero to 1 0 2 3. so now we need to read this value in our python code so we can make use of it we can plot it we can draw it we can use it in different projects and so on we can also click on auto format so it arranges everything for us so now we are going to create a new file and then we will put this on one side and we have the other on the other side and what we will do is we will use the code to send this value to our python code so here we will write file i will go to file examples and in the examples we'll go to cv zone and send potentiometer so these are the two basic examples that we have get the led and send the potentiometer so let's say that this is our code so what exactly are we doing so first of all we are going to include cv zone so this is the library that we are using so include cb zone dot h this is the header file then we are going to write serial data like we did before in our led example serial data and this time we are going to write integer send values and we will put the value of 2. now uh whatever the value you are sending if you are sending one value it's fine if you're sending two values you have to write minimum of two here so don't put less than two don't put 1 here sometimes it creates some issues so it is highly recommended that you don't put the value of less than 2 here even if you are using only one it is fine just use the value of minimum two here so minimum value of two even when using when sending one okay so that is the basic idea and then again the idea here is that we are going to send an array we don't just send one value one by one we send an array of values and that's how we communicate with the python code so here we are going to write serial data of serial data dot begin so last time we wrote it like this and as i mentioned before you can write the baud rate if you want so you can write it like this so then we are going to write in our potentiometer value so here we will write we only have one potentiometer so we are going to write integer potentiometer value uh is equals to analog read and we are going to read the value of a0 so we are not going to read the second potentiometer we don't have it at this point and then we are going to write send vals at 0 is equals to potentiometer value now here we are just storing the value in that array once we store all the values right now we have just one value so we have stored everything if we had multiple values like over here we had two values then we will store that also before sending it so once everything is stored in that array then we can write here serial data dot send and we will simply say send all the values that's it so that's how simple it is and remember don't use like we used it here serial begin don't use this you cannot use this because by default it is using it internally so now we can save it so file save as or save we can put this in inputs and this will be potentiometer so that is saved and now we can upload the code so if we open up our serial monitor you will see that the values are being sent and if we rotate you can see the values are sending so this is basically the idea and now what we can do is we can go to our python code and we can receive the values over there so here we are in our pyjama project and the first thing we will do is we will create a new file in the inputs and we are going to call it potentiometer and we are going to receive the values from the potentiometer so like before we are going to write from cv zone dot serial module import serial object and we will call this arduino is equals to serial object and we will not define anything because by default it will find the arduino so we will write here while true and then we are going to write my data is equals to is equals to uh arduino dot get data so whatever data is there just get it and then print my data at value zero because we know that we are uh we are receiving two values the second one we will ignore the first one is the one that we want so we are going to write it like this and let's run this and see what happens so there you go so now we are getting it and let's go ahead and i will move the potentiometer and there you go so it is updating in real time when i rotate it you can see now it's the maximum value and then i move it back and there you go it is reduced so this is how you can send the value of the potentiometer to your python code and you can print it out like this so now we are going to add some graphics to this potentiometer so we will right click and create a new python file and we will write here for 10 share meter graphics and we are going to copy this code and we are going to paste it here now to add the graphics we first need to import cv2 so we are going to write here import cv2 and then we also need imports numpy as np so i will tell you why we need numpy so once that is done we are going to read in our image so we will write here that our image is equals to cv2 dot im i am read and we are going to read again if you remember last time we have to go back first so we will put two dots and we will put a backslash and then we will go into the resources folder and then we will go into uh a new file we did we copy it yet or not resources no so i'm going to bring it here again this will already be available in the resources folder if you have downloaded it from the website but this is the file we are going to use potentiometer so potentiometer dot jpg and then we can simply show it so we will write here cb2 dot i am show we will call it image and img and then cv2 dot wait key which is there to delay so that should be good so let's go ahead and run it to see if it is running properly and there you go so this is the potentiometer control dial and here you can see this is the value that is coming here so what we need to do is what we can do is we can put a bar here a circular bar that rotates and tells us how much value do we have so here it will be 0 and then all the way till here it will be 1 0 2 3 so that will be visually appealing and in the middle we can write our text of what exactly are we getting in terms of the raw value coming from here so how can we do this first of all we cannot just draw a circle if we draw a circle uh we do not have the option to draw half a circle or 90 degree circle or we cannot draw a portion of it but what we can do is we can draw an ellipse in opencv it allows us to draw basically a portion of it rather than the complete part so for example we will write here cv2 dot ellipse and we will draw it on our image and then we have to give in the center position now these values i have already taken from the image so you can copy it exactly like this otherwise you can if you create your own image feel free to play around with these values so that you can find the correct center so this is the center for our image that we have right now and then we have the axis so this will be 131 and 131 and then we have the angle so the angle by default should be zero and then this is the starting point of the arc so that will be let's say 0 and then the ending point so let's say that is 90. for now later on we can change it and then we have the color so let's put the color 255 180 and zero and then we have the thickness let's put it as 10. so do we need anything else i think it should be fine so let's go ahead and run it and there we go so this is your arc that is been drawn right now so this is what we get so the first thing is that zero is at this point so we need to start from minus 90. so let's rectify that so instead of zero we will start from minus 90. there you go so it is starting from minus 90 and it is going till 90. there you go and then uh if you want to know what this is this is the value um of how much stretch do you want in the x and the y direction so if if it is not the same value it will not be a circle so let's say if i put here 200 you will see the effect there you go so now it is stretch in the vertical direction and if i put it 200 over here then it will stretch in the horizontal direction there you go it is stretched in the order so we don't want that we want it to be exactly like that 131 131 and this is of course the center point if i move this i might not draw let's see yeah it moves it just shifts offsets so this is the basic idea now what exactly do we need to change we need to change the last value the final value so what we can do is we can send in the direct value that we have but here the issue is that this will intake let's say from minus 90 to 270 right so this is the intake for this point if we put 270 it will complete the circle but our value that is coming from the sensor is from zero to one zero two three so we need to change this range into this range how can we do that let me put it like this as a comment so you can uh reference it later and so how can we do that we can do it by a little bit of maths but we can also do it with the numpy library it actually allows us to change the value so we can write here numpy dot interp and we need to give in our first value the one that we are getting the one that we want to change so that will be my data at 0 but this is a string value this is not an integer so we need to convert it into an integer this is just text so we will convert it and then we will give in the first range the range we are getting it is 0 to 1023 and the range that we want it in is minus 90 to 270 this is the range that we want it in and now the output value will be the correct value so we can simply write that let's say our value is equals to this so that's the idea and now i can take this and i can put it over here value so if i run this now uh did it change it's giving 500 something okay let's move it there you go so it is moving and it looks really smooth there you go now the thickness can be more let's say [Music] 16 yeah maybe uh if we want to cover its whole i think maybe 25 what is the value oh there's an error uh yes this error i will tell you why this happens sometimes the data is not coming properly so it gives us an error uh still we need to add a little bit more maybe 27. okay there you go i think that's fine so this looks nice and what can we do next oh uh yeah we need to fix this as well when it goes to zero it draws in the center so we don't want to do that so what we can do is we can say that only draw should we do it before or after so we can write here value is equals to my data or we can do it after as well actually no no we have to do it before my bad so here we have to write value is equals to my data at zero and we will write that if ma if value is not equals to zero then we are going to draw we will push this in and we can put value over here instead of my data 0. so if we have the value of 0 now it should not draw at all okay again we are getting the same error now this error you can remove by simply writing try and then accept some people might consider this lazy way but it is fine for this case it works um we need to push it in till here that is good and we need to remove this there you go so now it should not give us this error if it if it's unable to get the first value or something is wrong then uh it will ignore it and it will run again so this is this happens only once or twice in the beginning and later on it should work without any issues so writing it this way is not a big deal okay let's run it okay so it's still not working why is that oh this is outside wait why is this outside the loop shouldn't be by mistake i think i pushed it outside and there you go so but why is it still showing if value is not equals to zero then only it should draw let's check the value why is it giving print value let's check that it is 0 then why is it showing this ah because this here right now is a string so we need to change it to string or we can change this to integer um but we can do it like okay yeah so here it is not showing anything and then if we rotate it should show the values there you go so that is basically the idea uh we can remove this part here and what else do we need to do anything else yeah we can put the value actually so we can write here cb2 dot put text and we can put the image and then we will put the value now the value we can directly put it like this but then sometimes it will be two digits sometimes it will be three so the position will keep changing so what we can do is we can always give it four digits so if it is let's say 10 then it will write 0 0 1 0 if it is 100 it will write 0 1 0 0 so it will fill a zero in the start if it is not four digits and whatever digits are required so we can write uh value dot z fill and we will tell it to always have four digits and then we are going to write here the position so again i have already checked what the position should be cv2 dot font uh hershey plane and then we will write three and the color we are going to put white uh we are going to put white here two five five and two five five and then we are going to put three as the scale ah no not the scale the thickness okay so this is the scale this is the thickness so let's run that and there you go so sixty eights let's try it there you go it moves zero zero zero so what i was mentioning before let me show you again uh if we don't do z fill then you will see the values they they shift so now it's four values it looks good but then here two value one value it just looks bad it's not centered so it doesn't look that good so that's why we are using the z fill so it always remains in the center and it looks good whether it's two digits one digit or ten digits and not ten four digits so that's basically the idea so now we are going to create our face detection project so in this project whenever we detect a face we will have our led turn on so we will send the signal from the python script to our arduino code to turn on an led light so we will go ahead and right click to our project folder and we will create actually let's create a new folder first directory and we will call it project project one and we will call it face detection so here we are going to right click and we will create a new file and we will call it face detection basics so to detect faces is actually fairly simple we are going to use the media pi package that is provided by google and we have a wrapper for that which is our cv zone package which makes it easier for us to detect these faces so that's what we are going to use so here we are going to import cv2 and then we are going to write cap is equals to cv2 dot video capture so we are going to turn on our webcam and i have multiple cameras i will be using camera number one so if you have just one camera then you can use id number zero then we will write here while true and we are going to write success and our image is equals to cap.read so when we call this function cap.read it will output us an image and whether it has detected uh whether it has grabbed a frame or not so this is the basic idea and once we grab the frame we are going to write cv2 dot im show and then we will write image and image so this is the name of the window image and this is the image itself that we want to display and the last thing we have to do is we have to add a delay otherwise it will not work properly so we write cv2.weight key and we put one millisecond as the delay so this should open up our webcam so let's right click and run and there you go so you can see me and this is our webcam and you can see that it is working fine there is no lag no delay and what we are going to do is we are going to find my face and if the face is there then we will turn on the led if it's not there maybe i will block or i will do something like this and we will see what happens uh we want it to be closed when we want to turn off the light when the face is not detected so this is the basic idea so now we need to find the face so to do that what we will do is we will import the face detection module so we will write here from cv zone dot face detection module not face mesh face detection module import face detector so using this face detector we are going to find our faces and we will write here detector is equals to face phase detector so that is our face detector now to run this we need the media pipe package so we will make sure that our media pipe package is installed so we are going to write uh we are going to go to file settings and then we will go to the python interpreter and here you can see the media pipe package is not installed so what we will do is we will click on add and here we will write media pipe so this is the media pi package and we are using version 0.8.7.3 now you can use the latest version whatever you have uh if it doesn't work you can go back to this version a specific version so you can use it from here you can click on specify version and then you can select which version you want to use so we are going to install this package now if you are doing this for the very first time it might take a while so be patient once it's done we will continue with the code so now that the package is installed we will close this and click ok and now we can move ahead to our coding so we already initialized our face detector now what we need to do is we need to use this face detector to find the faces so we will say detector so this is this is the class and this is the object created from this class so we will write here detector dot find faces and then we will write image so we want to find the faces within this image that we have grabbed from our webcam image or webcam frame and what will it return it will return back the image with the bounding box drawn and it will also give us the information of the bounding boxes so if there are more than one face there are multiple faces then it will be stored over here so let's run that and see if it works there you go so if i move around you can see the face is detected if i tilt as well you will see it still works and this is very good because earlier methods like hard cascades they don't really work when you tilt the face and there's a limit to that so only the frontal faces are detected but here you can see when we move on the side as well it is detected and it is tracking as well so this is the basic idea of face detection now if you want to play around with the parameters of this phase detector then you can do it here so there is minimum confidence detection confidence means uh how much confidence you need to detect as a phase so by default it is 0.5 uh one will be 100 so if it's 100 sure it's a phase then it will detect it as a phase so we usually keep at keep it at 0.5 if you want it to be a little bit uh tough then you can write 0.8 for example to make it a little bit harder but if you are unable to find the faces you can reduce the value let's say 0.3 but then you might get false detection as well it might find the chair as the face um as well so there is a trade-off you have to work around with that so this is the basic idea of how you can find faces now we are going to add our led code to it so that whenever we detect the face the led turns on for the arduino code we are going to use the led code that we wrote in the output chapter so here it is very simple we are including our cv zone package we are initializing our serial data communication we are saying that we are going to receive one value and each value will have one digit so one value with one digit so it can be zero one two three four five six seven eight so this is the idea and then we have to initialize serial data dot begin here the baud rate will be 9600 by default and then we have the pin mode 13 as output because this is the one that we are using we are not connecting an external led we are using the internal led connected at pin number 13 which already has a resistor attached to it so that's why we are using that then we are saying get the data from our cdl communication get it from the python script and whatever value you get send it to digital rights pin number 13. so if it's on make it on if it's off turn it off so that's the basic idea now we are going to upload this so we will go to tools port and com9 and we are going to hit upload so the code is now uploaded we can go to the serial monitor and here we can write dollar and one and you can see the light turns on and if we write dollar and zero you can see the light turns off so this is the basic idea now we can go to our python script and based on that we are going to turn it on and off so here in pycharm we are going to go to our project folder we will right click and we will create a new file and we will call it face detection detection led so we will copy the face detection basics code and we are going to paste it and then we are going to import our serial module so that we can initialize our arduino code so we can write here from cv zone dot serial module we are going to import serial object this is the idea and then we are going to write that our arduino is equals to serial object so it will find automatically which com port it is and it will connect it to arduino then once the face is detected we need to do something we need to turn on our led so here first of all we need to check whether something is detected or not so we will write if bounding boxes this means that if there is something inside the bounding box then we are going to write arduino dot send data and we will send the data always in a list so we will create a list and then we will send the value of one and else if nothing is detected in the bounding box it's empty then we are going to write arduino dot send data and again in the list we are going to send the value of zero so if the face is detected it will send one and it will turn on the led if the face is not detected then it will send a zero and the led will turn off so let's go ahead and run this and see if it works so there you go now the face is detected and you can see the led is turned on now if i cover the image uh the camera now that led is off and now it turns off turns on off and on off off and on so it's very subtle so that's why what we are going to do is we are going to attach another led light the one that you are uh looking at on the side this red led is basically rgb and based on that we will turn on red or green light so that will be a better indication of this light so let's go ahead and do that so the first thing we will do is to connect our led light which is rgb now you can see that here it has three three negative pins for ground and then one voltage pin so the negative ones are rgb pins so if we give it zero then it will turn on if we give it one which is high then it will turn off so it is a little bit uh opposite to what we are used to so it is rgb and whenever we want the red color to turn on we will turn it off at our pin connection so it's a little bit weird but we can get used to of it now to connect this we are simply going to use our jumper cables we are going to connect them directly here there you go and then these pins we are going to connect directly to our arduino board so we will first connect the voltage pin which is the green pin the last pin here so we will connect it to 5 volts and the rest of the three pins they are going to go to our signal pins so we can pick any of these signal pins we will connect them to pin number eight nine and ten so these are the pins that we are using so this is basically our connection let me focus that here you can see this is our connection and now all we have to do is we have to plug it in and run it now for the rgb code it is actually fairly simple it might seem complicated but it is quite simple all we have to do is we have to initialize our pin numbers so we can write here integer red is equals to is eight then integer blue is nine and then integer green is 10. so it's not r gb it's rbg so the way they have connected the way the pins are laid out so we will define these numbers and then we will put all of them as outputs so we will write here pin mode our red led is basically output and then we will copy this part and we will say blue and green as well they are outputs so this is the idea and now all we have to do is we have to send them the signal so it is up to us what we want to send them now let's say i want to turn on all of them and make it white so if you have all three colors it will become white so we can write here digital rights and we will send the red as 0 and we will copy that paste it and paste it and here we are going to write blue and then green so if all of them are zero it should turn on and what we will do is we will add a delay so that we can see the difference and then we will turn all of them on so sorry we'll turn them off so this is on and this is off zero means on and one means off so let me write here turn on and this one is turn off so let's go ahead and run this so we will right click here and create a new folder we are going to call it projects and inside that we are going to write here project one project one and inside that we are going to save it as rgb led basic so this is the basic code we are not adding the serial part yet so let's go ahead and see if it works so right now it just turns on it never turns out oh i forgot to add a delay that's why so you need to add a delay at the bottom as well and there you go so now it's turning on and turning off now let's try different colors so if i wanted i could add let's say the red color so to add the red color these two blue should be off green should be off and red should be on so now it will go on zero and then red there you go so there you go now you have red white off red white off so that's the basic idea and what we can do is if we wanted to add more color we can for example write here blue so we will type blue and for blue we are going to put this as a one and put this as zero so that will turn on let's just auto format this to fix it and let's run that and now you will see that you have the blue color as well so red blue white so it's a little bit tough to see on the camera but in real life it's very easy to see and you can see what is happening and what we can do is we can also add a diffuser on top of this so something like this so now the color difference is very visible and you can tell which color it is so red blue white and nothing so let's do one thing we are going to add the green as well so to test three colors so we will write here green and we will put this as one and we'll put this as zero so let's run this and see what happens whites nothing red blue and green there you go so now we are able to see all the colors and it looks quite good so now we are going to write the code so that we can receive the values from our python code and we can turn on different colors of our led light so first we are going to include our cv zone package so we will write cv zone dot h and then we are going to initialize our objects which will be serial data and we will call it serial data let's put the d capital data and then we are going to receive three values r g and b and each one of them will be a single digit so either it will be on or off so because we are not going to use all the colors we are just using green blue and red okay so then we are going to write that our values received will be an array of three values we we'll write it like this and then we are going to write here that our serial data dot again and at the bottom we are going to receive the values so we will write here serial data data dot gets and we are going to write val's received so we are going to receive all the values and then we need to send them now how do we do that we will copy our code for the led rgb light we will paste it here then for the setup we will copy and paste it here and then we are going to copy one of these and instead of zero zero zero we are going to write values received at zero then value received at one then values received at two so should we do rgb or vgr so rgb let's do rgb so the first value will be going here zero and then the second value which is green will be going here and then the third value will be going here so it will be r g and b so that's basically our idea and yeah that's pretty much it so we can run this and we are going to save it we are going to save this as rgb led so now we can open up the serial monitor to test it and we can write dollar and if we write one one one it should turn off there we go and if we write dollar zero one one it should turn red there we go it turns red then dollar then we are going to write dollar and let's say zero let's say one zero one and that will turn it to green so second one is green then dollar uh we will write 1 1 0 that will turn it to blue so that is basically the idea now let's go ahead and we are going to run it from our python code so here in our pyjama project we are going to go to our project we will right click and we will create a new file and we are going to call it face detection rgb now this is not very different from our face detection led so we are going to copy that code and paste it here now the biggest difference here is that once the face is detected we are not sending one value instead we will sending three values so here it is uh r g and b so i can write here uh it will be r g and b so we wanted to turn on the r the red uh no this is when it is detected so we want it to be green so we will write here one zero and one so this will turn on so let's write here zero is on one is off so this is basically the idea let's write it like that okay so if we want to turn it uh what other color we have we wanted to turn it to red when it's not there then we will put it as zero and we'll put this as one so now it will turn red and green based on whether the face is detected or not so let's go ahead and try it out so now you can see my face is detected so it is green color and if i put my hand in front you will see it turns white why is it turning white it should be red so it is did i run okay i ran the wrong code we need to run face detection rgb i ran the previous one okay so now it is green and if i put my hand it turns red if it's not detecting then again you can see it becomes green when it finds a face and let me put something let's put the macbook if i put the macbook in front it becomes red now the face is detected i can move around it will still detect if i put the macbook again it turns red so this is basically the idea of how you can create a simple project using opencv and arduino and you can communicate this data between each other very easily using the cv zone package and you can detect phase using the media pi package and we have a wrapper for that within the cv zone package so it is literally 20 lines of code and we are able to create this project you
Info
Channel: Murtaza's Workshop - Robotics and AI
Views: 2,131,219
Rating: undefined out of 5
Keywords: arduino, arduino opencv, opencv, arduino computer vision, computer vision, computer vision arduino opencv, opencv practical projects, cvzone, murtazas workshop, computer vision zone
Id: mfiRJ1qgToc
Channel Id: undefined
Length: 125min 6sec (7506 seconds)
Published: Sun Sep 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.