'How to plot a marker around eye region according to face landmarks of mediapipe Python
The question is how to find the eye coordinate using the multipipe library and place a marker at the left,right, top, and bottom of each of the eyes using cv2 library. as shown in the image below.
One of alternate suggestion is given below. I save this as my and other future reference
This is the continuation from this OP
Image taken from this link
Solution 1:[1]
This can be achieve via mp.solutions.face_mesh.FaceMesh
First get the face bb
results = face_mesh.process(cv2.cvtColor(image_input , cv2.COLOR_BGR2RGB))
Then for each eye region of interest, get the pixel position using the _normalized_to_pixel_coordinates
eye_r = _normalized_to_pixel_coordinates()
The full code is as below
import cv2
import mediapipe as mp
from mediapipe.python.solutions.drawing_utils import _normalized_to_pixel_coordinates
dframe = cv2.imread("detect_face/person.png")
image_input = cv2.cvtColor(dframe, cv2.COLOR_BGR2RGB)
face_mesh = mp.solutions.face_mesh.FaceMesh(static_image_mode=True, max_num_faces=2,
min_detection_confidence=0.5)
image_rows, image_cols, _ = dframe.shape
results = face_mesh.process(cv2.cvtColor(image_input , cv2.COLOR_BGR2RGB))
fl=results.multi_face_landmarks[0]
##
for txt,cor in zip(['L','R','T','B'],[263, 362,386,374]):
eye_r = _normalized_to_pixel_coordinates(fl.landmark[cor].x,fl.landmark[cor].y,
image_cols,image_rows)
cv2.putText(image_input, txt, eye_r,cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 2)
for txt,cor in zip(['l','r','t','b'],[133,33,159,145]):
eye_r = _normalized_to_pixel_coordinates(fl.landmark[cor].x,fl.landmark[cor].y,
image_cols,image_rows)
cv2.putText(image_input, txt, eye_r,cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 2)
cv2.imwrite('test.png', image_input)
Which output
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rpb |


