MediaPipe来判断脸部朝向

使用MediaPipe来判断脸部朝向(向左或向右)

在正面朝向时,两眼的中心点位于水平线上,因此两眼的连线近似与水平线平行。
当脸部朝左侧时,右眼相对于左眼会有一定的水平偏移,两眼的连线会略微倾斜向左。
当脸部朝右侧时,左眼相对于右眼会有一定的水平偏移,两眼的连线会略微倾斜向右

import cv2
import mediapipe as mp
import math

mp_face_detection = mp.solutions.face_detection
mp_face_mesh = mp.solutions.face_mesh
mp_drawing = mp.solutions.drawing_utils

cap = cv2.VideoCapture(0)

def calculate_eye_angle(left_eye, right_eye):
    # 计算左右眼之间的角度
    eye_angle = math.degrees(math.atan2(right_eye.y - left_eye.y, right_eye.x - left_eye.x))
    return eye_angle

def get_face_direction(landmarks, iw):
    left_eye = landmarks.landmark[159]  # 左眼最外侧点
    right_eye = landmarks.landmark[386]  # 右眼最外侧点

    eye_angle = calculate_eye_angle(left_eye, right_eye)

    if abs(eye_angle) <= 15:
        return "向前"  
    elif eye_angle < 0:
        return "向左"
    else:
        return "向右"

def detect_face_direction(frame, face_detection):
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = face_detection.process(rgb_frame)

    if results.detections:
        for detection in results.detections:
            bboxC = detection.location_data.relative_bounding_box
            ih, iw, _ = frame.shape
            x, y, w, h = int(bboxC.xmin * iw), int(bboxC.ymin * ih), \
                         int(bboxC.width * iw), int(bboxC.height * ih)

            results_mesh = mp_face_mesh.FaceMesh(min_detection_confidence=0.5, min_tracking_confidence=0.5).process(rgb_frame)

            if results_mesh.multi_face_landmarks:
                landmarks = results_mesh.multi_face_landmarks[0]
                direction = get_face_direction(landmarks, iw)
                cv2.putText(frame, f"脸部朝向:{direction}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

    return frame

def main():
    with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                continue

            frame_with_direction = detect_face_direction(frame, face_detection)

            cv2.imshow('脸部朝向', frame_with_direction)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

if __name__ == "__main__":
    main()

cap.release()
cv2.destroyAllWindows()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于dlib计算面部朝向的示例,您可以参考以下代码: ``` import dlib import cv2 import numpy as np # 创建一个人脸检测器和一个面部朝向估计器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # 加载图片 img = cv2.imread('test.jpg') # 缩放图片 img = cv2.resize(img, (600, 600)) # 转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 识别脸部 faces = detector(gray) for face in faces: # 获取面部朝向估计器 landmarks = predictor(gray, face) # 将面部朝向转换为3D姿势 image_points = np.array([ (landmarks.part(30).x, landmarks.part(30).y), # 鼻子 (landmarks.part(8).x, landmarks.part(8).y), # 下巴 (landmarks.part(36).x, landmarks.part(36).y), # 左眼角 (landmarks.part(45).x, landmarks.part(45).y), # 右眼角 (landmarks.part(48).x, landmarks.part(48).y), # 左嘴角 (landmarks.part(54).x, landmarks.part(54).y) # 右嘴角 ], dtype="double") model_points = np.array([ (0.0, 0.0, 0.0), # 鼻子 (0.0, -330.0, -65.0), # 下巴 (-225.0, 170.0, -135.0), # 左眼角 (225.0, 170.0, -135.0), # 右眼角 (-150.0, -150.0, -125.0), # 左嘴角 (150.0, -150.0, -125.0) # 右嘴角 ], dtype="double") focal_length = img.shape[1] center = (img.shape[1]/2, img.shape[0]/2) camera_matrix = np.array( [[focal_length, 0, center[0]], [0, focal_length, center[1]], [0, 0, 1]], dtype = "double" ) dist_coeffs = np.zeros((4,1)) # 无畸变系数 (success, rotation_vector, translation_vector) = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs) # 四元数角度解码 rvec_matrix = cv2.Rodrigues(rotation_vector)[0] proj_matrix = np.hstack((rvec_matrix, translation_vector)) euler_angles = cv2.decomposeProjectionMatrix(proj_matrix)[6] pitch, yaw, roll = [np.rad2deg(angle) for angle in euler_angles] # 绘制面部朝向轴 p1 = (int(image_points[0][0]), int(image_points[0][1])) p2 = (int(image_points[0][0] + 200 * np.cos(np.radians(yaw))), int(image_points[0][1] - 200 * np.sin(np.radians(yaw)))) cv2.line(img, p1, p2, (0, 255, 0), 2) # 显示图像并等待按键 cv2.imshow("Output", img) cv2.waitKey(0) ``` 此代码可以将面部朝向轴绘制在输入图像上,并输出结果。注意,此代码需要下载才能使用dlib的数据文件shape_predictor_68_face_landmarks.dat。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值