MediaPipeを使用して、ジェスチャー認識アプリケーションを簡単に構築する方法について説明します。このガイドでは、Python APIを活用し、短いコードでリアルタイム処理を実現します。
環境設定とアーキテクチャ
MediaPipe Python APIのインストールは簡単です:
pip install mediapipe
ソリューションモジュールは、mediapipe/python/solutions/に配置されており、主要な機能には以下が含まれます:
- 手のジェスチャー認識(Hands)
- 顔メッシュ(FaceMesh)
- 全身ポーズ推定(Holistic)
- 画像セグメンテーション(SelfieSegmentation)
- 3Dオブジェクト検出(Objectron)
基本的な画像処理フロー
以下の5行のコードで、静止画からジェスチャー認識を行います:
import cv2
import mediapipe as mp
mp_gesture = mp.solutions.gestures.GestureDetector(static_image_mode=True)
img = cv2.imread("gesture.jpg")
results = mp_gesture.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
if results.multi_gesture_landmarks:
for gesture_marks in results.multi_gesture_landmarks:
mp.solutions.drawing_utils.draw_landmarks(
img, gesture_marks, mp.solutions.gestures_connections.GESTURE_CONNECTIONS)
cv2.imwrite("processed.jpg", img)
mp_gesture.close()
リアルタイムビデオ処理の最適化
動画からのジェスチャー認識には、カメラキャプチャループとコンテキストマネージャーを活用します:
import cv2
import mediapipe as mp
with mp.solutions.gestures.GestureDetector(
model_complexity=1,
min_detection_confidence=0.7) as detector:
cap = cv2.VideoCapture(0)
while cap.isOpened():
success, frame = cap.read()
if not success: break
frame.flags.writeable = False
results = detector.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
frame.flags.writeable = True
if results.multi_gesture_landmarks:
for gesture_marks in results.multi_gesture_landmarks:
mp.solutions.drawing_utils.draw_landmarks(
frame, gesture_marks,
mp.solutions.gestures_connections.GESTURE_CONNECTIONS)
cv2.imshow('Gesture Detection', cv2.flip(frame, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
高度な機能と可視化テクニック
ランドマークデータの解析や、プロフェッショナル向けの可視化ツールも提供されています。例えば、ランドマークをピクセル座標に変換するコードは以下の通りです:
height, width, channels = img.shape
for idx, landmark in enumerate(gesture_marks.landmark):
x_pos, y_pos = int(landmark.x * width), int(landmark.y * height)
cv2.circle(img, (x_pos, y_pos), 5, (0, 255, 0), -1)
シナリオ適応とパフォーマンスチューニング
異なるシーンへの適用やパフォーマンスの向上については、モデルの選択、入力解像度の調整、バッチ処理モードの使用などが挙げられます。
実践例:ジェスチャー制御メディアプレイヤー
以下は、ジェスチャーでメディア再生をコントロールする完全な例です:
import cv2
import mediapipe as mp
import pyautogui
import numpy as np
mp_gesture = mp.solutions.gestures.GestureDetector(min_detection_confidence=0.7)
cap = cv2.VideoCapture(0)
volume_step = 5
progress_step = 500
while cap.isOpened():
success, frame = cap.read()
if not success: break
results = mp_gesture.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if results.multi_gesture_landmarks:
for gesture_marks in results.multi_gesture_landmarks:
index_tip = gesture_marks.landmark[mp.solutions.gestures.GestureLandmark.INDEX_FINGER_TIP]
thumb_tip = gesture_marks.landmark[mp.solutions.gestures.GestureLandmark.THUMB_TIP]
distance = np.sqrt((index_tip.x - thumb_tip.x)**2 + (index_tip.y - thumb_tip.y)**2)
if distance < 0.05:
pyautogui.press('space')
elif index_tip.y < 0.3:
pyautogui.press('up')
elif index_tip.y > 0.7:
pyautogui.press('down')
cv2.imshow('Gesture Control', cv2.flip(frame, 1))
if cv2.waitKey(5) & 0xFF == 27:
break
mp_gesture.close()
cap.release()