muftawu
Published © GPL3+

Servo motor Control using Hand Gesture with Computer Vision

Rotate the servo by an angle when a hand gesture is made

IntermediateFull instructions provided4,102
Servo motor Control using Hand Gesture with Computer Vision

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
Pycharm IDE

Story

Read more

Schematics

Servo motor circuit connection

Servo pin attached to pinMode 9

Code

Arduino Code

C/C++
// we first import our libraries

#include "cvzone.h"    // this help us to communicate with python over the serial com port

#include "Servo.h"
Servo servo;                         // create our servo object
SerialData data(1,3);                // create our serial object
int value[2];                   //create an array to store our received values

void setup () {
  
  data.begin(9600);   // begin our serial communication
  servo.attach(9);*      // connect our servo pin
}

void loop () {
  data.Get(value);         // read the value from python and store it in value
  servo.write(value[0]);      // since our received value is an array, we only accept the first value since only one servo is attached
  
  delay(30);   // delay for 30 milli seconds

  // All done 
  // lets move to python 
}

Python code

Python
# for recording or capturing feed from web cam
import cv2  

#need for our hand detection
from cvzone.HandTrackingModule import HandDetector

from cvzone.SerialModule import SerialObject

# initialize our camera
cap = cv2.VideoCapture(0)

# set our hand tracking object
detector = HandDetector(detectionCon=0.8)
arduino = SerialObject("COM3")    # Select your own com port here


# start our while loop
while True:
    success, image = cap.read()
    hands, bboxInfo = detector.findHands(image)
    if len(hands)==1:
        print(detector.fingersUp(hands[0]))       
        if detector.fingersUp(hands[0]) == [0,1,0,0,0]:    
            arduino.sendData([180])               
        else:
            arduino.sendData([0])                        
    cv2.imshow('image',image)      
    cv2.waitKey(1)       

Credits

muftawu
6 projects • 6 followers

Comments