from operator import rshift
import cv2 as cv
import numpy as np
import mediapipe as mp
mp_face_mesh = [Link].face_mesh
LEFT_EYE =[ 362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386,
385,384, 398 ]
# right eyes indices
RIGHT_EYE=[ 33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160,
161 , 246 ]
LEFT_IRIS = [474,475, 476, 477]
RIGHT_IRIS = [469, 470, 471, 472]
cap = [Link](0)
with mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
) as face_mesh:
while True:
ret, frame = [Link]()
if not ret:
break
frame = [Link](frame, 1)
rgb_frame = [Link](frame, cv.COLOR_BGR2RGB)
img_h, img_w = [Link][:2]
results = face_mesh.process(rgb_frame)
if results.multi_face_landmarks:
# print(results.multi_face_landmarks[0].landmark)
mesh_points=[Link]([[Link]([p.x, p.y], [img_w,
img_h]).astype(int) for p in results.multi_face_landmarks[0].landmark])
# print(mesh_points.shape)
# [Link](frame, [mesh_points[LEFT_IRIS]], True, (0,255,0), 1,
cv.LINE_AA)
# [Link](frame, [mesh_points[RIGHT_IRIS]], True, (0,255,0), 1,
cv.LINE_AA)
(l_cx, l_cy), l_radius = [Link](mesh_points[LEFT_IRIS])
(r_cx, r_cy), r_radius = [Link](mesh_points[RIGHT_IRIS])
center_left = [Link]([l_cx, l_cy], dtype=np.int32)
center_right = [Link]([r_cx, r_cy], dtype=np.int32)
[Link](frame, center_left, int(l_radius), (255,0,255), 1,
cv.LINE_AA)
[Link](frame, center_right, int(r_radius), (255,0,255), 1,
cv.LINE_AA)
[Link]('img', frame)
key = [Link](1)
if key ==ord('q'):
break
[Link]()
[Link]()
segmentation_mask.py
import cv2 as cv
import numpy as np
import mediapipe as mp
mp_face_mesh = [Link].face_mesh
# left eyes indices
LEFT_EYE =[ 362, 382, 381, 380, 374, 373, 390, 249, 263, 466, 388, 387, 386,
385,384, 398 ]
# right eyes indices
RIGHT_EYE=[ 33, 7, 163, 144, 145, 153, 154, 155, 133, 173, 157, 158, 159, 160,
161 , 246 ]
# irises Indices list
LEFT_IRIS = [474,475, 476, 477]
RIGHT_IRIS = [469, 470, 471, 472]
cap = [Link](0)
with mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
) as face_mesh:
while True:
ret, frame = [Link]()
if not ret:
break
frame = [Link](frame, 1)
rgb_frame = [Link](frame, cv.COLOR_BGR2RGB)
img_h, img_w = [Link][:2]
results = face_mesh.process(rgb_frame)
mask = [Link]((img_h, img_w), dtype=np.uint8)
if results.multi_face_landmarks:
# print((results.multi_face_landmarks[0]))
# [print(p.x, p.y, p.z ) for p in
results.multi_face_landmarks[0].landmark]
mesh_points=[Link]([[Link]([p.x, p.y], [img_w,
img_h]).astype(int)
for p in results.multi_face_landmarks[0].landmark])
# [Link](frame, [mesh_points[LEFT_IRIS]], True, (0,255,0), 1,
cv.LINE_AA)
# [Link](frame, [mesh_points[RIGHT_IRIS]], True, (0,255,0), 1,
cv.LINE_AA)
(l_cx, l_cy), l_radius = [Link](mesh_points[LEFT_IRIS])
(r_cx, r_cy), r_radius = [Link](mesh_points[RIGHT_IRIS])
center_left = [Link]([l_cx, l_cy], dtype=np.int32)
center_right = [Link]([r_cx, r_cy], dtype=np.int32)
[Link](frame, center_left, int(l_radius), (0,255,0), 2, cv.LINE_AA)
[Link](frame, center_right, int(r_radius), (0,255,0), 2, cv.LINE_AA)
# [Link](frame, center_left, 1, (0,255,0), -1, cv.LINE_AA)
# [Link](frame, center_right, 1, (0,255,0), -1, cv.LINE_AA)
# drawing on the mask
[Link](mask, center_left, int(l_radius), (255,255,255), -1,
cv.LINE_AA)
[Link](mask, center_right, int(r_radius), (255,255,255), -1,
cv.LINE_AA)
[Link]('Mask', mask)
[Link]('img', frame)
key = [Link](1)
if key ==ord('q'):
break
[Link]()
[Link]()