-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1-2_extract-hand.py
49 lines (38 loc) · 1.58 KB
/
part1-2_extract-hand.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
import cv2
import numpy as np
import mediapipe as mp
image = cv2.imread("thumbup.jpeg")
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# Run MediaPipe Hands.
with mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.7) as hands:
# Convert the BGR image to RGB, flip the image around y-axis for correct
# handedness output and process it with MediaPipe Hands.
results = hands.process(cv2.flip(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), 1))
# Print handedness (left v.s. right hand).
print(results.multi_handedness)
# Draw hand landmarks of each hand.
image_hight, image_width, _ = image.shape
annotated_image = cv2.flip(image.copy(), 1)
for hand_landmarks in results.multi_hand_landmarks:
# Print index finger tip coordinates.
print(
f'Index finger tip coordinate: (',
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_hight})'
)
mp_drawing.draw_landmarks(
annotated_image,
hand_landmarks,
mp_hands.HAND_CONNECTIONS,
mp_drawing_styles.get_default_hand_landmarks_style(),
mp_drawing_styles.get_default_hand_connections_style())
cv2.imshow("Image", annotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1) # normally unnecessary, but it fixes a bug on
# MacOS where the window doesn't close view raw