Commit
·
0119ff3
1
Parent(s):
9386e65
Create Handgesture.py
Browse files- Handgesture.py +82 -0
Handgesture.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
|
| 4 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 5 |
+
mp_hands = mp.solutions.hands
|
| 6 |
+
handlist = []
|
| 7 |
+
v_4_cx = 0
|
| 8 |
+
v_8_cx = 0
|
| 9 |
+
v_12_cx = 0
|
| 10 |
+
v_16_cx = 0
|
| 11 |
+
v_20_cx = 0
|
| 12 |
+
v_4_cy = 0
|
| 13 |
+
h = 0
|
| 14 |
+
w = 0
|
| 15 |
+
|
| 16 |
+
cap = cv2.VideoCapture(0)
|
| 17 |
+
with mp_hands.Hands(False, min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
|
| 18 |
+
while cap.isOpened():
|
| 19 |
+
success, image = cap.read()
|
| 20 |
+
if not success:
|
| 21 |
+
print("Ignoring empty camera frame.")
|
| 22 |
+
# If loading a video, use 'break' instead of 'continue'.
|
| 23 |
+
continue
|
| 24 |
+
|
| 25 |
+
# Flip the image horizontally for a later selfie-view display, and convert
|
| 26 |
+
# the BGR image to RGB.
|
| 27 |
+
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
|
| 28 |
+
# To improve performance, optionally mark the image as not writeable to
|
| 29 |
+
# pass by reference.
|
| 30 |
+
image.flags.writeable = False
|
| 31 |
+
results = hands.process(image)
|
| 32 |
+
text = "NA"
|
| 33 |
+
|
| 34 |
+
# Draw the hand annotations on the image.
|
| 35 |
+
image.flags.writeable = True
|
| 36 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 37 |
+
if results.multi_hand_landmarks:
|
| 38 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
| 39 |
+
assert isinstance(hand_landmarks.landmark, object)
|
| 40 |
+
for id, landmks in enumerate(hand_landmarks.landmark):
|
| 41 |
+
# print(id, landmks)
|
| 42 |
+
h, w, c = image.shape
|
| 43 |
+
#print("windwow")
|
| 44 |
+
#print(h)
|
| 45 |
+
#print(w)
|
| 46 |
+
#print("end")
|
| 47 |
+
cx, cy = int(landmks.x * w), int(landmks.y * h)
|
| 48 |
+
handlist.append([id, cx, cy])
|
| 49 |
+
if id == 4:
|
| 50 |
+
v_4_cx = cx
|
| 51 |
+
v_4_cy = cy
|
| 52 |
+
if id == 8:
|
| 53 |
+
v_8_cx = cx
|
| 54 |
+
|
| 55 |
+
if id == 12:
|
| 56 |
+
v_12_cx = cx
|
| 57 |
+
|
| 58 |
+
if id == 16:
|
| 59 |
+
v_16_cx = cx
|
| 60 |
+
|
| 61 |
+
if id == 20:
|
| 62 |
+
v_20_cx = cx
|
| 63 |
+
|
| 64 |
+
if id == 0:
|
| 65 |
+
v_0_cx = cx
|
| 66 |
+
v_0_cy = cy
|
| 67 |
+
|
| 68 |
+
if v_4_cy > v_0_cy and (v_8_cx - v_12_cx) < 50 and (v_16_cx - v_20_cx) < 50:
|
| 69 |
+
text = "Not Good"
|
| 70 |
+
if v_4_cy < v_0_cy and (v_8_cx - v_12_cx) < 50 and (v_16_cx - v_20_cx) < 50:
|
| 71 |
+
text = "Good"
|
| 72 |
+
#print(v_4_cy)
|
| 73 |
+
#print(v_0_cy)
|
| 74 |
+
# if handlist[4][cx]= handlist[4][cx] :
|
| 75 |
+
# cv2.circle(image, (cx, cy), 10, (255, 0, 255), cv2.FILLED)
|
| 76 |
+
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 77 |
+
cv2.putText(image, text, (50, 120), cv2.FONT_HERSHEY_PLAIN , 2, (255,0,0))
|
| 78 |
+
cv2.imshow('Thumbsup', image)
|
| 79 |
+
# print(handlist)
|
| 80 |
+
if cv2.waitKey(5) & 0xFF == 27:
|
| 81 |
+
break
|
| 82 |
+
cap.release()
|