MediaPipe 地標工具工作可讓你偵測圖片中手部的地標。 以下操作說明將示範如何搭配 Python 使用手寫地標。您可以前往 GitHub 取得這些操作說明中提及的程式碼範例。
如要進一步瞭解這項工作的功能、模型和設定選項,請參閱總覽。
程式碼範例
《Handmarker》的範例程式碼提供這項工作在 Python 中的完整實作,供您參考。這段程式碼可協助您測試這項工作,開始建構自己的手地標偵測工具。現在,只要使用網路瀏覽器,就能檢視、執行和編輯 Handmarker 範例程式碼。
設定
本節說明設定開發環境及編寫程式碼專案的重要步驟,以便專供「手地標」使用。如需瞭解如何使用 MediaPipe 工作設定開發環境的一般資訊,包括平台版本需求,請參閱 Python 設定指南。
套裝組合
MediaPipe Put 地標工作需要 mediapipe PyPI 套件。您可以使用下列程式碼安裝並匯入這些依附元件:
$ python -m pip install mediapipe
匯入
匯入下列類別,即可使用「手鍊」工作功能:
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
型號
MediaPipe Put Marker 工作需要使用已訓練且與這項工作相容的模型。如要進一步瞭解「手寫地標」可用的已訓練模型,請參閱工作總覽「模型」一節。
選取並下載模型,然後儲存至本機目錄:
model_path = '/absolute/path/to/gesture_recognizer.task'
使用 BaseOptions
物件 model_asset_path
參數指定要使用的模型路徑。如需程式碼範例,請參閱下一節。
建立工作
MediaPipe Put 地標工作會使用 create_from_options
函式設定工作。create_from_options
函式可接受設定選項值來處理。如要進一步瞭解設定選項,請參閱「設定選項」一文。
下列程式碼示範如何建構及設定這項工作。
這些範例也會顯示圖片、影片檔案和直播工作建構的變化。
圖片
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the image mode: options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.IMAGE) with HandLandmarker.create_from_options(options) as landmarker: # The landmarker is initialized. Use it here. # ...
影片
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the video mode: options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.VIDEO) with HandLandmarker.create_from_options(options) as landmarker: # The landmarker is initialized. Use it here. # ...
直播
import mediapipe as mp BaseOptions = mp.tasks.BaseOptions HandLandmarker = mp.tasks.vision.HandLandmarker HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions HandLandmarkerResult = mp.tasks.vision.HandLandmarkerResult VisionRunningMode = mp.tasks.vision.RunningMode # Create a hand landmarker instance with the live stream mode: def print_result(result: HandLandmarkerResult, output_image: mp.Image, timestamp_ms: int): print('hand landmarker result: {}'.format(result)) options = HandLandmarkerOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.LIVE_STREAM, result_callback=print_result) with HandLandmarker.create_from_options(options) as landmarker: # The landmarker is initialized. Use it here. # ...
如需建立搭配使用圖片與圖片的完整範例,請參閱程式碼範例。
設定選項
這項工作的 Python 應用程式設定選項如下:
選項名稱 | 說明 | 值範圍 | 預設值 |
---|---|---|---|
running_mode |
設定工作的執行模式。共有三種模式: IMAGE:單一圖片輸入的模式。 影片:影片已解碼影格的模式。 LIVE_STREAM:輸入資料串流 (例如攝影機) 的直播模式。在這個模式下,必須呼叫 resultListener 才能設定事件監聽器,以非同步方式接收結果。 |
{IMAGE, VIDEO, LIVE_STREAM } |
IMAGE |
num_hands |
手部地標偵測器偵測到的手數量上限。 | Any integer > 0 |
1 |
min_hand_detection_confidence |
在手掌偵測模型中,系統判定手部偵測成功的最低信心分數。 | 0.0 - 1.0 |
0.5 |
min_hand_presence_confidence |
在手部地標偵測模型中,手持狀態分數的最低可信度分數。在影片模式和直播模式下,如果手持位置標記模型的信心分數低於這個門檻,手寫地標就會觸發手掌偵測模型。否則,為了後續的地標偵測,系統會透過輕量追蹤演算法判斷手部的位置。 | 0.0 - 1.0 |
0.5 |
min_tracking_confidence |
系統判定手機追蹤成功時的最低可信度分數。這是目前影格和最後一個影格之間的定界框 IoU 門檻。在影片模式和手持地標的「串流」模式中,如果追蹤失敗,手地標就會觸發手部偵測。否則,會略過手部偵測。 | 0.0 - 1.0 |
0.5 |
result_callback |
設定結果監聽器,在手標位置處於直播模式時,以非同步方式接收偵測結果。只有在執行模式設為 LIVE_STREAM 時適用 |
不適用 | 不適用 |
準備資料
將輸入準備為圖片檔或 numpy 陣列,然後再將其轉換為 mediapipe.Image
物件。如果輸入的內容是影片檔案,或是透過網路攝影機直播,可以使用外部程式庫 (例如 OpenCV) 將輸入影格載入為 numpy 陣列。
圖片
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)
執行工作
「手地標」會使用 Detection、Detect_for_video 和 detect_async 函式來觸發推論。偵測手地標時,這包括預先處理輸入資料、偵測圖片中的手,以及偵測手地標。
下列程式碼示範如何使用工作模型執行處理作業。
圖片
# Perform hand landmarks detection on the provided single image. # The hand landmarker must be created with the image mode. hand_landmarker_result = landmarker.detect(mp_image)
影片
# Perform hand landmarks detection on the provided single image. # The hand landmarker must be created with the video mode. hand_landmarker_result = landmarker.detect_for_video(mp_image, frame_timestamp_ms)
直播
# Send live image data to perform hand landmarks detection. # The results are accessible via the `result_callback` provided in # the `HandLandmarkerOptions` object. # The hand landmarker must be created with the live stream mode. landmarker.detect_async(mp_image, frame_timestamp_ms)
注意事項:
- 在影片模式或直播模式下執行時,您也必須為手持地標工作提供輸入影格的時間戳記。
- 在圖片或影片模型中執行時,「手地標」工作會封鎖目前的執行緒,直到處理完成輸入圖片或影格為止。
- 以直播模式執行時,「手持地標」工作不會封鎖目前的執行緒,但會立即傳回。每次完成輸入影格處理作業時,系統都會透過偵測結果叫用結果監聽器。如果手地標工作正在處理另一個影格時呼叫偵測功能,任務將忽略新的輸入框。
如需在圖片上執行「手持地標」的完整範例,請參閱程式碼範例。
處理並顯示結果
手寫地標會每次執行偵測時,都會產生手部地標結果物件。結果物件包含圖片座標中的手繪地標、世界座標中的手地標,以及偵測到的手掌(左/右手)。
以下為這項工作的輸出資料範例:
HandLandmarkerResult
輸出內容包含三個元件。每個元件都是陣列,其中每個元素都包含下列偵測到的單一手結果:
慣用手設計
慣用手是指偵測到的手是左手還是右手。
地標
有 21 個手部地標,每個地標由
x
、y
和z
座標組成。x
和y
座標會分別根據圖片寬度和高度正規化為 [0.0, 1.0]。z
座標代表地標深度,手腕的深度為起點。值越小,地標與相機越近。z
的規模與x
大致相同。世界著名地標
21 隻手的地標也會顯示在世界座標中。每個地標都由
x
、y
和z
組成,代表實際的 3D 座標 (以公尺為單位),其起點位於手的幾何中心。
HandLandmarkerResult:
Handedness:
Categories #0:
index : 0
score : 0.98396
categoryName : Left
Landmarks:
Landmark #0:
x : 0.638852
y : 0.671197
z : -3.41E-7
Landmark #1:
x : 0.634599
y : 0.536441
z : -0.06984
... (21 landmarks for a hand)
WorldLandmarks:
Landmark #0:
x : 0.067485
y : 0.031084
z : 0.055223
Landmark #1:
x : 0.063209
y : -0.00382
z : 0.020920
... (21 world landmarks for a hand)
下圖以視覺化方式呈現工作輸出內容:
「Handmarker」範例程式碼會示範如何顯示工作傳回的結果,詳情請參閱程式碼範例。