mediapipe关键点获取图片中手部、嘴巴、眼睛的截图并显示的代码
时间: 2023-07-10 21:23:37 浏览: 144
Python手势识别代码 基于MediaPipe手部关键点检测 识别数字手势
5星 · 资源好评率100%
以下是使用MediaPipe获取手部、嘴巴、眼睛关键点并显示截图的Python代码:
```python
import cv2
import mediapipe as mp
# 初始化MediaPipe手部、嘴巴、眼睛模型
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
mp_face_mesh = mp.solutions.face_mesh
mp_pose = mp.solutions.pose
# 初始化OpenCV视频捕捉
cap = cv2.VideoCapture(0)
# 循环读取视频帧
while True:
ret, frame = cap.read()
if not ret:
break
# 将捕捉到的帧RGB颜色空间转换为BGR颜色空间
image = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# 在MediaPipe手部、嘴巴、眼睛模型上处理当前帧
with mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
with mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5) as face_mesh:
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
# 将当前帧BGR颜色空间转换为RGB颜色空间
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 在当前帧中检测手部关键点
results_hands = hands.process(image)
# 在当前帧中检测嘴巴关键点
results_face_mesh = face_mesh.process(image)
# 在当前帧中检测眼睛关键点
results_pose = pose.process(image)
# 将当前帧RGB颜色空间转换为BGR颜色空间
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 绘制手部关键点
if results_hands.multi_hand_landmarks:
for hand_landmarks in results_hands.multi_hand_landmarks:
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 绘制嘴巴关键点
if results_face_mesh.multi_face_landmarks:
for face_landmarks in results_face_mesh.multi_face_landmarks:
mp_drawing.draw_landmarks(image, face_landmarks, mp_face_mesh.FACEMESH_CONTOURS)
# 绘制眼睛关键点
if results_pose.pose_landmarks:
mp_drawing.draw_landmarks(image, results_pose.pose_landmarks, mp_pose.POSE_CONNECTIONS)
# 显示当前帧
cv2.imshow('MediaPipe Hand, Face and Eye Detection', image)
# 按下q键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 停止视频捕捉、释放资源和关闭所有窗口
cap.release()
cv2.destroyAllWindows()
```
这段代码可以实时显示捕捉到的视频,并在视频中绘制手部、嘴巴、眼睛关键点。你可以根据自己的需要修改和优化代码。
阅读全文