环境:
python
pip --version
pip install --upgrade opencv-python==4.5.4.58
pip install mediapipe
代码:
骨架检测:
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 = cap = cv2.VideoCapture(0) # 0表示第一个摄像头
#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()
指关节检测:
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(0)
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()
关节连线:
import cv2
import mediapipe as mp
import math
#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)
resize_w = 640 # 宽
resize_h = 480 # 高
#mp.solutions.hands,是人的手
mp_hands = mp.solutions.hands
#参数:1、是否检测静态图片,2、手的数量,3、检测阈值,4、跟踪阈值
hands_mode = mp_hands.Hands(max_num_hands=2)
cap = cv2.VideoCapture(0)
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)
# 解析手指,存入各个手指坐标
landmark_list = []#初始化一个列表来存储
for landmark_id, finger_axis in enumerate(
hand_landmarks.landmark):#便利某个手的每个关节
landmark_list.append([
landmark_id, finger_axis.x, finger_axis.y,
finger_axis.z
])#将手指序号,像素点横、纵、深度坐标打包为一个列表,共同存入列表中
if landmark_list:
# 获取大拇指指尖坐标,序号为4
thumb_finger_tip = landmark_list[4]
# 向上取整,得到手指坐标的整数
thumb_finger_tip_x = math.ceil(thumb_finger_tip[1] * resize_w)#thumb_finger_tip[1]里存储的x值范围是0-1,乘以分辨率宽,便得到在图像上的位置
thumb_finger_tip_y = math.ceil(thumb_finger_tip[2] * resize_h) #thumb_finger_tip[2]里存储的x值范围是0-1,乘以分辨率高,便得到在图像上的位置
# 获取食指指尖坐标,序号为4,操作同理
index_finger_tip = landmark_list[8]
index_finger_tip_x = math.ceil(index_finger_tip[1] * resize_w)
index_finger_tip_y = math.ceil(index_finger_tip[2] * resize_h)
# 得到食指和拇指的中间点
finger_middle_point = (thumb_finger_tip_x + index_finger_tip_x) // 2, (
thumb_finger_tip_y + index_finger_tip_y) // 2
# print(thumb_finger_tip_x)
thumb_finger_point = (thumb_finger_tip_x, thumb_finger_tip_y)
index_finger_point = (index_finger_tip_x, index_finger_tip_y)
# 用opencv的circle函数画图,将食指、拇指和中间点画出
image = cv2.circle(image, thumb_finger_point, 10, (255, 0, 255), -1)
image = cv2.circle(image, index_finger_point, 10, (255, 0, 255), -1)
image = cv2.circle(image, finger_middle_point, 10, (255, 0, 255), -1)
# 用opencv的line函数将食指和拇指连接在一起
image = cv2.line(image, thumb_finger_point, index_finger_point, (255, 0, 255), 5)
# math.hypot为勾股定理计算两点长度的函数,得到食指和拇指的距离
line_len = math.hypot((index_finger_tip_x - thumb_finger_tip_x),
(index_finger_tip_y - thumb_finger_tip_y))
cv2.imshow('MediaPipe Hands',image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
hands_mode.close()
cv2.destroyAllWindows()
cap.release()
节点序号:
脸部检测:
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(0)
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.FACEMESH_TESSELATION, 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()