Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- '''
- Источник: https://developers.google.com/mediapipe/solutions/vision/face_landmarker/python
- Установка пререквизитов:
- wget -O face_landmarker_v2_with_blendshapes.task -q https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task
- pip install mediapipe
- '''
- from mediapipe import solutions
- from mediapipe.framework.formats import landmark_pb2
- import numpy as np
- import matplotlib.pyplot as plt
- import mediapipe as mp
- from mediapipe.tasks import python
- from mediapipe.tasks.python import vision
- import cv2
- def draw_landmarks_on_image(rgb_image, detection_result):
- face_landmarks_list = detection_result.face_landmarks
- annotated_image = np.copy(rgb_image)
- # Loop through the detected faces to visualize.
- for idx in range(len(face_landmarks_list)):
- face_landmarks = face_landmarks_list[idx]
- # Draw the face landmarks.
- face_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
- face_landmarks_proto.landmark.extend([
- landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in face_landmarks
- ])
- solutions.drawing_utils.draw_landmarks(
- image=annotated_image,
- landmark_list=face_landmarks_proto,
- connections=mp.solutions.face_mesh.FACEMESH_TESSELATION,
- landmark_drawing_spec=None,
- connection_drawing_spec=mp.solutions.drawing_styles
- .get_default_face_mesh_tesselation_style())
- solutions.drawing_utils.draw_landmarks(
- image=annotated_image,
- landmark_list=face_landmarks_proto,
- connections=mp.solutions.face_mesh.FACEMESH_CONTOURS,
- landmark_drawing_spec=None,
- connection_drawing_spec=mp.solutions.drawing_styles
- .get_default_face_mesh_contours_style())
- solutions.drawing_utils.draw_landmarks(
- image=annotated_image,
- landmark_list=face_landmarks_proto,
- connections=mp.solutions.face_mesh.FACEMESH_IRISES,
- landmark_drawing_spec=None,
- connection_drawing_spec=mp.solutions.drawing_styles
- .get_default_face_mesh_iris_connections_style())
- return annotated_image
- def plot_face_blendshapes_bar_graph(face_blendshapes):
- # Extract the face blendshapes category names and scores.
- face_blendshapes_names = [face_blendshapes_category.category_name for face_blendshapes_category in face_blendshapes]
- face_blendshapes_scores = [face_blendshapes_category.score for face_blendshapes_category in face_blendshapes]
- # The blendshapes are ordered in decreasing score value.
- face_blendshapes_ranks = range(len(face_blendshapes_names))
- fig, ax = plt.subplots(figsize=(12, 12))
- bar = ax.barh(face_blendshapes_ranks, face_blendshapes_scores, label=[str(x) for x in face_blendshapes_ranks])
- ax.set_yticks(face_blendshapes_ranks, face_blendshapes_names)
- ax.invert_yaxis()
- # Label each bar with values
- for score, patch in zip(face_blendshapes_scores, bar.patches):
- plt.text(patch.get_x() + patch.get_width(), patch.get_y(), f"{score:.4f}", va="top")
- ax.set_xlabel('Score')
- ax.set_title("Face Blendshapes")
- plt.tight_layout()
- plt.show()
- base_options = python.BaseOptions(model_asset_path='face_landmarker_v2_with_blendshapes.task')
- options = vision.FaceLandmarkerOptions(base_options=base_options,
- output_face_blendshapes=True,
- output_facial_transformation_matrixes=True,
- num_faces=1)
- detector = vision.FaceLandmarker.create_from_options(options)
- cap = cv2.VideoCapture(0)
- cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
- cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
- cap.set(cv2.CAP_PROP_FPS, 30)
- cap.set(cv2.CAP_PROP_FORMAT, -1)
- while(cap.isOpened()):
- ret, frame = cap.read()
- if ret!=True: continue
- image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
- detection_result = detector.detect(image)
- annotated_image = draw_landmarks_on_image(image.numpy_view(), detection_result)
- cv2.imshow('annotated', cv2.flip(annotated_image, 1))
- if cv2.waitKey(1) & 0xFF == ord('q'): break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement