您可以使用 ML Kit 在影片的各個影格中偵測及追蹤物件。
傳遞 ML Kit 圖片時,ML Kit 會針對每張圖片傳回最多五個偵測到的物件清單,以及這些物件在圖片中的位置。在影片串流中偵測物件時,每個物件都有一個 ID,可用於在圖片中追蹤物件。您也可以選擇啟用粗略物件分類功能,為物件加上廣泛的類別說明標籤。
事前準備
- 如果您尚未將 Firebase 新增至 Android 專案,請新增 Firebase。
- 將 ML Kit Android 程式庫的依附元件新增至模組 (應用程式層級) Gradle 檔案 (通常為
app/build.gradle
):apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.3' implementation 'com.google.firebase:firebase-ml-vision-object-detection-model:19.0.6' }
1. 設定物件偵測器
如要開始偵測及追蹤物件,請先建立 FirebaseVisionObjectDetector
的例項,並視需要指定要從預設值變更的任何偵測器設定。
使用
FirebaseVisionObjectDetectorOptions
物件,針對您的用途設定物體偵測器。您可以變更下列設定:物件偵測工具設定 偵測模式 STREAM_MODE
(預設) |SINGLE_IMAGE_MODE
在
STREAM_MODE
(預設) 中,物件偵測器的執行延遲較低,但在偵測器的前幾次叫用中,可能會產生不完整的結果 (例如未指定的定界框或類別標籤)。此外,在STREAM_MODE
中,偵測器會將追蹤 ID 指派給物件,您可以用來追蹤跨影格中的物件。如要追蹤物件,或需要低延遲 (例如處理即時的影片串流),請使用這個模式。在
SINGLE_IMAGE_MODE
中,物體偵測器會等到偵測到的物體邊界框和 (如果您已啟用分類功能) 類別標籤可用,才會傳回結果。因此,偵測延遲時間可能會更長。此外,在SINGLE_IMAGE_MODE
中,系統不會指派追蹤 ID。如果延遲時間不重要,且您不想處理部分結果,請使用這個模式。偵測並追蹤多個物件 false
(預設) |true
是否要偵測並追蹤最多五個物件,或只偵測最顯眼的物件 (預設值)。
分類物件 false
(預設) |true
是否將偵測到的物件分類為粗略類別。啟用後,物件偵測器會將物件分類為以下類別:時尚商品、食物、家居用品、地點、植物和不明物。
偵測及追蹤物件 API 已針對以下兩種核心用途進行最佳化:
- 即時偵測及追蹤攝影機觀景窗中最重要的物體
- 從靜態圖片偵測多個物件
如要針對這些用途設定 API,請按照下列步驟操作:
Java
// Live detection and tracking FirebaseVisionObjectDetectorOptions options = new FirebaseVisionObjectDetectorOptions.Builder() .setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE) .enableClassification() // Optional .build(); // Multiple object detection in static images FirebaseVisionObjectDetectorOptions options = new FirebaseVisionObjectDetectorOptions.Builder() .setDetectorMode(FirebaseVisionObjectDetectorOptions.SINGLE_IMAGE_MODE) .enableMultipleObjects() .enableClassification() // Optional .build();
Kotlin
// Live detection and tracking val options = FirebaseVisionObjectDetectorOptions.Builder() .setDetectorMode(FirebaseVisionObjectDetectorOptions.STREAM_MODE) .enableClassification() // Optional .build() // Multiple object detection in static images val options = FirebaseVisionObjectDetectorOptions.Builder() .setDetectorMode(FirebaseVisionObjectDetectorOptions.SINGLE_IMAGE_MODE) .enableMultipleObjects() .enableClassification() // Optional .build()
取得
FirebaseVisionObjectDetector
的例項:Java
FirebaseVisionObjectDetector objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(); // Or, to change the default settings: FirebaseVisionObjectDetector objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(options);
Kotlin
val objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector() // Or, to change the default settings: val objectDetector = FirebaseVision.getInstance().getOnDeviceObjectDetector(options)
2. 執行物件偵測工具
如要偵測及追蹤物件,請將圖片傳遞至 FirebaseVisionObjectDetector
例項的 processImage()
方法。
針對序列中的每個影片或圖片影格,執行下列操作:
從圖片建立
FirebaseVisionImage
物件。-
如要從
media.Image
物件建立FirebaseVisionImage
物件 (例如從裝置相機擷取圖片時),請將media.Image
物件和圖片的旋轉角度傳遞至FirebaseVisionImage.fromMediaImage()
。如果您使用 CameraX 程式庫,
OnImageCapturedListener
和ImageAnalysis.Analyzer
類別會為您計算旋轉值,因此您只需在呼叫FirebaseVisionImage.fromMediaImage()
之前,將旋轉值轉換為 ML Kit 的ROTATION_
常數:Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { private int degreesToFirebaseRotation(int degrees) { switch (degrees) { case 0: return FirebaseVisionImageMetadata.ROTATION_0; case 90: return FirebaseVisionImageMetadata.ROTATION_90; case 180: return FirebaseVisionImageMetadata.ROTATION_180; case 270: return FirebaseVisionImageMetadata.ROTATION_270; default: throw new IllegalArgumentException( "Rotation must be 0, 90, 180, or 270."); } } @Override public void analyze(ImageProxy imageProxy, int degrees) { if (imageProxy == null || imageProxy.getImage() == null) { return; } Image mediaImage = imageProxy.getImage(); int rotation = degreesToFirebaseRotation(degrees); FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); // Pass image to an ML Kit Vision API // ... } }
Kotlin
private class YourImageAnalyzer : ImageAnalysis.Analyzer { private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) { 0 -> FirebaseVisionImageMetadata.ROTATION_0 90 -> FirebaseVisionImageMetadata.ROTATION_90 180 -> FirebaseVisionImageMetadata.ROTATION_180 270 -> FirebaseVisionImageMetadata.ROTATION_270 else -> throw Exception("Rotation must be 0, 90, 180, or 270.") } override fun analyze(imageProxy: ImageProxy?, degrees: Int) { val mediaImage = imageProxy?.image val imageRotation = degreesToFirebaseRotation(degrees) if (mediaImage != null) { val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) // Pass image to an ML Kit Vision API // ... } } }
如果您未使用可提供圖片旋轉角度的相機程式庫,可以根據裝置旋轉角度和裝置中相機感應器的方向計算:
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private int getRotationCompensation(String cameraId, Activity activity, Context context) throws CameraAccessException { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation(); int rotationCompensation = ORIENTATIONS.get(deviceRotation); // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360; // Return the corresponding FirebaseVisionImageMetadata rotation value. int result; switch (rotationCompensation) { case 0: result = FirebaseVisionImageMetadata.ROTATION_0; break; case 90: result = FirebaseVisionImageMetadata.ROTATION_90; break; case 180: result = FirebaseVisionImageMetadata.ROTATION_180; break; case 270: result = FirebaseVisionImageMetadata.ROTATION_270; break; default: result = FirebaseVisionImageMetadata.ROTATION_0; Log.e(TAG, "Bad rotation value: " + rotationCompensation); } return result; }
Kotlin
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 90) ORIENTATIONS.append(Surface.ROTATION_90, 0) ORIENTATIONS.append(Surface.ROTATION_180, 270) ORIENTATIONS.append(Surface.ROTATION_270, 180) } /** * Get the angle by which an image must be rotated given the device's current * orientation. */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Throws(CameraAccessException::class) private fun getRotationCompensation(cameraId: String, activity: Activity, context: Context): Int { // Get the device's current rotation relative to its "native" orientation. // Then, from the ORIENTATIONS table, look up the angle the image must be // rotated to compensate for the device's rotation. val deviceRotation = activity.windowManager.defaultDisplay.rotation var rotationCompensation = ORIENTATIONS.get(deviceRotation) // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360 // Return the corresponding FirebaseVisionImageMetadata rotation value. val result: Int when (rotationCompensation) { 0 -> result = FirebaseVisionImageMetadata.ROTATION_0 90 -> result = FirebaseVisionImageMetadata.ROTATION_90 180 -> result = FirebaseVisionImageMetadata.ROTATION_180 270 -> result = FirebaseVisionImageMetadata.ROTATION_270 else -> { result = FirebaseVisionImageMetadata.ROTATION_0 Log.e(TAG, "Bad rotation value: $rotationCompensation") } } return result }
接著,將
media.Image
物件和旋轉值傳遞至FirebaseVisionImage.fromMediaImage()
:Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- 如要從檔案 URI 建立
FirebaseVisionImage
物件,請將應用程式背景資訊和檔案 URI 傳遞至FirebaseVisionImage.fromFilePath()
。這在您使用ACTION_GET_CONTENT
意圖,提示使用者從相片庫應用程式中選取圖片時,非常實用。Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
- 如要從
ByteBuffer
或位元組陣列建立FirebaseVisionImage
物件,請先計算圖片旋轉角度,如上文所述的media.Image
輸入資料。接著,請建立
FirebaseVisionImageMetadata
物件,其中包含圖片的高度、寬度、顏色編碼格式和旋轉角度:Java
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build();
Kotlin
val metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build()
使用緩衝區或陣列和中繼資料物件,建立
FirebaseVisionImage
物件:Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
- 如要從
Bitmap
物件建立FirebaseVisionImage
物件,請按照下列步驟操作:Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmap
物件所代表的圖片必須是直立的,不需要額外旋轉。
-
將圖片傳遞至
processImage()
方法:Java
objectDetector.processImage(image) .addOnSuccessListener( new OnSuccessListener<List<FirebaseVisionObject>>() { @Override public void onSuccess(List<FirebaseVisionObject> detectedObjects) { // Task completed successfully // ... } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
Kotlin
objectDetector.processImage(image) .addOnSuccessListener { detectedObjects -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
如果對
processImage()
的呼叫成功,系統會將FirebaseVisionObject
清單傳遞至成功事件監聽器。每個
FirebaseVisionObject
都包含下列屬性:定界框 Rect
:表示物件在圖片中的位置。追蹤 ID 用於在圖像中識別物件的整數。在 SINGLE_IMAGE_MODE 中為空值。 類別 物件的粗略類別。如果物件偵測器未啟用分類功能,這一值一律會是 FirebaseVisionObject.CATEGORY_UNKNOWN
。可信度 物件分類的信度值。如果物體偵測器未啟用分類功能,或是物體被歸類為不明物,則為 null
。Java
// The list of detected objects contains one item if multiple object detection wasn't enabled. for (FirebaseVisionObject obj : detectedObjects) { Integer id = obj.getTrackingId(); Rect bounds = obj.getBoundingBox(); // If classification was enabled: int category = obj.getClassificationCategory(); Float confidence = obj.getClassificationConfidence(); }
Kotlin
// The list of detected objects contains one item if multiple object detection wasn't enabled. for (obj in detectedObjects) { val id = obj.trackingId // A number that identifies the object across images val bounds = obj.boundingBox // The object's position in the image // If classification was enabled: val category = obj.classificationCategory val confidence = obj.classificationConfidence }
改善可用性和效能
為提供最佳使用者體驗,請在應用程式中遵守下列規範:
- 物體偵測成功與否取決於物體的視覺複雜度。物件若只有少數視覺特徵,可能需要佔用較大的圖片區域才能偵測。您應提供使用者指引,說明如何擷取與您要偵測的物件類型相容的輸入內容。
- 使用分類功能時,如果您想偵測不屬於支援類別的物體,請為不明物體實作特殊處理機制。
另外,請參閱 [ML Kit Material Design 展示應用程式][showcase-link]{: .external } 和 Material Design 機器學習功能的模式集合。
在即時應用程式中使用串流模式時,請遵循下列指南,以獲得最佳的幀率:
請勿在串流模式中使用多個物件偵測功能,因為大多數裝置無法產生足夠的幀率。
如果不需要分類,請停用分類功能。
- 限制對偵測器的呼叫。如果在偵測器執行期間有新的影片影格可用,請放棄該影格。
- 如果您要使用偵測器的輸出內容,在輸入圖片上疊加圖形,請先從 ML Kit 取得結果,然後在單一步驟中算繪圖片和疊加圖形。這樣一來,您只需為每個輸入影格轉譯一次顯示介面。
-
如果您使用 Camera2 API,請以
ImageFormat.YUV_420_888
格式擷取圖片。如果您使用舊版 Camera API,請以
ImageFormat.NV21
格式擷取圖片。