How to plot a marker around eye region according to face landmarks of mediapipe Python
Asked Answered
A

1

0

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.

enter image description here

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

Ardisardisj answered 14/5, 2022 at 10:43 Comment(0)
A
0

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

enter image description here

Ardisardisj answered 14/5, 2022 at 10:43 Comment(1)
To plot marker for all the face landmark > https://mcmap.net/q/723679/-python-how-to-get-face-mesh-landmarks-coordinates-in-mediapipeArdisardisj

© 2022 - 2025 — McMap. All rights reserved.