通过mediapipe获取摄像头识别手部、嘴巴、眼睛并分别截取三个区域的图片的代码
时间: 2023-08-07 12:02:30 浏览: 457
以下是使用mediapipe获取摄像头识别手部、嘴巴、眼睛并分别截取三个区域的Python代码:
```python
import cv2
import mediapipe as mp
# 初始化mediapipe的Hand, Face和Holistic模型
mp_hands = mp.solutions.hands
mp_face = mp.solutions.face_detection
mp_holistic = mp.solutions.holistic
# 初始化mediapipe的DrawingUtils模块
mp_drawing = mp.solutions.drawing_utils
# 初始化摄像头
cap = cv2.VideoCapture(0)
# 设置截取区域的大小和位置
hand_area = (100, 100, 300, 300) # (x, y, w, h)
face_area = (100, 100, 300, 300) # (x, y, w, h)
eye_area = (100, 100, 300, 300) # (x, y, w, h)
# 用于标记是否检测到手部、脸部和眼睛
hand_detected = False
face_detected = False
eye_detected = False
# 初始化Hand、Face和Holistic模型
with mp_hands.Hands() as hands, mp_face.FaceDetection() as face_detection, mp_holistic.Holistic() as holistic:
while True:
# 从摄像头读取一帧图像
ret, image = cap.read()
if not ret:
break
# 将图像转换为RGB格式
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 检测手部
results_hands = hands.process(image)
if results_hands.multi_hand_landmarks:
# 获取手部关键点坐标
hand_landmarks = results_hands.multi_hand_landmarks[0]
# 绘制手部关键点
mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 截取手部区域图片
x, y, w, h = hand_area
hand_image = image[y:y+h, x:x+w]
hand_detected = True
# 检测脸部
results_face = face_detection.process(image)
if results_face.detections:
# 获取脸部关键点坐标
face_landmarks = results_face.detections[0].location_data.relative_bounding_box
# 绘制脸部矩形框
h, w, _ = image.shape
x, y, w, h = int(face_landmarks.xmin * w), int(face_landmarks.ymin * h), int(face_landmarks.width * w), int(face_landmarks.height * h)
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 截取脸部区域图片
x, y, w, h = face_area
face_image = image[y:y+h, x:x+w]
face_detected = True
# 检测眼睛
results_holistic = holistic.process(image)
if results_holistic.face_landmarks:
# 获取眼睛关键点坐标
left_eye = results_holistic.face_landmarks.landmark[mp_holistic.FaceLandmark.LEFT_EYE]
right_eye = results_holistic.face_landmarks.landmark[mp_holistic.FaceLandmark.RIGHT_EYE]
# 绘制眼睛关键点
mp_drawing.draw_landmarks(image, [left_eye, right_eye], mp_holistic.FACE_CONNECTIONS)
# 截取眼睛区域图片
x, y, w, h = eye_area
eye_image = image[y:y+h, x:x+w]
eye_detected = True
# 显示图像
cv2.imshow('MediaPipe', image)
# 检查是否已经检测到手部、脸部和眼睛,如果是则退出循环
if hand_detected and face_detected and eye_detected:
break
# 按下q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头并关闭所有窗口
cap.release()
cv2.destroyAllWindows()
```
在上面的代码中,我们使用mediapipe模块的Hand、Face和Holistic模型来检测手部、脸部和眼睛,并使用mediapipe的DrawingUtils模块绘制关键点和矩形框。我们还使用OpenCV的cv2模块从摄像头读取图像,并使用cv2.imshow()函数显示图像。最后,我们设置了三个截取区域hand_area、face_area和eye_area,用于截取手部、脸部和眼睛区域的图片。截取的图片分别保存在hand_image、face_image和eye_image变量中。
阅读全文