kk159 发表于 2021-6-1 11:00

计算机视觉实时手势跟踪python简单实现

本帖最后由 kk159 于 2021-6-1 11:00 编辑

这是一个简单的实验,
利用谷歌开发的框架可以更快速帮助我们处理非常基本的ai问题,
需要的类库opencv-python,mediapipe,实现实时的手势跟踪
效果图:


1.创建摄像头对象
import cv2

cap=cv2.VideoCapture(0)

while 1:
    success,img=cap.read()
    cv2.imshow('xxxx',img)
    cv2.waitKey(1)

2.捕获坐标
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.描点和链接骨干显示
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)

完整代码:
# *_* coding : UTF-8 *_*
# author:Leemamas
# 开发时间:2021/6/110: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)

扩展:

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

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)
])

GiaoMan-wei 发表于 2021-6-1 13:49

yiwozhutou 发表于 2021-6-1 13:30
强啊 现在还不敢输入密码了 咋办楼上说的延迟 嗯 不知道咋办 呢大佬强啊祝你早点去 少林 欧里给

你这。。水的过fin了~~~~~{:301_1004:}

yiwozhutou 发表于 2021-6-1 13:30

强啊 现在还不敢输入密码了 咋办楼上说的延迟 嗯 不知道咋办 呢大佬强啊祝你早点去 少林 欧里给

qianshang666 发表于 2021-6-1 11:14

学到了,谢谢分享

龍謹 发表于 2021-6-1 11:15

这个干活含金量高啊,谢谢大佬无私分享!

pj明 发表于 2021-6-1 11:15

不错,没事可以下来学习一下

上单小鳄鱼 发表于 2021-6-1 11:29

学习学习 去

sam喵喵 发表于 2021-6-1 12:45

这玩意最大问题就是延迟

GiaoMan-wei 发表于 2021-6-1 13:43

强啊,,,大佬,,{:301_1003:}

love522014 发表于 2021-6-1 14:30

感谢楼主分享经验
页: [1] 2
查看完整版本: 计算机视觉实时手势跟踪python简单实现