You are currently viewing Gesture Controlled LED Using ML

Gesture Controlled LED Using ML

Table of Contents

[1] Description

[2] Component & Software

[3] Python Code & working

[4] Arduino Code & working

[5] Output Video

[6] YouTube Video


[1] Description

In this I’m going to make a Gesture controlled LED. For this will require software and some hardware components and here I’m using C and python because Arduino code is written in C and the sending code that is which is communicating with Arduino is written in Python so let’s move further and make this project, below are the list of component and software which you will required in this project.


[2] Component & Software

Components

[1] Arduino

[2] LED & Jumper Wires

[4] USB connector

Software

[1] Arduino IDE


[3] Python Code & working

Code :-

import cv2
import cvzone
from cvzone.HandTrackingModule import HandDetector
import serial

detector = HandDetector(detectionCon=0.8)
cap=cv2.VideoCapture(1) 

serialcomm = serial.Serial('COM5', 9600)
serialcomm.timeout = 1

while True:
    success,img =cap.read()
    img=cv2.resize(img,(550, 430))
    img = detector.findHands(img)
    l,box = detector.findPosition(img,draw=False)
   
    cv2.rectangle(img,(0,50),(170,100),(255,255,255),cv2.FILLED) 
    cv2.putText(img,"LED-ON",(30,85),cv2.FONT_HERSHEY_PLAIN,2,(0,0,0),3)

    cv2.rectangle(img,(370,50),(560,100),(255,255,255),cv2.FILLED) 
    cv2.putText(img,"LED-Off",(380,85),cv2.FONT_HERSHEY_PLAIN,2,(0,0,0),3)
    
    if l:
        if 20<l[8][0]<190:
             e='\n'
             serialcomm.write(e.encode())
             serialcomm.write(str(1).encode())
             cv2.rectangle(img,(20,50),(170,100),(0,255,0),cv2.FILLED)
             cv2.putText(img,"LED-ON",(30,85),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),3)  
            
        if 370<l[8][0]<540:
            e='\n'
            serialcomm.write(e.encode())
            serialcomm.write(str(0).encode())
            cv2.rectangle(img,(370,50),(530,100),(0,0,255),cv2.FILLED)
            cv2.putText(img,"LED-Off",(380,85),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),3)
            
    cv2.imshow("Image", img) 
    if cv2.waitKey(20) & 0xFF == 27:
        break                
cv2.destroyAllWindows()

Let understand how the above python code is working? here as you saw in the output I have designed two button on the screen and these two buttons are for responsible for On and Off the LED.

Before go through the code first we have to install some package/library because these are important one to run the code.

[1] pip install pyserial
[2] pip install opencv-python
[3] pip install cvzone

Install these library first one by one, for this you can use Jupyter-Notebook or CMD in your PC.

Below part of the code is for to make these buttons as you can see in above image. Here I used the rectangle function of opencv to built the rectangular box and to put the text on it I used putText function of opencv. You can find the below.

################ Button for led-On ###########################
cv2.rectangle(img,(0,50),(170,100),(255,255,255),cv2.FILLED) 
cv2.putText(img,"LED-ON",(30,85),cv2.FONT_HERSHEY_PLAIN,2,(0,0,0),3)
################ Button for led-off ###########################
cv2.rectangle(img,(370,50),(560,100),(255,255,255),cv2.FILLED) 
cv2.putText(img,"LED-Off",(380,85),cv2.FONT_HERSHEY_PLAIN,2,(0,0,0),3)

Now let’s understand how it is detecting the gesture? Below code is design to detect the left and right area of buttons. Here first I am checking for the List of fingers/hand is available or not? if it is available then it is checking for area in which button is lying or not , if it is the same area then it will send the command to Arduino .

If hand is in the area of LED-On button then it will send the str(1) to Arduino and if incase of LED-off it will send str(0) to Arduino. See the below code to understand more clearly.

if l:
        if 20<l[8][0]<190:
             e='\n'
             serialcomm.write(e.encode())
             serialcomm.write(str(1).encode())
             cv2.rectangle(img,(20,50),(170,100),(0,255,0),cv2.FILLED)
             cv2.putText(img,"LED-ON",(30,85),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),3)  
        if 370<l[8][0]<540:
            e='\n'
            serialcomm.write(e.encode())
            serialcomm.write(str(0).encode())
            cv2.rectangle(img,(370,50),(530,100),(0,0,255),cv2.FILLED)
            cv2.putText(img,"LED-Off",(380,85),cv2.FONT_HERSHEY_PLAIN,2,(255,255,255),3)

[4] Arduino Code & working

Let’s under stand how Arduino communication is done in this project? let’s understand the below Arduino code.

Complete Code:-

const int led = 2;
String arrivingdatabyte;  
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
if(Serial.available( ) > 0) 
 {  
    arrivingdatabyte = Serial.readStringUntil( '\n');
    
    if(arrivingdatabyte=="1")
     { 
       digitalWrite(led,HIGH);     
     }
     else if(arrivingdatabyte=="0")
     {
      digitalWrite(led,LOW);  
     }
  }
}

Here below part of code is responsible for communication between Python code and Arduino code. As you can see in the code there is a, if condition that is -when something is available for receive then it will receive otherwise it will not receive any thing.

So when ‘arrivingdatabyte’ received ‘1’ from PC then it check the condition and will ON the LED and if ‘arrivingdatabyte’ received ‘0’ then it will turn Off the LED, as you can see in the below code.

if(Serial.available( ) > 0) 
 {  
    arrivingdatabyte = Serial.readStringUntil( '\n');
    
    if(arrivingdatabyte=="1")
     { 
       digitalWrite(led,HIGH);     
     }
     else if(arrivingdatabyte=="0")
     {
      digitalWrite(led,LOW);  
     }
  }

So now we have done with coding part . Let’s move toward the circuit design as you saw in the output there is a single Led so circuit is going to very simple let’s do the connection.

Circuit-Diagram :-

[5] Output Video :-

Thankyou so much to read the article till now there is YouTube video on this project so please go through that video to understand the whole procedure in a better manner there is link for video go and check it out.