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ủ thể 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 | Không theo gói: mô hình được tải xuống linh động bằng Dịch vụ Google Play. |
| Tác động đến kích thước ứng dụng | Kích thước tăng thêm khoảng 200 KB. |
| Thời gian khởi chạy | 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. |
Dùng thử
- Hãy khám phá ứng dụng mẫu để xem ví dụ về cách sử dụng API này.
Trước khi bắt đầu
- Trong tệp
build.gradlecấp dự án, hãy nhớ thêm kho lưu trữ Maven của Google vào cả hai mụcbuildscriptvàallprojects. - 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 phạm vi cung cấp 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 tại thời điểm cài đặt hoặc không yêu cầu tải xuống rõ ràng, 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. Các yêu cầu bạn đưa ra trước khi quá trình tải xuống hoàn tất sẽ không có kết quả.
1. Chuẩn bị hình ảnh đầu vào
Để thực hiện phân đoạn trên một hình ảnh, hãy tạo một đối tượng InputImage từ Bitmap, media.Image, ByteBuffer, mảng byte hoặc tệp trên thiết bị.
Bạn có thể tạo đối tượng InputImage từ nhiều nguồn, mỗi nguồn được giải thích bên dưới.
Sử dụng media.Image
Để tạo đối tượng InputImage từ đối tượng media.Image, chẳng hạn như khi bạn chụp ảnh bằng camera của thiết bị, hãy truyền đối tượng media.Image và độ xoay của hình ảnh đến InputImage.fromMediaImage().
Nếu bạn sử dụng thư viện
CameraX, thì các lớp OnImageCapturedListener và ImageAnalysis.Analyzer sẽ tính giá trị 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 dùng thư viện máy ảnh cho biết độ xoay của hình ảnh, bạn có thể tính độ xoay đó từ độ xoay của thiết bị và hướng của cảm biến camera 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 đó, 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 truyề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 trong ứng dụng thư viện ảnh 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 đối tượng InputImage từ ByteBuffer hoặc ByteArray, trước tiên, hãy tính độ 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 như 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();Sau đây là thông tin chi tiết về từng lựa chọn:
Mặt nạ tin cậy nền trước
Mặt nạ tin cậy nền trước giúp bạn phân biệt đối tượng nền trước với nền sau.
Lệnh gọi enableForegroundConfidenceMask() trong các tuỳ chọn cho phép bạn truy xuất mặt nạ nền trước sau này 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 đối tượng trên nền trước.
Lệnh gọi enableForegroundBitmap() trong các tuỳ chọn cho phép bạn truy xuất bitmap 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 chủ thể
Giống như 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 đối tượng 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ủ thể
Tương tự, bạn có thể bật bitmap cho từng chủ thể:
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ủ thể
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 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ủ thể
Truy xuất mặt nạ và bitmap trên nền trước
Sau khi xử lý, bạn có thể truy xuất mặt nạ nền trước cho hình ảnh bằng cách 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 của nền trước của hình ảnh bằng cách gọi getForegroundBitmap():
Kotlin
val foregroundBitmap = result.foregroundBitmap
Java
Bitmap foregroundBitmap = result.getForegroundBitmap();
Truy xuất mặt nạ và bitmap cho từng chủ thể
Tương tự, bạn có thể truy xuất mặt nạ cho các chủ thể được phân đoạn bằng cách gọi getConfidenceMask() trên mỗi chủ thể 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
Listsubjects = 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 vào bitmap của từng chủ thể được phân đoạn như sau:
Kotlin
val bitmaps = mutableListOf() for (subject in subjects) { bitmaps.add(subject.bitmap) }
Java
Listbitmaps = 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, suy luận đầu tiên thường chậm hơn các suy luận tiếp theo do quá trình khởi chạy mô hình. Nếu độ trễ thấp là yếu tố quan trọng, hãy cân nhắc gọi một suy luận "giả" trước thời hạn.
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à 512x512 pixel.
- Hình ảnh không rõ nét cũng có thể ảnh hưởng đến độ chính xác. Nếu bạn không nhận được kết quả như mong đợi, hãy yêu cầu người dùng chụp lại hình ảnh.