Python 用顔ランドマーク検出ガイド

MediaPipe フェイス ランドマーカー タスクを使用すると、顔のランドマークと顔の表情を検出できます。 使用できます。このタスクを使用して人間の顔の表情を識別できます 顔のフィルタやエフェクトを適用して、バーチャル アバターを作成できます。このタスクで使用するもの 単一の画像または連続的な画像でも処理できる 必要があります。このタスクでは、顔の 3 次元のランドマーク、ブレンドシェイプが出力されます。 スコア(顔の表情を表す係数)を使って、顔の微妙な リアルタイムでの表面処理、変換行列を使用して、 エフェクトのレンダリングに必要な変換の数を表します。

この手順で説明するコードサンプルは、 GitHub。 機能、モデル、構成オプションの詳細については、 概要をご覧ください。

サンプルコード

顔認識機能のサンプル コードでは、この実装方法を完全に実装できます。 タスクを示しています。このコードは、このタスクをテストして、 独自の顔のランドマークの作成から始めました。を表示、実行、および編集できます。 顔認識機能の例 コード できます。

Raspberry Pi 用に顔認識用マーカーを実装する場合は、 Raspberry Pi の例 アプリ

セットアップ

このセクションでは、開発環境をセットアップする主な手順と 顔認識地マーカーを使用するコード プロジェクトです。一般的な情報については、 MediaPipe タスクを使用するための開発環境の設定 プラットフォーム バージョンの要件については、 Python

<ph type="x-smartling-placeholder">

パッケージ

MediaPipe フェイス ランドマーカー タスクには、mediapipe PyPI パッケージが必要です。Compute Engine インスタンスに 次のように依存関係をインポートします。

$ python -m pip install mediapipe

インポート

顔のランドマーク タスクの関数にアクセスするには、次のクラスをインポートします。

import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision

モデル

MediaPipe フェイス ランドマーカー タスクには、これと互換性のあるトレーニング済みモデルが必要です タスクを実行します。顔認識ツールで使用可能なトレーニング済みモデルについて詳しくは、以下をご覧ください。 タスクの概要のモデル セクションをご覧ください。

モデルを選択してダウンロードし、ローカル ディレクトリに保存します。

model_path = '/absolute/path/to/face_landmarker.task'

BaseOptions オブジェクトの model_asset_path パラメータを使用して、 モデルを定義します。コード例については、次のセクションをご覧ください。

タスクを作成する

MediaPipe フェイス ランドマーカー タスクでは、create_from_options 関数を使用して タスクを実行します。create_from_options 関数は構成の値を受け入れます。 いくつかあります。構成オプションの詳細については、このモジュールの 構成オプション

次のコードは、このタスクをビルドして構成する方法を示しています。

これらのサンプルは、画像のタスク構成のバリエーションも示しています。 動画ファイル、ライブ ストリームです。

画像

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode

options = FaceLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.IMAGE)

with FaceLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

動画

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode

# Create a face landmarker instance with the video mode:
options = FaceLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.VIDEO)

with FaceLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

ライブ配信

import mediapipe as mp

BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
FaceLandmarkerResult = mp.tasks.vision.FaceLandmarkerResult
VisionRunningMode = mp.tasks.vision.RunningMode

# Create a face landmarker instance with the live stream mode:
def print_result(result: FaceLandmarkerResult, output_image: mp.Image, timestamp_ms: int):
    print('face landmarker result: {}'.format(result))

options = FaceLandmarkerOptions(
    base_options=BaseOptions(model_asset_path=model_path),
    running_mode=VisionRunningMode.LIVE_STREAM,
    result_callback=print_result)

with FaceLandmarker.create_from_options(options) as landmarker:
  # The landmarker is initialized. Use it here.
  # ...
    

画像で使用する顔認識地マーカーを作成する詳細な例については、以下をご覧ください。 コード 例をご覧ください。

構成オプション

このタスクには、Python アプリケーション用の次の構成オプションがあります。

オプション名 説明 値の範囲 デフォルト値
running_mode タスクの実行モードを設定します。3 つの モード:

IMAGE: 単一画像入力のモード。

VIDEO: 動画のデコードされたフレームのモード。

LIVE_STREAM: 入力のライブ配信のモード カメラからのデータなどです。このモードでは、resultListener は 結果を受け取るリスナーを設定するために呼び出されます。 使用できます。
{IMAGE, VIDEO, LIVE_STREAM} IMAGE
num_faces FaceLandmarker。平滑化は次の場合にのみ適用されます。 num_faces は 1 に設定されています。 Integer > 0 1
min_face_detection_confidence 顔検出に必要な最小信頼スコア 成功したとみなされます。 Float [0.0,1.0] 0.5
min_face_presence_confidence 顔存在の最小信頼スコア 顔のランドマーク検出のスコアが出力されます。 Float [0.0,1.0] 0.5
min_tracking_confidence 顔トラッキングの最小信頼スコア 成功とみなされます。 Float [0.0,1.0] 0.5
output_face_blendshapes 顔認識地マーカーが顔ブレンドシェイプを出力するかどうか。 顔ブレンドシェイプは、3D 顔モデルのレンダリングに使用されます。 Boolean False
output_facial_transformation_matrixes FaceLandmarker が顔を出力するかどうか 変換行列が作成されますFaceLandMarker で 行列を使用して、顔のランドマークを正規の顔モデルから 検出されたランドマークに効果を適用できます。 Boolean False
result_callback ランドマークの結果を受け取るように結果リスナーを設定します。 自動的に同期されます。 実行モードが LIVE_STREAM に設定されている場合にのみ使用できます ResultListener N/A

データの準備

入力を画像ファイルまたは numpy 配列として準備し、 mediapipe.Image オブジェクト。入力が動画ファイルやストリーミングの場合、 などの外部ライブラリを使用して、 入力フレームを numpy として読み込むための OpenCV あります

画像

import mediapipe as mp

# Load the input image from an image file.
mp_image = mp.Image.create_from_file('/path/to/image')

# Load the input image from a numpy array.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_image)
    

動画

import mediapipe as mp

# Use OpenCV’s VideoCapture to load the input video.

# Load the frame rate of the video using OpenCV’s CV_CAP_PROP_FPS
# You’ll need it to calculate the timestamp for each frame.

# Loop through each frame in the video using VideoCapture#read()

# Convert the frame received from OpenCV to a MediaPipe’s Image object.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_frame_from_opencv)
    

ライブ配信

import mediapipe as mp

# Use OpenCV’s VideoCapture to start capturing from the webcam.

# Create a loop to read the latest frame from the camera using VideoCapture#read()

# Convert the frame received from OpenCV to a MediaPipe’s Image object.
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=numpy_frame_from_opencv)
    

タスクを実行する

顔認識マーカーでは、detectdetect_for_videodetect_async を使用します。 推論をトリガーできます顔のランドマークは、以下の操作を行います。 画像内の顔を検出できます。

次のコードは、タスクで処理を実行する方法を示しています。 モデルです。

画像

# Perform face landmarking on the provided single image.
# The face landmarker must be created with the image mode.
face_landmarker_result = landmarker.detect(mp_image)
    

動画

# Perform face landmarking on the provided single image.
# The face landmarker must be created with the video mode.
face_landmarker_result = landmarker.detect_for_video(mp_image, frame_timestamp_ms)
    

ライブ配信

# Send live image data to perform face landmarking.
# The results are accessible via the `result_callback` provided in
# the `FaceLandmarkerOptions` object.
# The face landmarker must be created with the live stream mode.
landmarker.detect_async(mp_image, frame_timestamp_ms)
    

次の点にご留意ください。

  • 動画モードまたはライブ配信モードで実行する場合は、 顔認識地マーカータスクは、入力フレームのタイムスタンプを返します。
  • 画像モデルまたは動画モデルで実行する場合、顔認識ツール タスクは、 入力画像やフレームの処理が完了するまで、現在のスレッドを待機状態にします。
  • ライブ ストリーム モードで実行している場合、顔認識ツールタスクは 現在のスレッドをブロックしません。このメソッドは、 完了するたびに、その検出結果が 表示されます。顔のランドマーク タスクの実行時に検出関数が呼び出されたかどうか 別のフレームの処理でビジー状態の場合、タスクは新しい入力フレームを無視します。

画像に顔認識マーカーを配置する詳細な例については、コード 例 をご覧ください。

結果の処理と表示

顔認識地マーカーは、検出のたびに FaceLandmarkerResult オブジェクトを返します。 あります。結果のオブジェクトには、検出された顔ごとに顔メッシュが含まれ、 各顔ランドマークの座標です。必要に応じて、結果オブジェクトには 顔の表情を表すブレンドシェイプと、 変換行列を使用して、検出されたランドマークに顔効果を適用します。

このタスクからの出力データの例を次に示します。

FaceLandmarkerResult:
  face_landmarks:
    NormalizedLandmark #0:
      x: 0.5971359014511108
      y: 0.485361784696579
      z: -0.038440968841314316
    NormalizedLandmark #1:
      x: 0.3302789330482483
      y: 0.29289937019348145
      z: -0.09489090740680695
    ... (478 landmarks for each face)
  face_blendshapes:
    browDownLeft: 0.8296722769737244
    browDownRight: 0.8096957206726074
    browInnerUp: 0.00035583582939580083
    browOuterUpLeft: 0.00035752105759456754
    ... (52 blendshapes for each face)
  facial_transformation_matrixes:
    [9.99158978e-01, -1.23036895e-02, 3.91213447e-02, -3.70770246e-01]
    [1.66496094e-02,  9.93480563e-01, -1.12779640e-01, 2.27719707e+01]
    ...

次の図は、タスク出力を可視化したものです。

顔認識マーカーのサンプルコードは、返された結果を表示する方法を示しています。 詳細については、コードの 例 をご覧ください。