Skip to content

Instantly share code, notes, and snippets.

View khalidmeister's full-sized avatar

Irfan Alghani Khalid khalidmeister

View GitHub Profile
# 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)
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
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('../shape_predictor_68_face_landmarks.dat')
cap = cv2.VideoCapture(0)
import cv2
import dlib
import numpy as np
# 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:
# Get angles
angles, mtxR, mtxQ, Qx, Qy, Qz = cv2.RQDecomp3x3(rmat)
# Get rotational matrix
rmat, jac = cv2.Rodrigues(rot_vec)
# Solve PnP
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
# The Distance Matrix
dist_matrix = np.zeros((4, 1), dtype=np.float64)
# 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]])