您可以使用 MediaPipe 圖片區隔器工作,根據預先定義的類別將圖片分割成區域,以便套用背景模糊處理等視覺效果。這些操作說明將說明如何使用 Python 語言搭配圖像分割器。如要進一步瞭解這項工作的功能、模型和設定選項,請參閱總覽。
程式碼範例
圖片區塊分割器的範例程式碼會提供 Python 中此工作的完整實作方式,供您參考。這段程式碼可協助您測試這項工作,並開始建構自己的圖片分割應用程式。您可以只使用網路瀏覽器,查看、執行及編輯圖片區塊分割器範例程式碼。
設定
本節將說明設定開發環境和程式碼專案的關鍵步驟,以便使用圖片分割器。如要進一步瞭解如何設定開發環境以使用 MediaPipe 工作,包括平台版本需求,請參閱 Python 設定指南。您可以在 GitHub 上查看這個範例的原始碼
套件
MediaPipe 圖片區塊劃分器工作需要 mediapipe
套件。您可以使用下列指令安裝必要的依附元件:
$ python -m pip install mediapipe
匯入
匯入下列類別,即可存取 ImageSegmenter 工作函式:
import mediapipe as mp
from mediapipe.tasks import python
from mediapipe.tasks.python import vision
型號
MediaPipe 圖片區塊劃分器工作需要訓練的模型,且必須與此工作相容。如要進一步瞭解可用訓練模型的圖片區隔器,請參閱任務總覽的「模型」一節。
選取並下載模型,然後將模型儲存在專案目錄中:
model_path = '/absolute/path/to/model.tflite'
在「Model Name」參數中指定模型路徑,如下所示:
base_options = BaseOptions(model_asset_path=model_path)
建立工作
MediaPipe 圖片區隔器工作會使用 create_from_options
函式設定工作。create_from_options
函式會接受設定選項的值,以便處理。如要進一步瞭解工作設定,請參閱「設定選項」。
這些範例也說明圖片、影片檔案和直播影片串流的任務建構變化。
圖片
BaseOptions = mp.tasks.BaseOptions ImageSegmenter = mp.tasks.vision.ImageSegmenter ImageSegmenterOptions = mp.tasks.vision.ImageSegmenterOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a image segmenter instance with the image mode: options = ImageSegmenterOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.IMAGE, output_category_mask=True) with ImageSegmenter.create_from_options(options) as segmenter:
影片
BaseOptions = mp.tasks.BaseOptions ImageSegmenter = mp.tasks.vision.ImageSegmenter ImageSegmenterOptions = mp.tasks.vision.ImageSegmenterOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a image segmenter instance with the video mode: options = ImageSegmenterOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.VIDEO, output_category_mask=True) with ImageSegmenter.create_from_options(options) as segmenter:
直播
BaseOptions = mp.tasks.BaseOptions ImageSegmenter = mp.tasks.vision.ImageSegmenter ImageSegmenterOptions = mp.tasks.vision.ImageSegmenterOptions VisionRunningMode = mp.tasks.vision.RunningMode # Create a image segmenter instance with the live stream mode: def print_result(result: List[Image], output_image: Image, timestamp_ms: int): print('segmented masks size: {}'.format(len(result))) options = ImageSegmenterOptions( base_options=BaseOptions(model_asset_path='/path/to/model.task'), running_mode=VisionRunningMode.VIDEO, output_category_mask=True) with ImageSegmenter.create_from_options(options) as segmenter:
設定選項
此工作包含下列 Python 應用程式的設定選項:
選項名稱 | 說明 | 值範圍 | 預設值 |
---|---|---|---|
running_mode |
設定工作執行模式。共有三種模式: IMAGE:單一圖片輸入模式。 VIDEO:影片解碼影格模式。 LIVE_STREAM:輸入資料 (例如來自攝影機的資料) 的直播模式。 在這個模式中,必須呼叫 resultListener,才能設定事件監聽器,以非同步方式接收結果。 |
{IMAGE, VIDEO, LIVE_STREAM } |
IMAGE |
output_category_mask |
如果設為 True ,輸出內容會包含分割遮罩,做為 uint8 圖片,其中每個像素值都會指出勝出的類別值。 |
{True, False } |
False |
output_confidence_masks |
如果設為 True ,輸出內容會包含分割遮罩,做為浮點值圖片,其中每個浮點值代表類別的信心分數對應。 |
{True, False } |
True |
display_names_locale |
設定標籤語言,用於工作模型中繼資料中提供的顯示名稱 (如有)。預設值為英文的 en 。您可以使用 TensorFlow Lite Metadata Writer API,在自訂模型的中繼資料中新增本地化標籤 |
語言代碼 | en |
result_callback |
在圖片分割器處於 LIVE_STREAM 模式時,將結果事件監聽器設為以非同步方式接收分割結果。只有在執行模式設為 LIVE_STREAM 時,才能使用 |
不適用 | 不適用 |
準備資料
將輸入內容設為圖片檔案或 Numpy 陣列,然後轉換為 mediapipe.Image
物件。如果輸入內容是網路攝影機的影片檔案或直播內容,您可以使用 OpenCV 等外部程式庫,將輸入影格載入為 Numpy 陣列。
圖片
# 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)
影片
# Use OpenCV’s VideoCapture to load the input video. # Load the frame rate of the video using OpenCV’s CV_CAP_PROP_FPS # You need the frame rate 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)
直播
# 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)
如需圖片區隔器資料準備作業的程式碼範例,請參閱程式碼範例。
執行工作
圖片區塊判斷器會使用 segment
、segment_for_video
和 segment_async
函式觸發推論。對於圖片區隔作業,這包括預先處理輸入資料、執行區隔模型,以及將原始模型輸出內容後處理為區隔遮罩。
以下程式碼範例說明如何使用工作模型執行處理作業。
圖片
# Perform image segmentation on the provided single image. # The image segmenter must be created with the image mode. segmented_masks = segmenter.segment(mp_image)
影片
# Perform image segmentation on the provided single image. # The image segmenter must be created with the video mode. segmented_masks = segmenter.segment_for_video(mp_image, frame_timestamp_ms)
直播
# Send live image data to perform image segmentation. # The results are accessible via the `result_callback` provided in # the `ImageSegmenterOptions` object. # The image segmenter must be created with the live stream mode. segmenter.segment_async(mp_image, frame_timestamp_ms)
注意事項:
- 在影片模式或直播模式中執行時,您也必須為圖像區隔器工作提供輸入影格時間戳記。
- 在圖片或影片模型中執行時,圖片區塊分割器工作會阻斷目前執行緒,直到處理完輸入圖片或影格為止。
如需執行 Image Segmenter 推論的完整範例,請參閱程式碼範例。
處理及顯示結果
圖片區隔器會輸出 Image
資料清單。如果 output_type
為 CATEGORY_MASK
,輸出內容會是清單,其中包含單一區隔遮罩,做為 uint8 圖片。像素會指出輸入圖片的已辨識類別索引。如果 output_type
是 CONFIDENCE_MASK
,輸出內容會是大小為類別數量的向量。每個區隔遮罩都是 [0,1]
範圍內的浮點圖片,代表屬於該類別的像素信心分數。
以下各節會列舉此工作輸出資料的範例:
類別可信度
以下圖片顯示類別信心遮罩的工作輸出結果視覺化資訊。信心遮罩輸出內容包含 [0, 1]
之間的浮點值。
原始圖片和類別信心遮罩輸出結果。Pascal VOC 2012 資料集的原始圖片。
類別值
下圖顯示類別值遮罩的工作輸出結果視覺化資訊。類別遮罩範圍為 [0, 255]
,每個像素值代表模型輸出的獲勝類別索引。勝出的類別索引是模型可辨識的類別中分數最高者。
原始圖片和類別遮罩輸出結果。Pascal VOC 2012 資料集的原始圖片。