Sử dụng ML Kit để dễ dàng thêm các tính năng phân đoạn đối tượng 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 đi kèm: mô hình được tải xuống linh hoạt bằng Dịch vụ Google Play. |
Ảnh hưởng đến kích thước ứng dụng | Tăng kích thước 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 dùng thử ứ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.gradle
cấp dự án, hãy nhớ thêm kho lưu trữ Maven của Google vào cả hai mụcbuildscript
vàallprojects
. - Thêm phần phụ thuộc cho thư viện phân đoạn đối tượng của ML Kit 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 qua Cửa hàng Play. Để thực hiện việc này, hãy thêm 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 yêu cầu tải mô hình xuống một cách rõ ràng, thì mô hình sẽ được tải xuống vào lần đầu tiên bạn chạy trình phân đoạn. Các yêu cầu bạn thực hiện 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
Để phân đoạn trên hình ảnh, hãy tạo một đố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 được giải thích bên dưới.
Sử dụng media.Image
Để tạo một đối tượng InputImage
từ mộ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à hướng xoay của hình ảnh đến InputImage.fromMediaImage()
.
Nếu bạn sử dụng thư viện
CameraX, các lớp OnImageCapturedListener
và ImageAnalysis.Analyzer
sẽ tính toán 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 bạn 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 đó, 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 một đối tượng InputImage
từ mộ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 hữu ích khi bạn 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 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 độ xoay của 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 một đố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 lựa chọn của bộ phân đoạn
Để phân đoạn hình ảnh, trước tiên, hãy tạo một phiên bản 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 của nền trước
Mặt nạ độ tin cậy của nền trước giúp bạn phân biệt chủ thể ở nền trước với nền sau.
Lệnh gọi enableForegroundConfidenceMask()
trong các lựa 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ể nhận được một bitmap của đối tượng trên nền trước.
Lệnh gọi enableForegroundBitmap()
trong các lựa 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 có nhiều đối tượng
Giống như các lựa 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 có nhiều đối tượng
Tương tự, bạn có thể bật bitmap cho từng đối tượng:
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 công cụ phân đoạn đối tượng
Sau khi chỉ định các lựa chọn SubjectSegmenterOptions
, hãy tạo một thực thể SubjectSegmenter
gọi getClient()
và truyền các lựa 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ị cho 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 đối tượng
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ạ tiền cảnh 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 mộ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 đố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
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 đối tượng đượ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 so với 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 của kết quả phụ thuộc vào chất lượng của hình ảnh đầu vào:
- Để ML Kit nhận đượ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ả chấp nhận được, hãy yêu cầu người dùng chụp lại hình ảnh.