Python+OpenCV手势识别Mediapipe(新手入门)
前言项目效果图认识Mediapipe项目环境代码核心代码视频帧率计算完整代码项目输出结语前言
本篇文章适合刚入门OpenCV的同学们。文章将介绍如何使用Python利用OpenCV图像捕捉,配合强大的Mediapipe库来实现手势检测与识别;本系列后续还会继续更新Mediapipe手势的各种衍生项目,还请多多关注!
项目效果图
视频捕捉帧数稳定在(25-30)
认识Mediapipe
项目的实现,核心是强大的Mediapipe,它是google的一个开源项目:
Mediapipe Dev
以上是Mediapipe的几个常用功能 ,这几个功能我们会在后续一一讲解实现
Python安装Mediapipe
pip install mediapipe==0.8.9.1
也可以用setup.py安装
/google/mediapipe
项目环境
Python 3.7 Mediapipe 0.8.9.1 Numpy 1.21.6 OpenCV-Python 4.5.5.64 OpenCV-contrib-Python 4.5.5.64
实测也支持Python3.8-3.9
代码
核心代码
OpenCV摄像头捕捉部分:
import cv2cap = cv2.VideoCapture(0) #OpenCV摄像头调用:0=内置摄像头(笔记本) 1=USB摄像头-1 2=USB摄像头-2while True:success, img = cap.read()imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #cv2图像初始化cv2.imshow("HandsImage", img) #CV2窗体cv2.waitKey(1)#关闭窗体
mediapipe 手势识别与绘制
#定义并引用mediapipe中的hands模块mpHands = mp.solutions.handshands = mpHands.Hands()mpDraw = mp.solutions.drawing_utilswhile True:success, img = cap.read()imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #cv2图像初始化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(id, cx, cy)# if id == 4:cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)#绘制手部特征点:mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
视频帧率计算
import time#帧率时间计算pTime = 0cTime = 0while TruecTime = time.time()fps = 1 / (cTime - pTime)pTime = cTimecv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,(255, 0, 255), 3) #FPS的字号,颜色等设置
完整代码
# Coding BIGBOSSyifi# Datatime:/4/24 21:41# Filename:HandsDetector.py# Toolby: PyCharmimport cv2import mediapipe as mpimport timecap = cv2.VideoCapture(0) #OpenCV摄像头调用:0=内置摄像头(笔记本) 1=USB摄像头-1 2=USB摄像头-2#定义并引用mediapipe中的hands模块mpHands = mp.solutions.handshands = mpHands.Hands()mpDraw = mp.solutions.drawing_utils#帧率时间计算pTime = 0cTime = 0while True:success, img = cap.read()imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #cv2图像初始化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(id, cx, cy)# if id == 4:cv2.circle(img, (cx, cy), 15, (255, 0, 255), cv2.FILLED)#绘制手部特征点:mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)'''''视频FPS计算'''cTime = time.time()fps = 1 / (cTime - pTime)pTime = cTimecv2.putText(img, str(int(fps)), (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,(255, 0, 255), 3) #FPS的字号,颜色等设置cv2.imshow("HandsImage", img) #CV2窗体cv2.waitKey(1)#关闭窗体
项目输出
结语
以此篇文章技术为基础,后续会更新利用此篇基础技术实现的《手势控制:音量,鼠标》
项目下载地址/BIGBOSS-dedsec/HandsDetection_Python