“Mediapipe”的版本间的差异
Liangdaozheng(讨论 | 贡献) |
Liangdaozheng(讨论 | 贡献) (→利用Mediapipe和Unity实现简易的动作捕捉) |
||
(未显示同一用户的13个中间版本) | |||
第2行: | 第2行: | ||
= Mediapipe简介 = | = Mediapipe简介 = | ||
− | + | Mediapipe是google的一个开源项目,可以提供开源的、跨平台的常用机器学习(machine learning)方案。Mediapipe实际上是一个集成的机器学习视觉算法的工具库,包含了人脸检测、人脸关键点、手势识别、头像分割和姿态识别等各种模型。<br>'''Mediapipe具备的优点有:'''<br>1)支持各种平台和语言,包括IOS,Android,C++,Python,JAVAScript,Coral等;<br>2)速度快,各种模型基本上可以做到实时运行。<br>'''Mediapipe在实际应用中的例子:'''<br>1)人脸检测;<br>2)FaceMesh:从图像/视频中重建出人脸的3D Mesh,可以用于AR渲染;<br>3)人像分割:从图像/视频中把人分割出来,可用于视频会议如Zoom、钉钉;<br>4)手势识别和跟踪:可以识别标出手部21个关键点的3D坐标;<br>5)人体姿态识别:可以识别标出人体33个关键点的3D坐标。 | |
* 官网地址:https://mediapipe.dev/ | * 官网地址:https://mediapipe.dev/ | ||
第13行: | 第13行: | ||
手势识别:https://code.mediapipe.dev/codepen/hands | 手势识别:https://code.mediapipe.dev/codepen/hands | ||
姿态识别:https://code.mediapipe.dev/codepen/pose | 姿态识别:https://code.mediapipe.dev/codepen/pose | ||
− | 自拍头像分割:https://code.mediapipe.dev/codepen/selfie_segmentation | + | 自拍头像分割:https://code.mediapipe.dev/codepen/selfie_segmentation |
= Mediapipe Python的安装和应用 = | = Mediapipe Python的安装和应用 = | ||
− | + | ==安装== | |
#安装python 3.7以上版本,下载地址:https://www.python.org/getit <br>(python安装教程,引自CSDN https://blog.csdn.net/weixin_49237144/article/details/122915089) | #安装python 3.7以上版本,下载地址:https://www.python.org/getit <br>(python安装教程,引自CSDN https://blog.csdn.net/weixin_49237144/article/details/122915089) | ||
− | #安装Mediapipe <br> 1)安装OpenCV,终端执行pip install opencv-contrib-python <br> 2)安装Mediapipe,终端执行pip install mediapipe,或者使用国内镜像 pip install mediapipe -i https://pypi.tuna.tsinghua.edu.cn/simple/ | + | #安装Mediapipe <br> 1)安装OpenCV,终端执行pip install opencv-contrib-python <br> 2)安装Mediapipe,终端执行pip install mediapipe,或者使用国内镜像 pip install mediapipe -i https://pypi.tuna.tsinghua.edu.cn/simple/br |
− | + | ==应用== | |
− | # | + | ==='''Mediapipe手势识别'''=== |
− | import cv2 | + | 1.OpenCV调用摄像头: |
− | cap = cv2.VideoCapture(0) | + | import cv2 |
+ | cap = cv2.VideoCapture(0) #OpenCV调用摄像头,0 == 摄像头,文件路径 == 打开视频 | ||
+ | while True: | ||
+ | success, image = cap.read() | ||
+ | img = cv2.cvtColor(iamge,cv2.COLOR_BGR2RGB) #cv2图像初始化 | ||
+ | cv2.imshow("Image", image) #CV2窗体,显示摄像头获取到的视频流 | ||
+ | cv2.waitKey(1) #关闭窗体 | ||
+ | 2.调用mediapipe中的hands模块: | ||
+ | mp_drawing = mp.solutions.drawing_utils | ||
+ | mp_drawing_styles = mp.solutions.drawing_styles | ||
+ | mp_hands = mp.solutions.hands | ||
+ | hands = mp_hands.Hands( | ||
+ | static_image_mode=False, | ||
+ | max_num_hands=2, | ||
+ | min_detection_confidence=0.75, | ||
+ | min_tracking_confidence=0.75) | ||
+ | mp.solutions.drawing_utils是一个绘图模块,将识别到的手部关键点信息绘制道cv2图像中,mp.solutions.drawing_style定义了绘制的风格。<br> | ||
+ | mp.solutions.hands是mediapipe中的手部识别模块,可以通过它调用手部识别的api,然后通过调用mp_hands.Hands初始化手部识别类。<br> | ||
+ | '''mp_hands.Hands中的参数:'''<br>1)static_image_mode=True适用于静态图片的手势识别,Flase适用于视频等动态识别,比较明显的区别是,若识别的手的数量超过了最大值,True时识别的手会在多个手之间不停闪烁,而False时,超出的手不会识别,系统会自动跟踪之前已经识别过的手。默认值为False;<br>2)max_num_hands用于指定识别手的最大数量。默认值为2;<br>3)min_detection_confidence 表示最小检测信度,取值为[0.0,1.0]这个值约小越容易识别出手,用时越短,但是识别的准确度就越差。越大识别的越精准,但是响应的时间也会增加。默认值为0.5;<br>4)min_tracking_confience 表示最小的追踪可信度,越大手部追踪的越准确,相应的响应时间也就越长。默认值为0.5。<br><br> | ||
+ | 3.demo示例: | ||
+ | import cv2 | ||
+ | import mediapipe as mp | ||
+ | |||
+ | mp_drawing = mp.solutions.drawing_utils | ||
+ | mp_hands = mp.solutions.hands | ||
+ | hands = mp_hands.Hands( | ||
+ | static_image_mode=False, | ||
+ | max_num_hands=2, | ||
+ | min_detection_confidence=0.75, | ||
+ | min_tracking_confidence=0.75) | ||
+ | |||
+ | cap = cv2.VideoCapture(0) | ||
+ | while True: | ||
+ | ret, frame = cap.read() | ||
+ | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | ||
+ | # 因为摄像头是镜像的,所以将摄像头水平翻转 | ||
+ | # 不是镜像的可以不翻转 | ||
+ | frame = cv2.flip(frame, 1) | ||
+ | results = hands.process(frame) # process()是手势识别最核心的方法,通过调用这个方法,将窗口对象作为参数,mediapipe就会将手势识别的信息存入到res对象中 | ||
+ | frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | ||
+ | if results.multi_handedness: | ||
+ | for hand_label in results.multi_handedness: | ||
+ | print(hand_label) | ||
+ | if results.multi_hand_landmarks: | ||
+ | for hand_landmarks in results.multi_hand_landmarks: | ||
+ | print('hand_landmarks:', hand_landmarks) | ||
+ | # 关键点可视化 | ||
+ | mp_drawing.draw_landmarks( | ||
+ | frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) | ||
+ | cv2.imshow('MediaPipe Hands', frame) | ||
+ | if cv2.waitKey(1) & 0xFF == 27: | ||
+ | break | ||
+ | cap.release() | ||
+ | |||
+ | ==='''Mediapipe姿态识别'''=== | ||
+ | 1.OpenCV调用摄像头(同手势识别)<br> | ||
+ | 2.调用Mediapipe中的pose模块 | ||
+ | import mediapipe as mp | ||
+ | mp_pose = mp.solutions.pose #调用pose api | ||
+ | |||
+ | pose = mp_pose.Pose(static_image_mode=True, | ||
+ | model_complexity=1, | ||
+ | smooth_landmarks=True, | ||
+ | enable_segmentation=True, | ||
+ | min_detection_confidence=0.5, | ||
+ | min_tracking_confidence=0.5) | ||
+ | mp_pose.Pose()其参数:<br>1)static_image_mode:静态图像还是连续帧视频;<br>2)model_complexity:人体姿态估计模型,0表示速度最快,精度最低(三者之中),1表示速度中间,精度中间(三者之中),2表示速度最慢,精度最高(三者之中);<br>3)smooth_landmarks:是否平滑关键点;<br>4)enable_segmentation:是否对人体进行抠图;<br>5)min_detection_confidence:检测置信度阈值;<br>6)min_tracking_confidence:各帧之间跟踪置信度阈值;<br><br> | ||
+ | 3.demo示例: | ||
+ | import cv2 | ||
+ | import mediapipe as mp | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | mp_pose = mp.solutions.pose | ||
+ | pose = mp_pose.Pose(static_image_mode=True, | ||
+ | model_complexity=1, | ||
+ | smooth_landmarks=True, | ||
+ | # enable_segmentation=True, | ||
+ | min_detection_confidence=0.5, | ||
+ | min_tracking_confidence=0.5) | ||
+ | drawing = mp.solutions.drawing_utils | ||
+ | |||
+ | # read img BGR to RGB | ||
+ | img = cv2.imread("1.jpg") | ||
+ | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | ||
+ | cv2.imshow("input", img) | ||
+ | |||
+ | results = pose.process(img) | ||
+ | drawing.draw_landmarks(img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) | ||
+ | cv2.imshow("keypoint", img) | ||
+ | |||
+ | drawing.plot_landmarks(results.pose_world_landmarks, mp_pose.POSE_CONNECTIONS) | ||
+ | |||
+ | cv2.waitKey(0) | ||
+ | cv2.destroyAllWindows() | ||
+ | === 参考资源 === | ||
+ | #https://blog.csdn.net/weixin_43229348/article/details/120530937 | ||
+ | #https://blog.csdn.net/XiaoyYidiaodiao/article/details/125280207 | ||
+ | |||
+ | = 利用Mediapipe和Unity实现简易的动作捕捉 = | ||
+ | == 概述 == | ||
+ | 通过Python使用Mediapipe进行人体姿态和手势识别,利用UDP通信技术将识别到的关节点数据传输到Unity中,实现人体模型在Unity的同步运动。 | ||
+ | == 参考资源 == | ||
+ | #https://blog.csdn.net/weixin_50679163/article/details/125081760 | ||
+ | #https://blog.csdn.net/weixin_50679163/article/details/124658314 |
2022年11月20日 (日) 13:44的最新版本
百科首页 | 3D虚拟世界 | 音乐与人工智能 | 人工智能机器人 | 关于我们 | 网站首页
Mediapipe简介
Mediapipe是google的一个开源项目,可以提供开源的、跨平台的常用机器学习(machine learning)方案。Mediapipe实际上是一个集成的机器学习视觉算法的工具库,包含了人脸检测、人脸关键点、手势识别、头像分割和姿态识别等各种模型。
Mediapipe具备的优点有:
1)支持各种平台和语言,包括IOS,Android,C++,Python,JAVAScript,Coral等;
2)速度快,各种模型基本上可以做到实时运行。
Mediapipe在实际应用中的例子:
1)人脸检测;
2)FaceMesh:从图像/视频中重建出人脸的3D Mesh,可以用于AR渲染;
3)人像分割:从图像/视频中把人分割出来,可用于视频会议如Zoom、钉钉;
4)手势识别和跟踪:可以识别标出手部21个关键点的3D坐标;
5)人体姿态识别:可以识别标出人体33个关键点的3D坐标。
- Github开源项目地址:https://github.com/google/mediapipe
- 一些模型的web体验地址(用到电脑摄像头):
人脸检测:https://code.mediapipe.dev/codepen/face_detection 人脸关键点:https://code.mediapipe.dev/codepen/face_mesh 手势识别:https://code.mediapipe.dev/codepen/hands 姿态识别:https://code.mediapipe.dev/codepen/pose 自拍头像分割:https://code.mediapipe.dev/codepen/selfie_segmentation
Mediapipe Python的安装和应用
安装
- 安装python 3.7以上版本,下载地址:https://www.python.org/getit
(python安装教程,引自CSDN https://blog.csdn.net/weixin_49237144/article/details/122915089) - 安装Mediapipe
1)安装OpenCV,终端执行pip install opencv-contrib-python
2)安装Mediapipe,终端执行pip install mediapipe,或者使用国内镜像 pip install mediapipe -i https://pypi.tuna.tsinghua.edu.cn/simple/br
应用
Mediapipe手势识别
1.OpenCV调用摄像头:
import cv2 cap = cv2.VideoCapture(0) #OpenCV调用摄像头,0 == 摄像头,文件路径 == 打开视频 while True: success, image = cap.read() img = cv2.cvtColor(iamge,cv2.COLOR_BGR2RGB) #cv2图像初始化 cv2.imshow("Image", image) #CV2窗体,显示摄像头获取到的视频流 cv2.waitKey(1) #关闭窗体
2.调用mediapipe中的hands模块:
mp_drawing = mp.solutions.drawing_utils mp_drawing_styles = mp.solutions.drawing_styles mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=False, max_num_hands=2, min_detection_confidence=0.75, min_tracking_confidence=0.75)
mp.solutions.drawing_utils是一个绘图模块,将识别到的手部关键点信息绘制道cv2图像中,mp.solutions.drawing_style定义了绘制的风格。
mp.solutions.hands是mediapipe中的手部识别模块,可以通过它调用手部识别的api,然后通过调用mp_hands.Hands初始化手部识别类。
mp_hands.Hands中的参数:
1)static_image_mode=True适用于静态图片的手势识别,Flase适用于视频等动态识别,比较明显的区别是,若识别的手的数量超过了最大值,True时识别的手会在多个手之间不停闪烁,而False时,超出的手不会识别,系统会自动跟踪之前已经识别过的手。默认值为False;
2)max_num_hands用于指定识别手的最大数量。默认值为2;
3)min_detection_confidence 表示最小检测信度,取值为[0.0,1.0]这个值约小越容易识别出手,用时越短,但是识别的准确度就越差。越大识别的越精准,但是响应的时间也会增加。默认值为0.5;
4)min_tracking_confience 表示最小的追踪可信度,越大手部追踪的越准确,相应的响应时间也就越长。默认值为0.5。
3.demo示例:
import cv2 import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils mp_hands = mp.solutions.hands hands = mp_hands.Hands( static_image_mode=False, max_num_hands=2, min_detection_confidence=0.75, min_tracking_confidence=0.75)
cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 因为摄像头是镜像的,所以将摄像头水平翻转 # 不是镜像的可以不翻转 frame = cv2.flip(frame, 1) results = hands.process(frame) # process()是手势识别最核心的方法,通过调用这个方法,将窗口对象作为参数,mediapipe就会将手势识别的信息存入到res对象中 frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) if results.multi_handedness: for hand_label in results.multi_handedness: print(hand_label) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: print('hand_landmarks:', hand_landmarks) # 关键点可视化 mp_drawing.draw_landmarks( frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) cv2.imshow('MediaPipe Hands', frame) if cv2.waitKey(1) & 0xFF == 27: break cap.release()
Mediapipe姿态识别
1.OpenCV调用摄像头(同手势识别)
2.调用Mediapipe中的pose模块
import mediapipe as mp mp_pose = mp.solutions.pose #调用pose api pose = mp_pose.Pose(static_image_mode=True, model_complexity=1, smooth_landmarks=True, enable_segmentation=True, min_detection_confidence=0.5, min_tracking_confidence=0.5)
mp_pose.Pose()其参数:
1)static_image_mode:静态图像还是连续帧视频;
2)model_complexity:人体姿态估计模型,0表示速度最快,精度最低(三者之中),1表示速度中间,精度中间(三者之中),2表示速度最慢,精度最高(三者之中);
3)smooth_landmarks:是否平滑关键点;
4)enable_segmentation:是否对人体进行抠图;
5)min_detection_confidence:检测置信度阈值;
6)min_tracking_confidence:各帧之间跟踪置信度阈值;
3.demo示例:
import cv2 import mediapipe as mp if __name__ == '__main__': mp_pose = mp.solutions.pose pose = mp_pose.Pose(static_image_mode=True, model_complexity=1, smooth_landmarks=True, # enable_segmentation=True, min_detection_confidence=0.5, min_tracking_confidence=0.5) drawing = mp.solutions.drawing_utils # read img BGR to RGB img = cv2.imread("1.jpg") img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) cv2.imshow("input", img) results = pose.process(img) drawing.draw_landmarks(img, results.pose_landmarks, mp_pose.POSE_CONNECTIONS) cv2.imshow("keypoint", img) drawing.plot_landmarks(results.pose_world_landmarks, mp_pose.POSE_CONNECTIONS) cv2.waitKey(0) cv2.destroyAllWindows()
参考资源
- https://blog.csdn.net/weixin_43229348/article/details/120530937
- https://blog.csdn.net/XiaoyYidiaodiao/article/details/125280207
利用Mediapipe和Unity实现简易的动作捕捉
概述
通过Python使用Mediapipe进行人体姿态和手势识别,利用UDP通信技术将识别到的关节点数据传输到Unity中,实现人体模型在Unity的同步运动。