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

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

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




代码如下:

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 = 1else:isStraight = 0except:angle_error = 1000isStraight = 0return 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 gesturecap = 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.shapecx, 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的手势数字识别相关推荐

  1. 数字识别java开源_Java基于opencv实现图像数字识别(三)—灰度化和二值化

    Java基于opencv实现图像数字识别(三)-灰度化和二值化 一.灰度化 灰度化:在RGB模型中,如果R=G=B时,则彩色表示灰度颜色,其中R=G=B的值叫灰度值:因此,灰度图像每个像素点只需一个字 ...

  2. Java基于opencv实现图像数字识别(一),java开发面试笔试题

    我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家. 扫描二维码或搜索下图红色VX号,加VX好友,拉你进[程序员面试学习交流群]免费领取.也欢迎各位一起 ...

  3. 基于深度学习的数字识别GUI的设计

    基于深度学习的数字识别GUI的设计 用matlab的deeplearning工具箱搭建了CNN来识别手写数字的GUI. 一.训练CNN 采用的是matlab自带的数字训练集和验证集,搭建的CNN的代码 ...

  4. 基于matlab的手写体数字识别系统,基于matlab的手写体数字识别系统研究

    基于matlab的手写体数字识别系统研究 丁禹鑫1,丁会2,张红娟2,杨彤彤1 [摘要]随着科学技术的发展,机器学习成为一大学科热门领域,是一门专门研究计算机怎样模拟或实现人类的学习行为的交叉学科.文 ...

  5. java图片降噪_Java基于opencv实现图像数字识别(四)—图像降噪

    Java基于opencv实现图像数字识别(四)-图像降噪 我们每一步的工作都是基于前一步的,我们先把我们前面的几个函数封装成一个工具类,以后我们所有的函数都基于这个工具类 这个工具类呢,就一个成员变量 ...

  6. 基于matlab的手写体数字识别系统

    摘要:随着科学技术的发展,机器学习成为一大学科热门领域,是一门专门研究计算机怎样模拟或实现人类的学习行为的交叉学科.文章在matlab软件的基础上,利用BP神经网络算法完成手写体数字的识别. 机器学习 ...

  7. 基于MATLAB的手写体数字识别算法的实现

    基于MATLAB的手写体数字识别 一.课题介绍 手写数字识别是模式识别领域的一个重要分支,它研究的核心问题是:如何利用计算机自动识别人手写在纸张上的阿拉伯数字.手写体数字识别问题,简而言之就是识别出1 ...

  8. Java基于opencv实现图像数字识别(一)

    Java基于opencv实现图像数字识别(一) 最近分到了一个任务,要做数字识别,我分配到的任务是把数字一个个的分开:当时一脸懵逼,直接百度java如何分割图片中的数字,然后就百度到了用Buffere ...

  9. 基于模板匹配的数字识别

    基于模板匹配的数字识别,将标准的8*16像素的数字0123456789读取,二值化,对每个数字进行等分区域分割,统计每个区域内的黑色像素点的个数,即为特征初值.采用欧式距离的模板匹配法. z//基于模 ...

最新文章

  1. Nginx与Redis解决高并发问题
  2. hdu 2544 最短路 Dijkstra算法
  3. 找到数组中第k小的值(利用快排的划分函数)
  4. PREFACE FPGA经典案例序言
  5. android AVB2.0(四)libavb库介绍
  6. 全网首发:怎样制作CDKEY(6)-CDKEY破解
  7. 运行c语言程序显示已停止运行程序,c – “此应用程序已请求运行时以不寻常的方式终止它.”...
  8. 正点原子STM32 ISP电路分析
  9. vivo4.0以上系统怎么样不用root激活XPOSED框架的教程
  10. js打开新窗口Window.open()方法
  11. android imageview 半透明,如何将半透明视图叠加到ImageView上?
  12. 自学编程的六种方法,你必须知道?
  13. 重来之大学版|社交生活篇——失恋了怎么办?失恋了很难受怎么办?如何走出失恋的痛苦?我失恋了该怎么办?如何从失恋的痛苦中走出来?
  14. dgesForExtendedLayout ios7新特性
  15. golang || gin运行,出现类似这种的错误: missing go.sum entry; to add it:
  16. 关于配线光缆,您需要了解什么
  17. NOIP模拟赛 太阳神
  18. 卷积核大小对网络参数和计算量的影响
  19. 这也太香了吧!阿里甩出2021最新秒杀系统设计实录!全新演绎!
  20. GM300铁损仪与目前同类机型直读式铁损测试仪的比较

热门文章

  1. c语言游戏泡泡糖,泡泡糖语言教案
  2. 【Java项目】期末大作业——SHJQ学院食堂管理系统
  3. 基于asp.net331婚纱影楼管理系统
  4. 计算机win7进不了,win7进不了系统怎么解决
  5. [Vue3]Console报错:不能将类型“string”分配给类型Refstring
  6. Mocha Pro:移除模块
  7. Apache Hue:Hue集成Impala
  8. 杂项-Java-百科:jar
  9. UE4Material_材质属性(1)
  10. 零基础如何去入门学习UI设计?学习步骤是什么?