-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoculus_v4.py
More file actions
151 lines (117 loc) · 5.22 KB
/
Copy pathoculus_v4.py
File metadata and controls
151 lines (117 loc) · 5.22 KB
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
import cv2
import mediapipe as mp
#import threading
import numpy as np
mp_hands=mp.solutions.hands
mp_draw=mp.solutions.drawing_utils
cap= cv2.VideoCapture(0)
running = True
"""
def listen_stop():
global running
while running:
user_input=input()
if user_input.lower() == "stop" or user_input.lower() == "exit" or user_input.lower() == "quit":
runnning = False
threading.Thread(target = listen_stop , daemon=True).start()
"""
def fin_deg(p1,p2,p3):
p1 = np.array([p1.x, p1.y, p1.z])
p2 = np.array([p2.x, p2.y, p2.z])
p3 = np.array([p3.x, p3.y, p3.z])
t1= p1-p2
t2= p3-p2
t1_u = t1/np.linalg.norm(t1)
t2_u = t2/np.linalg.norm(t2)
dot_t= np.clip(np.dot(t1_u, t2_u), -1.0, 1.0)
t_rad= np.arccos(dot_t)
t_deg= np.degrees(t_rad)
return t_deg
def finger_up(fin,mcp,pip,dip,tip):
#bhooliyo mat, yaad krle naam
#mcp= metacarpophalanges
#pip= proximal-interphalanges
#dip= diatal-interphalanges
#tip= finger tip
pip_angle= fin_deg(fin.landmark[mcp],fin.landmark[pip],fin.landmark[dip])
#tip_angle= fin_deg(fin.landmark[pip],fin.landmark[dip],fin.landmark[tip])
#print(f"PIP Angle: {pip_angle:.2f}, TIP Angle: {tip_angle:.2f}")
if pip_angle>140: #and tip_angle>165:
return 1
return 0
def detect_gesture(fingers):
s=sum(fingers)
if fingers == [0,0,0,0,0]:
return "FIST / ZERO-0"
elif fingers == [1,1,1,1,1]:
return "PALM / FIVE-5"
elif fingers == [0,1,1,0,0]:
return "PEACE / TWO-2"
elif fingers == [1,0,0,0,0]:
return "THUMBS UP / OKAY "
elif fingers == [0,0,0,0,1]:
return "EXCUSE ME!"
elif fingers == [1,0,0,0,1]:
return "CALL!"
elif fingers == [1,1,0,0,0]:
return "DIRECT / SHOOT"
elif fingers == [1,1,0,0,1] or fingers == [0,1,0,0,1]:
return "SPIDER-MAN / YO!"
elif fingers == [0,1,0,0,0]:
return "ONE-1"
elif fingers == [0,1,1,1,0]:
return "THREE-3"
elif fingers == [0,1,1,1,1]:
return "FOUR-4"
else:
return f"UNKNOWN GESTURE | {s} FINGERS UP"
print("\n----------------------------------------------------------------\nModel Started, Press 'q' to stop!\n----------------------------------------------------------------\n")
with mp_hands.Hands(static_image_mode=False,
max_num_hands=2,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while running:
success, img = cap.read()
if not success:
print("Camera not detected, jaake theek kar!")
break
img = cv2.flip(img,1)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(img_rgb)
if result.multi_hand_landmarks:
for i, hand_landmarks in enumerate(result.multi_hand_landmarks):
world_landmarks = result.multi_hand_world_landmarks[i]
fingers = [finger_up(world_landmarks,2,3,4,1),
finger_up(world_landmarks,5,6,7,8),
finger_up(world_landmarks,9,10,11,12),
finger_up(world_landmarks,13,14,15,16),
finger_up(world_landmarks,17,18,19,20)]
gesture = detect_gesture(fingers)
cv2.putText(img, gesture, (50, 80 + i*30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (8,143,143), 2)
mp_draw.draw_landmarks(img, hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_draw.DrawingSpec(color=(0,0,255),thickness=2, circle_radius=1),
mp_draw.DrawingSpec(color=(0,255,0), thickness=2))
cv2.imshow("Alex's Oculus Model 4", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
running = False
cap.release()
cv2.destroyAllWindows()
"""
if result.multi_hand_landmarks:
for hand_landmarks,handedness in zip(result.multi_hand_landmarks, result.multi_handedness):
if result.multi_hand_world_landmarks:
for world_landmarks in result.multi_hand_world_landmarks:
#fingers = [thumb,index,middle,ring,little]
fingers = [finger_up(world_landmarks,1,2,3,4),
finger_up(world_landmarks,5,6,7,8),
finger_up(world_landmarks,9,10,11,12),
finger_up(world_landmarks,13,14,15,16),
finger_up(world_landmarks,17,18,19,20)]
gesture= detect_gesture(fingers)
cv2.putText(img, gesture, (50,80), cv2.FONT_HERSHEY_SIMPLEX, 1, (8,143,143),2)
mp_draw.draw_landmarks(img, hand_landmarks, mp_hands.HAND_CONNECTIONS,
mp_draw.DrawingSpec(color=(0,0,255),thickness=2, circle_radius=1 ),
mp_draw.DrawingSpec(color=(0,255,0), thickness=2))
"""