【ハンドトラッキング】グーグル/メディアパイプの手の一部を再現

参照リンク:

1)githubコードリンク:https//github.com/google/mediapipe

2)ドキュメント:https//google.github.io/mediapipe

3)Python環境構成ドキュメント:https//google.github.io/mediapipe/getting_started/python

4)単純なAPI呼び出しのドキュメント:https//google.github.io/mediapipe/solutions/hands#python-solution-api

0.環境への準備

Python環境構成ドキュメント:https//google.github.io/mediapipe/getting_started/python

ubuntu20.04
cuda11.2
python3.8
opencv-python==4.1.2.30
mediapipe==0.8.2

sudo apt install -y protobuf-compiler
sudo apt install -y cmake

1はじめに

少し説明すると、ドキュメントは基本的に最初のリンクにあります。Pythonは、mediapipeのpypiライブラリをインストールしてAPIを呼び出すことで使用されます。

ドキュメント:https//google.github.io/mediapipe

githubコードリンク:https//github.com/google/mediapipe

2.実験

ここでのコードリファレンス:単純なAPI呼び出しのドキュメント:https//google.github.io/mediapipe/solutions/hands#python-solution-api

以下のコードから直接コピーされた公式文書は、自分で変更するのに便利です。画像を読み取る方法は2つあります。1つはリスト内のファイル名を読み取る方法です。実際には、ディレクトリ内のファイルを直接読み取るように書き換えて、リストに保存することができます。2つ目は、カメラを読み取る方法です。これは、ここで「ビデオを直接読む」と書き直すこともできます(デスクトップコンピューターの場合、カメラを使用できない場合があります。ここに移動してください)。

import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands

# For static images:
hands = mp_hands.Hands(
    static_image_mode=True,
    max_num_hands=2,
    min_detection_confidence=0.5)
for idx, file in enumerate(file_list):
  # Read an image, flip it around y-axis for correct handedness output (see
  # above).
  image = cv2.flip(cv2.imread(file), 1)
  # Convert the BGR image to RGB before processing.
  results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

  # Print handedness and draw hand landmarks on the image.
  print('Handedness:', results.multi_handedness)
  if not results.multi_hand_landmarks:
    continue
  image_hight, image_width, _ = image.shape
  annotated_image = image.copy()
  for hand_landmarks in results.multi_hand_landmarks:
    print('hand_landmarks:', hand_landmarks)
    print(
        f'Index finger tip coordinates: (',
        f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
        f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_hight})'
    )
    mp_drawing.draw_landmarks(
        annotated_image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
  cv2.imwrite(
      '/tmp/annotated_image' + str(idx) + '.png', cv2.flip(annotated_image, 1))
hands.close()

# For webcam input:
hands = mp_hands.Hands(
    min_detection_confidence=0.5, min_tracking_confidence=0.5)
cap = cv2.VideoCapture(0)
while cap.isOpened():
  success, image = cap.read()
  if not success:
    print("Ignoring empty camera frame.")
    # If loading a video, use 'break' instead of 'continue'.
    continue

  # Flip the image horizontally for a later selfie-view display, and convert
  # the BGR image to RGB.
  image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
  # To improve performance, optionally mark the image as not writeable to
  # pass by reference.
  image.flags.writeable = False
  results = hands.process(image)

  # Draw the hand annotations on the image.
  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('MediaPipe Hands', image)
  if cv2.waitKey(5) & 0xFF == 27:
    break
hands.close()
cap.release()

テストを実行した結果:

3.出力のhand_landmarksの解釈

ここでランドマークを例にとると、手には21のキーポイントがあり、xとyは幅と高さの正規化された結果であり、zは深さです。

hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x

上記と読むべきキーポイントの命名に従って、対応するポイントは座標します。

 

 

 

 

 

 

 

 

おすすめ

転載: blog.csdn.net/qq_35975447/article/details/113933444