Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions eyeGestures/eye.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
class Eye:
"""Class storing data related and representing a eye"""

LEFT_EYE_KEYPOINTS = np.array(list(mp.solutions.face_mesh.FACEMESH_LEFT_EYE))[:, 0]
RIGHT_EYE_KEYPOINTS = np.array(list(mp.solutions.face_mesh.FACEMESH_RIGHT_EYE))[:, 0]
LEFT_EYE_KEYPOINTS = np.array([
33, 7, 163, 144, 145, 153, 154, 155, 133, 246, 161, 160, 159, 158, 157, 173
])
RIGHT_EYE_KEYPOINTS = np.array([
263, 249, 390, 373, 374, 380, 381, 382, 362, 398, 384, 385, 386, 387, 388, 466
])
LEFT_EYE_PUPIL_KEYPOINT = [473]
RIGHT_EYE_PUPIL_KEYPOINT = [468]

Expand Down
34 changes: 24 additions & 10 deletions eyeGestures/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import cv2
import mediapipe as mp

from mediapipe.tasks import python
from mediapipe.tasks.python import vision
from mediapipe import Image, ImageFormat

import numpy as np
import numpy.typing as npt

Expand All @@ -14,21 +19,31 @@ class FaceFinder:
"""Class helping finding face"""

def __init__(self) -> None:
self.mp_face_mesh = mp.solutions.face_mesh.FaceMesh(
refine_landmarks=True,
static_image_mode=False,
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
BaseOptions = python.BaseOptions
FaceLandmarker = vision.FaceLandmarker
FaceLandmarkerOptions = vision.FaceLandmarkerOptions
VisionRunningMode = vision.RunningMode
options = FaceLandmarkerOptions(
base_options = BaseOptions(
model_asset_path = r"C:\Users\vvams\PycharmProjects\EyeGestures\eyeGestures\task_model\face_landmarker.task"
),
running_mode = VisionRunningMode.IMAGE,
num_faces = 1,
min_face_detection_confidence=0.5,
min_tracking_confidence=0.5
)
self.mp_face_mesh = FaceLandmarker.create_from_options(options)

def find(self, image: cv2.typing.MatLike) -> Optional[Any]:
"""Find face mesh"""

assert len(image.shape) > 2

try:
face_mesh = self.mp_face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if face_mesh.multi_face_landmarks is None:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mp_image = Image(image_format = ImageFormat.SRGB, data = image_rgb)
face_mesh = self.mp_face_mesh.detect(mp_image)
if face_mesh.face_landmarks is None:
return None

return face_mesh
Expand Down Expand Up @@ -78,11 +93,10 @@ def get_landmarks(self) -> Optional[npt.NDArray[np.float64]]:

def _landmarks(self, face: Any) -> npt.NDArray[np.float64]:

__complex_landmark_points = face.multi_face_landmarks
__complex_landmarks = __complex_landmark_points[0].landmark
__complex_landmark_points = face.face_landmarks[0]

__face_landmarks = []
for landmark in __complex_landmarks:
for landmark in __complex_landmark_points:
__face_landmarks.append((landmark.x * self.image_w, landmark.y * self.image_h))

return np.array(__face_landmarks)
Expand Down
2 changes: 1 addition & 1 deletion eyeGestures/gazeEstimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def estimate(
if not face_mesh:
return None

if face_mesh.multi_face_landmarks:
if face_mesh.face_landmarks:
self.face.process(image, face_mesh)

context = self.GContext.get(
Expand Down
4 changes: 2 additions & 2 deletions eyeGestures/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def append(self, pupil: Tuple[int, int], landmarks: npt.NDArray[np.float64], pup
self.min_y = np.min(self.landmarks[:, 1]) - margin
self.max_y = np.max(self.landmarks[:, 1]) + margin

assert self.pupil[0] > self.min_x
assert self.pupil[1] > self.min_y
# assert self.pupil[0] > self.min_x
# assert self.pupil[1] > self.min_y

width = self.max_x - self.min_x
height = (self.max_y - self.min_y) / 2
Expand Down
Binary file added eyeGestures/task_model/face_landmarker.task
Binary file not shown.