基于Mediapipe+Opencv实现手势检测
一、前言
想实现一下姿态识别的时候,感觉手势识别也蛮重要的就过来顺便实现一下。
下面是一些国内的pip源,有需要可自取
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
二、环境配置
软件:
ANACONDA3+Pycharm2019
环境:
- opencv-python>=4.5.5
- mediapipe>=0.8.9.1
注:一定关掉科学上网
三、全部源码
比较短小且只有一个源文件MediapipeHandTracking.py我就直接在这里贴了
MediapipeHandTracking.py程序结构:
- 第一步:保存mediapipe中的手势识别解决方案到mpHands,hands,mpDraw中
- 第二步:参数设定
- 第三步:循环读取视频流到img,img输入hands.hands函数得到结果,绘制结果到img并输出
MediapipeHandTracking.py源码与注释
import cv2
import mediapipe as mp
import time
mpHands = mp.solutions.hands
hands = mpHands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5)
mpDraw = mp.solutions.drawing_utils
handLmsStyle = mpDraw.DrawingSpec(color=(0, 0, 255), thickness=3)
handConStyle = mpDraw.DrawingSpec(color=(0, 255, 0), thickness=5)
pTime = 0
cTime = 0
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
if ret:
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(imgRGB)
imgHeight = img.shape[0]
imgWidth = img.shape[1]
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS, handLmsStyle, handConStyle)
for i, lm in enumerate(handLms.landmark):
xPos = int(lm.x * imgWidth)
yPos = int(lm.y * imgHeight)
cv2.putText(img, str(i), (xPos-25, yPos+5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255), 2)
if i == 8:
cv2.circle(img, (xPos, yPos), 10, (166, 0, 0), cv2.FILLED)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, f"FPS : {int(fps)}", (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3)
cv2.imshow('img', img)
if cv2.waitKey(1) == ord('q'):
break
四、环境配置
1、在Anaconda3上新建环境Gesture
打开Anaconda Prompt,输入:
conda create -n Gesture python=3.8
2、激活Gesture环境并下载opencv-python包
激活环境:conda activate Gesture 下载opencv-python包:pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple/ 
3、下载mediapipe包
pip install mediapipe -i https://pypi.tuna.tsinghua.edu.cn/simple/

4、打开Pycharm完成环境导入项目
配置代码运行环境    
五、运行程序:
用Pycharm打开包含hanTracking.py程序的文件夹,并运行  运行结果 
六、程序应用扩展
1、手部的关键点的位置和次序我们全部已知的特点
 该功能可用于图片ROI提取截取出图片,然后进行其他一些图片操作。 该功能可用于手势响应事件。比如约定,食指和大拇指也就是4号和8号触碰时候触发某事件 ,等等。 实现AL+操作

2、和其他AL结合
比如姿态检测AL,能将人识别成一个火柴人,开发空间有不少用处。
3、全身检测源码
import cv2
import time
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_holistic = mp.solutions.holistic
holistic = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)
handLmsStyle = mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=0)
handConStyle = mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=4)
cap = cv2.VideoCapture(0)
while True:
ret,image=cap.read()
if ret:
image = cv2.flip(image, 1)
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
results = holistic.process(image)
if results.pose_landmarks:
mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACEMESH_CONTOURS,handLmsStyle,handConStyle)
mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)
cv2.imshow("img",image)
if cv2.waitKey(1)==ord("q"):
break
holistic.close()
运行效果如下: 晒晒我帅气的舍友 
|