-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhand_tracking.py
69 lines (49 loc) · 2.11 KB
/
hand_tracking.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
import cv2
import mediapipe as mp
from settings import *
import numpy as np
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
class HandTracking:
def __init__(self):
self.hand_tracking = mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5)
self.hand_x = 0
self.hand_y = 0
self.results = None
self.hand_closed = False
def scan_hands(self, image):
rows, cols, _ = image.shape
# Flip the image horizontally for a later selfie-view display, and convert
# the BGR image to RGB.
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
self.results = self.hand_tracking.process(image)
# Draw the hand annotations on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
self.hand_closed = False
if self.results.multi_hand_landmarks:
for hand_landmarks in self.results.multi_hand_landmarks:
x, y = hand_landmarks.landmark[9].x, hand_landmarks.landmark[9].y
self.hand_x = int(x * SCREEN_WIDTH)
self.hand_y = int(y * SCREEN_HEIGHT)
x1, y1 = hand_landmarks.landmark[12].x, hand_landmarks.landmark[12].y
if y1 > y:
self.hand_closed = True
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())
return image
def get_hand_center(self):
return (self.hand_x, self.hand_y)
def display_hand(self):
cv2.imshow("image", self.image)
cv2.waitKey(1)
def is_hand_closed(self):
pass