facemesh_test.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import cv2
  2. import mediapipe as mp
  3. mp_drawing = mp.solutions.drawing_utils
  4. mp_drawing_styles = mp.solutions.drawing_styles
  5. mp_face_mesh = mp.solutions.face_mesh
  6. keypnt_68 = [127, 93, 58, 136, 150, 149, 176, 148, 152, 377, 400, 378, 379, 365, 288, 323, 356, 70, 63, 105, 66, 55,
  7. 285, 296, 334, 293, 300, 168, 6, 195, 4, 64, 60, 94, 290, 439, 33, 160, 158, 173, 153, 144, 398, 385, 387,
  8. 466, 373, 380, 61, 40, 39, 0, 269, 270, 291, 321, 405, 17, 181, 91, 78, 81, 13, 311, 306, 402, 14, 178,
  9. 162, 54, 67, 10, 297, 284, 389]
  10. # For static images:
  11. IMAGE_FILES = []
  12. drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
  13. with mp_face_mesh.FaceMesh(
  14. static_image_mode=True,
  15. max_num_faces=1,
  16. refine_landmarks=True,
  17. min_detection_confidence=0.5) as face_mesh:
  18. for idx, file in enumerate(IMAGE_FILES):
  19. image = cv2.imread(file)
  20. # Convert the BGR image to RGB before processing.
  21. results = face_mesh.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  22. # Print and draw face mesh landmarks on the image.
  23. if not results.multi_face_landmarks:
  24. continue
  25. annotated_image = image.copy()
  26. for face_landmarks in results.multi_face_landmarks:
  27. print('face_landmarks:', face_landmarks)
  28. mp_drawing.draw_landmarks(
  29. image=annotated_image,
  30. landmark_list=face_landmarks,
  31. connections=mp_face_mesh.FACEMESH_TESSELATION,
  32. landmark_drawing_spec=None,
  33. connection_drawing_spec=mp_drawing_styles
  34. .get_default_face_mesh_tesselation_style())
  35. mp_drawing.draw_landmarks(
  36. image=annotated_image,
  37. landmark_list=face_landmarks,
  38. connections=mp_face_mesh.FACEMESH_CONTOURS,
  39. landmark_drawing_spec=None,
  40. connection_drawing_spec=mp_drawing_styles
  41. .get_default_face_mesh_contours_style())
  42. mp_drawing.draw_landmarks(
  43. image=annotated_image,
  44. landmark_list=face_landmarks,
  45. connections=mp_face_mesh.FACEMESH_IRISES,
  46. landmark_drawing_spec=None,
  47. connection_drawing_spec=mp_drawing_styles
  48. .get_default_face_mesh_iris_connections_style())
  49. cv2.imwrite('/tmp/annotated_image' + str(idx) + '.png', annotated_image)
  50. # For webcam input:
  51. drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
  52. cap = cv2.VideoCapture(0)
  53. with mp_face_mesh.FaceMesh(
  54. max_num_faces=1,
  55. refine_landmarks=True,
  56. min_detection_confidence=0.5,
  57. min_tracking_confidence=0.5) as face_mesh:
  58. while cap.isOpened():
  59. success, image = cap.read()
  60. if not success:
  61. print("Ignoring empty camera frame.")
  62. # If loading a video, use 'break' instead of 'continue'.
  63. continue
  64. # To improve performance, optionally mark the image as not writeable to
  65. # pass by reference.
  66. image.flags.writeable = False
  67. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  68. results = face_mesh.process(image)
  69. # Draw the face mesh annotations on the image.
  70. image.flags.writeable = True
  71. image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  72. # image1 = image.copy()
  73. # image2 = image.copy()
  74. if results.multi_face_landmarks:
  75. contour_point_idxs = []
  76. for point in mp_face_mesh.FACEMESH_CONTOURS:
  77. contour_point_idxs.append(point[0])
  78. for face_landmarks in results.multi_face_landmarks:
  79. for idx, landmark in enumerate(face_landmarks.landmark):
  80. # if idx in contour_point_idxs:
  81. if idx in keypnt_68:
  82. # if True:
  83. height, width = image.shape[:2]
  84. point = (int(landmark.x * width), int(landmark.y * height))
  85. cv2.circle(image, point, 1, (255, 0, 0), -1)
  86. cv2.putText(image, str(idx), point, cv2.FONT_HERSHEY_SIMPLEX, .3, (255, 255, 255), 1)
  87. # if idx % 2 == 0:
  88. # cv2.circle(image2, point, 1, (255, 0, 0), -1)
  89. # cv2.putText(image2, str(idx), point, cv2.FONT_HERSHEY_SIMPLEX, .3, (255, 255, 255), 1)
  90. # elif (idx+1) % 2 == 0:
  91. # cv2.circle(image1, point, 1, (255, 0, 0), -1)
  92. # cv2.putText(image1, str(idx), point, cv2.FONT_HERSHEY_SIMPLEX, .3, (255, 255, 255), 1)
  93. # Flip the image horizontally for a selfie-view display.
  94. # cv2.imshow('MediaPipe Face Mesh', cv2.flip(image, 1))
  95. cv2.imshow('MediaPipe Face Mesh', image)
  96. # cv2.imshow('image2', image2)
  97. # cv2.imshow('image1', image1)
  98. if cv2.waitKey(5) & 0xFF == 27:
  99. break
  100. cap.release()