搭建环境以及所需的库

1
2
3
python:3.7
opencv-python
mediapipe

识别点位对照表

识别点位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0 - WRIST
1 - THUMB_CMC
2 - THUMB_MCP
3 - THUMB_IP
4 - THUMB_TIP
5 - INDEX_FINGER_MCP
6 - INDEX_FINGER_PIP
7 - INDEX_FINGER_DIP
8 - INDEX_FINGER_TIP
9 - MIDDLE_FINGER_MCP
10 - MIDDLE_FINGER_PIP
11 - MIDDLE_FINGER_DIP
12 - MIDDLE_FINGER_TIP
13 - RING_FINGER_MCP
14 - RING_FINGER_PIP
15 - RING_FINGER_DIP
16 - RING_FINGER_TIP
17 - PINKY_MCP
18 - PINKY_PIP
19 - PINKY_DIP
20 - PINKY_TIP

1.启动摄像头

1
2
3
4
5
6
7
8
9
import cv2

cap = cv2.VideoCapture(0)

while True:
success, img = cap.read()

cv2.imshow('Image', img)
cv2.waitKey(1)

2.识别手部动作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import mediapipe as mp

cap = cv2.VideoCapture(0)

mpHands = mp.solutions.hands
hands = mpHands.Hands()

while True:
success, img = cap.read()

imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
print(results.multi_hand_landmarks)

cv2.imshow('Image', img)
cv2.waitKey(1)

识别出的数据
每个手部21个点位

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
[
landmark {
x: 0.731960117816925
y: 0.8602098822593689
z: 3.7826006860086636e-07
}
landmark {
x: 0.6678993701934814
y: 0.825223445892334
z: -0.022978829219937325
}
landmark {
x: 0.6120011806488037
y: 0.7446209192276001
z: -0.032867904752492905
}
landmark {
x: 0.5765049457550049
y: 0.676508903503418
z: -0.04126569628715515
}
landmark {
x: 0.5479035973548889
y: 0.6209680438041687
z: -0.04858679696917534
}
landmark {
x: 0.6559033989906311
y: 0.5685721039772034
z: -0.009198924526572227
}
landmark {
x: 0.6436667442321777
y: 0.45814454555511475
z: -0.02262815274298191
}
landmark {
x: 0.6395666599273682
y: 0.391674280166626
z: -0.03663289546966553
}
landmark {
x: 0.6381848454475403
y: 0.3321998417377472
z: -0.04805788770318031
}
landmark {
x: 0.6958205103874207
y: 0.5510193109512329
z: -0.013065477833151817
}
landmark {
x: 0.6914491653442383
y: 0.42552679777145386
z: -0.023112589493393898
}
landmark {
x: 0.6911518573760986
y: 0.3481542468070984
z: -0.03534790873527527
}
landmark {
x: 0.6909988522529602
y: 0.2837005853652954
z: -0.0455666109919548
}
landmark {
x: 0.7336719036102295
y: 0.5597383379936218
z: -0.02150423638522625
}
landmark {
x: 0.734130859375
y: 0.4429841935634613
z: -0.03502415493130684
}
landmark {
x: 0.7332345247268677
y: 0.3698875904083252
z: -0.04540318623185158
}
landmark {
x: 0.7308086156845093
y: 0.3045901954174042
z: -0.053850580006837845
}
landmark {
x: 0.7715479731559753
y: 0.5895281434059143
z: -0.032650720328092575
}
landmark {
x: 0.7856296300888062
y: 0.5011546611785889
z: -0.046174854040145874
}
landmark {
x: 0.7944579124450684
y: 0.44144606590270996
z: -0.05218689516186714
}
landmark {
x: 0.8009870052337646
y: 0.3834943175315857
z: -0.05687923729419708
}
]

以上只是将手指点位信息打印到控制台,接下来要将点位信息在视频中显示出来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import cv2
import mediapipe as mp

cap = cv2.VideoCapture(0)

mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils

while True:
success, img = cap.read()

imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
print(results.multi_hand_landmarks)

if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
cv2.imshow('Image', img)
cv2.waitKey(1)

使用了mp.solutions.drawing_utils.draw_landmarks将点位信息在视频中显示出来

3.显示fps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import cv2
import mediapipe as mp
import time

cap = cv2.VideoCapture(0)

cTime = 0
pTime = 0

while True:
success, img = cap.read()

cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3)
cv2.imshow('Image', img)
cv2.waitKey(1)

4.获取手掌点位信息

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
import cv2
import mediapipe as mp

cap = cv2.VideoCapture(0)

mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils

while True:
success, img = cap.read()

imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)

if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
cv2.circle(img, (cx, cy), 15, (255, 0, 0), cv2.FILLED)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

cv2.imshow('Image', img)
cv2.waitKey(1)

以上既是手势识别的基本代码

全部代码(模块化)

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
import cv2
import mediapipe as mp
import time

class handDetector():
def __init__(self, mode=False, maxHands=2, complexity=1, detectionCon=0.5, trackCon=0.5):
self.mode = mode
self.maxHands = maxHands
self.complexity = complexity
self.detectionCon = detectionCon
self.trackCon = trackCon

self.mpHands = mp.solutions.hands
self.hands = self.mpHands.Hands(self.mode, self.maxHands, self.complexity, self.detectionCon, self.trackCon)
self.mpDraw = mp.solutions.drawing_utils



def findHands(self, img, draw=True):
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.results = self.hands.process(imgRGB)
# print(results.multi_hand_landmarks)

if self.results.multi_hand_landmarks:
for handLms in self.results.multi_hand_landmarks:
if draw:
self.mpDraw.draw_landmarks(img, handLms, self.mpHands.HAND_CONNECTIONS)
return img

def findPosition(self, img, handNo=0, draw=True):
lmList = []
if self.results.multi_hand_landmarks:
myHands = self.results.multi_hand_landmarks[handNo]
for id, lm in enumerate(myHands.landmark):
h, w, c = img.shape
cx, cy = int(lm.x * w), int(lm.y * h)
lmList.append([id, cx, cy])
if draw:
cv2.circle(img, (cx, cy), 15, (255, 0, 0), cv2.FILLED)
return lmList

def main():
pTime = 0
cTime = 0
cap = cv2.VideoCapture(0)
detector = handDetector()
print(1)
while True:
success, img = cap.read()
img = detector.findHands(img)
lmList = detector.findPosition(img)
if len(lmList) != 0:
print(lmList[4])
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3)
cv2.imshow('Image', img)
cv2.waitKey(1)


if __name__ == "__main__":
main()

最后更新: 2023年09月25日 22:31