-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (82 loc) · 4.46 KB
/
main.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
import cv2
import mediapipe as mp
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_draw = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0)
finger_tips = [8, 12, 16, 20]
thumb_tip = 4
# like_img = cv2.imread("images/like.png")
# like_img = cv2.resize(like_img, (200, 180))
#
# dislike_img = cv2.imread("images/dislike.png")
# dislike_img = cv2.resize(dislike_img, (200, 180))
while True:
ret, img = cap.read()
img = cv2.flip(img, 1)
h, w, c = img.shape
results = hands.process(img)
if results.multi_hand_landmarks:
for hand_landmark in results.multi_hand_landmarks:
lm_list = []
for id, lm in enumerate(hand_landmark.landmark):
lm_list.append(lm)
finger_fold_status = []
for tip in finger_tips:
x, y = int(lm_list[tip].x * w), int(lm_list[tip].y * h)
# print(id, ":", x, y)
#cv2.circle(img, (x, y), 15, (255, 0, 0), cv2.FILLED)
if lm_list[tip].x < lm_list[tip - 2].x:
#cv2.circle(img, (x, y), 15, (0, 255, 0), cv2.FILLED)
finger_fold_status.append(True)
else:
finger_fold_status.append(False)
print(finger_fold_status)
x, y = int(lm_list[8].x * w), int(lm_list[8].y * h)
print(x, y)
# stop
if lm_list[4].y < lm_list[2].y and lm_list[8].y < lm_list[6].y and lm_list[12].y < lm_list[10].y and \
lm_list[16].y < lm_list[14].y and lm_list[20].y < lm_list[18].y and lm_list[17].x < lm_list[0].x < \
lm_list[5].x:
cv2.putText(img, "STOP", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("STOP")
# Forward
if lm_list[3].x > lm_list[4].x and lm_list[8].y < lm_list[6].y and lm_list[12].y > lm_list[10].y and \
lm_list[16].y > lm_list[14].y and lm_list[20].y > lm_list[18].y:
cv2.putText(img, "FORWARD", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("FORWARD")
# Backward
if lm_list[3].x > lm_list[4].x and lm_list[3].y < lm_list[4].y and lm_list[8].y > lm_list[6].y and lm_list[12].y < lm_list[10].y and \
lm_list[16].y < lm_list[14].y and lm_list[20].y < lm_list[18].y:
cv2.putText(img, "BACKWARD", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("BACKWARD")
# Left
if lm_list[4].y < lm_list[2].y and lm_list[8].x < lm_list[6].x and lm_list[12].x > lm_list[10].x and \
lm_list[16].x > lm_list[14].x and lm_list[20].x > lm_list[18].x and lm_list[5].x < lm_list[0].x:
cv2.putText(img, "LEFT", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("LEFT")
# Right
if lm_list[4].y < lm_list[2].y and lm_list[8].x > lm_list[6].x and lm_list[12].x < lm_list[10].x and \
lm_list[16].x < lm_list[14].x and lm_list[20].x < lm_list[18].x:
cv2.putText(img, "RIGHT", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("RIGHT")
if all(finger_fold_status):
# like
if lm_list[thumb_tip].y < lm_list[thumb_tip - 1].y < lm_list[thumb_tip - 2].y and lm_list[0].x < lm_list[3].y:
print("LIKE")
cv2.putText(img, "LIKE", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
# h, w, c = like_img.shape
# img[35:h + 35, 30:w + 30] = like_img
# Dislike
if lm_list[thumb_tip].y > lm_list[thumb_tip - 1].y > lm_list[thumb_tip - 2].y and lm_list[0].x < lm_list[3].y:
cv2.putText(img, "DISLIKE", (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
print("DISLIKE")
# h, w, c = dislike_img.shape
# img[35:h + 35, 30:w + 30] = dislike_img
mp_draw.draw_landmarks(img, hand_landmark,
mp_hands.HAND_CONNECTIONS,
mp_draw.DrawingSpec((0, 0, 255), 6, 3),
mp_draw.DrawingSpec((0, 255, 0), 4, 2)
)
cv2.imshow("Hand Sign Detection", img)
cv2.waitKey(1)