Appendix A
Appendix A
time.sleep(2.0)
mp_draw = mp.solutions.drawing_utils
mp_hand = mp.solutions.hands
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()
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()
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.