0% found this document useful (0 votes)
33 views2 pages

Hand Gesture Calculator with OpenCV

Uploaded by

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

Hand Gesture Calculator with OpenCV

Uploaded by

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

import cv2

import mediapipe as mp

mp_hands = [Link]
mp_draw = [Link].drawing_utils
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)

# Simple calculator function


def calculate(a, b, gesture):
if gesture == 1:
return a + b, "Addition"
elif gesture == 2:
return a - b, "Subtraction"
elif gesture == 3:
return a * b, "Multiplication"
elif gesture == 4:
if b != 0:
return a / b, "Division"
else:
return "Error", "Divide by Zero"
elif gesture == 5:
return 0, "Cleared"
return None, ""

# Take 2 Input Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
result = 0

cap = [Link](0)

while True:
success, frame = [Link]()
frame = [Link](frame, 1)
h, w, c = [Link]

rgb = [Link](frame, cv2.COLOR_BGR2RGB)


res = [Link](rgb)

gesture = 0

if res.multi_hand_landmarks:
for handLms in res.multi_hand_landmarks:
lmList = []
for id, lm in enumerate([Link]):
[Link]([id, int(lm.x*w), int(lm.y*h)])

mp_draw.draw_landmarks(frame, handLms, mp_hands.HAND_CONNECTIONS)

# Count Fingers
tip_ids = [4, 8, 12, 16, 20]
fingers = []

# Thumb
[Link](1 if lmList[tip_ids[0]][1] > lmList[tip_ids[0]-1][1]
else 0)

# 4 Fingers
for i in range(1, 5):
[Link](1 if lmList[tip_ids[i]][2] < lmList[tip_ids[i]-2][2]
else 0)

gesture = [Link](1)

# Calculate result
if gesture != 0:
result, operation = calculate(a, b, gesture)

[Link](frame, f"Gesture: {gesture} fingers", (10, 80),


cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)

[Link](frame, f"Result: {result}", (10, 40),


cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

[Link]("Calculator with Hand Gestures", frame)


if [Link](1) == ord('q'):
break

[Link]()
[Link]()

You might also like