ObjectDetector

公開最終類別 ObjectDetector

對圖片執行物件偵測。

API 會使用 TFLite 模型中繼資料的 TFLite 模型。

這個 API 支援具有一個圖片輸入張量和四個輸出張量的模型。具體規定如下:

  • 輸入圖片張量 (kTfLiteUInt8/kTfLiteFloat32)
    • 大小為 [batch x height x width x channels] 的圖片輸入。
    • 不支援批次推論 (batch 須為 1)。
    • 僅支援 RGB 輸入值 (channels 必須是 3)。
    • 如果類型為 kTfLiteFloat32,則必須將 NormalizationOptions 附加至中繼資料,以便進行輸入正規化作業。
  • 輸出張量必須是 DetectionPostProcess 運算的 4 項輸出內容,例如:
    • 位置張量 (kTfLiteFloat32):
      • 大小為 [1 x num_results x 4] 的張量,代表定界框的內部陣列,格式為 [top、left、right、bottom]。
      • BoundingBoxProperties 必須附加至中繼資料,且指定 type=BOUNDARIEScoordinate_type=RATIO
    • 類別張量 (kTfLiteFloat32):
      • 大小為 [1 x num_results] 的張量,每個值都代表類別的整數索引。
      • 如果標籤對應關係是以 TENSOR_VALUE_LABELS 相關檔案的形式附加至中繼資料,就會用於將 Tensor 值轉換為標籤。
    • 分數張量 (kTfLiteFloat32):
      • 大小為 [1 x num_results] 的張量,每個值都代表偵測到物件的分數。
    • 偵測張量 (kTfLiteFloat32):
      • 整數 num_results 為大小 [1] 的張量。

TensorFlow Hub 提供了這類模型的範例。

巢狀類別

類別 ObjectDetector.ObjectDetectorOptions 設定 ObjectDetector 的選項。 

公用方法

static ObjectDetector
createFromBuffer(背景資訊、ByteBuffer modelBuffer)
從模型緩衝區和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。
static ObjectDetector
createFromFile(背景資訊、String modelPath)
從模型檔案和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。
static ObjectDetector
createFromFile(背景資訊、檔案 modelFile)
從模型檔案和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。
static ObjectDetector
ObjectDetectorResult
detect(MPImage 圖片)
使用預設圖片處理選項,對提供的單一圖片執行物件偵測,例如
ObjectDetectorResult
detect(MPImage 圖片、ImageProcessingOptions imageProcessingOptions)
對提供的單一圖片執行物件偵測。
void
detectAsync(MPImage 圖片、ImageProcessingOptions imageProcessingOptions、long timestampM)
傳送即時圖片資料來執行物件偵測,結果會透過 ObjectDetector.ObjectDetectorOptions 提供的 OutputHandler.ResultListener 取得結果。
void
detectAsync(MPImage 圖片、長時間戳記 M)
傳送即時圖像資料,以便透過預設圖片處理選項 (即
ObjectDetectorResult
detectForVideo(MPImage 圖片、ImageProcessingOptions imageProcessingOptions、long timestampM)
對提供的影片影格執行物件偵測。
ObjectDetectorResult
detectForVideo(MPImage 圖片、長時間戳記 M)
以預設的圖片處理選項,對提供的影片畫面執行物件偵測,

繼承的方法

公用方法

公開 靜態 ObjectDetector createFromBuffer (內容結構定義、ByteBuffer modelBuffer)

從模型緩衝區和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。

參數
context Android ERROR(/Context)
modelBuffer 直接 ByteBufferERROR(/MappedByteBuffer) 偵測模型。
擲回
如果 ObjectDetector 建立期間發生錯誤。

public static ObjectDetector createFromFile (內容結構定義、String modelPath)

從模型檔案和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。

參數
context Android ERROR(/Context)
modelPath 含有中繼資料的偵測模型路徑。
擲回
如果 ObjectDetector 建立期間發生錯誤。

public static ObjectDetector createFromFile (內容結構定義、檔案 modelFile)

從模型檔案和預設的 ObjectDetector.ObjectDetectorOptions 建立 ObjectDetector 例項。

參數
context Android ERROR(/Context)
modelFile 偵測模型 File 例項。
擲回
IOException 在開啟 tflite 模型檔案時發生 I/O 錯誤時。
如果 ObjectDetector 建立期間發生錯誤。

public static ObjectDetector createFromOptions (內容結構定義、ObjectDetector.ObjectDetectorOptions DetectionorOptions)

參數
context Android ERROR(/Context)
detectorOptions ObjectDetector.ObjectDetectorOptions 執行個體。
擲回
如果 ObjectDetector 建立期間發生錯誤。

public ObjectDetectorResult detect (MPImage 圖片)

使用預設圖片處理選項 (即不套用任何旋轉),對提供的單一圖片執行物件偵測。只有在使用 ERROR(/RunningMode.IMAGE) 建立 ObjectDetector 時,才能使用這個方法。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
擲回

public ObjectDetectorResult detect (MPImage 圖片、ImageProcessingOptions imageProcessingOptions)

對提供的單一圖片執行物件偵測。只有在使用 ERROR(/RunningMode.IMAGE) 建立 ObjectDetector 時,才能使用這個方法。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
imageProcessingOptions 用於指定在執行推論之前如何處理輸入圖片的 ImageProcessingOptions。請注意,這項工作「不」支援搜尋區域:指定 ImageProcessingOptions.regionOfInterest() 會導致這個方法擲回 IllegalArgumentException。
擲回
IllegalArgumentException 表示 ImageProcessingOptions 指定搜尋區域。

public void detectAsync (MPImage image、ImageProcessingOptions imageProcessingOptions、long timestampMs)

傳送即時圖片資料來執行物件偵測,結果會透過 ObjectDetector.ObjectDetectorOptions 提供的 OutputHandler.ResultListener 取得結果。只有在使用 ERROR(/RunningMode.LIVE_STREAM) 建立 ObjectDetector 時,才能使用這個方法。

您必須提供時間戳記 (以毫秒為單位),指出輸入圖片傳送至物件偵測工具的時間。輸入時間戳記必須單純遞增。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
imageProcessingOptions 用於指定在執行推論之前如何處理輸入圖片的 ImageProcessingOptions。請注意,這項工作「不」支援搜尋區域:指定 ImageProcessingOptions.regionOfInterest() 會導致這個方法擲回 IllegalArgumentException。
timestampMs 輸入時間戳記 (以毫秒為單位)。
擲回
IllegalArgumentException 表示 ImageProcessingOptions 指定搜尋區域。

public void detectAsync (MPImage 圖片、長 timestampM)

傳送即時圖片資料,透過預設圖片處理選項執行物件偵測 (也就是不套用任何旋轉),並透過 ObjectDetector.ObjectDetectorOptions 提供的 OutputHandler.ResultListener 取得結果。只有在使用 ERROR(/RunningMode.LIVE_STREAM) 建立 ObjectDetector 時,才能使用這個方法。

您必須提供時間戳記 (以毫秒為單位),指出輸入圖片傳送至物件偵測工具的時間。輸入時間戳記必須單純遞增。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
timestampMs 輸入時間戳記 (以毫秒為單位)。
擲回

public ObjectDetectorResult detectForVideo (MPImage image、ImageProcessingOptions imageProcessingOptions, long timestampMs)

對提供的影片影格執行物件偵測。只有在使用 ERROR(/RunningMode.VIDEO) 建立 ObjectDetector 時,才能使用這個方法。

您必須提供影片影格的時間戳記 (以毫秒為單位)。輸入時間戳記必須單純遞增。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
imageProcessingOptions 用於指定在執行推論之前如何處理輸入圖片的 ImageProcessingOptions。請注意,這項工作「不」支援搜尋區域:指定 ImageProcessingOptions.regionOfInterest() 會導致這個方法擲回 IllegalArgumentException。
timestampMs 輸入時間戳記 (以毫秒為單位)。
擲回
IllegalArgumentException 表示 ImageProcessingOptions 指定搜尋區域。

public ObjectDetectorResult detectForVideo (MPImage 圖片、長 timestampM)

透過預設的圖片處理選項 (即不套用任何旋轉),對提供的影片影格執行物件偵測。只有在使用 ERROR(/RunningMode.VIDEO) 建立 ObjectDetector 時,才能使用這個方法。

您必須提供影片影格的時間戳記 (以毫秒為單位)。輸入時間戳記必須單純遞增。

ObjectDetector 支援下列色彩空間類型:

參數
圖片 要處理的 MediaPipe MPImage 物件。
timestampMs 輸入時間戳記 (以毫秒為單位)。
擲回