Important
MediaPipe レガシーソリューションのサポートは、2023年3月1日で終了しています。
従来のソリューションのサンプルは _legacyディレクトリに移動しました。
MediaPipeは後方互換を保っており、現パッケージでもレガシーソリューションのサンプルを実行出来ます。
google-ai-edge/mediapipeのPythonパッケージのサンプルスクリプト集です。
2024/9/1時点でPython実装のある以下15機能について用意しています。
- 物体検出(Object Detection)
- 画像分類(Image Classification)
- 画像セグメンテーション(Image Segmentation)
- インタラクティブ セグメンテーション(Interactive segmentation)
- 手検出(Hand Landmark detection)
- 手のジェスチャー認識(Gesture Recognition)
- 画像の埋め込み表現(Image Embedding)
- 顔検出(Face Detection)
- 顔のランドマーク検出(Face Landmark Detection)
- 顔のスタイル変換(Face Stylization)
- 姿勢推定(Pose Landmark Detection)
- テキスト分類(Text Classification)
- テキストの埋め込み表現(Text Embedding)
- テキスト言語分類(Language Detector)
- 音分類(Audio Classification)
- mediapipe 0.10.14 or later
- opencv-python 4.10.0.84 or later
- tqdm 4.66.5 or later ※重みファイルダウンロードに使用
- requests 2.32.3 or later ※重みファイルダウンロードに使用
- scipy 1.14.1 or later ※音分類(Audio Classification)サンプルを実行する場合のみ
- numpy 1.26.4 ※NumPyは1.x系
pip install -r requirements.txt
デモの実行方法は以下です。
python sample_object_detection.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0, 1, 2, 3, 4, 5, 6, 7] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
COCOデータセットでトレーニングされた重みで、サポートされているラベルはlabelmap.txt
デフォルト:0
- 0:EfficientDet-Lite0(int8)
- 1:EfficientDet-Lite0(float 16)
- 2:EfficientDet-Lite0(float 32)
- 3:EfficientDet-Lite2(int8)
- 4:EfficientDet-Lite2(float 16)
- 5:EfficientDet-Lite2float 32)
- 6:SSDMobileNet-V2(int8)
- 7:SSDMobileNet-V2(float 32)
- --score_threshold
スコア閾値
デフォルト:0.5

python sample_image_classification.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0, 1, 2, 3] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
ImageNetでトレーニングされた重みで、サポートされているラベルはlabels.txt
デフォルト:0
- 0:EfficientNet-Lite0(int8)
- 1:EfficientNet-Lite0(float 32)
- 2:EfficientNet-Lite2(int8)
- 3:EfficientNet-Lite2(float 32)
- --max_results
結果出力数
デフォルト:5

python sample_image_segmentation.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0, 1, 2, 3, 4] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:SelfieSegmenter(square)
- 1:SelfieSegmenter(landscape)
- 2:HairSegmenter
- 3:SelfieMulticlass(256x256)
- 4:DeepLab-V3



python sample_interactive_image_segmentation.py
コマンドライン引数オプション
- --image
画像パスの指定
デフォルト:asset/hedgehog01.jpg - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:MagicTouch

python sample_hand_landmarks_detection.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --unuse_mirror
ミラー表示不使用
デフォルト:指定なし - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:HandLandmarker (full)
- --num_hands
検出数
デフォルト:2 - --use_world_landmark
ワールド座標表示
デフォルト:指定なし


python sample_hand_gesture_recognition.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --unuse_mirror
ミラー表示不使用
デフォルト:指定なし - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
認識ジェスチャーは「Closed fist」「Open palm」「Pointing up」「Thumbs down」「Thumbs up」「Victory」「Love」「Unknown」
デフォルト:0
- 0:HandGestureClassifier

python sample_image_embedding.py
コマンドライン引数オプション
- --image01
画像パス1の指定
デフォルト:asset/hedgehog01.jpg - --image02
画像パス2の指定
デフォルト:asset/hedgehog02.jpg - --model
使用モデル[0, 1] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:MobileNet-V3 (small)
- 1:MobileNet-V3 (large)
- --unuse_l2_normalize
特徴ベクトルを L2 ノルムで正規化しない
デフォルト:指定なし - --unuse_quantize
特徴ベクトルを スカラー量子化によってバイトに量子化しない
デフォルト:指定なし




python sample_face_landmark_detection.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:BlazeFace (short-range)

python sample_face_landmark_detection.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:FaceLandscapeer
- --num_faces
検出数
デフォルト:1 - --unuse_output_face_blendshapes
顔のブレンドシェイプを出力しない
デフォルト:指定なし - --unuse_output_facial_transformation_matrixes
顔変換行列を出力しない
デフォルト:指定なし

python sample_face_stylization.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --model
使用モデル[0, 1, 2] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:Color sketch
- 1:Color ink
- 2:Oil painting



python sample_pose_landmark_detection.py
コマンドライン引数オプション
- --device
カメラデバイス番号の指定
デフォルト:0 - --video
動画パスの指定 ※指定時はカメラより優先
デフォルト:None - --width
カメラキャプチャ時の横幅
デフォルト:960 - --height
カメラキャプチャ時の縦幅
デフォルト:540 - --unuse_mirror
ミラー表示不使用
デフォルト:指定なし - --model
使用モデル[0, 1, 2] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:Pose landmarker(lite)
- 1:Pose landmarker(Full)
- 2:Pose landmarker(Heavy)
- --use_output_segmentation_masks
セグメンテーションを実施
デフォルト:指定なし - --use_world_landmark
ワールド座標表示を実施
デフォルト:指定なし



python sample_text_classification.py
コマンドライン引数オプション
- --input_text
入力テキスト
デフォルト:I'm looking forward to what will come next. - --model
使用モデル[0, 1] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:BERT-classifier
- 1:Average word embedding

python sample_text_embedding.py
コマンドライン引数オプション
- --input_text01
入力テキスト1
デフォルト:I'm feeling so good - --input_text02
入力テキスト2
デフォルト:I'm okay I guess - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:Universal Sentence Encoder
- --unuse_l2_normalize
特徴ベクトルを L2 ノルムで正規化しない
デフォルト:指定なし - --use_quantize
特徴ベクトルを スカラー量子化によってバイトに量子化する
デフォルト:指定なし

python sample_text_language_detection.py
コマンドライン引数オプション
- --input_text
入力テキスト
デフォルト:分久必合合久必分 - --model
使用モデル[0, 1] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:Language Detector

python sample_audio_classification.py
コマンドライン引数オプション
- --input_audio
入力音声ファイルのパス
デフォルト:asset/hyakuninisshu_02.wav - --model
使用モデル[0] ※対象モデルの重みがmodelディレクトリ内に無い場合ダウンロードを実行
デフォルト:0
- 0:YamNet
- --max_results
結果出力数
デフォルト:5

高橋かずひと(https://twitter.com/KzhtTkhs)
mediapipe-python-sample is under Apache-2.0 License.
サンプル実行用に格納している画像などは以下を利用しています。