-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalt.py
135 lines (125 loc) · 4.32 KB
/
alt.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
import cv2
import mediapipe as mp
import numpy as np
import time
cap = cv2.VideoCapture(0)
locked = False
pw = []
def getInput(num):
num = int(num)
inputPw = []
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
# For webcam input:
for i in range(num) :
print("Prepare Gesture", (i + 1))
# time.sleep(1)
count = 0
anotherList = []
with mp_hands.Hands(model_complexity=0, min_detection_confidence=0.5, min_tracking_confidence=0.5, max_num_hands = 1) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.multi_hand_landmarks:
for hand_no, hand_landmarks in enumerate(results.multi_hand_landmarks):
mp_drawing.draw_landmarks(
image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
list = []
for i in range(21):
temp = []
temp.append(hand_no)
temp.append(i)
temp.append(int(hand_landmarks.landmark[mp_hands.HandLandmark(i).value].x * cap.get(3)))
temp.append(int(hand_landmarks.landmark[mp_hands.HandLandmark(i).value].y * cap.get(4)))
temp.append(int(hand_landmarks.landmark[mp_hands.HandLandmark(i).value].z * -100))
list.append(temp)
#todo: create average of palm points to accurately depict thumb distance
#identify gesture
s = ""
for i in range(5) :
averagePalmX = (list[0][2] + list[5][2] + list[13][2] + list[17][2]) / 4
averagePalmY = (list[0][3] + list[5][3] + list[13][3] + list[17][3]) / 4
dist1 = np.hypot(list[(i + 1) * 4][2] - averagePalmX, list[((i + 1) * 4)][3] - averagePalmY)
dist2 = np.hypot(list[(i + 1) * 4 - 2][2] - averagePalmX, list[((i + 1) * 4 - 2)][3] - averagePalmY)
dist3 = np.hypot(list[(i + 1) * 4 - 1][2] - averagePalmX, list[((i + 1) * 4 - 1)][3] - averagePalmY)
dist4 = np.hypot(list[(i + 1) * 4 - 3][2] - averagePalmX, list[((i + 1) * 4 - 3)][3] - averagePalmY)
if dist1 < dist2 or dist1 < dist3 or dist1 < dist4:
s = s + "0"
else :
s = s + "1"
dec_number = int(s[::-1], 2)
anotherList.append(dec_number)
else :
anotherList.append(-1)
# Flip the image horizontally for a selfie-view display.
cv2.imshow('Gesture Lock', cv2.flip(image, 1))
if count >= 70:
mode = max(set(anotherList), key = anotherList.count)
inputPw.append(mode)
print(mode)
break
count += 1
if cv2.waitKey(5) == ord('q'):
quit()
print(inputPw)
return inputPw
def checkPw() :
global locked, pw
print("pw is now", pw)
if locked :
if pw == getInput(len(pw)) :
locked = False
print("unlocked")
else :
print("wrong pw")
else :
print("already unlocked")
def setPw() :
global locked, pw
if locked == False :
print("Enter length of new password")
n = getInput(1)[0]
if not n == 0 :
pw = getInput(n)
print("Password is now", pw)
lock()
print("set and locked")
else :
print("cannot set pw")
def lock() :
global locked
if locked :
print("already locked")
else :
locked = True
print("locked")
print("PRESS Q IN THE CAMERA WINDOW TO QUIT PROGRAM")
setPw()
while True :
x = getInput(1)[0]
if x == 0 :
print("lockFunction")
lock()
elif x == 31 :
print("checkPwFunction")
checkPw()
elif x == 1 :
print("setPwFunction")
setPw()
# cap.release()