ハンドトラッキングのソースを書いてみた。
一応どんな形の手形でも動く。
パイソンのライブラリを使用。
import cv2
import mediapipe as mp
# MediaPipeの初期化
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
# カメラからの映像取得
cap = cv2.VideoCapture(0)
with mp_hands.Hands(
max_num_hands=4,
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as hands:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 画像をRGBに変換
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = True
# 手の検出を実行
results = hands.process(image)
# 画像を再びBGRに変換
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# 検出された手のランドマークを描画
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# 画像を表示
cv2.imshow('Hand Tracking', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()