基于mediapipe的手势数字识别

这篇具有很好参考价值的文章主要介绍了基于mediapipe的手势数字识别。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

基于mediapipe识别手势所对应的数字(一、二、三、四、五)。
mediapipe的官网
总体思路:mediapipe可以识别手掌的关键点,我的思路是识别单根手指是否弯曲,然后根据五根手指的弯曲程度判断手势所对应的数字。

那怎么判断单根手指是否弯曲呢?
我是根据手指的四个关键点的相对位置。比如识别大拇指的弯曲程度,先计算点4和点3的角度a,再计算点2和点1的角度b,最后计算角度a和角度b的差值的绝对值,如果绝对值小于12度,则认为大拇指是伸直的。其他手指同理。

那怎么根据五根手指的弯曲程度判断手势所对应的数字呢?
假设已知五根手指的弯曲程度,若五根手指均伸直,则手势为数字五;若食指、中指、无名指、小指均伸直,而大拇指弯曲,则认为手势是数字四。其它手势同理。
基于mediapipe的手势数字识别
基于mediapipe的手势数字识别
基于mediapipe的手势数字识别基于mediapipe的手势数字识别
基于mediapipe的手势数字识别
基于mediapipe的手势数字识别

代码如下:文章来源地址https://www.toymoban.com/news/detail-509960.html

import cv2
import mediapipe as mp
import time
import math


# 根据手指四个关节判断手指是否伸直
def get_angleError(point_4,point_3,point_2,point_1):
    try:
        point_4_cx, point_4_cy = int(point_4.x * w), int(point_4.y * h)
        point_3_cx, point_3_cy = int(point_3.x * w), int(point_3.y * h)
        point_2_cx, point_2_cy = int(point_2.x * w), int(point_2.y * h)
        point_1_cx, point_1_cy = int(point_1.x * w), int(point_1.y * h)

        angle_1 = math.degrees(math.atan((point_3_cx - point_4_cx) / (point_3_cy - point_4_cy)))
        angle_2 = math.degrees(math.atan((point_1_cx - point_2_cx) / (point_1_cy - point_2_cy)))
        angle_error = abs(angle_1 - angle_2)
        if angle_error<12:
            isStraight = 1
        else:
            isStraight = 0
    except:
        angle_error = 1000
        isStraight = 0

    return angle_error, isStraight


# 根据五根手指伸直程度识别手势
def getGesture(isStraight_list):
    if isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==0 and isStraight_list[3]==0 and isStraight_list[4]==0:
        gesture = "one"
    elif isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==0 and isStraight_list[4]==0:
        gesture = "two"
    elif isStraight_list[0]==0 and isStraight_list[1]==0 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "three"
    elif isStraight_list[0]==0 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "four"
    elif isStraight_list[0]==1 and isStraight_list[1]==1 and isStraight_list[2]==1 and isStraight_list[3]==1 and isStraight_list[4]==1:
        gesture = "five"
    else:
        gesture = "None"

    return gesture

cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(static_image_mode=False,
                      max_num_hands=2,
                      min_detection_confidence=0.5,
                      min_tracking_confidence=0.5)

mpDraw = mp.solutions.drawing_utils
pTime = 0
cTime = 0
while True:
    success, img = cap.read()
    img = cv2.flip(img, 1)
    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:
            for id, lm in enumerate(handLms.landmark):
                #print(id,lm)
                h, w, c = img.shape
                cx, cy = int(lm.x *w), int(lm.y*h)
                # print(cx,cy)
                #if id ==0:
                cv2.circle(img, (cx,cy), 3, (255,0,255), cv2.FILLED)
            mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)

        # 判断拇指手势方向:
        isStraight_list = []
        point_4 = handLms.landmark[4]
        point_3 = handLms.landmark[3]
        point_2 = handLms.landmark[2]
        point_1 = handLms.landmark[1]
        angle_error_1, isStraight_1 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_1:",isStraight_1)
        isStraight_list.append(isStraight_1)

        # 判断食指手势方向:
        point_4 = handLms.landmark[8]
        point_3 = handLms.landmark[7]
        point_2 = handLms.landmark[6]
        point_1 = handLms.landmark[5]
        angle_error_2, isStraight_2 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_2:",isStraight_2)
        isStraight_list.append(isStraight_2)

        # 判断中指手势方向:
        point_4 = handLms.landmark[12]
        point_3 = handLms.landmark[11]
        point_2 = handLms.landmark[10]
        point_1 = handLms.landmark[9]
        angle_error_3, isStraight_3 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_3:",isStraight_3)
        isStraight_list.append(isStraight_3)

        # 判断无名指手势方向:
        point_4 = handLms.landmark[16]
        point_3 = handLms.landmark[15]
        point_2 = handLms.landmark[14]
        point_1 = handLms.landmark[13]
        angle_error_4, isStraight_4 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_4:",isStraight_4)
        isStraight_list.append(isStraight_4)

        # 判断小指手势方向:
        point_4 = handLms.landmark[20]
        point_3 = handLms.landmark[19]
        point_2 = handLms.landmark[18]
        point_1 = handLms.landmark[17]
        angle_error_5, isStraight_5 = get_angleError(point_4,point_3,point_2,point_1)
        print("isStraight_5:",isStraight_5)
        isStraight_list.append(isStraight_5)

        # 根据五根手指的伸直程度判断手势所对应的数字
        gesture  = getGesture(isStraight_list)
        print("gesture:",gesture)

        cv2.putText(img, gesture, (10, 100), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 255), 3)


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

到了这里,关于基于mediapipe的手势数字识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Opencv + MediaPipe -> 手势识别

    一、概述         OpenCV(Open Source Computer Vision Library)是一个跨平台的计算机视觉库,它提供了许多用于图像和视频处理的功能,包括图像和视频的读取、预处理、特征提取、特征匹配、目标检测等。OpenCV是C++编写的,也提供了Python、Java等语言的接口,可以方便地在不同

    2024年02月05日
    浏览(60)
  • Mediapipe手势识别,并与unity通信

    Mediapipe是goole的一个开源项目,支持跨平台的常用ML方案,详情请戳下面链接 MediaPipe Mediapipe底层封装了手势识别的具体实现内容,而在Python中搭建完环境后经过很简单的调用就能够实现手势识别 环境如下: pip install mediapipe pip install opencv-python 简单的实现,代码很少,代码如

    2024年02月11日
    浏览(31)
  • mediapipe 手势节点识别自动控制音量

    参考:https://www.computervision.zone/topic/volumehandcontrol-py/ 主函数: VolumeHandControl.py

    2024年02月11日
    浏览(28)
  • 使用MediaPipe和OpenCV的Python实现手势识别

    手势识别技术是一种非常有用的技术,它可以将人类的手势转化为计算机可以理解的形式,从而实现更加自然、快速和直观的交互方式。本文将介绍一种基于MediaPipe和OpenCV的手势识别技术,可以实现对手势的实时识别和分析。 MediaPipe是一种开源的机器学习框架,可以用于构建

    2024年02月14日
    浏览(37)
  • MediaPipe+OpenCV 实现实时手势识别(附Python源码)

    MediaPipe官网:https://developers.google.com/mediapipe MediaPipe仓库:https://github.com/google/mediapipe MediaPipe 是一个由 Google 开发的开源跨平台机器学习框架,用于构建视觉和感知应用程序。它提供了一系列预训练的机器学习模型和工具,使开发者能够轻松地构建基于计算机视觉和机器学习的

    2024年02月07日
    浏览(34)
  • 基于mediapipe和opencv的手势控制电脑鼠标

    通过我的上一篇文章,可以了解到mediapipe关于手部检测的使用方法。这时我们就可以进行一些更加炫酷的操作。这篇文章我就来讲解一下如何用手势来控制电脑鼠标。 在开始之前我们要介绍一个能够操作电脑鼠标的库pyautogui,这里我简单介绍一下该库的一些函数,方便大家观

    2024年02月07日
    浏览(28)
  • 基于mediapipe的姿态识别和简单行为识别

    源码地址: 🚀🚀🚀🚀 其实这部分很简单,直接在windows命令行的环境下 就可以啦 Mediapipe是一个用于构建机器学习管道的框架,用户处理视频、音频等时间序列数据。这个跨平台框架适用于桌面/服务器、Android、ios和各类嵌入式设备。 目前mediapipe包含16个solutions,分别为 总

    2024年02月02日
    浏览(35)
  • 基于opencv与mediapipe的民族舞舞蹈动作识别

    需要项目的请关注、私信 Opencv(Open Source Computer Vision Library)是一个基于开源发行的跨平台计算机视觉库,它实现了图像处理和计算机视觉方面的很多通用算法,已成为计算机视觉领域最有力的研究工具。在这里我们要区分两个概念:图像处理和计算机视觉的区别:图像处理

    2024年02月10日
    浏览(35)
  • 基于Mediapipe的姿势识别并同步到Unity人体模型中

    如题,由于是商业项目,无法公开源码,这里主要说一下实现此功能的思路。 人体关节点识别 基于Mediapipe Unity插件进行开发,性能比较低的CPU主机,无法流畅地运行Mediapipe,这个要注意一下。 Mediapipe33个人体关节点图如下: Mediapipe关节点映射到Unity人体骨骼 这是开发此功能

    2024年02月11日
    浏览(35)
  • [ Python+OpenCV+Mediapipe ] 实现手势控制音量

    目录 一、写在前面 二、本文内容 三、开发环境  四、代码实现 4.1引入所需包 4.2 定义一个类并初始化 4.3 定义绘制一界面并实现音量控制的主方法 五、看一看实际效果吧 六、完整代码 七、小结  八、感谢        本文所用例子为个人学习的小结,如有不足之处请各位多多

    2024年02月22日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包