0% found this document useful (0 votes)
12 views8 pages

Appendix A

The document provides supplementary material including circuit diagrams and Python code snippets for controlling LEDs, adjusting brightness, and implementing a virtual mouse using hand gestures. It includes detailed code for an Arduino setup and utilizes libraries like OpenCV and MediaPipe for hand detection. Additional notes emphasize the importance of proper setup and testing for optimal performance.

Uploaded by

sonadas0409
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)
12 views8 pages

Appendix A

The document provides supplementary material including circuit diagrams and Python code snippets for controlling LEDs, adjusting brightness, and implementing a virtual mouse using hand gestures. It includes detailed code for an Arduino setup and utilizes libraries like OpenCV and MediaPipe for hand detection. Additional notes emphasize the importance of proper setup and testing for optimal performance.

Uploaded by

sonadas0409
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
You are on page 1/ 8

Appendix A: Supplementary Material

A.1 Circuit Diagrams


 Figure A1.1: Arduino LED Circuit Diagram

 Figure A1.2: Full System Setup


A.2 Python Code Snippets:
A.2.1 Code for Led control .
 A.2.1.1. Controller File
import pyfirmata2
comport='COM3'
board=pyfirmata2.Arduino(comport)
led_1=board.get_pin('d:8:o')
led_2=board.get_pin('d:12:o')
led_3=board.get_pin('d:11:o')
led_4=board.get_pin('d:10:o')
led_5=board.get_pin('d:9:o')
def led(total):
if total==0:
led_1.write(0)
led_2.write(0)
led_3.write(0)
led_4.write(0)
led_5.write(0)
elif total==1:
led_1.write(1)
led_2.write(0)
led_3.write(0)
led_4.write(0)
led_5.write(0)
elif total==2:
led_1.write(1)
led_2.write(1)
led_3.write(0)
led_4.write(0)
led_5.write(0)
elif total==3:
led_1.write(1)
led_2.write(1)
led_3.write(1)
led_4.write(0)
led_5.write(0)
elif total==4:
led_1.write(1)
led_2.write(1)
led_3.write(1)
led_4.write(1)
led_5.write(0)
elif total==5:
led_1.write(1)
led_2.write(1)
led_3.write(1)
led_4.write(1)
led_5.write(1)

 A.2.1.2. Main File to run


import cv2
import mediapipe as mp
import time
import controller as cnt

time.sleep(2.0)

mp_draw = mp.solutions.drawing_utils
mp_hand = mp.solutions.hands

tipIds = [4, 8, 12, 16, 20]

video = cv2.VideoCapture(0)

with mp_hand.Hands(min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while True:
ret, image = video.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
lmList = []
if results.multi_hand_landmarks:
for hand_landmark in results.multi_hand_landmarks:
myHands = results.multi_hand_landmarks[0]
for id, lm in enumerate(myHands.landmark):
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmList.append([id, cx, cy])
mp_draw.draw_landmarks(image, hand_landmark,
mp_hand.HAND_CONNECTIONS)
fingers = []
if len(lmList) != 0:
if lmList[tipIds[0]][1] > lmList[tipIds[0] - 1][1]:
fingers.append(1)
else:
fingers.append(0)
for id in range(1, 5):
if lmList[tipIds[id]][2] < lmList[tipIds[id] - 2][2]:
fingers.append(1)
else:
fingers.append(0)
total = fingers.count(1)
cnt.led(total)
if total == 0:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "0", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
elif total == 1:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "1", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
elif total == 2:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "2", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
elif total == 3:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "3", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
elif total == 4:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "4", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
elif total == 5:
cv2.rectangle(image, (20, 300), (270, 425), (0, 255, 0), cv2.FILLED)
cv2.putText(image, "5", (45, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.putText(image, "LED", (100, 375), cv2.FONT_HERSHEY_SIMPLEX,
2, (255, 0, 0), 5)
cv2.imshow("Frame", image)
k = cv2.waitKey(1)
if k == ord('q'):
break
video.release()
cv2.destroyAllWindows()

A.2.2 LED brightness Control


 A.2.2.1 Main file to run
import cv2
import mediapipe as mp
import pyfirmata2
import math
board=pyfirmata2.Arduino("COM3")
pin9=board.get_pin("d:9:p")

cap=cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,600)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,500)
mp_drawing=mp.solutions.drawing_utils
mp_hands=mp.solutions.hands
hand=mp_hands.Hands()

while True:

success,frame=cap.read()
if success:
rgb_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
results=hand.process(rgb_frame)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
print(hand_landmarks)
mp_drawing.draw_landmarks(frame,hand_landmarks,mp_hands.HAND_CONNECTIONS)
thumb = hand_landmarks.landmark[4]
index = hand_landmarks.landmark[8]
distance = math.sqrt((thumb.x - index.x) ** 2 + (thumb.y - index.y) ** 2)

pin9.write(distance)

cv2.imshow('capture',frame)
if cv2.waitKey(1)==ord('q'):
break

cv2.destroyAllWindows()

A.2.3 Virtual Mouse Code


A.2.3.1 Main File to run:
import cv2
import mediapipe as mp
import pyautogui

cap=cv2.VideoCapture(0)
hand_detector=mp.solutions.hands.Hands()
drawing=mp.solutions.drawing_utils
swidth,sheight=pyautogui.size()
index_y=0
while True:
_,frame=cap.read()
frame=cv2.flip(frame,1)
height,width,_=frame.shape
rgb_frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
output=hand_detector.process(rgb_frame)
hands=output.multi_hand_landmarks
if hands:
for i in hands:
drawing.draw_landmarks(frame,i)
landmarks=i.landmark
for j,landmarks in enumerate(landmarks):
x=int(landmarks.x*width)
y=int(landmarks.y*height)
print(x,y)
if j==8:
cv2.circle(img=frame,center=(x,y),radius=15,color=(0,255,255))
index_x=swidth/width*x
index_y=sheight/height*y
pyautogui.moveTo(index_x,index_y)
if j==4:
cv2.circle(img=frame,center=(x,y),radius=15,color=(0,255,255))
th_x=swidth/width*x
th_y=sheight/height*y
print('outside',abs(index_y-th_y))
if abs(index_y-th_y)<40:
pyautogui.click()
pyautogui.sleep(1)

cv2.imshow('Virtual Mouse',frame)
cv2.waitKey(1)
A.3 Additional Notes:
 Ensure StandardFirmata is uploaded to the Arduino via the IDE.
 Adjust max_distance based on your hand size and camera setup.
 Optimal lighting is crucial for gesture accuracy.
 Test system responsiveness under varied conditions for reliability evaluation.
 Run the files separately under one Environment
 Preferably use PyCharm by JetBrains as IDE (Integrated Development Environment)
for running the python programme.

You might also like