How to label eyes detected using mediapipe
Asked Answered
O

1

-1

How to label the person eye for a face detected via mp_face.process() of the mediapipe ?

I expect the output to be as below enter image description here

Image taken from this link

One of alternate suggestion is given below. I save this as my and other future reference

Overtime answered 12/5, 2022 at 6:47 Comment(1)
I post this as question, and having the answer below. Please dont down vote if for issue of lack of clarity!Overtime
O
0

To label the eye, first we need to find the left-right eye coordinate using

mp_face_detection.get_key_point()

Then, we need to get pixel coordinate of both eyes using

_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("person.png")

image_input = cv2.cvtColor(dframe, cv2.COLOR_BGR2RGB)
# load face detection model
mp_face = mp.solutions.face_detection.FaceDetection(
    model_selection=1,  # model selection
    min_detection_confidence=0.5  # confidence threshold
)
results = mp_face.process(image_input)

image_rows, image_cols, _ = dframe.shape
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils

detection = results.detections[0]

eye_left=mp_face_detection.get_key_point(detection, mp_face_detection.FaceKeyPoint.LEFT_EYE)
#
eye_right=mp_face_detection.get_key_point(detection, mp_face_detection.FaceKeyPoint.RIGHT_EYE)



eye_r = _normalized_to_pixel_coordinates(eye_right.x,eye_right.y, image_cols,image_rows)
eye_l = _normalized_to_pixel_coordinates(eye_left.x,eye_left.y, image_cols,image_rows)
#
cv2.putText(image_input, 'R', eye_r,cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.putText(image_input, 'L', eye_l,cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)


cv2.imwrite('this_output.png', image_input)

For list of facial keypoint

FACIAL_KEYPOINTS = mp.solutions.face_detection.FaceKeyPoint
Overtime answered 12/5, 2022 at 6:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.