0% found this document useful (0 votes)
38 views4 pages

Hand Gesture Control with Arduino

Uploaded by

B BABY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Hand Gesture Control with Arduino

Uploaded by

B BABY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import cv2

import mediapipe as mp
from pyfirmata import Arduino
from time import sleep

board = Arduino('Select your COM port')


#Arduino define pins
digitalPins =[7, 6, 5, 4]

for pins in digitalPins:


[Link][pins].write(1)

wCam, hCam = 640, 480


cap = [Link](0)

[Link](3, wCam)
[Link](4, hCam)

[Link](10, 250)#brightness
[Link](11, 50)#Contrast
[Link](12, 100)#saturation

class handDetector():
def __init__(self, mode = False, maxHands = 2,
modelComplexity=1,detectionCon=0.75, trackCon = 0.5):
[Link] = mode
[Link] = maxHands
[Link] = modelComplexity
[Link] = detectionCon
[Link] = trackCon

[Link] = [Link]
[Link] = [Link]([Link], [Link],
[Link],

[Link], [Link])
[Link] = [Link].drawing_utils

def findHands(self, img, draw = True):


imgRGB = [Link](img, cv2.COLOR_BGR2RGB)
[Link] = [Link](imgRGB)

if [Link].multi_hand_landmarks:
for handLms in [Link].multi_hand_landmarks:
if draw:
[Link].draw_landmarks(img, handLms,
[Link].HAND_CONNECTIONS)
return img

def findPosition(self, img, handNo = 0, draw = True):


lmlist = []
if [Link].multi_hand_landmarks:
myHand = [Link].multi_hand_landmarks[handNo]
for id, lm in enumerate([Link]):
h, w, c = [Link]
cx, cy = int(lm.x * w), int(lm.y * h)
[Link]([id, cx, cy])
if draw:
[Link](img, (cx, cy), 3, (178, 102, 255),
[Link])
return lmlist

counter, counter1, counter2, counter3 = 0,0,0,0


flag, flag1, flag2, flag3 = 0,0,0,0

detector = handDetector()
while True:
success, img = [Link]()
img = [Link](img)
lmList = [Link](img, draw=False)
y1, y2 = 15, 80
grey_color = (211,219,218)
if lmList:

tip8_x, tip8_y= lmList[8][1], lmList[8][2] # tracking tip of


index finger
#button 1
if tip8_x > 25 and tip8_x < 65 and tip8_y > 28 and tip8_y < 62:
counter += 1
[Link](img, (15,y1),(85,y2), (255, 255, 0),
[Link])
if counter == 1:
flag = not flag
else :
counter = 0
if flag:
[Link][digitalPins[0]].write(0)

[Link](img,(15,y1),(85,y2),(76,138,246),[Link])
[Link](img, "BULB1", (25, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)
else:
[Link][digitalPins[0]].write(1)
[Link](img,(15,y1),(85,y2), grey_color,[Link])
[Link](img, "BULB1", (25, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)

#button 2
if tip8_x > 115 and tip8_x < 155 and tip8_y > 28 and tip8_y < 62:
counter1 += 1
[Link](img, (175,y1),(105,y2), (255, 255, 0),
[Link])
if counter1 == 1:
flag1 = not flag1
else :
counter1 = 0
if flag1:
[Link][digitalPins[1]].write(0)

[Link](img,(175,y1),(105,y2),(77,198,254),[Link])
[Link](img, "BULB2", (115, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)
else:
[Link][digitalPins[1]].write(1)
[Link](img,(175,y1),(105,y2),
grey_color,[Link])
[Link](img, "BULB2", (115, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)

#button 3
if tip8_x > 200 and tip8_x < 245 and tip8_y > 28 and tip8_y < 62:
counter2 += 1
[Link](img, (265,y1),(195,y2),(255, 255, 0),
[Link])
if counter2 == 1:
flag2 = not flag2
else :
counter2 = 0
if flag2:
[Link][digitalPins[2]].write(0)

[Link](img,(265,y1),(195,y2),(135,186,118),[Link])
[Link](img, "BULB3", (205, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)
else:
[Link][digitalPins[2]].write(1)
[Link](img,(265,y1),(195,y2),
grey_color,[Link])
[Link](img, "BULB3", (205, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)

#button 4
if tip8_x > 295 and tip8_x < 335 and tip8_y > 28 and tip8_y < 62:
counter3 += 1
[Link](img, (355,y1),(285,y2),(255, 255, 0),
[Link])
if counter3 == 1:
flag3 = not flag3
else :
counter3 = 0
if flag3:
[Link][digitalPins[3]].write(0)

[Link](img,(355,y1),(285,y2),(127,129,29),[Link])
[Link](img, "BULB4", (295, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)
else:
[Link][digitalPins[3]].write(1)
[Link](img,(355,y1),(285,y2),
grey_color,[Link])
[Link](img, "BULB4", (295, 50),
cv2.FONT_HERSHEY_PLAIN,1, (255, 255, 255), 2)

# Exit button
[Link](img,(600,y1),(530,y2),(102,102,255),[Link])
[Link](img, "EXIT", (550, 50), cv2.FONT_HERSHEY_PLAIN,1,
(255, 255, 255), 2)
#EXIT by index finger
[Link](img, (tip8_x, tip8_y), 10, (255, 255, 51), [Link])
if tip8_x > 550 and tip8_x < 590 and tip8_y > 28 and tip8_y < 62:
[Link]()
[Link]()
break

[Link]("Image", img)
[Link](1)

You might also like