Phân đoạn đối tượng bằng Bộ công cụ học máy dành cho Android

Sử dụng Bộ công cụ học máy để dễ dàng thêm các tính năng phân đoạn chủ đề vào ứng dụng của bạn.

Tính năng Chi tiết
Tên SDK play-services-mlkit-subject-segmentation
Triển khai Chưa theo gói: mô hình được tải xuống động bằng cách sử dụng Dịch vụ Google Play.
Ảnh hưởng của kích thước ứng dụng Tăng kích thước khoảng 200 KB.
Thời gian khởi động Người dùng có thể phải đợi mô hình tải xuống trước khi sử dụng lần đầu tiên.

Dùng thử

Trước khi bắt đầu

  1. Trong tệp build.gradle cấp dự án, hãy nhớ đưa kho lưu trữ Maven của Google vào cả hai phần buildscriptallprojects.
  2. Thêm phần phụ thuộc cho thư viện phân đoạn chủ thể của Bộ công cụ học máy vào tệp gradle cấp ứng dụng của mô-đun, thường là app/build.gradle:
dependencies {
   implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}

Như đã đề cập ở trên, mô hình này do Dịch vụ Google Play cung cấp. Bạn có thể định cấu hình ứng dụng để tự động tải mô hình xuống thiết bị sau khi ứng dụng được cài đặt từ Cửa hàng Play. Để thực hiện việc này, hãy thêm nội dung khai báo sau vào tệp AndroidManifest.xml của ứng dụng:

<application ...>
      ...
      <meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="subject_segment" >
      <!-- To use multiple models: android:value="subject_segment,model2,model3" -->
</application>

Bạn cũng có thể kiểm tra rõ ràng tình trạng sẵn có của mô hình và yêu cầu tải xuống thông qua Dịch vụ Google Play bằng ModuleInstallClient API.

Nếu bạn không bật tính năng tải mô hình xuống khi cài đặt hoặc yêu cầu tải mô hình xuống rõ ràng, thì mô hình sẽ được tải xuống trong lần đầu tiên bạn chạy trình phân đoạn. Yêu cầu bạn thực hiện trước khi tải xuống hoàn tất không có kết quả nào.

1. Chuẩn bị hình ảnh đầu vào

Để thực hiện phân đoạn trên hình ảnh, hãy tạo đối tượng InputImage từ Bitmap, media.Image, ByteBuffer, mảng byte hoặc một tệp trên thiết bị.

Bạn có thể tạo một đối tượng InputImage từ nhiều nguồn. Mỗi nguồn sẽ được giải thích ở bên dưới.

Sử dụng media.Image

Để tạo một đối tượng InputImage từ đối tượng media.Image, chẳng hạn như khi bạn chụp ảnh bằng máy ảnh của thiết bị, hãy truyền đối tượng media.Image và chế độ xoay của hình ảnh đến InputImage.fromMediaImage().

Nếu bạn sử dụng thư viện CameraX, các lớp OnImageCapturedListenerImageAnalysis.Analyzer sẽ tính giá trị chế độ xoay cho bạn.

Kotlin

private class YourImageAnalyzer : ImageAnalysis.Analyzer {

    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy.image
        if (mediaImage != null) {
            val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
            // Pass image to an ML Kit Vision API
            // ...
        }
    }
}

Java

private class YourAnalyzer implements ImageAnalysis.Analyzer {

    @Override
    public void analyze(ImageProxy imageProxy) {
        Image mediaImage = imageProxy.getImage();
        if (mediaImage != null) {
          InputImage image =
                InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
          // Pass image to an ML Kit Vision API
          // ...
        }
    }
}

Nếu không sử dụng thư viện máy ảnh để cung cấp độ xoay của hình ảnh, bạn có thể tính độ xoay này dựa trên độ xoay của thiết bị và hướng của cảm biến máy ảnh trong thiết bị:

Kotlin

private val ORIENTATIONS = SparseIntArray()

init {
    ORIENTATIONS.append(Surface.ROTATION_0, 0)
    ORIENTATIONS.append(Surface.ROTATION_90, 90)
    ORIENTATIONS.append(Surface.ROTATION_180, 180)
    ORIENTATIONS.append(Surface.ROTATION_270, 270)
}

/**
 * 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, isFrontFacing: Boolean): 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)

    // Get the device's sensor orientation.
    val cameraManager = activity.getSystemService(CAMERA_SERVICE) as CameraManager
    val sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION)!!

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360
    }
    return rotationCompensation
}

Java

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
    ORIENTATIONS.append(Surface.ROTATION_0, 0);
    ORIENTATIONS.append(Surface.ROTATION_90, 90);
    ORIENTATIONS.append(Surface.ROTATION_180, 180);
    ORIENTATIONS.append(Surface.ROTATION_270, 270);
}

/**
 * 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, boolean isFrontFacing)
        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);

    // Get the device's sensor orientation.
    CameraManager cameraManager = (CameraManager) activity.getSystemService(CAMERA_SERVICE);
    int sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION);

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360;
    }
    return rotationCompensation;
}

Sau đó, hãy truyền đối tượng media.Image và giá trị độ xoay đến InputImage.fromMediaImage():

Kotlin

val image = InputImage.fromMediaImage(mediaImage, rotation)

Java

InputImage image = InputImage.fromMediaImage(mediaImage, rotation);

Sử dụng URI tệp

Để tạo đối tượng InputImage từ URI tệp, hãy chuyển ngữ cảnh ứng dụng và URI tệp đến InputImage.fromFilePath(). Điều này rất hữu ích khi bạn sử dụng ý định ACTION_GET_CONTENT để nhắc người dùng chọn một hình ảnh từ ứng dụng thư viện của họ.

Kotlin

val image: InputImage
try {
    image = InputImage.fromFilePath(context, uri)
} catch (e: IOException) {
    e.printStackTrace()
}

Java

InputImage image;
try {
    image = InputImage.fromFilePath(context, uri);
} catch (IOException e) {
    e.printStackTrace();
}

Sử dụng ByteBuffer hoặc ByteArray

Để tạo một đối tượng InputImage từ ByteBuffer hoặc ByteArray, trước tiên, hãy tính toán độ xoay hình ảnh như mô tả trước đó cho dữ liệu đầu vào media.Image. Sau đó, hãy tạo đối tượng InputImage bằng vùng đệm hoặc mảng, cùng với chiều cao, chiều rộng, định dạng mã hoá màu và độ xoay của hình ảnh:

Kotlin

val image = InputImage.fromByteBuffer(
        byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)
// Or:
val image = InputImage.fromByteArray(
        byteArray,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)

Java

InputImage image = InputImage.fromByteBuffer(byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);
// Or:
InputImage image = InputImage.fromByteArray(
        byteArray,
        /* image width */480,
        /* image height */360,
        rotation,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);

Sử dụng Bitmap

Để tạo đối tượng InputImage từ đối tượng Bitmap, hãy khai báo sau:

Kotlin

val image = InputImage.fromBitmap(bitmap, 0)

Java

InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);

Hình ảnh được biểu thị bằng đối tượng Bitmap cùng với độ xoay.

2. Tạo một thực thể của SubjectSegmenter

Xác định các tuỳ chọn của trình phân đoạn

Để phân đoạn hình ảnh, trước tiên hãy tạo một thực thể của SubjectSegmenterOptions như sau:

Kotlin

val options = SubjectSegmenterOptions.Builder()
       // enable options
       .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        // enable options
        .build();

Dưới đây là chi tiết về từng lựa chọn:

Mặt nạ tin cậy khi ở nền trước

Mặt nạ tin cậy ở nền trước cho phép bạn phân biệt chủ thể trên nền trước với nền.

Gọi enableForegroundConfidenceMask() trong các tuỳ chọn cho phép bạn truy xuất mặt nạ trên nền trước sau bằng cách gọi getForegroundMask() trên đối tượng SubjectSegmentationResult được trả về sau khi xử lý hình ảnh.

Kotlin

val options = SubjectSegmenterOptions.Builder()
        .enableForegroundConfidenceMask()
        .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        .enableForegroundConfidenceMask()
        .build();
bitmap nền trước

Tương tự, bạn cũng có thể lấy bitmap của chủ thể ở nền trước.

Khi gọi enableForegroundBitmap() trong các tuỳ chọn, bạn có thể truy xuất bitmap trên nền trước sau này bằng cách gọi getForegroundBitmap() trên đối tượng SubjectSegmentationResult được trả về sau khi xử lý hình ảnh.

Kotlin

val options = SubjectSegmenterOptions.Builder()
        .enableForegroundBitmap()
        .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        .enableForegroundBitmap()
        .build();
Mặt nạ tin cậy nhiều đối tượng

Giống như đối với các tuỳ chọn trên nền trước, bạn có thể sử dụng SubjectResultOptions để bật mặt nạ tin cậy cho từng chủ thể trên nền trước như sau:

Kotlin

val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder()
    .enableConfidenceMask()
    .build()

val options = SubjectSegmenterOptions.Builder()
    .enableMultipleSubjects(subjectResultOptions)
    .build()

Java

SubjectResultOptions subjectResultOptions =
        new SubjectSegmenterOptions.SubjectResultOptions.Builder()
            .enableConfidenceMask()
            .build()

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
      .enableMultipleSubjects(subjectResultOptions)
      .build()
Bitmap nhiều chủ đề

Và tương tự, bạn có thể bật bitmap cho từng tiêu đề:

Kotlin

val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder()
    .enableSubjectBitmap()
    .build()

val options = SubjectSegmenterOptions.Builder()
    .enableMultipleSubjects(subjectResultOptions)
    .build()

Java

SubjectResultOptions subjectResultOptions =
      new SubjectSegmenterOptions.SubjectResultOptions.Builder()
        .enableSubjectBitmap()
        .build()

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
      .enableMultipleSubjects(subjectResultOptions)
      .build()

Tạo trình phân đoạn chủ đề

Sau khi bạn chỉ định các tuỳ chọn SubjectSegmenterOptions, hãy tạo một thực thể SubjectSegmenter gọi getClient() và truyền các tuỳ chọn dưới dạng một tham số:

Kotlin

val segmenter = SubjectSegmentation.getClient(options)

Java

SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);

3. Xử lý hình ảnh

Truyền đối tượng InputImage đã chuẩn bị vào phương thức process của SubjectSegmenter:

Kotlin

segmenter.process(inputImage)
    .addOnSuccessListener { result ->
        // Task completed successfully
        // ...
    }
    .addOnFailureListener { e ->
        // Task failed with an exception
        // ...
    }

Java

segmenter.process(inputImage)
    .addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(SubjectSegmentationResult result) {
                // Task completed successfully
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Task failed with an exception
                // ...
            }
        });

4. Nhận kết quả phân đoạn chủ đề

Truy xuất bitmap và mặt nạ nền trước

Sau khi xử lý xong, bạn có thể truy xuất mặt nạ trên nền trước để hình ảnh gọi getForegroundConfidenceMask() như sau:

Kotlin

val colors = IntArray(image.width * image.height)

val foregroundMask = result.foregroundConfidenceMask
for (i in 0 until image.width * image.height) {
  if (foregroundMask[i] > 0.5f) {
    colors[i] = Color.argb(128, 255, 0, 255)
  }
}

val bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
)

Java

int[] colors = new int[image.getWidth() * image.getHeight()];

FloatBuffer foregroundMask = result.getForegroundConfidenceMask();
for (int i = 0; i < image.getWidth() * image.getHeight(); i++) {
  if (foregroundMask.get() > 0.5f) {
    colors[i] = Color.argb(128, 255, 0, 255);
  }
}

Bitmap bitmapMask = Bitmap.createBitmap(
      colors, image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888
);

Bạn cũng có thể truy xuất bitmap ở nền trước của hình ảnh gọi getForegroundBitmap():

Kotlin

val foregroundBitmap = result.foregroundBitmap

Java

Bitmap foregroundBitmap = result.getForegroundBitmap();

Truy xuất mặt nạ và bitmap cho từng đối tượng

Tương tự, bạn có thể truy xuất mặt nạ cho các đối tượng được phân đoạn bằng cách gọi getConfidenceMask() trên từng đối tượng như sau:

Kotlin

val subjects = result.subjects

val colors = IntArray(image.width * image.height)
for (subject in subjects) {
  val mask = subject.confidenceMask
  for (i in 0 until subject.width * subject.height) {
    val confidence = mask[i]
    if (confidence > 0.5f) {
      colors[image.width * (subject.startY - 1) + subject.startX] =
          Color.argb(128, 255, 0, 255)
    }
  }
}

val bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
)

Java

List subjects = result.getSubjects();

int[] colors = new int[image.getWidth() * image.getHeight()];
for (Subject subject : subjects) {
  FloatBuffer mask = subject.getConfidenceMask();
  for (int i = 0; i < subject.getWidth() * subject.getHeight(); i++) {
    float confidence = mask.get();
    if (confidence > 0.5f) {
      colors[width * (subject.getStartY() - 1) + subject.getStartX()]
          = Color.argb(128, 255, 0, 255);
    }
  }
}

Bitmap bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
);

Bạn cũng có thể truy cập bitmap của từng chủ đề được phân đoạn như sau:

Kotlin

val bitmaps = mutableListOf()
for (subject in subjects) {
  bitmaps.add(subject.bitmap)
}

Java

List bitmaps = new ArrayList<>();
for (Subject subject : subjects) {
  bitmaps.add(subject.getBitmap());
}

Mẹo cải thiện hiệu suất

Đối với mỗi phiên ứng dụng, kết quả dự đoán đầu tiên thường chậm hơn các dự đoán tiếp theo do quá trình khởi tạo mô hình. Nếu độ trễ thấp là rất quan trọng, hãy cân nhắc gọi trước một dự đoán "giả".

Chất lượng kết quả phụ thuộc vào chất lượng của hình ảnh đầu vào:

  • Để Bộ công cụ học máy có được kết quả phân đoạn chính xác, hình ảnh phải có kích thước tối thiểu là 512 x 512 pixel.
  • Hình ảnh lấy nét kém cũng có thể ảnh hưởng đến độ chính xác. Nếu bạn không nhận được kết quả có thể chấp nhận được, hãy yêu cầu người dùng chụp lại hình ảnh.