1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| import cv2 import mediapipe as mp import numpy as np
mp_face_mesh = mp.solutions.face_mesh face_mesh = mp_face_mesh.FaceMesh()
image = cv2.imread("faceinput.png") height, width, _ = image.shape rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
result = face_mesh.process(image)
left_eye = [46, 53, 52, 65, 55, 107, 66, 105, 63, 70]
right_eye = [276, 283, 282, 295, 285, 336, 296, 334, 293, 300]
left_pts = [[]] right_pts = [[]] for facial_landmarks in result.multi_face_landmarks: for i in range(0, 468): if i in right_eye or i in left_eye: pt1 = facial_landmarks.landmark[i] x = int(pt1.x * width) y = int(pt1.y * height)
cv2.circle(image, (x,y), 2, (100,100,0), -1) for i in left_eye: pt1 = facial_landmarks.landmark[i] x = int(pt1.x * width) y = int(pt1.y * height) left_pts[0].append([x, y])
for i in right_eye: pt1 = facial_landmarks.landmark[i] x = int(pt1.x * width) y = int(pt1.y * height) right_pts[0].append([x, y])
grayscaleImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) grayscaleImage = cv2.cvtColor(grayscaleImage, cv2.COLOR_GRAY2BGR) maskImage = np.zeros_like(image) cv2.drawContours(maskImage, np.array(left_pts), 0, (255, 255, 255), -1) cv2.drawContours(maskImage, np.array(right_pts), 0, (255, 255, 255), -1) extractedImage=np.bitwise_and(grayscaleImage, maskImage)
invert_maskImage = np.invert(maskImage) other = cv2.imread("face2.png")
extractedOther = np.bitwise_and(other, invert_maskImage)
res = np.bitwise_or(extractedImage, extractedOther)
cv2.imshow("Face Mesh detection", extractedImage) cv2.waitKey(0)
|