-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrial.py
248 lines (207 loc) · 8.56 KB
/
trial.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""import cv2
import mediapipe as mp
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
#fingerCoordinates = [(8, 6), (12, 10), (16, 14), (20, 18)]
littlefin=(20,18)
ringfin=(16,14)
middlefin=(12,10)
indexfin=(8,6)
thumbCoordinate = (4,2)
while True:
success, img = cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
multiLandMarks = results.multi_hand_landmarks
sentance=""
if multiLandMarks:
handPoints = []
for handLms in multiLandMarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
for idx, lm in enumerate(handLms.landmark):
print(idx,lm)
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
handPoints.append((cx, cy))
for point in handPoints:
cv2.circle(img, point, 10, (0, 0, 255), cv2.FILLED)
thumb_tip = landmarks[4]
index_tip = landmarks[8]
middle_tip = landmarks[12]
ring_tip = landmarks[16]
pinky_tip = landmarks[20]
thumb_ip = landmarks[3]
index_ip = landmarks[7]
middle_ip = landmarks[11]
ring_ip = landmarks[15]
pinky_ip = landmarks[19]
thumb_mcp = landmarks[2] # MCP joint of the thumb
index_mcp = landmarks[5] # MCP joint of the index finger
#if not handPoints[thumbCoordinate[0]][0] > handPoints[thumbCoordinate[1]][0]:
# cv2.putText(img, "A", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
# Example logic for "A" (fist-like gesture)
if (thumb_tip.x < thumb_ip.x and
index_tip.y > index_ip.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
cv2.putText(img, "A", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "A"
# Example logic for "B" (all fingers extended straight)
if (index_tip.y < index_ip.y and
middle_tip.y < middle_ip.y and
ring_tip.y < ring_ip.y and
pinky_tip.y < pinky_ip.y):
cv2.putText(img, "B", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "B"
# Example logic for "C" (forming a "C" with thumb and index)
if (thumb_tip.x < index_tip.x and
0 < (thumb_tip.y - index_tip.y) < 0.2 and
middle_tip.y < landmarks[9].y and
ring_tip.y < landmarks[13].y and
pinky_tip.y < landmarks[17].y):
cv2.putText(img, "C", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "C"
# Example logic for "D" (index finger pointing up, others curled in)
if (index_tip.y < index_mcp.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y and
thumb_tip.x > thumb_mcp.x):
cv2.putText(img, "D", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "D"
# Example logic for "E" (fingers curled in, thumb across palm)
if (thumb_tip.x > thumb_mcp.x and
index_tip.y > index_ip.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
cv2.putText(img, "E", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "E"
# Example logic for "F" (thumb and index forming a circle, others straight)
if (thumb_tip.x > index_tip.x and
index_tip.y < index_ip.y and
middle_tip.y < middle_ip.y and
ring_tip.y < ring_ip.y and
pinky_tip.y < pinky_ip.y):
cv2.putText(img, "F", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "F"
# Example logic for "G" (thumb and index parallel, others closed)
if (thumb_tip.y > thumb_mcp.y and
index_tip.y < index_mcp.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
cv2.putText(img, "G", (150,150), cv2.FONT_HERSHEY_PLAIN, 12, (255,0,0), 12)
#return "G"
cv2.imshow("Finger Counter", img)
cv2.waitKey(1)"""
import cv2
import mediapipe as mp
# Initialize MediaPipe hands and drawing utilities
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# Open the webcam
cap = cv2.VideoCapture(0)
# Define the hand gesture mappings based on the landmarks
def detect_gesture(landmarks):
# Extract landmark positions
sentance=""
thumb_tip = landmarks[4]
index_tip = landmarks[8]
middle_tip = landmarks[12]
ring_tip = landmarks[16]
pinky_tip = landmarks[20]
thumb_ip = landmarks[3]
index_ip = landmarks[7]
middle_ip = landmarks[11]
ring_ip = landmarks[15]
pinky_ip = landmarks[19]
thumb_mcp = landmarks[2] # MCP joint of the thumb
index_mcp = landmarks[5] # MCP joint of the index finger
# Example logic for "A" (fist-like gesture)
if (thumb_tip.x < thumb_ip.x and
index_tip.y > index_ip.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
sentance+="A"
return "A"
# Example logic for "B" (all fingers extended straight)
if (index_tip.y < index_ip.y and
middle_tip.y < middle_ip.y and
ring_tip.y < ring_ip.y and
pinky_tip.y < pinky_ip.y):
sentance+="B"
return "B"
# Example logic for "C" (forming a "C" with thumb and index)
if (thumb_tip.x < index_tip.x and
0 < (thumb_tip.y - index_tip.y) < 0.2 and
middle_tip.y < landmarks[9].y and
ring_tip.y < landmarks[13].y and
pinky_tip.y < landmarks[17].y):
sentance+="C"
return "C"
# Example logic for "D" (index finger pointing up, others curled in)
if (index_tip.y < index_mcp.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y and
thumb_tip.x > thumb_mcp.x):
sentance+="D"
return "D"
# Example logic for "E" (fingers curled in, thumb across palm)
if (thumb_tip.x > thumb_mcp.x and
index_tip.y > index_ip.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
sentance+="E"
return "E"
# Example logic for "F" (thumb and index forming a circle, others straight)
if (thumb_tip.x > index_tip.x and
index_tip.y < index_ip.y and
middle_tip.y < middle_ip.y and
ring_tip.y < ring_ip.y and
pinky_tip.y < pinky_ip.y):
sentance+="F"
return "F"
# Example logic for "G" (thumb and index parallel, others closed)
if (thumb_tip.y > thumb_mcp.y and
index_tip.y < index_mcp.y and
middle_tip.y > middle_ip.y and
ring_tip.y > ring_ip.y and
pinky_tip.y > pinky_ip.y):
sentance+="G"
return "G"
print(sentance)
with mp_hands.Hands(min_detection_confidence=0.7, min_tracking_confidence=0.7) as hands:
while True:
ret, frame = cap.read()
if not ret:
break
# Convert the frame to RGB
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Process the frame and detect hands
result = hands.process(rgb_frame)
if result.multi_hand_landmarks:
for hand_landmarks in result.multi_hand_landmarks:
# Draw landmarks on the frame
mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Get landmarks as a list of normalized coordinates
landmarks = hand_landmarks.landmark
# Detect the gesture based on landmarks
detected_gesture = detect_gesture(landmarks)
# Display the corresponding letter
if detected_gesture:
cv2.putText(frame, detected_gesture, (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
# Show the frame
cv2.imshow('Hand Gesture Recognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
print(sentance)
cap.release()
cv2.destroyAllWindows()