Advertisement
RushFuture

Untitled

Mar 25th, 2024
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.23 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''
  5. Источник: https://developers.google.com/mediapipe/solutions/vision/face_landmarker/python
  6.  
  7. Установка пререквизитов:
  8. wget -O face_landmarker_v2_with_blendshapes.task -q https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task
  9. pip install mediapipe
  10. '''
  11.  
  12. from mediapipe import solutions
  13. from mediapipe.framework.formats import landmark_pb2
  14. import numpy as np
  15. import matplotlib.pyplot as plt
  16. import mediapipe as mp
  17. from mediapipe.tasks import python
  18. from mediapipe.tasks.python import vision
  19. import cv2
  20.  
  21. def draw_landmarks_on_image(rgb_image, detection_result):
  22.   face_landmarks_list = detection_result.face_landmarks
  23.   annotated_image = np.copy(rgb_image)
  24.  
  25.   # Loop through the detected faces to visualize.
  26.   for idx in range(len(face_landmarks_list)):
  27.     face_landmarks = face_landmarks_list[idx]
  28.  
  29.     # Draw the face landmarks.
  30.     face_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
  31.     face_landmarks_proto.landmark.extend([
  32.       landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in face_landmarks
  33.     ])
  34.  
  35.     solutions.drawing_utils.draw_landmarks(
  36.         image=annotated_image,
  37.         landmark_list=face_landmarks_proto,
  38.         connections=mp.solutions.face_mesh.FACEMESH_TESSELATION,
  39.         landmark_drawing_spec=None,
  40.         connection_drawing_spec=mp.solutions.drawing_styles
  41.         .get_default_face_mesh_tesselation_style())
  42.     solutions.drawing_utils.draw_landmarks(
  43.         image=annotated_image,
  44.         landmark_list=face_landmarks_proto,
  45.         connections=mp.solutions.face_mesh.FACEMESH_CONTOURS,
  46.         landmark_drawing_spec=None,
  47.         connection_drawing_spec=mp.solutions.drawing_styles
  48.         .get_default_face_mesh_contours_style())
  49.     solutions.drawing_utils.draw_landmarks(
  50.         image=annotated_image,
  51.         landmark_list=face_landmarks_proto,
  52.         connections=mp.solutions.face_mesh.FACEMESH_IRISES,
  53.           landmark_drawing_spec=None,
  54.           connection_drawing_spec=mp.solutions.drawing_styles
  55.           .get_default_face_mesh_iris_connections_style())
  56.  
  57.   return annotated_image
  58.  
  59. def plot_face_blendshapes_bar_graph(face_blendshapes):
  60.   # Extract the face blendshapes category names and scores.
  61.   face_blendshapes_names = [face_blendshapes_category.category_name for face_blendshapes_category in face_blendshapes]
  62.   face_blendshapes_scores = [face_blendshapes_category.score for face_blendshapes_category in face_blendshapes]
  63.   # The blendshapes are ordered in decreasing score value.
  64.   face_blendshapes_ranks = range(len(face_blendshapes_names))
  65.  
  66.   fig, ax = plt.subplots(figsize=(12, 12))
  67.   bar = ax.barh(face_blendshapes_ranks, face_blendshapes_scores, label=[str(x) for x in face_blendshapes_ranks])
  68.   ax.set_yticks(face_blendshapes_ranks, face_blendshapes_names)
  69.   ax.invert_yaxis()
  70.  
  71.   # Label each bar with values
  72.   for score, patch in zip(face_blendshapes_scores, bar.patches):
  73.     plt.text(patch.get_x() + patch.get_width(), patch.get_y(), f"{score:.4f}", va="top")
  74.  
  75.   ax.set_xlabel('Score')
  76.   ax.set_title("Face Blendshapes")
  77.   plt.tight_layout()
  78.   plt.show()
  79.  
  80. base_options = python.BaseOptions(model_asset_path='face_landmarker_v2_with_blendshapes.task')
  81. options = vision.FaceLandmarkerOptions(base_options=base_options,
  82.                                        output_face_blendshapes=True,
  83.                                        output_facial_transformation_matrixes=True,
  84.                                        num_faces=1)
  85. detector = vision.FaceLandmarker.create_from_options(options)
  86.  
  87.  
  88. cap = cv2.VideoCapture(0)
  89. cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
  90. cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
  91. cap.set(cv2.CAP_PROP_FPS, 30)
  92. cap.set(cv2.CAP_PROP_FORMAT, -1)
  93.  
  94. while(cap.isOpened()):
  95.     ret, frame = cap.read()
  96.     if ret!=True: continue
  97.  
  98.     image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
  99.     detection_result = detector.detect(image)
  100.     annotated_image = draw_landmarks_on_image(image.numpy_view(), detection_result)
  101.     cv2.imshow('annotated', cv2.flip(annotated_image, 1))
  102.     if cv2.waitKey(1) & 0xFF == ord('q'): break
  103.  
  104. cap.release()
  105. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement