吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2979|回复: 12
收起左侧

[Python 转载] 计算机视觉实时手势跟踪python简单实现

[复制链接]
kk159 发表于 2021-6-1 11:00
本帖最后由 kk159 于 2021-6-1 11:00 编辑

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

效果图:
image.png

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)

image.png

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)


扩展:
Snipaste_2021-06-01_10-21-39.png
[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)
])

免费评分

参与人数 4吾爱币 +10 热心值 +2 收起 理由
烟凌 + 1 我很赞同!
hwh425 + 1 热心回复!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
qianshang666 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

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

你这。。水的过fin了~~~~~
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
强啊,,,大佬,,
love522014 发表于 2021-6-1 14:30
感谢楼主分享经验
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 16:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表