This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# IMPORTING LIBRARIES | |
import cv2 | |
import mediapipe as mp | |
# INITIALIZING OBJECTS | |
mp_drawing = mp.solutions.drawing_utils | |
mp_drawing_styles = mp.solutions.drawing_styles | |
mp_face_mesh = mp.solutions.face_mesh | |
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while True: | |
# Capture the image from the webcam | |
ret, image = cap.read() | |
# Convert the image color to grayscale | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
# Detect the face | |
rects = detector(gray, 1) | |
# Detect landmarks for each face | |
for rect in rects: | |
# Get the landmark points |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
detector = dlib.get_frontal_face_detector() | |
predictor = dlib.shape_predictor('../shape_predictor_68_face_landmarks.dat') | |
cap = cv2.VideoCapture(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import dlib | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the y rotation degree | |
x = angles[0] * 360 | |
y = angles[1] * 360 | |
# print(y) | |
# See where the user's head tilting | |
if y < -10: | |
text = "Looking Left" | |
elif y > 10: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get angles | |
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get rotational matrix | |
rmat, jac = cv2.Rodrigues(rot_vec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solve PnP | |
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Distance Matrix | |
dist_matrix = np.zeros((4, 1), dtype=np.float64) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The camera matrix | |
focal_length = 1 * img_w | |
cam_matrix = np.array([ [focal_length, 0, img_h / 2], | |
[0, focal_length, img_w / 2], | |
[0, 0, 1]]) |
NewerOlder