-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinger_counter.py
73 lines (58 loc) · 3.11 KB
/
finger_counter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
# For webcam input:
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
model_complexity=0,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Unable to capture a frame from the camera.")
continue
image = cv2.flip(image, 1)
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
fingerCount = 0
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
handIndex = results.multi_hand_landmarks.index(hand_landmarks)
handLabel = results.multi_handedness[handIndex].classification[0].label
handLandmarks = []
for landmarks in hand_landmarks.landmark:
handLandmarks.append([landmarks.x, landmarks.y])
if handLabel == "Left" and handLandmarks[4][0] > handLandmarks[3][0]: # Left Thumb
fingerCount += 1
elif handLabel == "Right" and handLandmarks[4][0] < handLandmarks[3][0]: # Right Thumb
fingerCount += 1
if handLandmarks[8][1] < handLandmarks[6][1]: # Left & Right Index finger
fingerCount += 1
if handLandmarks[12][1] < handLandmarks[10][1]: # Left & Right Middle finger
fingerCount += 1
if handLandmarks[16][1] < handLandmarks[14][1]: # Left & Right Ring finger
fingerCount += 1
if handLandmarks[20][1] < handLandmarks[18][1]: # Left & Right Pinky
fingerCount += 1
# Set the color to red and decrease the dot size
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2),
connection_drawing_spec=mp_drawing.DrawingSpec(color=(255, 255, 255), thickness=2))
height, width, _ = image.shape
text_position_label = ((width - cv2.getTextSize("No. of raised fingers:", cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)[0][0]) // 2, 40)
text_position_count = ((width - cv2.getTextSize(str(fingerCount), cv2.FONT_HERSHEY_SIMPLEX, 1.5, 4)[0][0]) // 2, 95)
cv2.putText(image, "No. of raised fingers:", text_position_label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.putText(image, str(fingerCount), text_position_count, cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 4)
cv2.imshow("Raised Finger Counter", image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()