本帖最后由 kk159 于 2021-6-1 11:00 编辑
这是一个简单的实验,
利用谷歌开发的框架可以更快速帮助我们处理非常基本的ai问题,
需要的类库opencv-python,mediapipe,实现实时的手势跟踪
效果图:
1.创建摄像头对象
[Python] 纯文本查看 复制代码 import cv2
cap=cv2.VideoCapture(0)
while 1:
success,img=cap.read()
cv2.imshow('xxxx',img)
cv2.waitKey(1)
2.捕获坐标
[Python] 纯文本查看 复制代码 import mediapipe
mpHands=mediapipe.solutions.hands
hands=mpHands.Hands()
##cv2的颜色是BGR的,摄像头的是RGB,所有要转换一下
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
print(results.multi_hand_landmarks)
3.描点和链接骨干显示
[Python] 纯文本查看 复制代码 mpDraw=mediapipe.solutions.drawing_utils
##如果检测到
if results.multi_hand_landmarks:
##每只手
for hand in results.multi_hand_landmarks:
mpDraw.draw_landmarks(img,hand,mpHands.HAND_CONNECTIONS)
完整代码:
[Python] 纯文本查看 复制代码 # *_* coding : UTF-8 *_*
# author : Leemamas
# 开发时间 : 2021/6/1 10:24
import cv2
import mediapipe
cap=cv2.VideoCapture(0)
mpHands=mediapipe.solutions.hands
hands=mpHands.Hands()
mpDraw=mediapipe.solutions.drawing_utils
while 1:
success,img=cap.read()
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
##如果检测到
if results.multi_hand_landmarks:
##每只手
for hand in results.multi_hand_landmarks:
mpDraw.draw_landmarks(img,hand,mpHands.HAND_CONNECTIONS)
cv2.imshow('xxxx',img)
cv2.waitKey(1)
扩展:
[Python] 纯文本查看 复制代码 class HandLandmark(enum.IntEnum):
"""The 21 hand landmarks."""
WRIST = 0
THUMB_CMC = 1
THUMB_MCP = 2
THUMB_IP = 3
THUMB_TIP = 4
INDEX_FINGER_MCP = 5
INDEX_FINGER_PIP = 6
INDEX_FINGER_DIP = 7
INDEX_FINGER_TIP = 8
MIDDLE_FINGER_MCP = 9
MIDDLE_FINGER_PIP = 10
MIDDLE_FINGER_DIP = 11
MIDDLE_FINGER_TIP = 12
RING_FINGER_MCP = 13
RING_FINGER_PIP = 14
RING_FINGER_DIP = 15
RING_FINGER_TIP = 16
PINKY_MCP = 17
PINKY_PIP = 18
PINKY_DIP = 19
PINKY_TIP = 20
[Python] 纯文本查看 复制代码 HAND_CONNECTIONS = frozenset([
(HandLandmark.WRIST, HandLandmark.THUMB_CMC),
(HandLandmark.THUMB_CMC, HandLandmark.THUMB_MCP),
(HandLandmark.THUMB_MCP, HandLandmark.THUMB_IP),
(HandLandmark.THUMB_IP, HandLandmark.THUMB_TIP),
(HandLandmark.WRIST, HandLandmark.INDEX_FINGER_MCP),
(HandLandmark.INDEX_FINGER_MCP, HandLandmark.INDEX_FINGER_PIP),
(HandLandmark.INDEX_FINGER_PIP, HandLandmark.INDEX_FINGER_DIP),
(HandLandmark.INDEX_FINGER_DIP, HandLandmark.INDEX_FINGER_TIP),
(HandLandmark.INDEX_FINGER_MCP, HandLandmark.MIDDLE_FINGER_MCP),
(HandLandmark.MIDDLE_FINGER_MCP, HandLandmark.MIDDLE_FINGER_PIP),
(HandLandmark.MIDDLE_FINGER_PIP, HandLandmark.MIDDLE_FINGER_DIP),
(HandLandmark.MIDDLE_FINGER_DIP, HandLandmark.MIDDLE_FINGER_TIP),
(HandLandmark.MIDDLE_FINGER_MCP, HandLandmark.RING_FINGER_MCP),
(HandLandmark.RING_FINGER_MCP, HandLandmark.RING_FINGER_PIP),
(HandLandmark.RING_FINGER_PIP, HandLandmark.RING_FINGER_DIP),
(HandLandmark.RING_FINGER_DIP, HandLandmark.RING_FINGER_TIP),
(HandLandmark.RING_FINGER_MCP, HandLandmark.PINKY_MCP),
(HandLandmark.WRIST, HandLandmark.PINKY_MCP),
(HandLandmark.PINKY_MCP, HandLandmark.PINKY_PIP),
(HandLandmark.PINKY_PIP, HandLandmark.PINKY_DIP),
(HandLandmark.PINKY_DIP, HandLandmark.PINKY_TIP)
]) |