mediapipe教程1:python代码
·
一、背景
因为领导想用这个东西,因此开始研究,首先对于mediapipe的介绍不多说,百度一大堆,
主要是(1)跨平台,(2)对于移动设备友好,实时性足够
二、环境
因为对这东西不了解,这里先用python代码简单的看看效果,代码的参考网址:
(1)https://blog.csdn.net/javastart/article/details/116423090
(2)https://google.github.io/mediapipe/solutions/holistic
例子中只用到了cv2和mediapipe两个库,对于库安装下载不下来的可以考虑换源,参考:https://blog.csdn.net/afeiqiang/article/details/108257584
三、代码(没啥好说的,直接上代码,看代码即可)
(1)全身检测(图片)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 2, 2)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 2, 2)
#mp.solutions.holistic是一个类别,是人的整体
mp_holistic = mp.solutions.holistic
#参数:1、是否检测静态图片,2、姿态模型的复杂度,3、结果看起来平滑(用于video有效),4、检测阈值,5、跟踪阈值
holistic = mp_holistic.Holistic(static_image_mode=True)
file = 'input.jpg'
image = cv2.imread(file)
image_hight, image_width, _ = image.shape
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#处理RGB图像
results = holistic.process(image1)
#某一个点的坐标
if results.pose_landmarks:
print(
f'Nose coordinates: ('
f'{results.pose_landmarks.landmark[mp_holistic.PoseLandmark.NOSE].x * image_width}, '
f'{results.pose_landmarks.landmark[mp_holistic.PoseLandmark.NOSE].y * image_hight})'
)
'''
mp_holistic.PoseLandmark类中共33个人体骨骼点
mp_holistic.HandLandmark类中共21个手部关键点
脸部有468个关键点
'''
#绘制
mp_drawing.draw_landmarks(
image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
#cv2.imshow('image',image)
#cv2.waitKey(0)
cv2.imwrite('image-holistic.jpg', image)
holistic.close()
(2)全身检测(视频)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 1 , 1)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 1, 1)
#mp.solutions.holistic是一个类别,是人的整体
mp_holistic = mp.solutions.holistic
#参数:1、是否检测静态图片,2、姿态模型的复杂度,3、结果看起来平滑(用于video有效),4、检测阈值,5、跟踪阈值
holistic = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)
cap = cv2.VideoCapture('input.mp4')
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理RGB图像
results = holistic.process(image1)
'''
mp_holistic.PoseLandmark类中共33个人体骨骼点
mp_holistic.HandLandmark类中共21个手部关键点
脸部有468个关键点
'''
# 绘制
mp_drawing.draw_landmarks(
image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
cv2.imshow('MediaPipe Holistic', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
holistic.close()
cv2.destroyAllWindows()
cap.release()
(3)人体骨架检测(图片)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 2, 2)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 2, 2)
#mp.solutions.pose,是人的骨架
mp_pose = mp.solutions.pose
#参数:1、是否检测静态图片,2、姿态模型的复杂度,3、结果看起来平滑(用于video有效),4、检测阈值,5、跟踪阈值
pose_mode = mp_pose.Pose(static_image_mode=True)
file = 'input.jpg'
image = cv2.imread(file)
image_hight, image_width, _ = image.shape
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理RGB图像
results = pose_mode.process(image1)
'''
mp_pose.PoseLandmark类中共33个人体骨骼点
'''
if results.pose_landmarks:
print(
f'Nose coordinates: ('
f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].x * image_width}, '
f'{results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].y * image_hight})'
)
#绘制
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
#cv2.imshow('image',image)
#cv2.waitKey(0)
cv2.imwrite('image-pose.jpg', image)
pose_mode.close()
(4)人体骨架检测(视频)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 1 , 1)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 1, 1)
#mp.solutions.pose,是人的骨架
mp_pose = mp.solutions.pose
#参数:1、是否检测静态图片,2、姿态模型的复杂度,3、结果看起来平滑(用于video有效),4、检测阈值,5、跟踪阈值
pose_mode = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
cap = cv2.VideoCapture('input.mp4')
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理RGB图像
results = pose_mode.process(image1)
'''
mp_holistic.PoseLandmark类中共33个人体骨骼点
'''
# 绘制
mp_drawing.draw_landmarks(
image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pose_mode.close()
cv2.destroyAllWindows()
cap.release()
(5)手部关键点检测(图片)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 2, 2)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 2, 2)
#mp.solutions.hands,是人的手
mp_hands = mp.solutions.hands
#参数:1、是否检测静态图片,2、手的数量,3、检测阈值,4、跟踪阈值
hands_mode = mp_hands.Hands(static_image_mode=True, max_num_hands=2)
file = 'input.jpg'
image = cv2.imread(file)
image_hight, image_width, _ = image.shape
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#处理RGB图像
results = hands_mode.process(image1)
print('Handedness:', results.multi_handedness)
for hand_landmarks in results.multi_hand_landmarks:
print('hand_landmarks:', hand_landmarks)
print(
f'Index finger tip coordinates: (',
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_hight})'
)
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
#cv2.imshow('image',image)
#cv2.waitKey(0)
cv2.imwrite('image-hands.jpg', image)
hands_mode.close()
(6)手部关键点检测(视频)
import cv2
import mediapipe as mp
#mp.solutions.drawing_utils用于绘制
mp_drawing = mp.solutions.drawing_utils
#参数:1、颜色,2、线条粗细,3、点的半径
DrawingSpec_point = mp_drawing.DrawingSpec((0, 255, 0), 1 , 1)
DrawingSpec_line = mp_drawing.DrawingSpec((0, 0, 255), 1, 1)
#mp.solutions.hands,是人的手
mp_hands = mp.solutions.hands
#参数:1、是否检测静态图片,2、手的数量,3、检测阈值,4、跟踪阈值
hands_mode = mp_hands.Hands(max_num_hands=2)
cap = cv2.VideoCapture('input.mp4')
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 处理RGB图像
results = hands_mode.process(image1)
# 绘制
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS, DrawingSpec_point, DrawingSpec_line)
cv2.imshow('MediaPipe Hands', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
hands_mode.close()
cv2.destroyAllWindows()
cap.release()
更多推荐
已为社区贡献2条内容
所有评论(0)